public class FunctionParser extends Object implements Serializable
operator
and can be displayed by implementing the following snippet:
String[] text = { "Variables","Functions", "Binary Infix Operators","Ternary Operators" }; for (int i = 0; i < FunctionParser.operator.length; i++) { System.out.println(text[i] + ":"); for (int j = 0; j < FunctionParser.operator[i].length; j++) { System.out.print(FunctionParser.operator[i][j] + ", "); } System.out.println("\n"); }Fractional numbers (i.e.,
double
values) have to be
inputted with a decimal point (not a comma).Modifier and Type | Field and Description |
---|---|
static String[] |
constantName
Names of predefined constants.
|
static double[] |
constantValue
Values of the predefined constants.
|
static int |
maxOpLength
Maximum number of letters an operator can have.
|
static String[][] |
operator
Table of predefined operators.
|
Constructor and Description |
---|
FunctionParser(ArrayList<String[]> functions)
Creates a function parser from the list of functions in postfix notation.
|
FunctionParser(String function)
Creates a function parser from a single function in usual notation,
infix for binary operators and prefix for functions and ternary operators.
|
FunctionParser(String[] functionList)
Creates a function parser from a list of functions in usual
(infix or prefix) notation.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
checkSyntax(String function)
Analyses and parses the function being input as a string.
|
double |
evaluate(int i,
double x)
Evaluates the i-th function of the current object at the value x.
|
double |
evaluate(int i,
double x,
double y)
Evaluates the i-th function f(x, y) of the
this object at the values x and y.
|
double |
evaluateInt(int x,
int z)
Evaluates the 0-th function f(x, z) of this object
at the values x and z.
|
static double |
evaluatePostFix(String[] function,
double x)
Evaluates the function (in postfix notation) at the value x.
|
String[] |
getFunction(int i)
Returns an array of the parts of the i-th function in postfix notation.
|
String |
getFunction(int i,
boolean postfix)
Returns a string of the i-th function, either in postfix or in
usual notation.
|
public static final String[] constantName
public static final double[] constantValue
public static final String[][] operator
public static final String[][] operator = { {// variables: "x", "y", "z" }, {// unary operators: "ln", "ld", "exp", "log", "sqrt", "w", "sin", "cos", "tan", "cot", "sec", "csc", "asin", "acos", "atan", "acot", "sinh", "cosh", "tanh", "coth", "arsinh", "arcosh", "artanh", "arcoth", "Z" }, {// binary operators: "+", "-", "*", "/", "%", "^", "mod", "=", "<", ">", "==", "<=", ">=", "&&", "|", "&", "and", "or", "xor", ";", "," }, {// ternary operators: "if", "modPow" }, };
public static int maxOpLength
public FunctionParser(String function)
operator
and can be displayed by implementing the following snippet:
String[] text = { "Variables","Functions", "Binary Infix Operators","Ternary Operators" }; for (int i = 0; i < FunctionParser.operator.length; i++) { System.out.println(text[i] + ":"); for (int j = 0; j < FunctionParser.operator[i].length; j++) { System.out.print(FunctionParser.operator[i][j] + ", "); } System.out.println("\n"); }Fractional numbers (i.e.,
double
values) have to be
inputted with a decimal point (not a comma).function
- a string of the function in usual notationpublic FunctionParser(String[] functionList)
FunctionParser(String)
for detailed description of the
requirements of the input strings.functionList
- an array of strings of functions in usual notationpublic FunctionParser(ArrayList<String[]> functions)
functions
- a list of functions in parsed postfix notationpublic static boolean checkSyntax(String function)
function
- a string representing a functionpublic String[] getFunction(int i)
i
- specifies the index of the functionpublic String getFunction(int i, boolean postfix)
((2*x) + 2)
, and prefix notation for functions and
ternary operators, e.g., sin(x)
or
if(-1 < x && x < 1; 1; 0)
.i
- the index of the function to be returnedpostfix
- specifies whether the returned string is in postfix notationpublic double evaluate(int i, double x)
i
- index of the function (starting at 0)x
- the double value to be insertedpublic double evaluate(int i, double x, double y)
i
- index of the function (starting at 0)x
- the first parameter value to be insertedy
- the second parameter value to be insertedpublic double evaluateInt(int x, int z)
x
- the x-value to be insertedz
- the z-value to be insertedpublic static double evaluatePostFix(String[] function, double x)
function
- the function in postfix notation, each array element being a postfix componentx
- the double value to be inserted in the function