Class ParsedFunction

java.lang.Object
  |
  +--ParsedFunction

class ParsedFunction
extends java.lang.Object
implements Function

A simple function parser. Operates recursively on the entire input.

The standard arithmetic operators (+ - * /) are supported, with the caret symbol (^) meaning "to the power of".

The sin, cos, tan, abs and exp functions are supported; function arguments should be enclosed within brackets (eg. sin(x) instead of sin x. The letter 'x' represents the x parameter to the function; the name "pi" will give the value of pi. Extra whitespace between operators is permitted, but not between a function name and the opening bracket.

The string is searched for a top-level operator (outside of any brackets). If one is found, the string is split in half, and each half parsed separately to produce to Expression objects. These are combined with an appropriate object to represent the operator (eg. Addition). Lower-precedence operators are searched for first. Functions names have the opening bracket appended to prevent precedence confusion (eg. "sin(" is treated as an operator).


Inner Class Summary
(package private)  class ParsedFunction.Abs
           
(package private)  class ParsedFunction.Addition
           
(package private)  class ParsedFunction.Constant
           
(package private)  class ParsedFunction.Cosine
           
(package private)  class ParsedFunction.Division
           
(package private)  class ParsedFunction.Equal
           
(package private)  class ParsedFunction.Exp
           
(package private)  class ParsedFunction.Expression
          Object to represent an expression which may be evaluated with some parameter x.
(package private)  class ParsedFunction.Fn
           
(package private)  class ParsedFunction.GreaterThan
           
(package private)  class ParsedFunction.GreaterThanEqual
           
(package private)  class ParsedFunction.LessThan
           
(package private)  class ParsedFunction.LessThanEqual
           
(package private)  class ParsedFunction.Multiplication
           
(package private)  class ParsedFunction.NotEqual
           
(package private)  class ParsedFunction.Power
           
(package private)  class ParsedFunction.Sine
           
(package private)  class ParsedFunction.Subtraction
           
(package private)  class ParsedFunction.Tan
           
(package private)  class ParsedFunction.X
           
 
Field Summary
 ParsedFunction.Expression expression
          The Expression object representing this function.
 java.lang.String string
          The string from which the function was parsed.
 
Constructor Summary
ParsedFunction(java.lang.String s)
          Creates a new function by parsing the supplied string.
 
Method Summary
 double evaluate(double x)
           
static int findOperator(char op, java.lang.String s)
          Helper method to search for a top-level operator in a string.
static int findOperator(char op, java.lang.String s, int startPos)
          Helper method to search for a top-level operator in a string, from a specified starting index.
static void main(java.lang.String[] args)
          Test harness used for debugging
 ParsedFunction.Expression parseExpression(java.lang.String s)
          Creates a new Expression object to represent the supplied string.
 void setString(java.lang.String s)
          Changes the function and causes a reparse.
 java.lang.String toString()
          Returns the string from which the function was parsed.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

expression

public ParsedFunction.Expression expression
The Expression object representing this function.

string

public java.lang.String string
The string from which the function was parsed.
Constructor Detail

ParsedFunction

public ParsedFunction(java.lang.String s)
Creates a new function by parsing the supplied string.
Method Detail

main

public static void main(java.lang.String[] args)
Test harness used for debugging

parseExpression

public ParsedFunction.Expression parseExpression(java.lang.String s)
Creates a new Expression object to represent the supplied string.

findOperator

public static int findOperator(char op,
                               java.lang.String s)
Helper method to search for a top-level operator in a string. This differs from the indexOf() method of String, as there is the additional requirement that the character must occur outside of any brackets.
Parameters:
op - The character to search for
s - The string to search

findOperator

public static int findOperator(char op,
                               java.lang.String s,
                               int startPos)
Helper method to search for a top-level operator in a string, from a specified starting index. This differs from the behaviour of the indexOf() method of String, as there is the additional requirement that the character must occur outside of any brackets. (Bracket counting is applied to the whole string, regardless of the starting index.)
Parameters:
op - The character to search for
s - The string to search
startPos - The minimum index at which the character should be found

evaluate

public double evaluate(double x)
Specified by:
evaluate in interface Function

setString

public void setString(java.lang.String s)
Changes the function and causes a reparse.

toString

public java.lang.String toString()
Returns the string from which the function was parsed.
Overrides:
toString in class java.lang.Object