12,511,882 members (42,870 online)
Rate this:
See more:
Hello there, Can anyone help me get started on this:
What i need is: to read a text file containing equations like these

2x - 5y + 3z = 10
5x + y - 2z = 4
-4x +3y -z = 6

and then do jacobi on them. Can you tell me how I can read those equations from file. I am planning to have those before the "=" sign in an A[][] matrix and the numbers after "=" in B[] matrix.
Posted 9-Oct-12 12:22pm

Rate this:

## Solution 1

Runtime mathematical Formulla[^]
How Can I use the Math char set?[^]
Reduce equation with c#[^]

Well... um... relevant. Oh, my past answer is also there? Sure, a 5. :-)
I also provided a fresh answer here (this time, way more detailed) -- please see.
--SA
Rate this:

## Solution 2

You need to create a well-known data structure called "expression tree". A node of a tree should represent an expression and it should have 0, 1 or 2 child nodes representing sub-expressions. The node should be of one of few different classes derived from the common abstract class, let's call this class "Expression". Each terminal derived class representing a node should represent a concrete mathematical operator, like − or +. You need to define some virtual functions used to transform the sub-trees (sub-expressions) under each node, according to the algebra you are going to define. These function should be overridden in each derived class. This way, you will be able to do the operations on the tree based on defined operations on the sub-trees, recursively.

In other words, you should define the algebra over the C++ tree structure made of the node (expression) classes I mentioned above and the mapping between this algebra and some traditional mathematical algebra you need to cover in your calculations. Naturally, these two algebras should be isomorphic, and the mapping should be the on-to-one correspondence.

After that, you should develop the method to "output" the expression in the form of text, pretty much like the one you've shown. You can also consider support of HTML, XML, and, in particular, MathML, for most advanced option, or some notation of your own. I'm not sure you need it, but this is something you better be aware of:
http://en.wikipedia.org/wiki/MathML[^].

And finally, the procedure if population of the expression tree will need to be developed as some kind of parsing. You may need to learn some theory of parsers or… not, as you can do it all by yourself. Anyway, if you feel a need to learn this area, this is a good place to start:
http://en.wikipedia.org/wiki/Parsing_expression_grammar[^].

http://en.wikipedia.org/wiki/Expression_tree[^].

They are widely used at least in two areas: in compilers from a computer languages and, more closely to our topic, in computer algebra systems:
http://en.wikipedia.org/wiki/Compiler[^],
http://en.wikipedia.org/wiki/Computer_algebra_system[^].

Reduce equation with c#[^].

The computer algebra systems is something you need to learn, but, in my opinion, general mathematical and programming culture and a reasonably good brain in one's head would be enough to develop such system on your own, especially in case when it does not have to be most comprehensive in the world. As you can see, most of the knowledge you would need is a commonplace pretty well covered by Wikipedia.

Wish you the best of luck,
—SA
v2
Kuthuparakkal 9-Oct-12 22:11pm

My 5+

Thank you,
--SA
pasztorpisti 10-Oct-12 4:15am

5ed, I hope this wasn't wasted effort! :-)
Sergey Alexandrovich Kryukov 10-Oct-12 10:53am

Thank you. At least, I'll be able to refer to this post. :-)
--SA
Nelek 10-Oct-12 4:33am

Nice one :) +5
Sergey Alexandrovich Kryukov 10-Oct-12 10:53am

Thank you, Nelek.
--SA
Rate this:

## Solution 3

I think the question was how to read the strings from the text file.
So, it depends on what platform Sam is going to use. He has tagged the question as C++, so, the best guess is that he is going to use an stl. So, for reading from files he will need sdt::istream with getline(). The he will need to parse each line using std::string's methods find(), rfind(), etc. methods.

If his library of choice is MFC then it will be CStdioFile with its ReadString method. Then he will need to process CStrings using Find, Tokenize methods. ATL has similar classes

If it is C#, then there are similar classes there as well.

Top Experts
Last 24hrsThis month
 David_Wimbley 170 OriginalGriff 145 Maciej Los 100 John Simmons / outlaw programmer 85 Dave Kreskowiak 85
 Dave Kreskowiak 25 Maciej Los 10 OriginalGriff 10 Karthik Bangalore 10