org.opensourcephysics.numerics
Class SuryonoParser

java.lang.Object
  extended by org.opensourcephysics.numerics.MathExpParser
      extended by org.opensourcephysics.numerics.SuryonoParser
All Implemented Interfaces:
Function, MultiVarFunction

public final class SuryonoParser
extends MathExpParser

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
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.
 
Fields inherited from class org.opensourcephysics.numerics.MathExpParser
NO_ERROR, SYNTAX_ERROR
 
Constructor Summary
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.
 
Method Summary
 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)
           
 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.
 
Methods inherited from class org.opensourcephysics.numerics.MathExpParser
createParser
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAREN_EXPECTED

public static final int PAREN_EXPECTED
Parentheses expected.

See Also:
Constant Field Values

UNCOMPILED_FUNCTION

public static final int UNCOMPILED_FUNCTION
Attempt to evaluate an uncompiled function.

See Also:
Constant Field Values

EXPRESSION_EXPECTED

public static final int EXPRESSION_EXPECTED
Expression expected.

See Also:
Constant Field Values

UNKNOWN_IDENTIFIER

public static final int UNKNOWN_IDENTIFIER
Unknown identifier.

See Also:
Constant Field Values

OPERATOR_EXPECTED

public static final int OPERATOR_EXPECTED
Operator expected.

See Also:
Constant Field Values

PAREN_NOT_MATCH

public static final int PAREN_NOT_MATCH
Parenthesis mismatch.

See Also:
Constant Field Values

CODE_DAMAGED

public static final int CODE_DAMAGED
Code damaged.

See Also:
Constant Field Values

STACK_OVERFLOW

public static final int STACK_OVERFLOW
Stack overflow.

See Also:
Constant Field Values

TOO_MANY_CONSTS

public static final int TOO_MANY_CONSTS
Too many constants.

See Also:
Constant Field Values

COMMA_EXPECTED

public static final int COMMA_EXPECTED
Comma expected.

See Also:
Constant Field Values

INVALID_OPERAND

public static final int INVALID_OPERAND
Invalid operand.

See Also:
Constant Field Values

INVALID_OPERATOR

public static final int INVALID_OPERATOR
Invalid operator.

See Also:
Constant Field Values

NO_FUNC_DEFINITION

public static final int NO_FUNC_DEFINITION
No function definition to parse.

See Also:
Constant Field Values

REF_NAME_EXPECTED

public static final int REF_NAME_EXPECTED
Referenced name could not be found.

See Also:
Constant Field Values
Constructor Detail

SuryonoParser

public SuryonoParser(java.lang.String f,
                     java.lang.String v)
              throws ParserException
The constructor of Parser. Added by W. Christian to make it easy to construct a parser for with one variable.

Parameters:
f - function
v - variable
Throws:
ParserException

SuryonoParser

public SuryonoParser(java.lang.String f,
                     java.lang.String v1,
                     java.lang.String v2)
              throws ParserException
The constructor of Parser. Added by W. Christian to make it easy to construct a parser for with two variables.

Parameters:
f - the function
v1 - variable 1
v2 - variable 2
Throws:
ParserException

SuryonoParser

public SuryonoParser(java.lang.String f,
                     java.lang.String[] v)
              throws ParserException
The constructor of Parser. Added by W. Christian to make it easy to construct a parser for with multiple variables.

Parameters:
f - the function
v - variables
Throws:
ParserException

SuryonoParser

public SuryonoParser(int variablecount)
The constructor of Parser.

Parameters:
variablecount - the number of variables
Method Detail

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

public void defineVariable(int index,
                           java.lang.String name)
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

public void setVariable(java.lang.String name,
                        double value)
Sets the variable value. The variable is accessed by name. Nothing happens if variable could not be found.

Parameters:
name - the variable name
value - the variable value

define

public void define(java.lang.String definition)
Defines a function. Current postfix code becomes invalid.

Parameters:
definition - the function definition

parse

public void parse(java.lang.String function)
           throws ParserException
Parses defined function.

Throws:
ParserException

parseUnknown

public java.lang.String[] parseUnknown(java.lang.String function)
                                throws ParserException
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

public java.lang.String[] getVariableNames()

getFunctionNames

public java.lang.String[] getFunctionNames()
Returns all built-in and extended function names. Added by D. Brown 06 Jul 2008

Specified by:
getFunctionNames in class MathExpParser
Returns:
array of function names

parse

public void parse()
Parses defined function.


evaluate

public double evaluate(double x,
                       double y)

evaluate

public double evaluate(double x,
                       double y,
                       double z)

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[] v)

evaluate

public double evaluate()
Evaluates compiled function.

Returns:
the result of the function

getErrorCode

public int getErrorCode()
Gets error code of last operation.

Returns:
the error code

getErrorString

public java.lang.String 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

public static java.lang.String toErrorString(int errorcode)
Converts error code to error string.

Returns:
the error string

getFunction

public java.lang.String getFunction()
Gets function string of last operation. Added by W. Christian to implement the MathExpParser interface.

Specified by:
getFunction in class MathExpParser
Returns:
the function string

setFunction

public void setFunction(java.lang.String funcStr)
                 throws ParserException
Parse the function string using the existing variables. Added by W. Christian to implement the MathExpParser interface.

Specified by:
setFunction in class MathExpParser
Parameters:
funcStr - the function to be parsed
Throws:
ParserException

setFunction

public void setFunction(java.lang.String funcStr,
                        java.lang.String[] vars)
                 throws ParserException
Parse the function string using new variable names. Added by W. Christian to implement the MathExpParser interface.

Specified by:
setFunction in class MathExpParser
Parameters:
funcStr - the function to be parsed
vars - the function's variables
Throws:
ParserException