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
Modifier 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_REWRITER
and theCONST_REWRITER
, in this specific order. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
double
eval
(double... args) Convenient method, which lets you apply the program function without explicitly create a wrapper array.static double
Evaluates the givenexpression
with the given arguments.static double
Evaluates the givenexpression
with the given arguments.static String
Return the string representation of the giventree
object.int
hashCode()
static MathExpr
Parses the givenexpression
into an AST tree.Parses the given mathematical expression string and returns the mathematical expression tree.static int
Applies theREWRITER
to the given (mutable)tree
.static int
Applies theREWRITER
to the given (mutable)tree
.simplify()
Simplifiesthis
expression by applying the defaultREWRITER
.simplify
(TreeRewriter<Op<Double>> rewriter) Simplifyingthis
expression by applying the givenrewriter
.simplify
(TreeRewriter<Op<Double>> rewriter, int limit) Simplifyingthis
expression by applying the givenrewriter
and the given rewritelimit
.toString()
Return the string representation of thisMathExpr
object.toTree()
Deprecated, for removal: This API element is subject to removal in a future version.tree()
Return the operation tree underlyingthis
math 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_REWRITER
and theCONST_REWRITER
, in this specific order.- Since:
- 5.0
-
-
Constructor Details
-
MathExpr
Create a newMathExpr
object from the given operation tree.- Parameters:
tree
- the underlying operation tree- Throws:
NullPointerException
- if the givenprogram
isnull
IllegalArgumentException
- 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 underlyingthis
math 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 an 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 isnull
IllegalArgumentException
- if the length of the argument array is smaller than the program arity- See Also:
-
hashCode
-
equals
-
toString
Return the string representation of thisMathExpr
object. 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
Simplifyingthis
expression by applying the givenrewriter
and the given rewritelimit
.- Parameters:
rewriter
- the rewriter used for simplifyingthis
expressionlimit
- the rewrite limit- Returns:
- a newly created math expression object
- Throws:
NullPointerException
- if therewriter
isnull
IllegalArgumentException
- if thelimit
is smaller than zero
-
simplify
Simplifyingthis
expression by applying the givenrewriter
.- Parameters:
rewriter
- the rewriter used for simplifyingthis
expression- Returns:
- a newly created math expression object
- Throws:
NullPointerException
- if therewriter
isnull
-
simplify
Simplifiesthis
expression by applying the defaultREWRITER
.- Returns:
- a newly created math expression object
-
format
Return the string representation of the giventree
object. 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 giventree
isnull
- Since:
- 4.3
-
parse
Parses the givenexpression
into an AST tree.- Parameters:
expression
- the expression string- Returns:
- the tree representation of the given
expression
- Throws:
NullPointerException
- if the givenexpression
isnull
IllegalArgumentException
- 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 givenexpression
isnull
IllegalArgumentException
- if the given expression is invalid or can't be parsed.
-
eval
Evaluates the givenexpression
with 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 givenexpression
isnull
IllegalArgumentException
- 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 givenexpression
with the given arguments.- Parameters:
expression
- the expression to evaluateargs
- the expression arguments, in alphabetical order- Returns:
- the evaluation result
- Throws:
NullPointerException
- if the givenexpression
isnull
- Since:
- 4.4
- See Also:
-
rewrite
Applies theREWRITER
to 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 giventree
isnull
IllegalArgumentException
- if thelimit
is smaller than one- Since:
- 5.0
- See Also:
-
rewrite
Applies theREWRITER
to 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:
true
if the tree has been changed (rewritten) by this method,false
if the tree hasn't been changed- Throws:
NullPointerException
- if the giventree
isnull
- Since:
- 5.0
- See Also:
-
tree()
instead