Package edu.hws.jcm.functions

# Class ExpressionFunction

java.lang.Object
edu.hws.jcm.functions.FunctionParserExtension
edu.hws.jcm.functions.ExpressionFunction

All Implemented Interfaces:
  `ExpressionCommand`, `Function`, `MathObject`, `ParserExtension`, `Serializable`

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

### Fields inherited from class edu.hws.jcm.functions.FunctionParserExtension

`name`

## Constructor Summary

Constructors
Constructor
Description
`ExpressionFunction(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.
`ExpressionFunction(String name, String def)`
Constuct a function of one parameter, named "x", by parsing the String, def, to get the definition of the function.
`ExpressionFunction(String name, String[] paramNames, String def, Parser parser)`
Constuct a function of one or more parameters by parsing the String, def, to get the definition of the function.

## Method Summary

Modifier and Type
Method
Description
`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.
`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(String def)`
Set the definition of this function by parsing the given string, using a default parser.
`void`
`redefine(String def, Parser parser)`
Set the definition of this function, using the specified parser (or a default parser if parser is null).
`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 edu.hws.jcm.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`

## Constructor Details

### ExpressionFunction

public ExpressionFunction(String name, 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(String name, String[] paramNames, 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(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.

## Method Details

### redefine

public void redefine(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.

### redefine

public void redefine(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.

### getDefinitionString

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

### toString

public 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 `Object`

### getArity

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

### 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.

### 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.

### 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.

### 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);

### 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!)

### 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` - contains results of previous commands in the program.
  `cases` - if non-null, any case information generated during evaluation should be recorded here.