Maths
Class MathsParse

java.lang.Object
  extended by Maths.MathsParse

public class MathsParse
extends java.lang.Object

Class used to parse an equation represented by a string and return the result. Variables are represented by a letter followed by any number of alphanumeric characters. Multiply (represented by "*" should be stated explicitly, e.g. a * b NOT a b or ab (the later of which would be parsed as a single variable). Functions should be represented by "f[a,b,...]" where f is the function name and a,b etc. are inputs. Inputs cannot contain other functions but can otherwise contain an expression. The following functions are defined by default:

Additional functions can be defined by passing instances of classes that implement FunctionParser to the constructor.

Version:
1.2

Constructor Summary
MathsParse(FunctionParser... fps)
          Default constructor.
 
Method Summary
 CompiledFunction compileFunction(java.lang.String equation)
          Parses an equation and returns a compiled function that can then be used to compute the value fo an equation.
 double parseEquation(java.lang.String equation, java.util.Map<java.lang.String,java.lang.Double> values)
          Parses an equation and returns the result
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathsParse

public MathsParse(FunctionParser... fps)
Default constructor.

Parameters:
fps - A variable number of parsers that define additional functions. If none is passed then only the default functions are avaliable.
Method Detail

parseEquation

public double parseEquation(java.lang.String equation,
                            java.util.Map<java.lang.String,java.lang.Double> values)
                     throws NoSuchFunction,
                            WrongNumberOfVariables,
                            NoSuchVariable
Parses an equation and returns the result

Parameters:
equation - The equation to parse
values - Map from a string to a double that represents the value of any variables in the equation
Returns:
The result of the equation
Throws:
NoSuchFunction - If the equation uses an undefined function
WrongNumberOfVariables - If the equation uses a function and passes it the wrong number of variables
NoSuchVariable - Thrown if values does not contain a needed variable

compileFunction

public CompiledFunction compileFunction(java.lang.String equation)
                                 throws NoSuchFunction,
                                        WrongNumberOfVariables
Parses an equation and returns a compiled function that can then be used to compute the value fo an equation. Compiling functions saves a lot of time if it is to be called multiple times

Parameters:
equation - The equation to parse
Returns:
Compiled function that can be used to calculate the functions value
Throws:
NoSuchFunction - If the equation uses an undefined function
WrongNumberOfVariables - If the equation uses a function and passes it the wrong number of variables