Package parser

Class MathExpression

  • Direct Known Subclasses:
    BaseNFunction, MatrixFunction, PolynomialExpression

    public class MathExpression
    extends java.lang.Object
    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.
    Author:
    GBENRO
    • Field Detail

      • DRG

        protected int DRG
      • lastResult

        public static java.lang.String lastResult
      • correctFunction

        protected boolean correctFunction
      • noOfListReturningOperators

        protected int noOfListReturningOperators
      • scanner

        protected java.util.ArrayList<java.lang.String> scanner
      • hasListReturningOperators

        protected boolean hasListReturningOperators
    • Constructor Detail

      • MathExpression

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

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

      • getExpression

        public java.lang.String getExpression()
      • setExpression

        public final void setExpression​(java.lang.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()
      • getDRG

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

        public void setDRG​(int 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 java.util.ArrayList<java.lang.String> getScanner()
        Returns:
        the ArrayList object that the input is scanned into.
      • setScanner

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

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

        public void setWhitespaceremover​(java.util.ArrayList<java.lang.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​(java.lang.String lastResult)
        Parameters:
        lastResult - sets the last answer gotten by this parser
      • getLastResult

        public static java.lang.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 java.lang.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()
      • setVariableManager

        public void setVariableManager​(VariableManager variableManager)
      • getVars

        public java.util.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()
      • 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​(java.util.ArrayList<java.lang.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​(java.util.ArrayList<java.lang.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 java.lang.String getValue​(java.lang.String name)
                                  throws java.lang.NullPointerException
        Parameters:
        name - The name of the variable or constant.
        Returns:
        the value of the named variable or constant
        Throws:
        java.lang.NullPointerException - if a Variable object that has that name id not found.
      • setValue

        public void setValue​(java.lang.String name,
                             java.lang.String value)
                      throws java.lang.NullPointerException,
                             java.lang.NumberFormatException
        Parameters:
        name - The name of the variable or constant.
        value - The value to set to the variable
        Throws:
        java.lang.NullPointerException - if a Variable object that has that name id not found.
        java.lang.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.
      • listToString

        protected java.lang.String listToString​(java.util.ArrayList<java.lang.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 java.lang.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
        Returns:
        the result of the evaluation
      • solve

        public java.util.List<java.lang.String> solve​(java.util.List<java.lang.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 java.util.List<java.lang.String> solveSubPortions​(java.util.List<java.lang.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​(java.lang.String[] args)