math parser ?

M

mark

Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

I have found "JEP", but it's not free:-(


Thanks



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4474 (20091001) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
A

Alessio Stalla

Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

What do you mean by "calculating"? Are you talking about substituting
x, y and z with actual numeric values and compute a numeric result, or
are you referring to symbolic manipulation (e.g. expression
simplification)? In the first case, assuming the syntax of your
expressions is the same as Java's, you can use an interpreted language
with Java-like syntax (BeanShell, JavaScript, ...).
 
M

Martin Gregorie

Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

I have found "JEP", but it's not free:-(
Try a compiler-generator, such as Coco/R. You describe the expression
syntax to it and it spits out Java source that can parse expressions
input and do whatever you want with the result.
 
M

mark

well let's say I have a string like this String s = "(x+y)+(z+k)"; And some
util has function, and than I write
SomeUtil.getExpressionVariavles and it returnes expression variables, and
then i can write
SomeUtil.setVariable("x", 100) for example... and it calculates whole
expression if i set all the values.
I've downloaded jep.jar and it has limited number of functions.How can a jar
file be a trial and last for
30 days :) ? is it possible ?

Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

What do you mean by "calculating"? Are you talking about substituting
x, y and z with actual numeric values and compute a numeric result, or
are you referring to symbolic manipulation (e.g. expression
simplification)? In the first case, assuming the syntax of your
expressions is the same as Java's, you can use an interpreted language
with Java-like syntax (BeanShell, JavaScript, ...).

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4475 (20091002) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4475 (20091002) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
R

Roedy Green

Does anyone knows any api or example for parsing and calculating math
expressions like this ?

see http://mindprod.com/jgloss/parser.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is one brain organ that is optimised for understanding and articulating logical processes, and that is the outer layer of the brain, called the cerebral cortex. Unlike the rest of the brain, this relatively recent evolutionary development is rather flat, only about 0.32 cm (0.12 in) thick, and includes a mere 6 million neurons. This elaborately folded organ provides us with what little competence we do possess for understanding what we do and who we do it.
~ Ray Kurzweil (born: 1948-02-12 age: 61)
 
A

Alessio Stalla

well let's say I have a string like this String s = "(x+y)+(z+k)"; And some
util has function, and than I write
SomeUtil.getExpressionVariavles and it returnes expression variables, and
then i can write
SomeUtil.setVariable("x", 100) for example... and it calculates whole
expression if i set all the values.

Check out JSR-223 (Java scripting API). It allows you to invoke
scripting languages (JavaScript is even bundled with Java 1.6) and set
variables the way you describe. As long as your expressions follow the
mathematical syntax of Java/JavaScript and C-like languages in
general, i.e. + - * / as operators and () for grouping (and no ['s and
{'s) you can use this approach. Depending on the users and the
required security level you might want to check the input strings so
that no one tries to evaluate malicious code.
 
E

Eric Sosman

mark said:
Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

I've written such a thing, but it's a bit long for a
Usenet post (checks: 1148 lines). It may also be overly
elaborate to the point of being baroque, since I did it as
a learning exercise and threw in all sorts of bells and
whistles. The usage is like

Expression expr = Expression.compile(
"length * width * sin(angle * PI / 180)",
new String[] { "length", "width", "angle" } );
double v1 = expr.value( new double[] { 3, 4, 45 } );
double v2 = expr.value( new double[] { 12, 13, 22.5 } );
...

Drop me a line if you'd like a copy by E-mail.
 
D

Daniel Pitts

mark said:
well let's say I have a string like this String s = "(x+y)+(z+k)"; And some
util has function, and than I write
SomeUtil.getExpressionVariavles and it returnes expression variables, and
then i can write
SomeUtil.setVariable("x", 100) for example... and it calculates whole
expression if i set all the values.
I've downloaded jep.jar and it has limited number of functions.How can a jar
file be a trial and last for
30 days :) ? is it possible ?

OGNL can do that kind of thing.
<http://www.opensymphony.com/ognl/>

public Integer calculate() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("x", 100);
model.put("y", 50);
model.put("z", 32);
model.put("k", 16);
return (Integer) Ognl.getValue("(x+y)+(z+k)", new OgnlContext(), model);
}
 
D

Daniel Pitts

Eric said:
mark said:
Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

I've written such a thing, but it's a bit long for a
Usenet post (checks: 1148 lines). It may also be overly
elaborate to the point of being baroque, since I did it as
a learning exercise and threw in all sorts of bells and
whistles. The usage is like

Expression expr = Expression.compile(
"length * width * sin(angle * PI / 180)",
new String[] { "length", "width", "angle" } );
double v1 = expr.value( new double[] { 3, 4, 45 } );
double v2 = expr.value( new double[] { 12, 13, 22.5 } );
...

Drop me a line if you'd like a copy by E-mail.

I don't get why so many people write there own. There are plenty of
easy and free libraries, depending on your need.

OGNL <http://www.opensymphony.com/ognl/> is a great one, I use all the
time as an embedded language. There are plenty of others as well.
 
S

Stefan Ram

Daniel Pitts said:
public Integer calculate() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("x", 100);
model.put("y", 50);
model.put("z", 32);
model.put("k", 16);
return (Integer) Ognl.getValue("(x+y)+(z+k)", new OgnlContext(), model);
}

This is the pure Java SE version
(albeit with a different example expression):

public class Main
{ public static void main( final java.lang.String[] args )
throws javax.script.ScriptException /* js.eval */
{ final javax.script.ScriptEngine js =
new javax.script.ScriptEngineManager().
getEngineByName( "JavaScript" );
js.put( "variable1", 1 );
js.put( "variable2", 2 );
java.lang.System.out.println
( js.eval( "variable1 + 3 * variable2" )); }}

7.0
 
A

Arne Vajhøj

mark said:
Does anyone knows any api or example for parsing and calculating math
expressions like this ?

(x+y)/(y+z)

I have found "JEP", but it's not free:-(

I can see 3 different approaches:

1) Use a library code that parses and evaluates the expression.
The basic technique is wellknow - split up in tokens, convert
from infix to postfix, evaluate via a stack. Other have already
provided URL's for such libraries. I think compiler-compiler
technology is overkill for the purpose (even though I did learn
Bison 20 years ago from such an example).

2) Write the expression in the language of an existing script
language and load a script interpreter for that. Since Java 1.6
that process has been standardized and Java ships with a
JavaScript engine. But many other languages are available.

3) Convert the string expression to a string with valid Java code,
compile it and load and call the resulting class. Since Java 1.6
it has been easy to do memory to memory compile.

Arne
 
R

Roedy Green

What do you mean by "calculating"? Are you talking about substituting
x, y and z with actual numeric values and compute a numeric result, or
are you referring to symbolic manipulation (e.g. expression
simplification)? In the first case, assuming the syntax of your
expressions is the same as Java's, you can use an interpreted language
with Java-like syntax (BeanShell, JavaScript, ...).

Even without scripting languages, you can take an expression, wrap
some source code around it, compile it on the fly, and execute it.
Your snippet can then potentially do anything Java can do, and you
don't need to write any parsers.
see http://mindprod.com/jgloss/onthefly.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

When you can’t find a bug, you are probably looking in the wrong place. When you can’t find your glasses, you don’t keep scanning the same spot because you are convinced that is where you left them.
~ Roedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top