public abstract class Function extends Object
Function
object represents a mathematical function with a
name, one or more parameters and a function definition. The function
definition is an Expression
.
A Function
object is used for the Evaluator
to be
able to evaluate FunctionCall
Expressions
.
Before evaluating a FunctionCall
in the
expression-tree-hirarchy, the appropriate Function
must be
defined or added to the DefinitionModel
, e.g.:
Type
defaultType=
RealType.TYPE
;
ExpressionConfiguration
config=new
ExpressionConfiguration(defaultType);
//define the function f(x,y)=x+y
config.defineFunction
("f", new String[] {"x","y"}, "x+y");
config.setExpression
("2*f(3,4)")
All PredefinedFunctions
were added to the
DefinitionModel
by that constructor.
In opposite to Variables
, a
Function
must be already defined before parsing a String
containing the appropriate FunctionCall
, otherwise the
Parser
can't differ between a Variable
in
an implicit multiplication or a Function
- it looks identical,
for example:
You want to parse the Expression
"f(2+2)".
If the parsed identifier "f" is defined as a Function
, the
Parser
(respectively its ExpressionConfiguration
) will
create a FunctionCall
.
But if "f" is defined as a Variable
(or not at all), the
Parser
(respectively its ExpressionConfiguration
) will create
a BinaryOperation
"f*(2+2)".
Modifier and Type | Field and Description |
---|---|
protected String |
name
This function's name.
|
protected String[] |
parameter
This function's parameters.
|
Constructor and Description |
---|
Function(String name,
String[] p)
Creates a new
Function with the specified name
and parameters . |
Modifier and Type | Method and Description |
---|---|
abstract Evaluator |
getCallEvaluator(Type defaultType,
Context context)
Return the
Evaluator for this Function . |
abstract Function |
getDerivative(int paramIndex)
Returns the derivative function of this Function with respect to the
paramIndex' parameter.
|
String |
getName()
Returns this' function name.
|
String |
getParameter(int index)
Returns the index' parameter name.
|
int |
getParameterCount()
Returns the number of parameters this
Function depends on. |
int |
getParameterIndex(String param)
Returns the index of the parameter
param or -1
if this Function does not have that parameter. |
String[] |
getParameters()
Returns all parameter names.
|
abstract Expression |
insertArguments(Expression param)
Returns this function dissolved.
|
void |
setParameters(String[] newParameters)
Sets the parameter names of this
Function to the
Strings in the specified array. |
protected String name
protected String[] parameter
public abstract Evaluator getCallEvaluator(Type defaultType, Context context)
Evaluator
for this Function
. If this
Function is a PredefinedFunction
, this method will call the method
Type.getEvaluator(PredefinedFunction,Evaluator)
on that Type the parameter evaluator returns.defaultType
- the Type
, Symbols
will be
evaluated to.context
- the Context
, Evaluators
for
Symbols
will be get from.Evaluator
for this Function
.public abstract Function getDerivative(int paramIndex)
Function
that is the derivative of this with
respect to the paramIndex' parameterExpression.differentiate(Symbol)
public String getName()
Function
will be
defined by any constructor. A Function
can't be renamed.public String getParameter(int index)
index
- the index of the parameter which name is to be returned.public int getParameterCount()
Function
depends on.Function
.public int getParameterIndex(String param)
param
or -1
if this Function
does not have that parameter.param
- the parameter which index is in demand.param
or -1
if this Function
does not have that parameter.public String[] getParameters()
public abstract Expression insertArguments(Expression param)
param
- public void setParameters(String[] newParameters)
Function
to the
Strings
in the specified array.newParameters
- an array of the new parameter names.