Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Please have a look to the answers in these messages:
Runtime mathematical Formulla[^]
How Can I use the Math char set?[^]
Reduce equation with c#[^]

there are plenty of usefull answers and links that can be helpful for you
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Oct-12 19:13pm    
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
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[^].

To learn more of expression trees, please see:
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[^].

Please also see my answer and other answers to this question:
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
 
Share this answer
 
v2
Comments
Kuthuparakkal 9-Oct-12 22:11pm    
My 5+
Sergey Alexandrovich Kryukov 10-Oct-12 1:22am    
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
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900