jwo.utils.expression
Class MathEvaluator

java.lang.Object
  extended byjwo.utils.expression.MathEvaluator

public final class MathEvaluator
extends Object

Evaluates a mathematical expression stored in an postfix stack (created using MathParser). Can handle basic arithmetic and trig operators. +, -, *, /, ^, sin, cos, tan, sqrt, asin, acos, atan, ln. Can handle constants pi and e. Can handle random number generators rand (rectangular distribution between 0-1) and gauss (normal disitribution with mean of 0 and variance of 1). Will substitute variables z1,z2,x and y with numbers defined by the constructor or the relevant evaluate() method.

Modified from original code described at www.devx.com/premier/mgznarch/javapro/2001/05may01/ps0105/ps0105.asp.

Version:
2.2, 15th February, 2001, modified 4th August, 2004.
Author:
Daniel Savarese, modified by Jo Wood.

Constructor Summary
MathEvaluator()
          Creates an evaluator with all variables set to 0.
MathEvaluator(double[] z)
          Creates an evaluator with variables z0-z9 defined in the given array.
 
Method Summary
 double evaluate(Stack operations)
          Evalutes the given expression stack using (if required), the stored variable substitutions.
 double evaluate(Stack operations, double z1, double z2, double x, double y)
          Evalutes the given expression stack using (if required), the stored variable substitutions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathEvaluator

public MathEvaluator()
Creates an evaluator with all variables set to 0.


MathEvaluator

public MathEvaluator(double[] z)
Creates an evaluator with variables z0-z9 defined in the given array.

Parameters:
z - List of variables for substitution. Expressions that use variables should name them z0 - z9.
Method Detail

evaluate

public double evaluate(Stack operations)
Evalutes the given expression stack using (if required), the stored variable substitutions.

Parameters:
operations - Expression stack containing postfixed expression.
Returns:
Evaluated expression.

evaluate

public double evaluate(Stack operations,
                       double z1,
                       double z2,
                       double x,
                       double y)
Evalutes the given expression stack using (if required), the stored variable substitutions. Variables z1 and z2 are replaced with the given values. This version is simple a speed-optimised version suitable for four-variable subsitutions that are likely to be applied iteratively.

Parameters:
operations - Expression stack containing postfixed expression.
z1 - Value of z1 to substitute.
z2 - Value of z2 to substitute.
x - Value of x to substitute.
y - Value of y to substitute.
Returns:
Evaluated expression.


Copyright Jo Wood, 1996-2005, last modified, 11th March, 2005