edu.hws.jcm.functions
Class FunctionParserExtension

java.lang.Object
  |
  +--edu.hws.jcm.functions.FunctionParserExtension
All Implemented Interfaces:
ExpressionCommand, Function, MathObject, ParserExtension, java.io.Serializable
Direct Known Subclasses:
ExpressionFunction, TableFunction, WrapperFunction

public abstract class FunctionParserExtension
extends java.lang.Object
implements Function, ParserExtension, ExpressionCommand

An object belonging to a concrete subclass of FunctionParserExtesion is a mathematical function that can be registered with a Parser and then used in strings that are parsed by that parser. (See the Function inteface for more information.)

Since a FunctionParserExtension object implements the ParserExtension interface, it has a name and can be registered with a Parser. When the parser encounters the name of a function in a string, it turns control of the parsing process over to the function. When a Function occurs in an ExpressionProgram, the Function is responsible for evaluating itself and for differentiating itself. This functionality is defined in the abstract class FunctionParserExtension. The concrete subclasses of FunctionParserExtension represent actual functions. They must implement the five methods defined in the Function interface, but most of the parser- and expression-related behavior can probably be inherited from this class. (This is good, since the programming is rather tricky.)

(The point of all this is that Parsers and Expressions can deal with Functions, even though there is no reference to Functions in the Parser or ExpressionProgram classes. Everything is done through the ParserExtension interface.)

See Also:
Serialized Form

Field Summary
protected  java.lang.String name
          The name of this MathObject, possibly null.
 
Constructor Summary
FunctionParserExtension()
           
 
Method Summary
 void appendOutputString(ExpressionProgram prog, int myIndex, java.lang.StringBuffer buffer)
          Append a string representation of the function and its arguments to the buffer
 void apply(StackOfDouble stack, Cases cases)
          Evaluate the function applied to argument values popped from the stack, and leave the result on the stack.
 void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
          The function object occurs as a command at index myIndex in prog.
 void doParse(Parser parser, ParserContext context)
          If this ParserExtension is registered with a parser and the parser encounters the name of the function in the string it is parsing, then the parser will call this routine.
 int extent(ExpressionProgram prog, int myIndex)
          Return the number of commands in prog that are part of this function reference, including the space occupied by the commands that compute the values of the function's arguments.
 java.lang.String getName()
          Get the name of this MathObject.
 void setName(java.lang.String name)
          Set the name of this object.
 void setParensCanBeOptional(boolean b)
          Call this function with b = true if this is a function of one variable and you want it to behave like a standard function in that parentheses can be optional around the argument of the function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.hws.jcm.data.Function
dependsOn, derivative, derivative, getArity, getVal, getValueWithCases
 
Methods inherited from interface edu.hws.jcm.data.ExpressionCommand
dependsOn
 

Field Detail

name

protected java.lang.String name
The name of this MathObject, possibly null.
Constructor Detail

FunctionParserExtension

public FunctionParserExtension()
Method Detail

setParensCanBeOptional

public void setParensCanBeOptional(boolean b)
Call this function with b = true if this is a function of one variable and you want it to behave like a standard function in that parentheses can be optional around the argument of the function. For the parentheses to be treated as optional, the option Parser.OPTIONAL_PARENS must ALSO be set in the parser. As an example, if you wanted to define the function sinh and allow the syntax "sinh x", you would turn this option on in the Function and turn OTPTIONAL_PARENS on in the Parser.
Parameters:
b - set whether parenthesis are optional in one variable functions.

setName

public void setName(java.lang.String name)
Set the name of this object. It is not a good idea to do this if the object has been registered with a Parser.
Specified by:
setName in interface MathObject

getName

public java.lang.String getName()
Get the name of this MathObject.
Specified by:
getName in interface MathObject

doParse

public void doParse(Parser parser,
                    ParserContext context)
If this ParserExtension is registered with a parser and the parser encounters the name of the function in the string it is parsing, then the parser will call this routine. This routine parses the function's parameter list and generates the code for evaluating the function, applied to those parameters. When this routine is called, the name of the function has already been read. The code that is generated consists of code to evalueate each of the parameters, leaving the results on the stack, followed by an application of the function.
Specified by:
doParse in interface ParserExtension
Parameters:
parser - parser that is parsing the string.
context - the ParseContext in effect at the time this method is called.

apply

public void apply(StackOfDouble stack,
                  Cases cases)
Evaluate the function applied to argument values popped from the stack, and leave the result on the stack. The argument values have already been computed and placed on the stack when this is called. They should be popped off the stack in reverse order. This general method can't deal properly with "cases", so it will probably have to be overridden in subclasses.
Specified by:
apply in interface ExpressionCommand
Following copied from interface: edu.hws.jcm.data.ExpressionCommand
Parameters:
stack - contains results of previous commands in the program.
cases - if non-null, any case information generated during evaluation should be recorded here.

compileDerivative

public void compileDerivative(ExpressionProgram prog,
                              int myIndex,
                              ExpressionProgram deriv,
                              Variable wrt)
The function object occurs as a command at index myIndex in prog. The commands for computing its arguments can be found in prog in positions preceding myIndex. Generate commands for computing the derivative of the function reference with respect to the Variable wrt and add them to the deriv program. The computation of the derivative uses the chain rule.
Specified by:
compileDerivative in interface ExpressionCommand
Parameters:
prog - program this function object occurs in.
myIndex - index at which this function occurs.
deriv - commands for computing the derivative are placed here.
wrt - the derivative is taken with respect to this Variable.

extent

public int extent(ExpressionProgram prog,
                  int myIndex)
Return the number of commands in prog that are part of this function reference, including the space occupied by the commands that compute the values of the function's arguments.
Specified by:
extent in interface ExpressionCommand
Parameters:
prog - program to check commands against.
myIndex - index in program.
Returns:
the number of commands in prog that are part of this function reference.

appendOutputString

public void appendOutputString(ExpressionProgram prog,
                               int myIndex,
                               java.lang.StringBuffer buffer)
Append a string representation of the function and its arguments to the buffer
Specified by:
appendOutputString in interface ExpressionCommand
Parameters:
prog - program whose string representation is being generated.
myIndex - index of this ExpressionCommand in prog.
buffer - string representation is placed here.