public class FunctionEvaluator
extends java.lang.Object
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 byGenOpt Copyright (c) 1998-2021, 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.
Modifier and Type | Class and Description |
---|---|
private class |
FunctionEvaluator.Element
Internal class for an element that contains either a
String , a double , or
a char value. |
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
FunctionEvaluator(java.lang.String name,
java.lang.String function)
Creates a new
FunctionEvaluator instance. |
Modifier and Type | Method and Description |
---|---|
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() |
private static final java.lang.String LS
protected java.lang.String Name
protected java.lang.String Func
protected java.util.ArrayList<FunctionEvaluator.Element> arrLis
public FunctionEvaluator(java.lang.String name, java.lang.String function)
FunctionEvaluator
instance.name
- name of the functionfunction
- function to be evaluatedpublic double evaluate() throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
- if the method could not be foundjava.lang.IllegalAccessException
- if an error occursjava.lang.reflect.InvocationTargetException
- if an error occursjava.lang.IndexOutOfBoundsException
- if an error occursprivate void _setArrayList()
private java.lang.String _getErrorMessagePrefix()
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
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 invokedElement
containing the function valuejava.lang.NoSuchMethodException
- if an error occursjava.lang.IllegalAccessException
- if an error occursjava.lang.reflect.InvocationTargetException
- if an error occursprivate 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
fun
using all elements in sta
as the argument.fun
- the name of the function that will be invokedsta
- a stack of Double
s that are the arguments of
fun
Element
that contains the function valuejava.lang.NoSuchMethodException
- if an error occursjava.lang.IllegalAccessException
- if an error occursjava.lang.reflect.InvocationTargetException
- if an error occurspublic java.lang.String getName()
public java.lang.String getFunction()