commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
Subject Re: [math] Improvement: Interpreter for mathematical expressions
Date Tue, 27 Mar 2012 09:55:40 GMT
Hello.

> 
> it would be nice to have an interpreter for mathematical expressions so that functions
may be defined at runtime.
> 
> Example for f(x):
> 
>   ExpInter ei = new ExpInter();
>   ei.setFunction("ln(3*sin(2*x)+3)");
>   // Plot the function:
>   for (double x=0.0;x<3.0;x+=0.1) {
>     y = ei(x);
>     // ... do something useful with x,y
>   }
> 
> Can be expanded to f(x,y) or functions wit params f(x,p1,p2,p3):
> 
>   ei.setFunction("1+p0*x-5*p1*(p2+7)*p3/x");
>   double[] params = { 1.0, 2.0, 4.0, -1 };
>   ei.setParams(params);
>   // plot ....
> 
> If the interpreter implements UnivariateFunction it would be even nicer:
> 
> ExpInter function = new ExpInter("x^3-2*x^2+x-27");
> double root = solver.solve(99999, function, 0.0, 5.0);
> System.out.println("root is " + root + " / " + function.value(root));
> 
> function.setFunction("sin(x)");
> root = solver.solve(99999, function, 3.0, 4.0);
> System.out.println("root is " + root + " / " + function.value(root));
> 
> Result:
> de.guenther.roots.RegulaFalsi:
>         [java] root is 3.7009897720655234 / 0.0
>         [java] root is 3.141592653589793 / 1.2246467991473532E-16
> 
> I may post my interpreter.

Did you implement a parser from scratch, or do you use existing libraries?

[I must say that I'm wary of such a functionality, that has more to do with
string manipulation than mathematical concepts or numerical algorithms.
Clearly, it's something that could rely on CM when it comes to evaluation.
But it should probably not (IMHO) become part of CM.]


Best regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message