commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guillaume CHAUVET <guillaume.chau...@qualiformed.com>
Subject Re: [math] Expression evaluator
Date Fri, 17 Aug 2012 09:26:12 GMT
> Hi
> [...]
> Could you provide a few small examples of what the usage would look like?
> Also, some use-cases in "real" applications would be welcome in order to
> figure out whether this functionality would fit in Commons Math.
> Thanks,
> Gilles

Hello,
I apologize for not having replied earlier, due to a lack of time :/
We develop softwares solutions for automatic analyses and traceability of quality controls
in radiation therapy and medical imaging : radiotherapy machines treatment (linacs), scanners,
gamma-cameras, Digitalized radiology installations, and MRI. We need to provide mathematical
formulas used in software and allow medical physicists to customize theses formulas, for legal
reasons.

Below, my first sketch of using this potential feature:

Expression.java : Interface
=======================================================
public void addExpression(ExpressionInsertion ei, TerminalExpression te);
public void addExpression(ExpressionInsertion ei, NonTerminalExpression nte);
public void addExpression(ExpressionInsertion ei, FunctionExpression nte, Expression... args);
public void addExpression(FunctionExpression nte, Expression... args);
public void setVariable(String name, double value);
public void removeVariable(String name);

Snippet :
=======================================================
ExpressionBuilder eb = ExpressionBuilderFactory.getInstance().create();

// Creating a custom function with 2 parameters to our ExpressionBuilder
Operator testFunction = new FunctionExpression("test", new FunctionAdapter(new BivariateFunction()
{
    double value(double x, double y) {
       return (x + x) * (y + y) + 5; 
    }
});

// Create a new Expression :
Expression exp = eb.createExpression(new ConstantExpression(22));	
// exp = "22"
exp.setVariable("var1", 1.0);
exp.addExpression(ExpressionInsertion.RIGHT, new AddExpression(20));
// exp = "22 + 20"  
exp.addExpression(ExpressionInsertion.RIGHT, new MulExpression(exp.getVariable("var1")));
// exp = "(22 + 20) * ${var1}"
exp.addExpression(new SqrtExpression());
// exp = " sqrt((22 + 20) * ${var1})"
exp.addExpression(ExpressionInsertion.LEFT, new ConstantExpression(20));
// exp = "20 + sqrt((22 + 20) * ${var1})"
exp.addExpression(testFunction, 10, exp.getCurrentExpression);
// exp = "test(10, 20 + sqrt((22 + 20) * ${var1}))" 

// We can connect to another expression, builded separatly :
Expression exp2 = eb.createExpression(new testFunction, 1, 2);
//exp2 = " test(1, 2)" 
exp2.setVariable("var2", 5.0);
exp2.addExpression(ExpressionInsertion.LEFT, new AddOperation(exp2.getVariable("var2"));
// exp2 = "${var2} + test(1, 2)" 
exp.addExpression(ExpressionInsertion.LEFT, exp2);
// exp2 = "(${var2} + test(1, 2)) + test(10, 20 + sqrt((22 + 20) * ${var1}))"

// Custom visitor who build a string representation of this expression
ExpressionVisitor sev = new StringExpressionVisitor();
exp.accept(exp);
try {
System.out.println("result for expression '" + exp.toString() + "' = " + exp.evaluate());
} catch (MathParseException ex) {
// [...]
}

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


Mime
View raw message