Package io.jenetics.prog.op
Class MathExpr
- java.lang.Object
-
- io.jenetics.prog.op.MathExpr
-
- All Implemented Interfaces:
Serializable,Function<Double[],Double>
public final class MathExpr extends Object implements Function<Double[],Double>, Serializable
This class allows you to create a math operation tree from an expression string. The expression string may only contain functions/operations defined inMathOp.final MathExpr expr = MathExpr.parse("5 + 6*x + sin(x)^34 + (1 + sin(x*5)/4)/6"); final double result = expr.eval(4.32); assert result == 31.170600453465315; assert 12.0 == MathExpr.eval("3*4"); assert 24.0 == MathExpr.eval("3*4*x", 2); assert 28.0 == MathExpr.eval("3*4*x + y", 2, 4);- Since:
- 4.1
- Version:
- 5.0
- See Also:
MathOp, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static TreeRewriter<Op<Double>>ARITHMETIC_REWRITERThis rewriter implements some common arithmetic identities, in exactly this order.static TreeRewriter<Op<Double>>CONST_REWRITERThis tree-rewriter rewrites constant expressions to its single value.static TreeRewriter<Op<Double>>REWRITERCombination of theARITHMETIC_REWRITERand theCONST_REWRITER, in this specific order.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Doubleapply(Double[] args)booleanequals(Object obj)doubleeval(double... args)Convenient method, which lets you apply the program function without explicitly create a wrapper array.static doubleeval(Tree<? extends Op<Double>,?> expression, double... args)Evaluates the givenexpressionwith the given arguments.static doubleeval(String expression, double... args)Evaluates the givenexpressionwith the given arguments.static Stringformat(Tree<? extends Op<Double>,?> tree)Return the string representation of the giventreeobject.inthashCode()static MathExprparse(String expression)Parses the givenexpressioninto a AST tree.static TreeNode<Op<Double>>parseTree(String expression)Parses the given mathematical expression string and returns the mathematical expression tree.static intrewrite(TreeNode<Op<Double>> tree)Applies theREWRITERto the given (mutable)tree.static intrewrite(TreeNode<Op<Double>> tree, int limit)Applies theREWRITERto the given (mutable)tree.MathExprsimplify()Simplifiesthisexpression by applying the defaultREWRITER.MathExprsimplify(TreeRewriter<Op<Double>> rewriter)Simplifyingthisexpression by applying the givenrewriter.MathExprsimplify(TreeRewriter<Op<Double>> rewriter, int limit)Simplifyingthisexpression by applying the givenrewriterand the given rewritelimit.StringtoString()Return the string representation of thisMathExprobject.TreeNode<Op<Double>>toTree()Return the math expression as operation tree.ISeq<Var<Double>>vars()Return the variable list of this math expression.
-
-
-
Field Detail
-
CONST_REWRITER
public static final TreeRewriter<Op<Double>> CONST_REWRITER
This tree-rewriter rewrites constant expressions to its single value.final TreeNode<Op<Double>> tree = MathExpr.parseTree("1 + 2*(6 + 7)"); MathExpr.CONST_REWRITER.rewrite(tree); assertEquals(tree.getValue(), Const.of(27.0));- Since:
- 5.0
-
ARITHMETIC_REWRITER
public static final TreeRewriter<Op<Double>> ARITHMETIC_REWRITER
This rewriter implements some common arithmetic identities, in exactly this order.sub($x,$x) -> 0 sub($x,0) -> $x add($x,0) -> $x add(0,$x) -> $x add($x,$x) -> mul(2,$x) div($x,$x) -> 1 div(0,$x) -> 0 mul($x,0) -> 0 mul(0,$x) -> 0 mul($x,1) -> $x mul(1,$x) -> $x mul($x,$x) -> pow($x,2) pow($x,0) -> 1 pow(0,$x) -> 0 pow($x,1) -> $x pow(1,$x) -> 1- Since:
- 5.0
-
REWRITER
public static final TreeRewriter<Op<Double>> REWRITER
Combination of theARITHMETIC_REWRITERand theCONST_REWRITER, in this specific order.- Since:
- 5.0
-
-
Constructor Detail
-
MathExpr
public MathExpr(Tree<? extends Op<Double>,?> tree)
Create a newMathExprobject from the given operation tree.- Parameters:
tree- the underlying operation tree- Throws:
NullPointerException- if the givenprogramisnullIllegalArgumentException- if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.
-
-
Method Detail
-
vars
public ISeq<Var<Double>> vars()
Return the variable list of this math expression.- Returns:
- the variable list of this math expression
-
toTree
public TreeNode<Op<Double>> toTree()
Return the math expression as operation tree.- Returns:
- a new expression tree
-
apply
public Double apply(Double[] args)
- Specified by:
applyin interfaceFunction<Double[],Double>- See Also:
eval(double...),eval(String, double...)
-
eval
public double eval(double... args)
Convenient method, which lets you apply the program function without explicitly create a wrapper array.final double result = MathExpr.parse("2*z + 3*x - y").eval(3, 2, 1); assert result == 9.0;- Parameters:
args- the function arguments- Returns:
- the evaluated value
- Throws:
NullPointerException- if the given variable array isnullIllegalArgumentException- if the length of the arguments array is smaller than the program arity- See Also:
apply(Double[]),eval(String, double...)
-
toString
public String toString()
Return the string representation of thisMathExprobject. The string returned by this method can be parsed again and will result in the same expression object.final String expr = "5.0 + 6.0*x + sin(x)^34.0 + (1.0 + sin(x*5.0)/4.0) + 6.5"; final MathExpr tree = MathExpr.parse(expr); assert tree.toString().equals(expr);
-
simplify
public MathExpr simplify(TreeRewriter<Op<Double>> rewriter, int limit)
Simplifyingthisexpression by applying the givenrewriterand the given rewritelimit.- Parameters:
rewriter- the rewriter used for simplifyingthisexpressionlimit- the rewrite limit- Returns:
- a newly created math expression object
- Throws:
NullPointerException- if therewriterisnullIllegalArgumentException- if thelimitis smaller than zero
-
simplify
public MathExpr simplify(TreeRewriter<Op<Double>> rewriter)
Simplifyingthisexpression by applying the givenrewriter.- Parameters:
rewriter- the rewriter used for simplifyingthisexpression- Returns:
- a newly created math expression object
- Throws:
NullPointerException- if therewriterisnull
-
simplify
public MathExpr simplify()
Simplifiesthisexpression by applying the defaultREWRITER.- Returns:
- a newly created math expression object
-
format
public static String format(Tree<? extends Op<Double>,?> tree)
Return the string representation of the giventreeobject. The string returned by this method can be parsed again and will result in the same expression object.final String expr = "5.0 + 6.0*x + sin(x)^34.0 + (1.0 + sin(x*5.0)/4.0) + 6.5"; final MathExpr tree = MathExpr.parse(expr); assert MathExpr.format(tree.tree()).equals(expr);- Parameters:
tree- the tree object to convert to a string- Returns:
- a new expression string
- Throws:
NullPointerException- if the giventreeisnull- Since:
- 4.3
-
parse
public static MathExpr parse(String expression)
Parses the givenexpressioninto a AST tree.- Parameters:
expression- the expression string- Returns:
- the tree representation of the given
expression - Throws:
NullPointerException- if the givenexpressionisnullIllegalArgumentException- if the given expression is invalid or can't be parsed.
-
parseTree
public static TreeNode<Op<Double>> parseTree(String expression)
Parses the given mathematical expression string and returns the mathematical expression tree. The expression may contain all functions defined inMathOp.The example above will lead to the following tree:final Tree<? extends Op<Double>, ?> tree = MathExpr .parseTree("5 + 6*x + sin(x)^34 + (1 + sin(x*5)/4)/6");add ├── add │ ├── add │ │ ├── 5.0 │ │ └── mul │ │ ├── 6.0 │ │ └── x │ └── pow │ ├── sin │ │ └── x │ └── 34.0 └── div ├── add │ ├── 1.0 │ └── div │ ├── sin │ │ └── mul │ │ ├── x │ │ └── 5.0 │ └── 4.0 └── 6.0- Parameters:
expression- the expression string- Returns:
- the parsed expression tree
- Throws:
NullPointerException- if the givenexpressionisnullIllegalArgumentException- if the given expression is invalid or can't be parsed.
-
eval
public static double eval(String expression, double... args)
Evaluates the givenexpressionwith the given arguments.final double result = MathExpr.eval("2*z + 3*x - y", 3, 2, 1); assert result == 9.0;- Parameters:
expression- the expression to evaluateargs- the expression arguments, in alphabetical order- Returns:
- the evaluation result
- Throws:
NullPointerException- if the givenexpressionisnullIllegalArgumentException- if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.- See Also:
apply(Double[]),eval(double...)
-
eval
public static double eval(Tree<? extends Op<Double>,?> expression, double... args)
Evaluates the givenexpressionwith the given arguments.- Parameters:
expression- the expression to evaluateargs- the expression arguments, in alphabetical order- Returns:
- the evaluation result
- Throws:
NullPointerException- if the givenexpressionisnull- Since:
- 4.4
- See Also:
apply(Double[]),eval(double...),eval(String, double...)
-
rewrite
public static int rewrite(TreeNode<Op<Double>> tree, int limit)
Applies theREWRITERto the given (mutable)tree. The tree rewrite is done in place.- Parameters:
tree- the tree to be rewrittenlimit- the maximal number this rewrite rule is applied to the given tree. This guarantees the termination of the rewrite method.- Returns:
- the number of rewrites applied to the input
tree - Throws:
NullPointerException- if the giventreeisnullIllegalArgumentException- if thelimitis smaller than one- Since:
- 5.0
- See Also:
TreeRewriter.rewrite(TreeNode, int)
-
rewrite
public static int rewrite(TreeNode<Op<Double>> tree)
Applies theREWRITERto the given (mutable)tree. The tree rewrite is done in place. The limit of the applied rewrites is set unlimited (Integer.MAX_VALUE).- Parameters:
tree- the tree to be rewritten- Returns:
trueif the tree has been changed (rewritten) by this method,falseif the tree hasn't been changed- Throws:
NullPointerException- if the giventreeisnull- Since:
- 5.0
- See Also:
rewrite(TreeNode, int),TreeRewriter.rewrite(TreeNode)
-
-