Package org.opensourcephysics.numerics
Class SuryonoParser
java.lang.Object
org.opensourcephysics.numerics.MathExpParser
org.opensourcephysics.numerics.SuryonoParser
- All Implemented Interfaces:
Function
,MultiVarFunction
The class
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); }
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Code damaged.static final int
Comma expected.static final int
Expression expected.static final int
Invalid operand.static final int
Invalid operator.static final int
No function definition to parse.static final String
static final String
static final int
Operator expected.static final int
Parentheses expected.static final int
Parenthesis mismatch.static final int
Referenced name could not be found.static final int
Stack overflow.static final int
Too many constants.static final int
Attempt to evaluate an uncompiled function.static final int
Unknown identifier.Fields inherited from class org.opensourcephysics.numerics.MathExpParser
NO_ERROR, SYNTAX_ERROR
-
Constructor Summary
ConstructorsConstructorDescriptionSuryonoParser
(int nVar) The constructor ofParser
.SuryonoParser
(String f, String v) The constructor ofParser
.SuryonoParser
(String f, String[] v) The constructor ofParser
.SuryonoParser
(String funcStr, String[] vars, boolean allowUnkownIdentifiers) SuryonoParser
(String f, String v1, String v2) The constructor ofParser
. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
before we test for a NFE, at least check that it COULD be a number.void
Defines a function.void
defineVariable
(int index, 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
Determines if last evaluation resulted in NaN.int
Gets error code of last operation.int
Gets error position.Gets error string/message of last operation.Gets function string of last parsing operation.String[]
Returns all built-in and extended function names.static double
String[]
void
parse()
Parses defined function.void
Parses defined function.String[]
parseUnknown
(String function) Parses a function looking for unknown variables.void
setError
(int err) void
setFunction
(String funcStr) Parse the function string using the existing variables.void
setFunction
(String funcStr, String[] vars) Parse the function string using new variable names.void
Sets the funtion to zero.void
setVariable
(int index, double value) Sets the variable value.void
setVariable
(String name, double value) Sets the variable value.static String
toErrorString
(int errorcode) Converts error code to error string.toString()
void
Sets the angle unit to degree.void
Sets the angle unit to radian.Methods inherited from class org.opensourcephysics.numerics.MathExpParser
createParser
-
Field Details
-
PAREN_EXPECTED
public static final int PAREN_EXPECTEDParentheses expected.- See Also:
-
UNCOMPILED_FUNCTION
public static final int UNCOMPILED_FUNCTIONAttempt to evaluate an uncompiled function.- See Also:
-
EXPRESSION_EXPECTED
public static final int EXPRESSION_EXPECTEDExpression expected.- See Also:
-
UNKNOWN_IDENTIFIER
public static final int UNKNOWN_IDENTIFIERUnknown identifier.- See Also:
-
OPERATOR_EXPECTED
public static final int OPERATOR_EXPECTEDOperator expected.- See Also:
-
PAREN_NOT_MATCH
public static final int PAREN_NOT_MATCHParenthesis mismatch.- See Also:
-
CODE_DAMAGED
public static final int CODE_DAMAGEDCode damaged.- See Also:
-
STACK_OVERFLOW
public static final int STACK_OVERFLOWStack overflow.- See Also:
-
TOO_MANY_CONSTS
public static final int TOO_MANY_CONSTSToo many constants.- See Also:
-
COMMA_EXPECTED
public static final int COMMA_EXPECTEDComma expected.- See Also:
-
INVALID_OPERAND
public static final int INVALID_OPERANDInvalid operand.- See Also:
-
INVALID_OPERATOR
public static final int INVALID_OPERATORInvalid operator.- See Also:
-
NO_FUNC_DEFINITION
public static final int NO_FUNC_DEFINITIONNo function definition to parse.- See Also:
-
REF_NAME_EXPECTED
public static final int REF_NAME_EXPECTEDReferenced name could not be found.- See Also:
-
NULL
- See Also:
-
NULL_D
- See Also:
-
-
Constructor Details
-
SuryonoParser
The constructor ofParser
. Added by W. Christian to make it easy to construct a parser for with one variable.- Parameters:
f
- functionv
- variable- Throws:
ParserException
-
SuryonoParser
The constructor ofParser
. Added by W. Christian to make it easy to construct a parser for with two variables.- Parameters:
f
- the functionv1
- variable 1v2
- variable 2- Throws:
ParserException
-
SuryonoParser
The constructor ofParser
. Added by W. Christian to make it easy to construct a parser for with multiple variables.- Parameters:
f
- the functionv
- variables- Throws:
ParserException
-
SuryonoParser
public SuryonoParser(String funcStr, String[] vars, boolean allowUnkownIdentifiers) throws ParserException - Parameters:
funcStr
-vars
-allowUnkownIdentifiers
- always false- Throws:
ParserException
-
SuryonoParser
public SuryonoParser(int nVar) The constructor ofParser
.- Parameters:
nVar
- the number of variables
-
-
Method Details
-
setError
public void setError(int err) -
setToZero
public void setToZero()Sets the funtion to zero. -
useRadian
public void useRadian()Sets the angle unit to radian. Default upon construction. -
useDegree
public void useDegree()Sets the angle unit to degree. -
defineVariable
Sets the variable names. Nothing happens if variable index > number of variables.- Parameters:
index
- the variable index (one based)name
- the variable name
-
setVariable
public void setVariable(int index, double value) Sets the variable value. The variable is accessed by index. Nothing happens if variable index > number of variables.- Parameters:
index
- the variable index (one based)value
- the variable value
-
setVariable
Sets the variable value. The variable is accessed by name. Nothing happens if variable could not be found.- Parameters:
name
- the variable namevalue
- the variable value
-
define
Defines a function. Current postfix code becomes invalid.- Parameters:
def
- the function definition
-
parse
Parses defined function.- Throws:
ParserException
-
parseUnknown
Parses a function looking for unknown variables. Unknown tokens are used to create the variable list in the order that they are found.- Throws:
ParserException
-
getVariableNames
-
getFunctionNames
Returns all built-in and extended function names. Added by D. Brown 06 Jul 2008- Specified by:
getFunctionNames
in classMathExpParser
- Returns:
- array of function names
-
parse
public void parse()Parses defined function. -
evaluate
public double evaluate(double x) Description copied from interface:Function
Evaluates the function at x.- Returns:
- double f(x)
-
evaluate
public double evaluate(double x, double y) -
evaluate
public double evaluate(double x, double y, double z) -
evaluate
public double evaluate(double[] v) -
evaluate
public double evaluate()Evaluates compiled function.- Returns:
- the result of the functio
-
evaluatedToNaN
public boolean evaluatedToNaN()Determines if last evaluation resulted in NaN. Added by D Brown 15 Sep 2010.- Returns:
- true if result was converted from NaN to zero
-
getErrorCode
public int getErrorCode()Gets error code of last operation.- Returns:
- the error code
-
getErrorString
Gets error string/message of last operation.- Returns:
- the error string
-
getErrorPosition
public int getErrorPosition()Gets error position. Valid only if error code != NO_ERROR- Returns:
- error position (one based)
-
toErrorString
Converts error code to error string.- Returns:
- the error string
-
getFunction
Gets function string of last parsing operation. Added by W. Christian to implement the MathExpParser interface.- Specified by:
getFunction
in classMathExpParser
- Returns:
- the function string
-
setFunction
Parse the function string using the existing variables. Added by W. Christian to implement the MathExpParser interface.- Specified by:
setFunction
in classMathExpParser
- Parameters:
funcStr
- the function to be parsed- Throws:
ParserException
-
setFunction
Parse the function string using new variable names. Added by W. Christian to implement the MathExpParser interface.- Specified by:
setFunction
in classMathExpParser
- Parameters:
funcStr
- the function to be parsedvars
- the function's variables- Throws:
ParserException
-
getNumber
-
couldBeNumber
before we test for a NFE, at least check that it COULD be a number. I is for "Infinity"- Parameters:
n
-- Returns:
-
toString
-