Class MathExpression

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

public class MathExpression extends Object implements Savable, Solvable

This class is NOT thread-safe. If multiple threads access a MathExpression concurrently, they must synchronize externally or use separate instances.

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 containsAlgebraicFunction 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
See Also:
  • Field Details

    • parser_Result

      public ParserResult parser_Result
    • correctFunction

      protected boolean correctFunction
    • noOfListReturningOperators

      protected int noOfListReturningOperators
    • scanner

      protected List<String> scanner
    • hasListReturningOperators

      protected boolean hasListReturningOperators
    • SYNTAX_ERROR

      public static final String SYNTAX_ERROR
      See Also:
  • 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 containsAlgebraicFunction 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.
    • MathExpression

      public MathExpression(String input, boolean foldConstants)
    • MathExpression

      public MathExpression(String input, VariableManager variableManager, boolean foldConstants)
  • Method Details

    • getExecutionFrame

      public double[] getExecutionFrame()
    • createNewVariableRegistry

      public static final MathExpression.VariableRegistry createNewVariableRegistry()
    • 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()
    • getCompiledTurbo

      public FastCompositeExpression getCompiledTurbo()
    • copyErrorLogTo

      public void copyErrorLogTo(ErrorLog log)
    • setWillFoldConstants

      public void setWillFoldConstants(boolean willFoldConstants)
    • isWillFoldConstants

      public boolean isWillFoldConstants()
    • isTurboCompiled

      public boolean isTurboCompiled()
      Check if turbo mode is available.
    • compileTurbo

      public FastCompositeExpression compileTurbo() throws Throwable
      Compile to Turbo mode using adaptive compiler selection. Auto-selects scalar or matrix compiler based on expression analysis. Performance: - Pure scalar: ~5-10 ns - Matrix ops: ~50-1000 ns (vs 5-100 μs interpreted) - First call: ~5-10 ms (compilation), cached thereafter
      Returns:
      FastCompositeExpression ready for high-speed evaluation
      Throws:
      IllegalStateException - if expression not properly compiled
      Throwable - if turbo compilation fails
    • 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)
    • setDRG

      public void setDRG(int mode)
    • 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.
    • setNoOfListReturningOperators

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

      public List<String> getScanner()
      Returns:
      the ArrayList object that the input is scanned into.
    • 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
    • getTreeStats

      public MathExpressionTreeDepth.Result getTreeStats()
    • 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 at times need that function name...So we cache this value here.
      Returns:
      the object returned via its string reference name
    • setVariableHandlerOnly

      public void setVariableHandlerOnly(boolean variableHandlerOnly)
    • isVariableHandlerOnly

      public boolean isVariableHandlerOnly()
    • getVariableManager

      public VariableManager getVariableManager()
    • setVariableManager

      public void setVariableManager(VariableManager variableManager)
    • getRegistry

      public MathExpression.VariableRegistry getRegistry()
    • hasVariable

      public boolean hasVariable(String var)
    • getVariablesNames

      public String[] getVariablesNames()
    • getSlots

      public int[] getSlots()
    • getSlotItems

      public MathExpression.Slot[] getSlotItems()
      Returns:
      an array of MathExpression.Slot objects(name of variable and frame index of variables in the expression)
    • getVariables

      public MathExpression.Pair<String[], Integer[]> getVariables()
    • getVariable

      public Variable getVariable(String name)
      Retrieves a Variable handle from the expression's registry. This handle is "Pre-Bound" to the correct slot in the execution frame.
      Parameters:
      name -
      Returns:
      Variable
    • checkErrorLogs

      public String checkErrorLogs()
    • getCachedPostfix

      public MathExpression.Token[] getCachedPostfix()
    • setVariableValuesInFunction

      public void setVariableValuesInFunction(List<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 double 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, double value)
      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. public void setValue(String name, double value) throws NullPointerException, NumberFormatException { Variable v = VariableManager.lookUp(name); if (v != null) { v.setValue(value); } }
    • updateArgs

      public void updateArgs(double... values)
      Updates specific slots with specific values. Usage: updateArgs(new int[]{0, 2}, new double[]{valX, valZ});
      Parameters:
      values -
    • updateSlot

      public void updateSlot(int slot, double value)
    • setReturnType

      public void setReturnType(TYPE returnType)
    • getReturnType

      public TYPE getReturnType()
    • solveGeneric

      public MathExpression.EvalResult solveGeneric()
    • solveGeneric

      public MathExpression.EvalResult solveGeneric(double... args)
    • solve

      public String solve()
      Specified by:
      solve in interface Solvable
    • solve

      public String solve(double... args)
    • solve

      protected List<String> solve(List<String> list)
    • readErrorLog

      public void readErrorLog()
    • getNextResult

      public MathExpression.EvalResult getNextResult()
    • copy

      public MathExpression copy()
      Manual deep copy method that will most certainly outperform the clone method. But needs to be updated whenever the MathExpression object's field structure changes. The clone method does not necessarily need this, except for its need for all fields of the MathExpression to be Savable
      Returns:
    • clone

      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • main1

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

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

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

      public MathExpression parse(String enc)