Package parser
Class MathExpression
java.lang.Object
parser.MathExpression
- All Implemented Interfaces:
Savable
,Solvable
,Serializable
- Direct Known Subclasses:
BaseNFunction
,MatrixFunction
,PolynomialExpression
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static boolean
If set to true, MathExpression objects will automatically initialize undeclared variables to zero and store them.private Bracket[]
protected boolean
protected int
private String
The expression to evaluate.private boolean
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.protected boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
private boolean
static String
protected int
private boolean
private String
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.(package private) TYPE
The type of output returned by the parser.(package private) StringBuilder
Utility attribute used throughout the class for string appending operations.private VariableManager
The VariableManager object that allows an object of this class to remember its variables. -
Constructor Summary
ConstructorsConstructorDescriptiondefault no argument constructor for class MathExpression.MathExpression
(String input) -
Method Summary
Modifier and TypeMethodDescriptionvoid
The method establishes meaning to some shorthand techniques in math that the average mathematician might expect to see in a math device.protected Bracket[]
void
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.private void
Display the indices of all brackets in the function,bracket pair by bracket pairprivate void
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....)void
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 onBracket[]
int
getDRG()
static String
int
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.getVars()
private void
initializing
(String expression) static boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
protected String
listToString
(ArrayList<String> scan) static void
void
Method mapBrackets goes over an input equation and maps all positions that have corresponding bracketsstatic Bracket[]
mapBrackets
(ArrayList<String> scanner) 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.private void
static void
setAutoInitOn
(boolean autoInitOn) void
setBracket
(Bracket[] bracket) void
setCorrectFunction
(boolean correctFunction) void
setDRG
(int DRG) sets the DRG propertyfinal void
setExpression
(String expression) void
setHasListReturningOperators
(boolean hasListReturningOperators) void
setHasLogicOperators
(boolean hasLogicOperators) void
setHasMulOrDivOperators
(boolean hasMulOrDivOperators) void
setHasNumberReturningStatsOperators
(boolean hasNumberReturningStatsOperators) void
setHasPermOrCombOperators
(boolean hasPermOrCombOperators) void
setHasPlusOrMinusOperators
(boolean hasPlusOrMinusOperators) void
setHasPostNumberOperators
(boolean hasPostNumberOperators) void
setHasPowerOperators
(boolean hasPowerOperators) void
setHasPreNumberOperators
(boolean hasPreNumberOperators) void
setHasRemainderOperators
(boolean hasRemainderOperators) static void
setLastResult
(String lastResult) void
setNoOfListReturningOperators
(int noOfListReturningOperators) void
setOptimizable
(boolean optimizable) void
setReturnType
(TYPE returnType) void
setScanner
(ArrayList<String> scanner) void
void
setVariableHandlerOnly
(boolean variableHandlerOnly) void
setVariableManager
(VariableManager variableManager) void
An important process that must occur before the function is solved.void
setWhitespaceremover
(ArrayList<String> whitespaceremover) solve()
Method solve is the main parser used to evaluate the input multi-bracket pair (MBP) expressions used to initialize the constructor of class MathExpressionused by the main parser solve to figure out SBP portions of a multi-bracketed expression (MBP)solveSubPortions
(List<String> scanner) void
void
Removes encapsulating brackets from data set returning statistical operators.
-
Field Details
-
parser_Result
-
DRG
protected int DRG -
lastResult
-
whitespaceremover
-
expression
The expression to evaluate. -
correctFunction
protected boolean correctFunction -
noOfListReturningOperators
protected int noOfListReturningOperators -
scanner
-
optimizable
private boolean optimizable -
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 autoInitOnIf set to true, MathExpression objects will automatically initialize undeclared variables to zero and store them. -
hasFunctionOrVariableInitStatement
private boolean hasFunctionOrVariableInitStatementSet 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
The VariableManager object that allows an object of this class to remember its variables. -
utility
StringBuilder utilityUtility attribute used throughout the class for string appending operations. -
returnType
TYPE returnTypeThe type of output returned by the parser. -
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 aMatrix
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
- 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
-
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() -
initializing
-
removeCommas
private void removeCommas() -
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
- Returns:
- the Brackets ArrayList containing all Bracket objects found in the input.
-
setBracket
- 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
- Returns:
- the ArrayList object that the input is scanned into.
-
setScanner
- Parameters:
scanner
- sets the ArrayList object that the input is scanned into.
-
getWhitespaceremover
- Returns:
- the white space remover ArrayList object.
-
setWhitespaceremover
- 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
- Parameters:
lastResult
- sets the last answer gotten by this parser
-
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
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 aMatrix
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
-
setVariableManager
-
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
- 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
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.NumberFormatException
-
modifyBracketIndices
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 modifystartPosition
- the index in the ArrayList where the modification is to startincrement
- the amount by which each bracket index is to be decreasedrun
- 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
- Parameters:
scan
- The ArrayList object.- Returns:
- the string version of the ArrayList and removes the braces i.e. []
-
copyArrayToArray
- Returns:
- an Array object containing duplicate contents of the List object alone
-
setReturnType
-
getReturnType
-
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 -
solve
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
- 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
-
serialize
-
parse
-