Class FuncMap
FuncMap maps a name to a function. A FuncMap is used in the eval method of an Expression object. This class can be used as the default function-map. The loadDefaultFunctions() method can be used to take advantage of the many already implemented functions (see below).
During the evaluation of an expression, if a function is not supported then a RuntimeException is thrown.
Default functions:
- No Parameters
-
- e() → Math.E
- pi() → Math.PI
- rand() → Math.random()
- min() → Double.MIN_VALUE
- max() → Double.MAX_VALUE
- 1 Parameter
-
- sin(x) → Math.sin(double)
- cos(x) → Math.cos(double)
- tan(x) → Math.tan(double)
- asin(x) → Math.asin(double)
- acos(x) → Math.acos(double)
- atan(x) → Math.atan(double)
- asinh(x) → 2 * ln(sqrt((x+1)/2) + sqrt((x-1)/2))
- acosh(x) → ln(x + sqrt(1 + x2))
- atanh(x) → (ln(1+x) - ln(1-x)) / 2
- sinh(x) → (ex - e-x)/2
- cosh(x) → (ex + e-x)/2
- tanh(x) → (ex - e-x)/(ex + e-x)
- sqrt(x) → Math.sqrt(double)
- abs(x) → Math.abs(double)
- ceil(x) → Math.ceil(double)
- floor(x) → Math.floor(double)
- exp(x) → ex
- ln(x) → logex
- lg(x) → log2x
- log(x) → log10x
- sign(x) → x > 0 = 1, x < 0 = -1, else 0
- fact(n) → n! = 1 * 2 * ... * (n - 1) * n
- round(x) → Math.round(double)
- 2 Parameters
-
- log(x,y) → logyx
- combin(n, r) → PascalsTriangle.nCr(n, r)
- mod(x, y) → x % y
- pow(x, y) → xy
- n Parameters
-
- min(x1,x2,...,xn)
- max(x1,x2,...,xn)
- sum(x1,x2,...,xn) → x1 + x2 + ... + xn
- avg(x1,x2,...,xn) → (x1 + x2 + ... + xn) / n
Note: Case sensitivity can only be specified in the constructor (for consistency). When case sensitivity is false, the String.equalsIgnoreCase method is used. When case sensitivity is true, the String.equals method is used. The matching does not include the parenthesis. For example, when case sensitivity is false and the default functions have been loaded, then "RaNd", "rand", and "RAND" all map to the RandFunction(). By default, case sensitivity is false.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetFunction(String funcName, int numParam) Returns a function based on the name and the specified number of parameters.String[]Returns an array of exact length of the function names stored in this map.Function[]Returns an array of exact length of the functions stored in this map.booleanReturns true if the case of the function names is considered.voidAdds the mappings for many common functions.voidRemoves the function-name and the associated function from the map.voidsetFunction(String funcName, Function f) Assigns the name to map to the specified function.
-
Constructor Details
-
FuncMap
public FuncMap() -
FuncMap
public FuncMap(boolean caseSensitive)
-
-
Method Details
-
loadDefaultFunctions
public void loadDefaultFunctions()Adds the mappings for many common functions. The names are specified in all lowercase letters. -
getFunction
Returns a function based on the name and the specified number of parameters.- Throws:
RuntimeException- If no supporting function can be found.
-
setFunction
Assigns the name to map to the specified function.- Throws:
IllegalArgumentException- If any of the parameters are null.
-
isCaseSensitive
public boolean isCaseSensitive()Returns true if the case of the function names is considered. -
getFunctionNames
Returns an array of exact length of the function names stored in this map. -
getFunctions
Returns an array of exact length of the functions stored in this map. The returned array corresponds to the order of the names returned by getFunctionNames. -
remove
Removes the function-name and the associated function from the map. Does nothing if the function-name is not found.
-