java.lang.Object
io.jenetics.prog.op.MathExpr
- All Implemented Interfaces:
Serializable,Function<Double[],Double>
Contains methods for parsing mathematical expression.
- Since:
- 4.1
- Version:
- 7.1
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TreeRewriter<Op<Double>>This rewriter implements some common arithmetic identities, in exactly this order.static final TreeRewriter<Op<Double>>This tree-rewriter rewrites constant expressions to its single value.static final TreeRewriter<Op<Double>>Combination of theARITHMETIC_REWRITERand theCONST_REWRITER, in this specific order. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleandoubleeval(double... args) Convenient method, which lets you apply the program function without explicitly create a wrapper array.static doubleEvaluates the givenexpressionwith the given arguments.static doubleEvaluates the givenexpressionwith the given arguments.static StringReturn the string representation of the giventreeobject.inthashCode()static MathExprParses the givenexpressioninto a AST tree.Parses the given mathematical expression string and returns the mathematical expression tree.static intApplies theREWRITERto the given (mutable)tree.static intApplies theREWRITERto the given (mutable)tree.simplify()Simplifiesthisexpression by applying the defaultREWRITER.simplify(TreeRewriter<Op<Double>> rewriter) Simplifyingthisexpression by applying the givenrewriter.simplify(TreeRewriter<Op<Double>> rewriter, int limit) Simplifyingthisexpression by applying the givenrewriterand the given rewritelimit.toString()Return the string representation of thisMathExprobject.toTree()Deprecated, for removal: This API element is subject to removal in a future version.tree()Return the operation tree underlyingthismath expression.vars()Return the variable list of this math expression.
-
Field Details
-
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
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
Combination of theARITHMETIC_REWRITERand theCONST_REWRITER, in this specific order.- Since:
- 5.0
-
-
Constructor Details
-
MathExpr
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 Details
-
vars
Return the variable list of this math expression.- Returns:
- the variable list of this math expression
-
tree
Return the operation tree underlyingthismath expression.- Returns:
- the operation tree s
- Since:
- 7.1
-
toTree
Deprecated, for removal: This API element is subject to removal in a future version.Will be removed, usetree()insteadReturn the math expression as operation tree.- Returns:
- a new expression tree
-
apply
-
eval
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:
-
hashCode
-
equals
-
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
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
Simplifyingthisexpression by applying the givenrewriter.- Parameters:
rewriter- the rewriter used for simplifyingthisexpression- Returns:
- a newly created math expression object
- Throws:
NullPointerException- if therewriterisnull
-
simplify
Simplifiesthisexpression by applying the defaultREWRITER.- Returns:
- a newly created math expression object
-
format
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
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
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
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:
-
eval
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:
-
rewrite
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:
-
rewrite
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:
-
tree()instead