genopt.algorithm.util.math
Class FunctionEvaluator

java.lang.Object
  extended by genopt.algorithm.util.math.FunctionEvaluator

public class FunctionEvaluator
extends java.lang.Object

Function parser. This class parses a given String argument and computes the function value as specified by the String. To invoke the function, the class genopt.algorithm.util.math.Fun is parsed. If the function cannot be found in this class, then the class java.lang.StrictMath is parsed. A typical argment has the form add(2, subtract(sin(3), 1)).

This project was carried out at:

and supported by

GenOpt Copyright (c) 1998-2011, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.

Version:
GenOpt(R) 3.1.0 (December 8, 2011)

Author:
Michael Wetter

Nested Class Summary
private  class FunctionEvaluator.Element
          Internal class for an element that contains either a String, a double, or a char value.
 
Field Summary
protected  java.util.ArrayList<FunctionEvaluator.Element> arrLis
          List with Elements that resulted from function parsing.
protected  java.lang.String Func
          Function to be evaluated.
private static java.lang.String LS
          Line separator
protected  java.lang.String Name
          Function name.
 
Constructor Summary
FunctionEvaluator(java.lang.String name, java.lang.String function)
          Creates a new FunctionEvaluator instance.
 
Method Summary
private  FunctionEvaluator.Element _evaluate(java.lang.String fun, java.util.Stack<java.lang.Double> sta)
          Evaluates the function fun using all elements in sta as the argument.
private  java.lang.String _getErrorMessagePrefix()
          Gets the prefix for the error message.
private  FunctionEvaluator.Element _parse(java.util.ArrayList<FunctionEvaluator.Element> lis, java.lang.String fun)
          Parses the function.
private  void _setArrayList()
          Sets the array list that will be used to evaluate the function
 double evaluate()
          Evaluates the function.
 java.lang.String getFunction()
           
 java.lang.String getName()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LS

private static final java.lang.String LS
Line separator


Name

protected java.lang.String Name
Function name.


Func

protected java.lang.String Func
Function to be evaluated.


arrLis

protected java.util.ArrayList<FunctionEvaluator.Element> arrLis
List with Elements that resulted from function parsing.

Constructor Detail

FunctionEvaluator

public FunctionEvaluator(java.lang.String name,
                         java.lang.String function)
Creates a new FunctionEvaluator instance.

Parameters:
name - name of the function
function - function to be evaluated
Method Detail

evaluate

public double evaluate()
                throws java.lang.NoSuchMethodException,
                       java.lang.IllegalAccessException,
                       java.lang.reflect.InvocationTargetException
Evaluates the function.

Returns:
the function value
Throws:
java.lang.NoSuchMethodException - if the method could not be found
java.lang.IllegalAccessException - if an error occurs
java.lang.reflect.InvocationTargetException - if an error occurs
java.lang.IndexOutOfBoundsException - if an error occurs

_setArrayList

private void _setArrayList()
Sets the array list that will be used to evaluate the function


_getErrorMessagePrefix

private java.lang.String _getErrorMessagePrefix()
Gets the prefix for the error message.

Returns:
the prefix for the error message

_parse

private FunctionEvaluator.Element _parse(java.util.ArrayList<FunctionEvaluator.Element> lis,
                                         java.lang.String fun)
                                  throws java.lang.NoSuchMethodException,
                                         java.lang.IllegalAccessException,
                                         java.lang.reflect.InvocationTargetException
Parses the function.

Parameters:
lis - the ArrayList with the function being evaluated. The function name must be passed in the fun argument.
fun - name of the function that has to be invoked
Returns:
an Element containing the function value
Throws:
java.lang.NoSuchMethodException - if an error occurs
java.lang.IllegalAccessException - if an error occurs
java.lang.reflect.InvocationTargetException - if an error occurs

_evaluate

private FunctionEvaluator.Element _evaluate(java.lang.String fun,
                                            java.util.Stack<java.lang.Double> sta)
                                     throws java.lang.NoSuchMethodException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
Evaluates the function fun using all elements in sta as the argument.

Parameters:
fun - the name of the function that will be invoked
sta - a stack of Doubles that are the arguments of fun
Returns:
an Element that contains the function value
Throws:
java.lang.NoSuchMethodException - if an error occurs
java.lang.IllegalAccessException - if an error occurs
java.lang.reflect.InvocationTargetException - if an error occurs

getName

public java.lang.String getName()

getFunction

public java.lang.String getFunction()