commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Detlef Günther" <Detlef.Guent...@gmx.net>
Subject Re: [math] Improvement: Interpreter for mathematical expressions
Date Tue, 27 Mar 2012 10:53:25 GMT
Hi Thomas,

congrace may be used by writing a wrapper "Expression" to UnivarteFunction:


import org.apache.commons.math3.analysis.UnivariateFunction;

import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;

public class Expression implements UnivariateFunction {
	private Calculable	calc	= null;

	public Expression(String function) throws Exception {
		calc = new ExpressionBuilder(function).withVariableNames("x").build();
	}

	public double value(double x) {
		calc.setVariable("x", x);
		return calc.calculate();
	}

}

Using this you get the folowing code:

final double relativeAccuracy = 1.0e-10;
final double absoluteAccuracy = 1.0e-10;
UnivariateSolver solver = new RegulaFalsiSolver(relativeAccuracy,
						absoluteAccuracy);
Expression exp = new Expression("sin(3*x)");
double root = solver.solve(99999, exp, 9.4, 9.7);
System.out.println("root is " + root + " / " + exp.value(root));

with output:

root is 9.42477796076938 / 1.102182119232618E-15  (= 3*PI)

==> Interpreter may be integrated in commons/math, the same result as my interpreter.

Now I'll compare the functions implemented in congrace.  



Regards Detlef











-------- Original-Nachricht --------
> Datum: Tue, 27 Mar 2012 10:10:41 +0200
> Von: Thomas Neidhart <thomas.neidhart@gmail.com>
> An: Commons Developers List <dev@commons.apache.org>
> Betreff: Re: [math] Improvement: Interpreter for mathematical expressions

> On Tue, Mar 27, 2012 at 10:06 AM, Thomas Neidhart
> <thomas.neidhart@gmail.com
> > wrote:
> 
> > 2012/3/27 "Detlef Günther" <Detlef.Guenther@gmx.net>
> >
> >> Hello,
> >>
> >> it would be nice to have an interpreter for mathematical expressions so
> >> that functions may be defined at runtime.
> >>
> >
> [snip]
> 
> ah something else I have found that looks quite interesting and may be
> used
> to get inspiration:
> 
> http://projects.congrace.de/exp4j/index.html
> 
> Thomas

-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

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


Mime
View raw message