public final class MathExpr extends Object implements Function<Double[],Double>, Serializable
MathOp
.
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);
MathOp
,
Serialized FormConstructor and Description |
---|
MathExpr(Tree<? extends Op<Double>,?> tree)
Create a new
MathExpr object from the given operation tree. |
Modifier and Type | Method and Description |
---|---|
Double |
apply(Double[] args) |
boolean |
equals(Object obj) |
double |
eval(double... args)
Convenient method, which lets you apply the program function without
explicitly create a wrapper array.
|
static double |
eval(String expression,
double... args)
Evaluates the given
expression with the given arguments. |
int |
hashCode() |
static MathExpr |
parse(String expression)
Parses the given
expression into a AST tree. |
static Tree<? extends Op<Double>,?> |
parseTree(String expression)
Parses the given mathematical expression string and returns the
mathematical expression tree.
|
static void |
prune(TreeNode<Op<Double>> tree)
Tries to simplify the given math tree in place.
|
MathExpr |
simplify()
Tries to simplify
this math expression. |
static Tree<? extends Op<Double>,?> |
simplify(Tree<? extends Op<Double>,?> tree)
Tries to simplify the given math tree.
|
String |
toString()
Return the string representation of this
MathExpr object. |
static String |
toString(Tree<? extends Op<Double>,?> tree)
Return the string representation of the given
tree object. |
Tree<? extends Op<Double>,?> |
toTree()
Return the math expression as operation tree.
|
ISeq<Var<Double>> |
vars()
Return the variable list of this math expression.
|
public MathExpr(Tree<? extends Op<Double>,?> tree)
MathExpr
object from the given operation tree.tree
- the underlying operation treeNullPointerException
- if the given program
is null
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.public ISeq<Var<Double>> vars()
public Tree<? extends Op<Double>,?> toTree()
public Double apply(Double[] args)
apply
in interface Function<Double[],Double>
eval(double...)
,
eval(String, double...)
public double eval(double... args)
final double result = MathExpr.parse("2*z + 3*x - y").eval(3, 2, 1);
assert result == 9.0;
args
- the function argumentsNullPointerException
- if the given variable array is null
IllegalArgumentException
- if the length of the arguments array
is smaller than the program arityapply(Double[])
,
eval(String, double...)
public String toString()
MathExpr
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);
public MathExpr simplify()
this
math expression.
final MathExpr expr = MathExpr.parse("4.0 + 4.0 + x*(5.0 + 13.0)");
final MathExpr simplified = expr.simplify()
System.out.println(simplified);
8.0 + (x*18.0)
.prune(TreeNode)
,
simplify(Tree)
public static String toString(Tree<? extends Op<Double>,?> tree)
tree
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.toString(tree.tree()).equals(expr);
tree
- the tree object to convert to a stringNullPointerException
- if the given tree
is null
public static MathExpr parse(String expression)
expression
into a AST tree.expression
- the expression stringexpression
public static Tree<? extends Op<Double>,?> parseTree(String expression)
MathOp
.
final TreeNode<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
expression
- the expression stringNullPointerException
- if the given expression
is null
IllegalArgumentException
- if the given expression is invalid or
can't be parsed.public static double eval(String expression, double... args)
expression
with the given arguments.
final double result = MathExpr.eval("2*z + 3*x - y", 3, 2, 1);
assert result == 9.0;
expression
- the expression to evaluateargs
- the expression arguments, in alphabetical orderNullPointerException
- if the given program
is null
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.apply(Double[])
,
eval(double...)
public static Tree<? extends Op<Double>,?> simplify(Tree<? extends Op<Double>,?> tree)
final Tree<? extends Op<Double>, ?> tree =
MathExpr.parseTree("4.0 + 4.0 + x*(5.0 + 13.0)");
final Tree<? extends Op<Double>, ?> simplified = MathExpr.simplify(tree)
System.out.println(simplified);
add
├── 8.0
└── mul
├── x
└── 18.0
tree
- the math tree to simplifyNullPointerException
- if the given tree
is null
prune(TreeNode)
,
simplify()
public static void prune(TreeNode<Op<Double>> tree)
tree
- the math tree to simplifyNullPointerException
- if the given tree
is null
simplify(Tree)
,
simplify()
© 2007-2018 Franz Wilhelmstötter (2018-02-14 08:27)