On Mar 27, 2012, at 5:55 AM, Gilles Sadowski wrote:
> 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: [email protected]
> For additional commands, e-mail: [email protected]
>
>
Somewhat relevant to this discussion is my AMath4JTcl project. It's still
early in the project and so I wouldn't recommend running a nuclear power plant
with it, but it is getting to a point where it is showing its utility.
Basically it adds an expression processor to the JTcl project (Java
implementation of the Tcl dynamic programming language) that supports using
vectors and matrices (as implemented in Commons Math) as well as direct access
to various Commons Math features such as linear algebra, Fourier Transforms,
and optimization (and more coming). For those who have experience in Tcl, one
would normally write a simple math expression (here the equation of a line)
with the somewhat ugly:
set y [expr {$a*$x+$b}]
with the AMath4JTcl library you can write
set y [= a*x+b]
and the variables (like x and y) can be vectors and matrices (much like an
environment like MatLab)
Integration with my Fleet concurrent programming library for JTcl (very new) is
also possible to allow distributing scripted calculations across multiple
cores/processors.
The project is at: http://amath4jtcl.kenai.com/
Look at the Getting Started page for some simple examples.
AMath4JTcl can also be integrated with my Swank ("Tk GUI toolkit in Java") to
allow for a full GUI with plotting etc.
Suggestions, contributions comments etc. are all welcomed.
cheers
Bruce
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]