org.dyndns.kwitte.jfunction
Class Terms

java.lang.Object
  extended by org.dyndns.kwitte.jfunction.Terms

public final class Terms
extends java.lang.Object

A class to evaluate Terms.

Grammar

[expr] -- [term] [term_tail]
[term_tail] -- [add_op] [term] [term_tail] | e
[term] -- [factor] [factor_tail]
[factor] -- [power] [power_tail]
[power_tail] -- ^ [power] [power_tail] | e
[factor_tail] -- [mult_op] [factor] [factor_tail] | e
[power] -- ( expr ) | [unary_operator] [power] | x | unsigned_float
[add_op] -- + | -
[mult_op] -- * | /
[unary_operator] -> abs | acos | add | asin | atan | ceil | cos | cosh | exp | floor | ln | round | signum | sin | sinh | sqrt | tan | tanh
unsigned_float is a positive float value as specified in the grammar of the Java language specification. e is the empty word. The meaning of the unary operators is the same as in the corresponding method of java.lang.Math, except that sin, cos and tan take degrees. ln is like the log method in java.lang.Math.

Additionally, a ";" can be used to mark everything that is right of the term as comment. Whitespace is ignored, just newline characters are not allowed within a term.


Method Summary
static Term compile(java.lang.String term)
          Creates a Term.
static double evaluate(java.lang.String term)
          Evaluates a term.
static double evaluate(java.lang.String term, double value)
          Evaluates a term.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

compile

public static Term compile(java.lang.String term)
                    throws FunctionFormatException
Creates a Term. Use this method if you have to evaluate the same term frequently (for different x-values). Use evaluate(String, double) or evaluate(String) otherwise.

Example:

Create a value table for the function f(x) = sin(3*x)+2

Pre-compile once to increase performance. This is a lot faster than calling Terms.evaluate("sin(3*x)+2") 20000 times inside the loop.

 Term myTerm = Terms.compile("sin(3*x)+2");

 for (double d = -1000.0; d < 1000.0; d+=0.1) {
     double result = myTerm.evaluate(d);
     System.out.println(d + "\t" + result);
 }
 

Parameters:
term - the term to be compiled. For the grammar see class documentation
Returns:
the compiled Term, ready for fast evaluation for different x-values
Throws:
FunctionFormatException - iff the argument is not an element of the language specified in the class documentation

evaluate

public static double evaluate(java.lang.String term,
                              double value)
                       throws FunctionFormatException
Evaluates a term.

Example:

 System.out.println(Terms.evaluate("2*x", 12)); // prints 24
 

Parameters:
term - the term to be evaluated. For the grammar see class documentation
Returns:
the result
Throws:
FunctionFormatException - iff the argument is not an element of the language specified in the class documentation

evaluate

public static double evaluate(java.lang.String term)
                       throws FunctionFormatException
Evaluates a term. If the term contains an "x" then this value will be interpreted as 0.0.

Example:

 System.out.println(Terms.evaluate("3+4")); // prints 7
 

Parameters:
term - the term to be evaluated. For the grammar see class documentation
Returns:
the result
Throws:
FunctionFormatException - iff the argument is not an element of the language specified in the class documentation