Class MathExpression
- All Implemented Interfaces:
Savable,Solvable,Serializable
- Direct Known Subclasses:
BaseNFunction,MatrixExpression,MatrixFunction,PolynomialExpression
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final classstatic final classstatic final classstatic final classManages the mapping of variable names to frame slots. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanprotected intstatic final String -
Constructor Summary
ConstructorsConstructorDescriptiondefault no argument constructor for class MathExpression.MathExpression(String input) MathExpression(String input, boolean foldConstants) MathExpression(String input, VariableManager variableManager, boolean foldConstants) -
Method Summary
Modifier and TypeMethodDescriptionclone()Compile to Turbo mode using adaptive compiler selection.copy()Manual deep copy method that will most certainly outperform the clone method.voidcopyErrorLogTo(ErrorLog log) static final MathExpression.VariableRegistrygetDRG()double[]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.int[]getSlots()doublegetVariable(String name) Retrieves a Variable handle from the expression's registry.MathExpression.Pair<String[], Integer[]> String[]booleanhasVariable(String var) static booleanbooleanbooleanbooleanbooleanbooleanCheck if turbo mode is available.booleanbooleanstatic voidstatic voidvoidstatic voidsetAutoInitOn(boolean autoInitOn) voidsetCorrectFunction(boolean correctFunction) voidsetDRG(int mode) voidfinal voidsetExpression(String expression) voidsetHasListReturningOperators(boolean hasListReturningOperators) voidsetNoOfListReturningOperators(int noOfListReturningOperators) voidsetOptimizable(boolean optimizable) voidsetReturnType(TYPE returnType) voidvoidsetVariableHandlerOnly(boolean variableHandlerOnly) voidsetVariableManager(VariableManager variableManager) voidAn important process that must occur before the function is solved.voidsetWillFoldConstants(boolean willFoldConstants) solve()solve(double... args) solveGeneric(double... args) voidupdateArgs(double... values) Updates specific slots with specific values.voidupdateSlot(int slot, double value)
-
Field Details
-
parser_Result
-
correctFunction
protected boolean correctFunction -
noOfListReturningOperators
protected int noOfListReturningOperators -
scanner
-
hasListReturningOperators
protected boolean hasListReturningOperators -
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
- 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
-
MathExpression
-
-
Method Details
-
getExecutionFrame
public double[] getExecutionFrame() -
createNewVariableRegistry
-
getExpression
-
setExpression
- 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
-
copyErrorLogTo
-
setWillFoldConstants
public void setWillFoldConstants(boolean willFoldConstants) -
isWillFoldConstants
public boolean isWillFoldConstants() -
isTurboCompiled
public boolean isTurboCompiled()Check if turbo mode is available. -
compileTurbo
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 compiledThrowable- if turbo compilation fails
-
getDRG
- Returns:
- the DRG value:0 for degrees, 1 for rads, 2 for grads
-
setDRG
-
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
-
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
-
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 aMatrixfunction 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
-
setVariableManager
-
getRegistry
-
hasVariable
-
getVariablesNames
-
getSlots
public int[] getSlots() -
getSlotItems
- Returns:
- an array of
MathExpression.Slotobjects(name of variable and frame index of variables in the expression)
-
getVariables
-
getVariable
-
checkErrorLogs
-
getCachedPostfix
-
setVariableValuesInFunction
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
- 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
- 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
-
getReturnType
-
solveGeneric
-
solveGeneric
-
solve
-
solve
-
solve
-
readErrorLog
public void readErrorLog() -
getNextResult
-
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 theMathExpressionto beSavable- Returns:
-
clone
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
main1
-
main
-
serialize
-
parse
-