net.sourceforge.webcompmath.functions
Class ExpressionFunction

java.lang.Object
  extended by net.sourceforge.webcompmath.functions.FunctionParserExtension
      extended by net.sourceforge.webcompmath.functions.ExpressionFunction
All Implemented Interfaces:
java.io.Serializable, ExpressionCommand, Function, MathObject, ParserExtension
Direct Known Subclasses:
WCMExpressionFunctionBean

public class ExpressionFunction
extends FunctionParserExtension

An ExpressionFunction is a Function that is created from an expression and a list of variables that serve as the parameter(s) of the function. (This is essentially a lambda operation, forming a function such as "lambda(x,y) (x^2+y^2)") Since an ExpressionFunction is a FunctionParserExtension, functions defined from this class can be added to a Parser and then used in expressions parsed by that parser.

See Also:
Serialized Form

Field Summary
protected  Expression definition
           
protected  Variable[] params
          Variable(s) that act as the parameters.
 
Fields inherited from class net.sourceforge.webcompmath.functions.FunctionParserExtension
name
 
Constructor Summary
ExpressionFunction(java.lang.String name, java.lang.String def)
          Constuct a function of one parameter, named "x", by parsing the String, def, to get the definition of the function.
ExpressionFunction(java.lang.String name, java.lang.String[] paramNames, java.lang.String def, Parser parser)
          Constuct a function of one or more parameters by parsing the String, def, to get the definition of the function.
ExpressionFunction(java.lang.String name, Variable[] params, Expression definition)
          Construct a function from a list of variables that serve as parameters and an expression that, presumably, can include those variables.
 
Method Summary
 void apply(StackOfDouble stack, Cases cases)
          Find the value of the function applied to arguments popped from the stack, and push the result back onto the stack.
 boolean dependsOn(Variable x)
          Return true if the definition of this function depends in some way on the variable x.
 Function derivative(int wrt)
          Return the derivative of the function with repect to argument number wrt, where the arguments are numbered 1, 2, 3,....
 Function derivative(Variable x)
          Return the derivative of the function with respect to the variable x.
 int getArity()
          Return the number of arguments of this function.
 java.lang.String getDefinitionString()
          Return the expression that defines this function, as a string.
 double getVal(double[] arguments)
          Find the value of the function at the argument values given by arguments[0], arguments[1], ...
 double getValueWithCases(double[] arguments, Cases cases)
          Find the value of the function at the argument values given by arguments[0], arguments[1], ...
 void redefine(java.lang.String def)
          Set the definition of this function by parsing the given string, using a default parser.
 void redefine(java.lang.String def, Parser parser)
          Set the definition of this function, using the specified parser (or a default parser if parser is null).
 java.lang.String toString()
          Return a string that describes this function, such as "function f(x,y) given by x^2 - y^2".
 
Methods inherited from class net.sourceforge.webcompmath.functions.FunctionParserExtension
appendOutputString, compileDerivative, doParse, extent, getName, setName, setParensCanBeOptional
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

definition

protected Expression definition

params

protected Variable[] params
Variable(s) that act as the parameters.

Constructor Detail

ExpressionFunction

public ExpressionFunction(java.lang.String name,
                          java.lang.String def)
Constuct a function of one parameter, named "x", by parsing the String, def, to get the definition of the function. A standard Parser, with default options and knowledge only of "pi", "e" and the standard functions is used. The variable "x" is also added to this parser while the function is being parsed.

Parameters:
name - Name of function. This should not be null if the function is to be used in a Parser.
def - contains definition of the function, as a function of "x".

ExpressionFunction

public ExpressionFunction(java.lang.String name,
                          java.lang.String[] paramNames,
                          java.lang.String def,
                          Parser parser)
Constuct a function of one or more parameters by parsing the String, def, to get the definition of the function. The given parser is used to parse the definition, so the definition can refer to objects registered with the parser (such as other variables or functions). Furthermore, if both name and parser are non-null, then the function is registered with the parser so that it can then be used in expressions parsed by the parser. (It's possible to have a function of zero arguements. In that case, the function serves as a "named expression".)

Parameters:
name - Name of function.
paramNames - Names of the parameters of the function. The lenght of this array determines the arity of the function.
def - The definition of the function, in terms of the parameters from the paramNames array.
parser - Used to parse the definition. If this is null, a standard parser is used. The paramaters are temporarily added onto the parser while the function definition is being parsed.

ExpressionFunction

public ExpressionFunction(java.lang.String name,
                          Variable[] params,
                          Expression definition)
Construct a function from a list of variables that serve as parameters and an expression that, presumably, can include those variables. WARNING: When the function is evaluated, the values of the parameter variables can change, so you should probably not use variables that are being used elsewhere in your program.

Parameters:
name - name of the function
params - variables to use
definition - definition of the function
Method Detail

redefine

public void redefine(java.lang.String def)
Set the definition of this function by parsing the given string, using a default parser. The definition is in terms of the parameter names originally provided in the constructor.

Parameters:
def - definition of the function

redefine

public void redefine(java.lang.String def,
                     Parser parser)
Set the definition of this function, using the specified parser (or a default parser if parser is null). The definition is in terms of the parameter names originally provided in the constructor. (This routine does not register the function with the parser, but if it was already registered with the parser, it stays registered with the new definition.) Note that changing the definition of the function effectively changes the definition of any other expression that refers to this function.

Parameters:
def - definition of the function
parser - parser to use

getDefinitionString

public java.lang.String getDefinitionString()
Return the expression that defines this function, as a string.

Returns:
definition of the function

toString

public java.lang.String toString()
Return a string that describes this function, such as "function f(x,y) given by x^2 - y^2".

Overrides:
toString in class java.lang.Object
Returns:
string representation of the function

getArity

public int getArity()
Return the number of arguments of this function.

Returns:
number of args.

getVal

public double getVal(double[] arguments)
Find the value of the function at the argument values given by arguments[0], arguments[1], ... The length of the array argument should be equal to the arity of the function. If not, an IllegalArgumentException is thrown.

Parameters:
arguments - array of args. to use in evaluation
Returns:
the value

getValueWithCases

public double getValueWithCases(double[] arguments,
                                Cases cases)
Find the value of the function at the argument values given by arguments[0], arguments[1], ... The length of the array argument should be equal to the arity of the function. If not, an IllegalArgumentException is thrown. Store information about "cases" that occur in the evaluation in the second parameter, if that parameter is non-null.

Parameters:
arguments - array of args. to use in evaluation
cases - cases to use
Returns:
the value

derivative

public Function derivative(int wrt)
Return the derivative of the function with repect to argument number wrt, where the arguments are numbered 1, 2, 3,.... For a function of one variable, call derivative(1) to find its derivative. If arity > 1, this is effectively a partial derivative. If wrt is not in the legal range, an IllegalArgumentException is thrown.

Parameters:
wrt - index of the arg. with respect to
Returns:
the derivative

derivative

public Function derivative(Variable x)
Return the derivative of the function with respect to the variable x. This will be non-zero if x occurs somehow in the definition of x: For example, f(y) = sin(x*y);

Parameters:
x - variable with respect to
Returns:
the derivative

dependsOn

public boolean dependsOn(Variable x)
Return true if the definition of this function depends in some way on the variable x. (Note that a function does NOT depend on its parameter variables!)

Parameters:
x - variable to check
Returns:
true or false

apply

public void apply(StackOfDouble stack,
                  Cases cases)
Find the value of the function applied to arguments popped from the stack, and push the result back onto the stack. (Overrides general method inherited from FunctionParserExtension. This is done for efficiency and because the general method can't deal properly with "cases".) Not meant to be called directly

Specified by:
apply in interface ExpressionCommand
Overrides:
apply in class FunctionParserExtension
Parameters:
stack - stack to use
cases - cases to use