views:

276

answers:

3

Hi, I have to built dynamically equations like following:

x + x/3 + (x/3)/4 + (x/3/4)/2 = 50

Now I would like to evaluate this equation and get x. The equation is built dynamically. x is the leaf node in a taxonomy, the other 3 nodes are the super concepts. The divisor represents the number of children of the child nodes.

Is there a library that allows to build such equations dynamically and resolve x?

Thanks, Chris

A: 

If you're planning to use Java, you can try JAS. It claims to be able to solve polynomials equations.

FTA:

The Java Algebra System (JAS) is an object oriented, type safe and multi-threaded approach to computer algebra. JAS provides a well designed software library using generic types for algebraic computations implemented in the Java programming language. The library can be used as any other Java software package or it can be used interactively or interpreted through an jython (Java Python) front end. The focus of JAS is at the moment on commutative and solvable polynomials, Groebner bases and applications. By the use of Java as implementation language JAS is 64-bit and multi-core cpu ready.

artgon
+1  A: 

Are your equations always of this form (linear in x)? If so, when building the equation, just set x to 1 and evaluate the lhs. This will give you lhs = 1 + 1/3 + (1/3)/4 + (1/3/4)/2 = 1.4583.. Then calculate x = rhs / lhs = 50 / 1.4583

Henrik
yes they are always in this form! is there a java library handling this? THANKS
+1  A: 

It might help you to do some algebra on it.

Note that:

x= 3*x/3 = (x*4*3*2)/(4*3*2)
x+x/3 = 3x/3 + x/3 = 4x/3

and in your particular case:

x + x/3 + (x/3)/4 + (x/3/4)/2 = (x*4*3*2)/(4*3*2) + (x*4*2)/(4*3*2) + (x*2)/(4*3*2) + (x)/(4*3*2) 
= (4*3*2x + 4*2x + 2*x + x)/(4*3*2)

Perhaps if you can find a way to have the left hand side rewritten as a single big fraction like this, the solution will come much easier. Also, factor out the x

(4*3*2x + 4*2x + 2*x + x)/(4*3*2) = x*(4*3*2 + 4*2 + 2 + 1)/(4*3*2)

Then solve for x

50= x*(a/b)
50*(b/a) = x

Since you have some code generating the polynomial, you should be able to generate this big (a/b) fraction thing pretty easily too. I purposely did not simplify the multiplications so that it is clear where each component comes from.

Karl