All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphbuilder.math.FuncMap Maven / Gradle / Ivy

Go to download

Implementation of various mathematical curves that define themselves over a set of control points. The API is written in Java. The curves supported are: Bezier, B-Spline, Cardinal Spline, Catmull-Rom Spline, Lagrange, Natural Cubic Spline, and NURBS.

There is a newer version: 1.08
Show newest version
package com.graphbuilder.math;

import com.graphbuilder.math.func.*;

/**

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. */ public class FuncMap { private String[] name = new String[50]; private Function[] func = new Function[50]; private int numFunc = 0; private boolean caseSensitive = false; public FuncMap() {} public FuncMap(boolean caseSensitive) { this.caseSensitive = caseSensitive; } /** Adds the mappings for many common functions. The names are specified in all lowercase letters. */ public void loadDefaultFunctions() { // >= 0 parameters setFunction("min", new MinFunction()); setFunction("max", new MaxFunction()); // > 0 parameters setFunction("sum", new SumFunction()); setFunction("avg", new AvgFunction()); // 0 parameters setFunction("pi", new PiFunction()); setFunction("e", new EFunction()); setFunction("rand", new RandFunction()); // 1 parameter setFunction("sin", new SinFunction()); setFunction("cos", new CosFunction()); setFunction("tan", new TanFunction()); setFunction("sqrt", new SqrtFunction()); setFunction("abs", new AbsFunction()); setFunction("ceil", new CeilFunction()); setFunction("floor", new FloorFunction()); setFunction("exp", new ExpFunction()); setFunction("lg", new LgFunction()); setFunction("ln", new LnFunction()); setFunction("sign", new SignFunction()); setFunction("round", new RoundFunction()); setFunction("fact", new FactFunction()); setFunction("cosh", new CoshFunction()); setFunction("sinh", new SinhFunction()); setFunction("tanh", new TanhFunction()); setFunction("acos", new AcosFunction()); setFunction("asin", new AsinFunction()); setFunction("atan", new AtanFunction()); setFunction("acosh", new AcoshFunction()); setFunction("asinh", new AsinhFunction()); setFunction("atanh", new AtanhFunction()); // 2 parameters setFunction("pow", new PowFunction()); setFunction("mod", new ModFunction()); setFunction("combin", new CombinFunction()); // 1 or 2 parameters setFunction("log", new LogFunction()); } /** Returns a function based on the name and the specified number of parameters. @throws RuntimeException If no supporting function can be found. */ public Function getFunction(String funcName, int numParam) { for (int i = 0; i < numFunc; i++) { if (func[i].acceptNumParam(numParam) && (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName))) return func[i]; } throw new RuntimeException("function not found: " + funcName + " " + numParam); } /** Assigns the name to map to the specified function. @throws IllegalArgumentException If any of the parameters are null. */ public void setFunction(String funcName, Function f) { if (funcName == null) throw new IllegalArgumentException("function name cannot be null"); if (f == null) throw new IllegalArgumentException("function cannot be null"); for (int i = 0; i < numFunc; i++) { if (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName)) { func[i] = f; return; } } if (numFunc == name.length) { String[] tmp1 = new String[2 * numFunc]; Function[] tmp2 = new Function[tmp1.length]; for (int i = 0; i < numFunc; i++) { tmp1[i] = name[i]; tmp2[i] = func[i]; } name = tmp1; func = tmp2; } name[numFunc] = funcName; func[numFunc] = f; numFunc++; } /** Returns true if the case of the function names is considered. */ public boolean isCaseSensitive() { return caseSensitive; } /** Returns an array of exact length of the function names stored in this map. */ public String[] getFunctionNames() { String[] arr = new String[numFunc]; for (int i = 0; i < arr.length; i++) arr[i] = name[i]; return arr; } /** 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. */ public Function[] getFunctions() { Function[] arr = new Function[numFunc]; for (int i = 0; i < arr.length; i++) arr[i] = func[i]; return arr; } /** Removes the function-name and the associated function from the map. Does nothing if the function-name is not found. */ public void remove(String funcName) { for (int i = 0; i < numFunc; i++) { if (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName)) { for (int j = i + 1; j < numFunc; j++) { name[j - 1] = name[j]; func[j - 1] = func[j]; } numFunc--; name[numFunc] = null; func[numFunc] = null; break; } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy