org.jscience.mathematics.analysis.expressions
Class ExpressionParser

java.lang.Object
  extended by org.jscience.mathematics.analysis.expressions.ExpressionParser
All Implemented Interfaces:
java.io.Serializable

public class ExpressionParser
extends java.lang.Object
implements java.io.Serializable

Expression parser for mathematical expressions. The parser has some predefined constants: pi and e that are available if source code is parsed. If only single expressions are parsed pi and e are not available. Known bugs (may not be fixed anytime soon): Using an incorrect number of arguments for functions may lead to incorrect expressions, such as sin(x,y)+ which results in code generation of the expression x+sin(y). Similarly, it is possible to use stack oriented syntax (reverse polish notation syntax) like in the example x,y,sin()+ which also generates the code x+sin(y). Hint: Do not use functions with associated bugs until bug comments have been removed.
Examples of use:

 ExpressionParser parser = new ExpressionParser();
 Expression cnst = parser.parse( "1+sin(3/2.0)" );
 if ( cnst != null ) {
 double doubleValue = cnst.eval();
 System.out.println( "value=" + doubleValue );
 }
 
which generates the output value=1.9974949866040543.
A slightly more complicated example is the following which include a single independent variable x.
 ExpressionParser parser = new ExpressionParser();
 java.util.List variableNames = new java.util.ArrayList();
 java.util.List variables = new java.util.ArrayList();
 variableNames.add( "x" );
 Variable x = new Variable( "x", 2.3 );
 variables.add( x );
 parser.setVariableNames( variableNames );
 parser.setVariables( variables );
 Expression expr = parser.parse( "x+sin(3*x)" );
 Expression derivative = expr.diff( x );
 Expression optderivative = derivative.optimize();
 System.out.println( "expr=" + expr );
 System.out.println( "derivative=" + derivative );
 System.out.println( "optimized derivative=" + optderivative );
 
which generates the following output.
 expr=(x+sin((3.0*x)))
 derivative=(1.0+(cos((3.0*x))*((0.0*x)+(3.0*1.0))))
 optderivative=(1.0+(cos((3.0*x))*3.0))
 
An even more complicated example which includes both variables and parameters is seen below.
 import org.jscience.mathematics.analysis.*;
 import java.util.Vector;
 public class TestExp {
 public static void main( String[] args ) {
 String[] vars = new String[] { "x", "y" };
 Vector variableNames  = new Vector();
 Vector variables = new Vector();
 for (int i = 0; i < vars.length; i++) {
 variableNames.add( vars[ i ] );
 variables.add( new Variable( vars[ i ], 0 ) );
 }
 String[] pars = new String[] { "a", "b" };
 Vector parameterNames = new Vector();
 Vector parameters = new Vector();
 for (int i = 0; i < pars.length; i++) {
 parameterNames.add( pars[ i ] );
 parameters.add( new Parameter( pars[ i ], 0 ) );
 }
 Vector constantNames  = new Vector();
 Vector auxiliaryNames = new Vector();
 ExpressionParser parser = new ExpressionParser();
 parser.setVariableNames(  variableNames );
 parser.setVariables( variables );
 parser.setParameterNames( parameterNames );
 parser.setParameters( parameters  );
 parser.setConstantNames(  constantNames );
 parser.setAuxiliaryNames( auxiliaryNames );
 Expression expr = (Expression)parser.parse( "a*x-x*a+b*sin(4*y*1+0+b/1)" );
 System.out.println( "Expression = " + expr );
 expr = expr.optimize();
 System.out.println( "Expression = " + expr );
 }
 }
 

The above code generates something like the following output:
 Expression = (((a*x)-(x*a))+(b*sin(((((4.0*y)*1.0)+0.0)+(b/1.0)))))
 Expression = (b*sin(((4.0*y)+b)))
 

See Also:
Serialized Form

Nested Class Summary
static class ExpressionParser.Compiler
          The static class Compiler is intended for command line compilation of source files defining dynamical systems.
 
Constructor Summary
ExpressionParser()
          Public constructor for the ExpressionParser class.
 
Method Summary
 void generateJavaSource(java.lang.String fileName)
          Generates Java source code for computing the right hand side.
 java.util.List getAuxiliaries()
           
 java.util.List getAuxiliaryNames()
           
 java.util.List getConstantNames()
           
 java.util.List getConstants()
           
 java.lang.String getError()
           
 java.lang.String getInformation()
           
 Expression[] getInverseRHS()
           
 Expression[][] getJacobiMatrix()
           
 java.lang.String getJavaSource()
           
 java.util.List getLogicalNames()
           
 java.util.List getLogicals()
           
 int getNumberOfAuxillaries()
           
 int getNumberOfConstants()
           
 int getNumberOfEvents()
           
 int getNumberOfParameters()
           
 int getNumberOfProcesses()
           
 int getNumberOfUserFunctions()
           
 int getNumberOfVariables()
           
 Expression[][] getParameterJacobiMatrix()
           
 java.util.List getParameterNames()
           
 java.util.List getParameters()
           
 Expression[] getRHS()
           
 java.util.List getUserFunctionNames()
           
 java.util.List getVariableNames()
           
 java.util.List getVariables()
           
 java.lang.String infixToPostfix(java.lang.String infix)
          Converts a mathematical expression in the Stringinfix in infix format to a mathematical expression in postfix format.
 boolean isCompiled()
           
 boolean isDifferentiable()
           
 boolean isExpandable()
           
 boolean isInvertible()
           
 boolean isNondifferentiable()
           
 boolean isParameterDifferentiable()
           
 boolean isTimeContinuous()
           
 boolean isTimeDiscrete()
           
static void main(java.lang.String[] args)
           
 java.lang.String oldparseSource(java.lang.String s)
          DOCUMENT ME!
 java.lang.Object parse(java.lang.String symbolic)
          The method parses the argument String using the method uses the methods infixToPostfix(java.lang.String) and postfixToExpression(java.lang.String) to generate a Expression.
 java.lang.String parseSource(java.lang.String s)
          Parses the String and attempts to generate symbolic expressions for the right hand side(s).
 java.lang.Object postfixToExpression(java.lang.String postfix)
          The method converts a mathematical expression in postfix format to an Expression in symbolic format.
 java.lang.String postfixToInfix(java.lang.String postfix)
          Converts a postfix expression to it corresponding infix form.
 TaylorDouble postfixToTaylorDouble(java.lang.String postfix)
          The method converts a mathematical expression in postfix format to a TaylorMap in symbolic format.
 void setAccessLevel(java.lang.String s)
          DOCUMENT ME!
 void setAuxiliaries(java.util.List auxs)
           
 void setAuxiliaryNames(java.util.List auxiliaryNames)
           
 void setConstantNames(java.util.List constantNames)
           
 void setConstants(java.util.List constants)
           
 void setJavaSource(java.lang.String source)
           
 void setLogicalNames(java.util.List logicalNames)
           
 void setLogicals(java.util.List logicals)
           
 void setOptimizationLevel(int level)
          DOCUMENT ME!
 void setParameterNames(java.util.List parameterNames)
           
 void setParameters(java.util.List parameters)
           
 void setStrict(boolean s)
          DOCUMENT ME!
 void setVariableNames(java.util.List variableNames)
           
 void setVariables(java.util.List variables)
           
 java.lang.String toXML()
          The method returns an xml representation of the system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionParser

public ExpressionParser()
Public constructor for the ExpressionParser class. Initialises the vector operatorVector containing the names of all unary and binary operators allowed.

Method Detail

setOptimizationLevel

public void setOptimizationLevel(int level)
DOCUMENT ME!

Parameters:
level - DOCUMENT ME!

setStrict

public void setStrict(boolean s)
DOCUMENT ME!

Parameters:
s - DOCUMENT ME!

setAccessLevel

public void setAccessLevel(java.lang.String s)
DOCUMENT ME!

Parameters:
s - DOCUMENT ME!

getInformation

public java.lang.String getInformation()

getError

public java.lang.String getError()

getJavaSource

public java.lang.String getJavaSource()

setJavaSource

public void setJavaSource(java.lang.String source)

isCompiled

public boolean isCompiled()

isDifferentiable

public boolean isDifferentiable()

isNondifferentiable

public boolean isNondifferentiable()

isParameterDifferentiable

public boolean isParameterDifferentiable()

isExpandable

public boolean isExpandable()

isInvertible

public boolean isInvertible()

getNumberOfVariables

public int getNumberOfVariables()

getNumberOfParameters

public int getNumberOfParameters()

getNumberOfConstants

public int getNumberOfConstants()

getNumberOfAuxillaries

public int getNumberOfAuxillaries()

getNumberOfEvents

public int getNumberOfEvents()

getNumberOfProcesses

public int getNumberOfProcesses()

getNumberOfUserFunctions

public int getNumberOfUserFunctions()

getVariableNames

public java.util.List getVariableNames()

getParameterNames

public java.util.List getParameterNames()

getConstantNames

public java.util.List getConstantNames()

getAuxiliaryNames

public java.util.List getAuxiliaryNames()

getUserFunctionNames

public java.util.List getUserFunctionNames()

getLogicalNames

public java.util.List getLogicalNames()

setVariableNames

public void setVariableNames(java.util.List variableNames)

setAuxiliaryNames

public void setAuxiliaryNames(java.util.List auxiliaryNames)

setParameterNames

public void setParameterNames(java.util.List parameterNames)

setConstantNames

public void setConstantNames(java.util.List constantNames)

setLogicalNames

public void setLogicalNames(java.util.List logicalNames)

getVariables

public java.util.List getVariables()

getAuxiliaries

public java.util.List getAuxiliaries()

setAuxiliaries

public void setAuxiliaries(java.util.List auxs)

setVariables

public void setVariables(java.util.List variables)

getParameters

public java.util.List getParameters()

setParameters

public void setParameters(java.util.List parameters)

getConstants

public java.util.List getConstants()

setConstants

public void setConstants(java.util.List constants)

getLogicals

public java.util.List getLogicals()

setLogicals

public void setLogicals(java.util.List logicals)

getRHS

public Expression[] getRHS()

getInverseRHS

public Expression[] getInverseRHS()

getJacobiMatrix

public Expression[][] getJacobiMatrix()

getParameterJacobiMatrix

public Expression[][] getParameterJacobiMatrix()

isTimeDiscrete

public boolean isTimeDiscrete()

isTimeContinuous

public boolean isTimeContinuous()

parseSource

public java.lang.String parseSource(java.lang.String s)
Parses the String and attempts to generate symbolic expressions for the right hand side(s). See examples above. Diagnostic information about what happende during parsing can be retrieved with the method getInformation. If no error then null is returned.

Parameters:
s - DOCUMENT ME!
Returns:
String representing error message.

oldparseSource

public java.lang.String oldparseSource(java.lang.String s)
DOCUMENT ME!

Parameters:
s - DOCUMENT ME!
Returns:
DOCUMENT ME!

generateJavaSource

public void generateJavaSource(java.lang.String fileName)
Generates Java source code for computing the right hand side. The source code is placed in the privateString which can be retrieved by the method getJavaSource. If the String argument to the method differs from the empty String (""), say, is "w.ode" the generated source will be saved in a file "w.java" if possible.

Parameters:
fileName - DOCUMENT ME!

toXML

public java.lang.String toXML()
The method returns an xml representation of the system.

Returns:
DOCUMENT ME!

parse

public java.lang.Object parse(java.lang.String symbolic)
The method parses the argument String using the method uses the methods infixToPostfix(java.lang.String) and postfixToExpression(java.lang.String) to generate a Expression. Before these methods are invoked, it is determined whether the argument is a number that can be handled by the static parseDouble(java.lang.String) method from the java.lang.Double class. If this is the case a Constant with the value is returned. Syntax errors are signalled by the return of a null pointer. The concrete syntax error detected can be determined by the method call getError() which returns a String.

Parameters:
symbolic - DOCUMENT ME!
Returns:
DOCUMENT ME!

postfixToExpression

public java.lang.Object postfixToExpression(java.lang.String postfix)
The method converts a mathematical expression in postfix format to an Expression in symbolic format.

Parameters:
postfix - Mathematical expressioin in postfix format.
Returns:
null if a syntax error is found.

postfixToTaylorDouble

public TaylorDouble postfixToTaylorDouble(java.lang.String postfix)
The method converts a mathematical expression in postfix format to a TaylorMap in symbolic format.

Parameters:
postfix - Mathematical expressioin in postfix format.
Returns:
null if a syntax error is found.

infixToPostfix

public java.lang.String infixToPostfix(java.lang.String infix)
Converts a mathematical expression in the Stringinfix in infix format to a mathematical expression in postfix format. E.g. "x+y(z-4)" is converted to "x y z 4 - +." In case of a syntax error in postfix the method returns null and sets the StringerrorString to a descriptive error message. This error description can be retrieved with the public method getError.

Parameters:
infix - DOCUMENT ME!
Returns:
DOCUMENT ME!

postfixToInfix

public java.lang.String postfixToInfix(java.lang.String postfix)
Converts a postfix expression to it corresponding infix form. Is not used in ExpressionParser. It is not tested and almost certainly will not work with anything but one argument functions. CK 03032001

Parameters:
postfix - DOCUMENT ME!
Returns:
DOCUMENT ME!

main

public static void main(java.lang.String[] args)