public abstract class Expression extends Object implements Cloneable
Expression
describes a mathematical expression (term)
and usually it is the result of a String
-parsing by
the Parser
.
Every Expression
consists in further Expressions
building an expression-tree-hirarchy. BinaryOperations
,
UnaryOperations
and Lists
are containers holding
at least one other Expression
. Symbols
are the
atomic components in the expression-tree-hirarchy - they are the "leafs of
the tree".
Parser.parse()
Constructor and Description |
---|
Expression() |
Modifier and Type | Method and Description |
---|---|
Expression |
differentiate(String str)
Creates a new Symbol with String str and delegates to
differentiate(Symbol) . |
abstract Expression |
differentiate(Symbol s)
Returns the derivative of this Expression with respect to the Symbol s.
|
abstract Expression |
dissolveFunctionCalls()
Inserts the arguments of any FunctionCall in the expression-tree-hierarchy
into the appropriate function definitions and replaces the FunctionCalls
by that new Expressions.
|
Collection |
getEssentialVariables()
|
abstract void |
getEssentialVariables(Collection c)
Add all names of essential
Variables into the specified
Collection . |
abstract Evaluator |
getEvaluator(Type defaultType,
Context context)
Return the
Evaluator for this Expression . |
protected abstract int |
getPriority()
Return the evaluation priority of this
Expression . |
abstract boolean |
isComplete()
Return
true if this Expressions is ready
to get evaluated, false otherwise. |
abstract boolean |
isConstant()
Returns
true if this Expression is a
constant value, false otherwise. |
abstract Expression |
replaceSymbol(Symbol s,
Expression expr)
Replaces all Symbols equal to s in the expression-tree-hierarchy
with the specified Expression.
|
abstract Expression |
simplify()
Returns a new Expression which is equal to this but simplified.
|
public Expression differentiate(String str)
differentiate(Symbol)
.public abstract Expression differentiate(Symbol s)
simplify
the derivative as
well as possible before returning it.public abstract Expression dissolveFunctionCalls()
Function.insertArguments(Expression)
public Collection getEssentialVariables()
Collection
of the names of all
Variables
which are essential to evaluate this
Expression
. In other words: If at least one
Variable
(the returning Collection
contains) is
not defined by the DefinitionModel
yet, this
Expression
can't be evaluated.getEssentialVariables(Collection)
to store the Variable
names in a Collection defined by
yourself.
Attention: If you call this method on an Expression
which is a UserDefinedFunction
definition, Symbols
representing function parameters in that
Expression
can't be differ from Symbols
representing global Variables
. That's why the parameter names
will be also added to the Collection
, although they are not
representing essential Variables
because
Evaluators
for the
parameters will be provided by the appropriate FunctionCallEvaluator
and not by Variable
objects.
Variable
namesUserDefinedFunction.getDefinition()
,
FunctionCallEvaluator.getEvaluator(Symbol)
public abstract void getEssentialVariables(Collection c)
Variables
into the specified
Collection
. See getEssentialVariables()
for details.c
- the Collection
the Variable
names
will be added to.public abstract Evaluator getEvaluator(Type defaultType, Context context)
Evaluator
for this Expression
.
The specified Type
determines the kind the ReturnType of
any Symbol
in the expression-tree-hirarchy should be evaluated to.
The specified Context
provides the evaluation of that
Symbols
, which can be global Variables
but
also function-parameters.defaultType
- the Type
any Symbol
will be
evaluated to.context
- the Context
, the Evaluators for
the Symbols
are getting from.
Evaluator
for this Expression
.Evaluator.getReturnType()
,
Context.getEvaluator(Symbol)
protected abstract int getPriority()
Expression
. For
example, a BinaryOperation
of type '+' returns a lower priority
than a BinaryOperation
of type '*' (3+4*5).
The priority of any Expression
decides on the structure
in the expression-tree-hirarchy respectively the correct sequence of the
later Evaluator
tree.
Usually the Parser
respectively the ExpressionFactory
defines the priority of any created Expression
.
Expression
.Parser.parse()
,
getEvaluator(Type,Context)
public abstract boolean isComplete()
true
if this Expressions
is ready
to get evaluated, false otherwise.
If this Expression
is not a Symbol
,
it's evaluatable if all Sub-Expressions
(e.g.: the operands
of a BinaryOperation
) are evaluatable in a kind
of recursion.true
if this Expressions
is ready
to get evaluated, false otherwise.public abstract boolean isConstant()
true
if this Expression
is a
constant value, false otherwise.
If this Expression
is not a Symbol
,
it's constant if all Sub-Expressions
(e.g.: the operands
of a BinaryOperation
) are constant in a kind
of recursion.true
if this Expression
is a
constant value, false otherwise.public abstract Expression replaceSymbol(Symbol s, Expression expr)
s
- the Symbol to be replace.expr
- the Expression for replacement.Symbol.equals(Object)
public abstract Expression simplify()
The following table shows source operations and it's simplifications. a and b are arbitrary expressions. Note that often there is no simplification (e.g. 1 + a).
source operation # | + | - | * | / | ^ |
---|---|---|---|---|---|
#a | a | -a | n.a. | ||
#(#a) | a | a | |||
0 # a | a | -a | 0 | 0 | 0 |
a # 0 | a | a | 0 | a / 0 | 1 |
1 # a | 1 + a | 1 - a | a | 1 / a | 1 |
a # 1 | a + 1 | a - 1 | a | a | a |
a # a | a + a | 0 | a * a | 1 | a ^ a |
a # b | a + b | a - b | a * b | a / b | a ^ b |
-a # b | b - a | -(a + b) | -(a * b) | -(a / b) | -a ^ b |
a # -b | a - b | a + b | -(a * b) | -(a / b) | a ^ -b |
-a # -b | -(a + b) | b - a | a * b | a / b | -(a ^ -b) |
Note: "-0" will be seen equal to "0" in any case (e.g.: "1/-0" -> "1/0")