Package parser

Class MathExpression

java.lang.Object
parser.MathExpression
All Implemented Interfaces:
Savable, Solvable, Serializable
Direct Known Subclasses:
BaseNFunction, MatrixFunction, PolynomialExpression

public class MathExpression extends Object implements Savable, Solvable
This class models a mathematical parser. It is designed to handle div operators, methods(inbuilt and user-defined.) The parser is designed to work with a VariableManager and a FunctionManager. To work with expressions that contain variables,the parser looks up the value of variables stored in its VariableManager and sets those values in the expression. To work with expressions containing user defined functions, the parser looks up the underlying expression of the function in the FunctionManager and employs that value in evaluating the function. NOTE: The parser operation is divided into: Step 1. Expression Processing...This step takes time. Step 2. Expression Evaluation....Is an high speed one. For expressions that contain either user defined functions or statistical functions however, The second part is self-referentially mixed with the first and so this gives rise to a problem. For iterative processes, the parser only needs parse (Step 1.)the expression once(which takes the bulk of the time) and then it can evaluate it many times over iteratively in a loop. Step 2 is an high speed one. But if the expression contains statistical functions or user defined ones, the self-referential processes modify the scanner output and so this scanner output cannot be reliably referred to later on by iterative processes or any process that seeks to reuse the scanner's output.
See Also:
  • Field Details

    • parser_Result

      public Parser_Result parser_Result
    • DRG

      private DRG_MODE DRG
    • lastResult

      public static String lastResult
    • whitespaceremover

      private ArrayList<String> whitespaceremover
    • expression

      private String expression
      The expression to evaluate.
    • correctFunction

      protected boolean correctFunction
    • noOfListReturningOperators

      protected int noOfListReturningOperators
    • scanner

      protected ArrayList<String> scanner
    • optimizable

      private boolean optimizable
    • bracket

      private Bracket[] bracket
    • hasListReturningOperators

      protected boolean hasListReturningOperators
    • hasNumberReturningStatsOperators

      private boolean hasNumberReturningStatsOperators
    • hasPlusOrMinusOperators

      private boolean hasPlusOrMinusOperators
    • hasMulOrDivOperators

      private boolean hasMulOrDivOperators
    • hasPowerOperators

      private boolean hasPowerOperators
    • hasPostNumberOperators

      private boolean hasPostNumberOperators
    • hasPreNumberOperators

      private boolean hasPreNumberOperators
    • hasRemainderOperators

      private boolean hasRemainderOperators
    • hasPermOrCombOperators

      private boolean hasPermOrCombOperators
    • hasLogicOperators

      private boolean hasLogicOperators
    • autoInitOn

      private static boolean autoInitOn
      If set to true, MathExpression objects will automatically initialize undeclared variables to zero and store them.
    • hasFunctionOrVariableInitStatement

      private boolean hasFunctionOrVariableInitStatement
      Set this attribute to true, if and only if the input into this system is meant to initialize or change the value of variable data alone and is not calculating a standalone math expression. For example, var a =2;var b=3;var c=cos(a-b);f(x)=3*x^2; d=3cos(c); where c has been declared before!
    • variableManager

      private VariableManager variableManager
      The VariableManager object that allows an object of this class to remember its variables.
    • utility

      StringBuilder utility
      Utility attribute used throughout the class for string appending operations.
    • returnType

      TYPE returnType
      The type of output returned by the parser.
    • returnObjectName

      private String returnObjectName
      Sometimes, after evaluation the evaluation list which is a local variable, is reduced to a function name(or other object as time goes on) instead of a number of other list. The parser unfortunately will not return this variable, but instead returns the data which it refers to..e.g a Matrix function or other. But we atimes need that function name...So we cache this value here.
  • Constructor Details

    • MathExpression

      public MathExpression()
      default no argument constructor for class MathExpression. It creates a function which has the value 0;
    • MathExpression

      public MathExpression(String input)
      Parameters:
      input - The function to be evaluated. The general format contains variable, constant and function declarations for variables, constants and functions that are not yet initialized, assignment expressions for those that have been initialized and then an expression to evaluate. e.g. x = -12; y =x+1/12; const x1,x2,x3=10; z =sin(3x-1)+2.98cos(4x);cos(3x+12); The last expression is a function to be evaluated and it is always without any equals sign and may or may not end with a semicolon.
  • Method Details

    • getExpression

      public String getExpression()
    • setExpression

      public final void setExpression(String expression)
      Parameters:
      expression - The expression
    • isScannedAndOptimized

      public boolean isScannedAndOptimized()
      Returns:
      true if this object has been scanned and is found valid. In this state, objects of this class are optimized to run at very high speeds.
    • setAutoInitOn

      public static void setAutoInitOn(boolean autoInitOn)
    • isAutoInitOn

      public static boolean isAutoInitOn()
    • initializing

      private void initializing(String expression)
    • removeCommas

      private void removeCommas()
    • getDRG

      public DRG_MODE getDRG()
      Returns:
      the DRG value:0 for degrees, 1 for rads, 2 for grads
    • setDRG

      public void setDRG(DRG_MODE DRG)
      sets the DRG property
      Parameters:
      DRG -
    • getBracket

      public Bracket[] getBracket()
      Returns:
      the Brackets ArrayList containing all Bracket objects found in the input.
    • setBracket

      public void setBracket(Bracket[] bracket)
      Parameters:
      bracket - the Brackets ArrayList containing all Bracket objects found in the input.
    • isCorrectFunction

      public boolean isCorrectFunction()
      Returns:
      true if the input can be evaluated.
    • setCorrectFunction

      public void setCorrectFunction(boolean correctFunction)
      Parameters:
      correctFunction - sets if the input is valid and can be evaluated or not.
    • getNoOfListReturningOperators

      public int getNoOfListReturningOperators()
      Returns:
      the number of list returning operators found in the input.
    • setNoOfListReturningOperators

      public void setNoOfListReturningOperators(int noOfListReturningOperators)
      Parameters:
      noOfListReturningOperators - sets the number of list returning operators found in the input.
    • getScanner

      public ArrayList<String> getScanner()
      Returns:
      the ArrayList object that the input is scanned into.
    • setScanner

      public void setScanner(ArrayList<String> scanner)
      Parameters:
      scanner - sets the ArrayList object that the input is scanned into.
    • getWhitespaceremover

      public ArrayList<String> getWhitespaceremover()
      Returns:
      the white space remover ArrayList object.
    • setWhitespaceremover

      public void setWhitespaceremover(ArrayList<String> whitespaceremover)
      Parameters:
      whitespaceremover - sets the white space remover ArrayList object.
    • isHasListReturningOperators

      public boolean isHasListReturningOperators()
      Returns:
      true if there are list-returning operators
    • setHasListReturningOperators

      public void setHasListReturningOperators(boolean hasListReturningOperators)
      Parameters:
      hasListReturningOperators - sets the number of list returning operators.
    • setOptimizable

      public void setOptimizable(boolean optimizable)
      Parameters:
      optimizable - sets whether this input can be optimized.
    • isOptimizable

      public boolean isOptimizable()
      Returns:
      whether or not this input can be optimized
    • setLastResult

      public static void setLastResult(String lastResult)
      Parameters:
      lastResult - sets the last answer gotten by this parser
    • getLastResult

      public static String getLastResult()
      Returns:
      the last answer calculated by this tool
    • setHasPreNumberOperators

      public void setHasPreNumberOperators(boolean hasPreNumberOperators)
      Parameters:
      hasPreNumberOperators - sets whether the input has pre-number operators or not
    • isHasPreNumberOperators

      public boolean isHasPreNumberOperators()
      Returns:
      true if the input has pre number operators
    • setHasLogicOperators

      public void setHasLogicOperators(boolean hasLogicOperators)
      Parameters:
      hasLogicOperators - sets whether the input has logic operators or not.
    • isHasLogicOperators

      public boolean isHasLogicOperators()
      Returns:
      true if the input has logic operators
    • setHasPostNumberOperators

      public void setHasPostNumberOperators(boolean hasPostNumberOperators)
      Parameters:
      hasPostNumberOperators - sets whether the input has post number operators
    • isHasPostNumberOperators

      public boolean isHasPostNumberOperators()
      Returns:
      true if post number operators like factorial, inverse e.t.c
    • setHasPowerOperators

      public void setHasPowerOperators(boolean hasPowerOperators)
      Parameters:
      hasPowerOperators - sets whether or not the input has the power operator
    • isHasPowerOperators

      public boolean isHasPowerOperators()
      Returns:
      true if the input has the power operator
    • setHasMulOrDivOperators

      public void setHasMulOrDivOperators(boolean hasMulOrDivOperators)
      Parameters:
      hasMulOrDivOperators - sets whether the input has multiplication or division operators
    • isHasMulOrDivOperators

      public boolean isHasMulOrDivOperators()
      Returns:
      true if the input has multiplication or division operators
    • getReturnObjectName

      public String getReturnObjectName()
      Sometimes, after evaluation the evaluation list which is a local variable, is reduced to a function name(or other object as time goes on) instead of a number of other list. The parser unfortunately will not return this variable, but instead returns the data which it refers to..e.g a Matrix function or other. But we atimes need that function name...So we cache this value here.
    • setHasPlusOrMinusOperators

      public void setHasPlusOrMinusOperators(boolean hasPlusOrMinusOperators)
      Parameters:
      hasPlusOrMinusOperators - sets whether or not the input contains plus or minus operators
    • isHasPlusOrMinusOperators

      public boolean isHasPlusOrMinusOperators()
      Returns:
      true if plus or minus operators are found in the input
    • setHasRemainderOperators

      public void setHasRemainderOperators(boolean hasRemainderOperators)
      Parameters:
      hasRemainderOperators - sets whether or not remainder operators are found in the input
    • isHasRemainderOperators

      public boolean isHasRemainderOperators()
      Returns:
      true if remainder operators are found in the input
    • setHasPermOrCombOperators

      public void setHasPermOrCombOperators(boolean hasPermOrCombOperators)
      Parameters:
      hasPermOrCombOperators - sets whether permutation and combination operators are found in the input
    • isHasPermOrCombOperators

      public boolean isHasPermOrCombOperators()
      Returns:
      true if permutation and combination operators are found in the input
    • setHasNumberReturningStatsOperators

      public void setHasNumberReturningStatsOperators(boolean hasNumberReturningStatsOperators)
      Parameters:
      hasNumberReturningStatsOperators - sets whether or not the input contains a data set that will evaluate to a number
    • isHasNumberReturningStatsOperators

      public boolean isHasNumberReturningStatsOperators()
      Returns:
      true if the input contains a data set that will evaluate to a number
    • setVariableHandlerOnly

      public void setVariableHandlerOnly(boolean variableHandlerOnly)
    • isVariableHandlerOnly

      public boolean isVariableHandlerOnly()
    • getVariableManager

      public VariableManager getVariableManager()
    • setVariableManager

      public void setVariableManager(VariableManager variableManager)
    • getVars

      public ArrayList<Variable> getVars()
      Returns:
      an ArrayList object containing all Variable objects found in the current input expression. This is only a subset of all Variable objects used in the workspace of operation of this MathExpression object.
    • unBracketDataSetReturningStatsOperators

      public void unBracketDataSetReturningStatsOperators()
      Removes encapsulating brackets from data set returning statistical operators. For example, (((sort(3,sin(4),5,-1))) does not need the two enclosing bracket pairs so turn it into... sort(3,sin(4),5,-1).
    • statsVerifier

      public void statsVerifier()
    • evaluateCompoundStructuresInStatisticalInput

      private void evaluateCompoundStructuresInStatisticalInput()
      e.g in structures like sort(3,sin2,2sin3,5,3,2,sin(4+5),4!...) This method will help to reduce the input to the simple form i.e sort(num1,num2,num3....)
    • codeModifier

      public void codeModifier()
      The method establishes meaning to some shorthand techniques in math that the average mathematician might expect to see in a math device. :e.g(3+4)(1+2) will become (3+4)*(1+2).It is essentially a stage that generates code.
    • detectKeyOperators

      public void detectKeyOperators()
      Serves as a powerful optimizer of the evaluation section as it can govern what sections of code will be executed and which ones will be ignored.
    • mapBrackets

      public static Bracket[] mapBrackets(ArrayList<String> scanner)
      Parameters:
      scanner - The ArrayList object that holds the string values in the scanned function.
      Returns:
      a Bracket array that holds related brackets pairs.
    • mapBrackets

      public void mapBrackets()
      Method mapBrackets goes over an input equation and maps all positions that have corresponding brackets
    • functionComponentsAssociation

      public void functionComponentsAssociation()
      method finishUpScanner does final adjustments to the scanner function e.g it will check for errors in operator combination in the scanner function and so on
    • setVariableValuesInFunction

      public void setVariableValuesInFunction(ArrayList<String> scan)
      An important process that must occur before the function is solved. Variables must be replaced by their values. The method checks the variable store and assumes that between function input time and function solution time, the user would have modified the value attribute stored in the variables. So it gets the values there and fixes them in the appropriate points in the equation. Ensure that no shift has occurred in Variable object position during the time that the record was taken and the time when the position is about to be referenced.
      Parameters:
      scan - the data it is to process
    • getValue

      public String getValue(String name) throws NullPointerException
      Parameters:
      name - The name of the variable or constant.
      Returns:
      the value of the named variable or constant
      Throws:
      NullPointerException - if a Variable object that has that name id not found.
    • setValue

      public void setValue(String name, String value) throws NullPointerException, NumberFormatException
      Parameters:
      name - The name of the variable or constant.
      value - The value to set to the variable
      Throws:
      NullPointerException - if a Variable object that has that name id not found.
      NumberFormatException
    • modifyBracketIndices

      protected void modifyBracketIndices(Bracket[] brac, int startPosition, int increment, boolean run)
      Utility method used to dynamically change the indices of brackets in the governing bracket map of the scanner function. When method solve is performing its task,it uses the bracket ArrayList to know the next portion to evaluate in the scanner function. However when it evaluates this portion,the size of the scanner function changes(reduces) and this means that the bracket ArrayList is no longer relevant in determining the next point to evaluate in the scanner function. So this method is called to automatically re-configure the bracket ArrayList so that it can continue to be relevant to the solution process. The process occurs during method solve and is a sort of automatic compression in response to the solution process which brings about changes in the number of elements in the scanner function. LOGIC: Bearing in mind the fact that each bracket stores its current index in the MathExpression object's ArrayList,scanner; we traverse the bracket list starting from the bracket from which evaluation is to begin in the client method solve and looping on to the end of the bracket list. We check for the current index (say A) ( as in its position in the ArrayList object) stored by each bracket we meet during this scan and compare it with the index (say B)stored by the bracket from which we began the loop the last time this method was called. This is the bracket at index startPosition-2. If A<B then we apply the decrement or shrinking factor to it.Else we continue the scan.
      Parameters:
      brac - the Bracket store to modify
      startPosition - the index in the ArrayList where the modification is to start
      increment - the amount by which each bracket index is to be decreased
      run - will run this method if given the sign to do so.
    • displayIndicesStoredInBrackets

      private void displayIndicesStoredInBrackets()
      Display the indices of all brackets in the function,bracket pair by bracket pair
    • listToString

      protected String listToString(ArrayList<String> scan)
      Parameters:
      scan - The ArrayList object.
      Returns:
      the string version of the ArrayList and removes the braces i.e. []
    • copyArrayToArray

      protected Bracket[] copyArrayToArray()
      Returns:
      an Array object containing duplicate contents of the List object alone
    • setReturnType

      public void setReturnType(TYPE returnType)
    • getReturnType

      public TYPE getReturnType()
    • solve

      public String solve()
      Method solve is the main parser used to evaluate the input multi-bracket pair (MBP) expressions used to initialize the constructor of class MathExpression
      Specified by:
      solve in interface Solvable
      Returns:
      the result of the evaluation
    • solve

      protected List<String> solve(List<String> list)
      used by the main parser solve to figure out SBP portions of a multi-bracketed expression (MBP)
      Parameters:
      list - a list of scanner tokens of a maths expression
      Returns:
      the solution to a SBP maths expression
    • solveSubPortions

      public List<String> solveSubPortions(List<String> scanner)
      Parameters:
      scanner - is a list of scanner functions, gotten during the evaluation of sets of data that contain functions that need to be evaluated instead of numbers.If the data set does not contain functions e.g avg(2,3,7,1,0,9,5), then method solve will easily solve it. But if it does e.g avg(2,sin,3,5,cos,(,5,) ), then we invoke this method in class Set's constructor before we evaluate the data set. Note this is method is not called directly by MathExpression objects but by objects of class Set invoked by a MathExpression object.
      Returns:
      the solution to the scanner function
    • main

      public static void main(String... args)
    • serialize

      public String serialize()
      Specified by:
      serialize in interface Savable
    • parse

      public MathExpression parse(String enc)