Rearranging a mathematical equation
hi,
the problem is say we have an equation a=b+c+d and i have values of a,b,d. now i need to evaluate d. so the equation should be rearranged as d=a-b-c.
how do we do this programatically. i mean we have athe equation stored and at runtime we need to rearrange the equation. how do we go about doing it.
[323 byte] By [
elitejyoa] at [2007-10-2 21:16:24]

> how do we do this programatically. i mean we have
> athe equation stored and at runtime we need to
> rearrange the equation. how do we go about doing it.
Define "we have it stored"
How do you have it stored? Is it a string? Is it an expression tree? Is it Java code?
How complicated is the strored expression. Is is just a sum of variables with no coefficients, or could it involve trigonometric functions and differential operators.
Needless to say, different problems require different solutions.
You can look at simple constraint systems, such as this example:
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html#%_sec_3.3.5
If you need something more sophisticated, then looking at the implementation of OpenModelica or Maxima.
It's not too difficult to do simple constraint systems in Java, though all the big tools use functional languages of some kind.
Pete
hi,
let me elaborate on what i want.
I have an equation say x=y+3 stored as a string and i want to evaluate this equation for y. i give the value of x as 2 and want to evalutae equation for y which should go like this. it should first rearrange the equation as y=x-3 and then evaluate it. I am looking for a piece of code or something like that which would do such rearrangement of an equation. The equation would be simple first order equations like x=2y*3d+z where x,y,d,z are variables.
> The equation would be simple first order equations like
> x=2y*3d+z where x,y,d,z are variables.
What about equations like this: x=2*y*3*d+3*y*4*e+z? If you want to
solve w.r.t. 'y' you have to collect a bunch of terms first before you can
start rewriting your expression.
Collecting == rewriting the expression such that 'y' only occurs once
in one subexpression.
kind regards,
Jos
do u have any idea how to go about this.
Here is a way to go about solving for the one variable, but it does not involve doing a rearrangement
You parse your string into expression form. This requires that you decide what expressions you will allow and which you will not allow. You claim that they are linear and then show a quadratic expression as an example. You have two variables mulitplied, technically that is quadratic. True, once you have substituted all variables but one it becomes linear, perhaps that is what you meant by linear.
Furthermore, sometimes you show a multiply with a * and sometimes you write 2y with a number adjacent to a variable. You can't parse a little expression language unless you know what your little language is. Are these strings being given to you? Are they already in some fixed form or can you define your little expression language? If you can define your own language you can make the expressions MUCH easier to parse.
As it is, you have given conflicting information about your language and we can't tell you how to parse something that only lives in your head.
In any case, the only rearrangement that you do is to convert from an equation to an expression that equals zero. During your parse, just subtract one side of your equation from the other.
What the parse does is convert the string expression into a tree expression that you can evaluate. You will have a tree where each internal node (that would be a node with two children) is associated with an operator (either plus or minus or times) and every leaf node (one with no children) is either a variable or a constant.
Once you plug in values for the variables, you can use a recursive routine to evaluate your tree basically eval(node) = eval(leftChild) op eval(rightChild); where op is the operator associated with node.
If your expressions really are linear. You can solve for the single unknown in the following way.
Plug in all the known variables and use the value 0 for the one unknown variable.
Evaluate the entrie expression. If it is zero you are done, you got lucky and the proper value for the unknown just happened to be zero.
if it was not zero remember the result (call it A) and evaluate the expression again this time plugging in 1 for the unknown variable.
Once again if you got zero you got lucky and know that one is the answer.
If not call the result B and now you use the linearity to solve for the value that makes the expression 0
since expression is linear:
expression = m*x + b
A = expression(0) = m*0 + b = b
B = expression(1) = m*1 + b = m + b
so expression(-A/(B-A)) = m*(-b/m) + b = 0
And of course if B == A there is no solution (e.g. x = 2y - 2y has no solution for y when x = 3)
Solving the linear equation is easy, parsing you input is easy or messy depending on how messy your little expression language is.
If you are free to redefine your language the easiest parse is if you write your expression in reverse polish. e.g. instead of writing x + y you write xy+ instead of 3*z you have 3z* instead of 2x+y you have x2*y+
Reverse polish is easy to parse and evaluate because it follows the simple rule, "First you create the left child, then you create the right child then you create the node that operates on those two children."
I hope that this helps.
> since expression is linear:
> expression = m*x + b
> A = expression(0) = m*0 + b = b
> B = expression(1) = m*1 + b = m + b
> so expression(-A/(B-A)) = m*(-b/m) + b = 0
Nice little trick. I once used pattern rewriting for expression manipulation
once. It isn't perfect yet for two reasons:
1) I wrote it;
2) it isn't finished yet.
Expressions are parsed to their ASTs and a rule based rewriting
engine tries to apply rules repeatedly until no rule applies anymore.
It's a bit like a Thue system applied to trees. What I like about this
scenario is that the 'rules' are extremely simple while the results
amaze me now and then. I tried this e.g.
> '((a*y)+a*a+(1-a)*y-(a-b)*(a+b) == b*b) collect;
((((1+a)-a)*y+a**2)-(a+b)*(a-b)==b**2)
> simplify;
(y==0)
Cute eh? Just a bunch of extremely simple rules and no algebraic
knowledge in the rewriting system itself under the sleeves ;-)
kind regards,
Jos