Function, MultiVarFunctionpublic final class SuryonoParser extends MathExpParser
Parser is a mathematical expression parser.Example of code that uses this class:
Parser parser = new Parser(1); // creates parser with one variable
parser.defineVariable(1,"x"); // lets the variable be 'x'
parser.define("sin(x)/x"); // defines function: sin(x)/x
parser.parse(); // parses the function
// calculates: sin(x)/x with x = -5.0 .. +5.0 in 20 steps
// and prints the result to standard output.
float result;
for (i=-10; i <= 10; i++) {
parser.setVariable(1,(float)i/2.0f);
result = parser.evaluate();
System.out.println(result);
}
| Modifier and Type | Field | Description |
|---|---|---|
static int |
CODE_DAMAGED |
Code damaged.
|
static int |
COMMA_EXPECTED |
Comma expected.
|
static int |
EXPRESSION_EXPECTED |
Expression expected.
|
static int |
INVALID_OPERAND |
Invalid operand.
|
static int |
INVALID_OPERATOR |
Invalid operator.
|
static int |
NO_FUNC_DEFINITION |
No function definition to parse.
|
static int |
OPERATOR_EXPECTED |
Operator expected.
|
static int |
PAREN_EXPECTED |
Parentheses expected.
|
static int |
PAREN_NOT_MATCH |
Parenthesis mismatch.
|
static int |
REF_NAME_EXPECTED |
Referenced name could not be found.
|
static int |
STACK_OVERFLOW |
Stack overflow.
|
static int |
TOO_MANY_CONSTS |
Too many constants.
|
static int |
UNCOMPILED_FUNCTION |
Attempt to evaluate an uncompiled function.
|
static int |
UNKNOWN_IDENTIFIER |
Unknown identifier.
|
NO_ERROR, SYNTAX_ERROR| Constructor | Description |
|---|---|
SuryonoParser(int variablecount) |
The constructor of
Parser. |
SuryonoParser(java.lang.String f,
java.lang.String v) |
The constructor of
Parser. |
SuryonoParser(java.lang.String f,
java.lang.String[] v) |
The constructor of
Parser. |
SuryonoParser(java.lang.String f,
java.lang.String v1,
java.lang.String v2) |
The constructor of
Parser. |
| Modifier and Type | Method | Description |
|---|---|---|
void |
define(java.lang.String definition) |
Defines a function.
|
void |
defineVariable(int index,
java.lang.String name) |
Sets the variable names.
|
double |
evaluate() |
Evaluates compiled function.
|
double |
evaluate(double x) |
Evaluates the function at x.
|
double |
evaluate(double[] v) |
|
double |
evaluate(double x,
double y) |
|
double |
evaluate(double x,
double y,
double z) |
|
boolean |
evaluatedToNaN() |
Determines if last evaluation resulted in NaN.
|
int |
getErrorCode() |
Gets error code of last operation.
|
int |
getErrorPosition() |
Gets error position.
|
java.lang.String |
getErrorString() |
Gets error string/message of last operation.
|
java.lang.String |
getFunction() |
Gets function string of last operation.
|
java.lang.String[] |
getFunctionNames() |
Returns all built-in and extended function names.
|
java.lang.String[] |
getVariableNames() |
|
void |
parse() |
Parses defined function.
|
void |
parse(java.lang.String function) |
Parses defined function.
|
java.lang.String[] |
parseUnknown(java.lang.String function) |
Parses a function looking for unknown variables.
|
void |
setFunction(java.lang.String funcStr) |
Parse the function string using the existing variables.
|
void |
setFunction(java.lang.String funcStr,
java.lang.String[] vars) |
Parse the function string using new variable names.
|
void |
setToZero() |
Sets the funtion to zero.
|
void |
setVariable(int index,
double value) |
Sets the variable value.
|
void |
setVariable(java.lang.String name,
double value) |
Sets the variable value.
|
static java.lang.String |
toErrorString(int errorcode) |
Converts error code to error string.
|
void |
useDegree() |
Sets the angle unit to degree.
|
void |
useRadian() |
Sets the angle unit to radian.
|
createParserpublic static final int PAREN_EXPECTED
public static final int UNCOMPILED_FUNCTION
public static final int EXPRESSION_EXPECTED
public static final int UNKNOWN_IDENTIFIER
public static final int OPERATOR_EXPECTED
public static final int PAREN_NOT_MATCH
public static final int CODE_DAMAGED
public static final int STACK_OVERFLOW
public static final int TOO_MANY_CONSTS
public static final int COMMA_EXPECTED
public static final int INVALID_OPERAND
public static final int INVALID_OPERATOR
public static final int NO_FUNC_DEFINITION
public static final int REF_NAME_EXPECTED
public SuryonoParser(java.lang.String f,
java.lang.String v)
throws ParserException
Parser.
Added by W. Christian to make it easy to construct a parser for with one variable.f - functionv - variableParserExceptionpublic SuryonoParser(java.lang.String f,
java.lang.String v1,
java.lang.String v2)
throws ParserException
Parser.
Added by W. Christian to make it easy to construct a parser for with two variables.f - the functionv1 - variable 1v2 - variable 2ParserExceptionpublic SuryonoParser(java.lang.String f,
java.lang.String[] v)
throws ParserException
Parser.
Added by W. Christian to make it easy to construct a parser for with multiple variables.f - the functionv - variablesParserExceptionpublic SuryonoParser(int variablecount)
Parser.variablecount - the number of variablespublic void setToZero()
public void useRadian()
public void useDegree()
public void defineVariable(int index,
java.lang.String name)
index - the variable index (one based)name - the variable namepublic void setVariable(int index,
double value)
index - the variable index (one based)value - the variable valuepublic void setVariable(java.lang.String name,
double value)
name - the variable namevalue - the variable valuepublic void define(java.lang.String definition)
definition - the function definitionpublic void parse(java.lang.String function)
throws ParserException
ParserExceptionpublic java.lang.String[] parseUnknown(java.lang.String function)
throws ParserException
ParserExceptionpublic java.lang.String[] getVariableNames()
public java.lang.String[] getFunctionNames()
getFunctionNames in class MathExpParserpublic void parse()
public double evaluate(double x,
double y)
public double evaluate(double x,
double y,
double z)
public double evaluate(double x)
Functionpublic double evaluate(double[] v)
public double evaluate()
public boolean evaluatedToNaN()
public int getErrorCode()
public java.lang.String getErrorString()
public int getErrorPosition()
public static java.lang.String toErrorString(int errorcode)
public java.lang.String getFunction()
getFunction in class MathExpParserpublic void setFunction(java.lang.String funcStr)
throws ParserException
setFunction in class MathExpParserfuncStr - the function to be parsedParserExceptionpublic void setFunction(java.lang.String funcStr,
java.lang.String[] vars)
throws ParserException
setFunction in class MathExpParserfuncStr - the function to be parsedvars - the function's variablesParserException