ca.nengo.math.impl
Class DefaultFunctionInterpreter

java.lang.Object
  extended by ca.nengo.math.impl.DefaultFunctionInterpreter
All Implemented Interfaces:
FunctionInterpreter

public class DefaultFunctionInterpreter
extends java.lang.Object
implements FunctionInterpreter

Default implementation of FunctionInterpreter. This implementation produces PostfixFunctions.

TODO: faster Functions could be produced by compiling expressions into Java classes.

Author:
Bryan Tripp

Constructor Summary
DefaultFunctionInterpreter()
           
 
Method Summary
 java.util.List getPostfixList(java.lang.String expression)
           
 java.util.Map<java.lang.String,Function> getRegisteredFunctions()
          Returns a map of registered functions.
 Function parse(java.lang.String expression, int dimension)
          Parses a mathematical expression into a Function instance.
 void registerFunction(java.lang.String name, Function function)
          Registers a non-standard function for use in future interpretation of function expressions.
 void removeRegisteredFunction(java.lang.String name)
          Removes a registered function.
static DefaultFunctionInterpreter sharedInstance()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultFunctionInterpreter

public DefaultFunctionInterpreter()
Method Detail

sharedInstance

public static DefaultFunctionInterpreter sharedInstance()
Returns:
A singleton instance of DefaultFunctionInterpreter

registerFunction

public void registerFunction(java.lang.String name,
                             Function function)
Description copied from interface: FunctionInterpreter
Registers a non-standard function for use in future interpretation of function expressions. Standard function names (eg sin, cos, ln) are not allowed. The Function must be 1-dimensional. Example: suppose you have a sophisticated Function implementation class called FooImpl. You could register is here as registerFunction("foo", new FooImpl()) and thereafter use it in expressions like parse("x1 + x2 * foo(x3)")

Specified by:
registerFunction in interface FunctionInterpreter
Parameters:
name - Short name of function as it is to appear in parsed expressions
function - Function instance
See Also:
FunctionInterpreter.registerFunction(java.lang.String, ca.nengo.math.Function)

parse

public Function parse(java.lang.String expression,
                      int dimension)
Description copied from interface: FunctionInterpreter

Parses a mathematical expression into a Function instance. The function can include the unary operators - and !, the binary operators +-/^*&|, the standard functions sin() cos() tan() asin(), acos(), atan(), exp(), and ln(), and user-defined unary functions functions that have been registered with the registerFunction(...) method. The constant "pi" and literal constants are also allowed. The logical operators produce 1f or 0f, on the basis of whether their operands are > 1/2. Functions must be given in infix notation and can include brackets.

Function operands have the form x0, x1, etc., which serve as placeholders for values that are given at each dimension of the map() method of the resulting function.

An example of a valid expression would be "x0 * (x1 + x2)^x3 * ln(x4/3)"

Specified by:
parse in interface FunctionInterpreter
Parameters:
expression - Mathematical expression
dimension - Input dimension of the desired Function (must be at least as great as any referenced inputs, eg if x3 appears in the expression, the dimension must be at least 4)
Returns:
Instance of a Function implementing the given expression
See Also:
FunctionInterpreter.parse(java.lang.String, int)

getPostfixList

public java.util.List getPostfixList(java.lang.String expression)
Parameters:
expression - Mathematical expression, as in parse(...)
Returns:
List of operators and operands in postfix order

getRegisteredFunctions

public java.util.Map<java.lang.String,Function> getRegisteredFunctions()
Description copied from interface: FunctionInterpreter
Returns a map of registered functions.

Specified by:
getRegisteredFunctions in interface FunctionInterpreter
Returns:
The registered functions

removeRegisteredFunction

public void removeRegisteredFunction(java.lang.String name)
Description copied from interface: FunctionInterpreter
Removes a registered function.

Specified by:
removeRegisteredFunction in interface FunctionInterpreter
Parameters:
name - Name of function to be removed