Solving a variable

92 views
Skip to first unread message

Sebb

unread,
Jan 3, 2014, 2:08:02 PM1/3/14
to jep-...@googlegroups.com
Hello, I'm having a bit of trouble getting the values that I need. I need to be able to parse an equation that could for example be "X+20=30" but If have other values on the same side as the X I don't get the correct answer. "X=30" works.

                    Node node = mathParser.parseExpression("X+20=30");
                    mathParser.evaluate(node);
                    txtAnswer.setText(mathParser.getSymbolTable().toString());

Richard Morris

unread,
Jan 4, 2014, 8:40:14 AM1/4/14
to jep-...@googlegroups.com, sebastian.p...@gmail.com
Hi Sebb,

I'm not quite sure what you want to do. Are you trying to solve the equation, test equality of both sides or something else.

By default = is an assignment operation, it takes a variable on the left hand side and sets the value of the variable to the result of evaluating the right hand side.

The equality operator is ==, returning true if both sides evaluate to the same value and false otherwise.

You may find X+20==30 will give the answer you want. 

You can change the symbols used so that assignment is := and equals is = buy using the configurable parser. See http://www.singularsys.com/jep/doc/html/confparser.html and http://www.singularsys.com/jep/doc/html/operators.html
Jep jep = new Jep(new StandardConfigurableParser());
jep.getOperatorTable().getAssign().setSymbol(":=");
jep.getOperatorTable().getEquals().setSymbol("=");
jep.reinitializeComponents();
If you are trying to solve the equation X+20=30 which has solution X=10, this is beyond the capability of the standard Jep but I'm working on extensions which allow certain classes of equations to be solved.

Sebb

unread,
Jan 4, 2014, 8:47:13 AM1/4/14
to jep-...@googlegroups.com, sebastian.p...@gmail.com

If you are trying to solve the equation X+20=30 which has solution X=10, this is beyond the capability of the standard Jep but I'm working on extensions which allow certain classes of equations to be solved.


This is what I am trying to do.  

Richard Morris

unread,
Jan 4, 2014, 3:48:06 PM1/4/14
to jep-...@googlegroups.com, sebastian.p...@gmail.com
The way to tackle this depends on the type of equation or equations you are trying to solve. 

The first question is how many equations and how many variables? 

The second question is the type of equation, simple linear equations:  3 x + 4 = 13, 
quadratics: x^2 + 2x - 9 = 0, polynomials: x^3 + x^2 + x +1 = 7, or general equations with other functions.

The third question is whether you want symbolic solutions which involve rearranging the equation to get your desired result or numeric solutions.

If the example is typical of the problems you are likely to encounter, i.e. single linear equations in a single variable then a numerical solution would be possible to implement with not too much code. Anything beyond that require considerably more code, linear equation in any number of variables are readily solvable as are quadratics in a single variable but require an additional extension to jep. Beyond that would involve a full blow computer algebra system.

Richard
Reply all
Reply to author
Forward
0 new messages