Click here to Skip to main content
16,019,072 members
Please Sign up or sign in to vote.
4.60/5 (5 votes)
See more:
Hi folks!

I try to set up a method, that reduces an equation. I'll provide a sample:

User Input: (4x + 7x) / (xy)
where x and y are unknown variables which are filled during the calculation process.

in RPN the formula above would be:

4 x * 7 x * + x y * /

To calculate the expression, the calculator would have to proceed 11 token. Imagine me solving this formula 10 million times with different values for x and y. This would work, but it is suboptimal since the expression could be reduced to

11 / y (or RPN 11 y /)

Now, my question is how to reduce the formula programatically to the value mentioned above. Any suggesions?

Thanks a lot in advance

Regards
Posted

This is a pretty complex task. You need to work not with the digital, but with symbolic calculations.
The field of computing is called Computer Algebra System (CAS, see http://en.wikipedia.org/wiki/Computer_algebra_system[^]).

You need to parse your expression symbolically into an expression tree. Your operations should be represented as the nodes, your sub-expressions should be the node children, and your terminal nodes should be the objects representing expression arguments or constants.

Good news: with your v.4.0 you can get expression trees right from your C# code in the form of lambda expression: http://msdn.microsoft.com/en-us/library/bb397951.aspx[^].

Everything beyond that is much more difficult: you will need to develop a system which performs mathematical operations over a tree. Your algebra operations should be the operations over an expression of the tree; they will takes expression tree(s) as an argument and build resulting expression tree for return value. For example, differentiation function takes two parameters: a reference to argument to differentiate by and the expression tree representing a function to be differentiate, the result will be a new expression tree representing the derivative; due to the property of the derivative the function should be recursive.

Why did I explain the example of derivative? Simply because this is the simplest non-trivial operation in all Computer Algebra! Simplification of expressions is already much more difficult. You can develop simplification algorithm for some partial cases, but a general case is quite difficult.

Good luck,
—SA
 
Share this answer
 
v6
Comments
Olivier Levrey 26-Apr-11 12:21pm    
My 5 SA. You are absolutely right. I added a few comments to point out a few other problems concerning simplifications (but this is not an answer).
Sergey Alexandrovich Kryukov 26-Apr-11 13:41pm    
Thank you, Olivier.
Those comments are good, I voted, too.
--SA
RowanArkison 26-Apr-11 12:46pm    
Jap, I try. Thanks!
Sergey Alexandrovich Kryukov 26-Apr-11 13:40pm    
You're welcome.
I would say, this is feasible for some limited task (and may take a lifetime for a comprehensive; it's usually done by a good teams -- with different results). I developed a whole technology about it, but my mathematical part is still limited by differentiation and some rudimentary classes of reduction, such as field-algebra operations).

Will you formally accept the Solution (a green button)?
Thank you.
--SA
CPallini 26-Apr-11 13:00pm    
5.:-)
SA is right.

Simplication might appear as a simple task at first sight since it looks very natural. But it is not at all.

First of all, one should agree on the simplest form of an expression. For example, should you keep:
1 / sqrt(x)
or prefer
sqrt(x) / x?

Some algebra programs will give you the second expression as the simplest one since they don't like roots under a ratio.

And even your own example might bring problems:
Simplifying (4x + 7x) / (xy) into 11/y is OK as long as x doesn't equal 0. Mathematically, both expressions are not equivalent (the first one is not defined for x=0, but the second is).

This is definitely not a trivial problem…
 
Share this answer
 
v2
Comments
RowanArkison 26-Apr-11 12:46pm    
Indeed, it is not (spent the whole weekend thinking about it and trying around with fractions). I could maybe accept the performance issue since I can use parallelism to solve the expression and I do not expect very compley ones / more than 100k computations of a formula at once.
If I break down the example, I definitly need only exactly the case mentioned above - just remove x from the equation. I try a bit around with the CAS / Expression tree topic, never used the lambda stuff before. But thanks a lot for now :-)
Sergey Alexandrovich Kryukov 26-Apr-11 13:35pm    
A 5. These are very good points giving the idea of purely mathematical issues (while I mentioned only the technical one).

(My edit was just fixing my name (should be capital because this is 2 initials), 1 typo and 1 formatting, a HTML entity; hope you don't mind, do you? :-)
--SA
Olivier Levrey 27-Apr-11 3:41am    
Thank you for correcting me, and for the vote ;)
Sergey Alexandrovich Kryukov 27-Apr-11 3:56am    
You're very welcome.
--SA
Espen Harlinn 26-Apr-11 15:35pm    
Good points - 5ed :)
I guess you should take a look at Math.NET : About & Features[^]

Math.NET LinqAlegebra provides elements of general purpose computer algebra systems directly on top of pure Linq Expressions, including automatic simplification, differentiation and MathML I/O.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 15:42pm    
Espen, do you mean LinqAlegebra or anything else? Good to know anyway, thank you and get my 5.
--SA
Espen Harlinn 26-Apr-11 15:46pm    
Yes :)
Sergey Alexandrovich Kryukov 26-Apr-11 19:45pm    
Thank you very much, I'll need to get familiar with this stuff later. Bookmarked.
--SA
RowanArkison 27-Apr-11 0:52am    
Math.NET is a nice framework. But that is the point why I do not want to use it: it is overpowered for my case and I do not learn very much from using foreign code. So I'll try to set up my own version of reducing the termns with expression trees. I altered my code to build a tree with expressions (not that hard as I thought) and now try to figure out how to reduce the terms.

First idea:

- Find all patterns that can easyly be reduced (2x + 3x for instance)
- search the tree for stand alone "denominators" -> so the right leaf must be present in every leaf on the left side (or the other way round) to reduce the fraction
- ...
Albin Abel 28-Apr-11 5:35am    
Good Information. My 5
Hi,

Symbolism is a C# library which implements automatic simplification of algebraic expressions.

The following bit of code:

C#
var x = new Symbol("x");
var y = new Symbol("y");

var result = (4 * x + 7 * x) / (x * y);

result.DispLong();

displays the following when run:
11 * y ^ -1


Ed
 
Share this answer
 
v2
Comments
Philippe Mori 30-Jul-15 21:07pm    
Why downvote that solution? Reusing something that exists is always a good solution if it satisfay the needs.

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