Package com.singularsys.jep
Class Operator
java.lang.Object
com.singularsys.jep.Operator
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
TernaryOperator
,TernaryOperator.RhsTernaryOperator
An Operator with additional information about its commutativity etc.
Operators have a number of properties:
- A symbol or name of the operator "+".
- The number of arguments NO_ARGS 0, UNARY 1 (e.g. UMINUS -x), BINARY 2 (e.g. x+y), and NARY either 3 ( a>b ? a : b) or unspecified like a list [x,y,z,w].
- The binding of the operator, LEFT 1+2+3 -> (1+2)+3 or RIGHT 1=2=3 -> 1=(2=3).
- Whether the operator is ASSOCIATIVE or COMMUTATIVE.
- The precedence of the operators + has a higher precedence than *.
- For unary operators they can either be PREFIX like -x or SUFFIX like x%.
- Comparative operators can be REFLEXIVE, SYMMETRIC, TRANSITIVE or EQUIVALENCE which has all three properties.
- A reference to a PostfixtMathCommandI object which is used to evaluate an equation containing the operator.
- Author:
- Rich Morris Created on 19-Oct-2003
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Associative operators x*(y*z) == (x*y)*z .static final int
Binary operators, such as x+y, x>ystatic final int
Commutative operators x*y = y*x.static final int
composite operators, like a-b which is a+(-b)static final int
Equivalence relations = reflexive, transitive and symmetric.protected int
Flags for type of operator Access level changed to protected in version 3.5protected int
Key used to identify the operator in the operator table Access level changed to protected in version 3.5static final int
Left binding like +: 1+2+3 -> (1+2)+3protected final String
A unique name defining the operator.static final int
Trinary ops such as ?: and or higher like [x,y,z,w]static final int
No arguments to operatorstatic final int
Non-standard operators, like array access.protected PostfixMathCommandI
The PostfixMathCommandI for evaluating Access level changed to protected in version 3.5protected int
Precedence of operator, 0 is most tightly bound, so prec("*") < prec("+").static final int
prefix operators -xprotected String
The symbol for the operator, used for printing.static final int
Reflective relations x=x for all x.static final int
Right binding like =: 1=2=3 -> 1=(2=3)static final int
self inverse operators like -(-x) !(!x)static final int
postfix operators x%protected String
The symbol for the operator, used for printing.static final int
Symmetric relation x=y implies y=x.static final int
three argument operators like java's cond?tval:fvalstatic final int
Transitive relations x=y and y=z implies x=zstatic final int
Unary operators, such as -x !x ~xstatic final int
For non-commutative operators printing can be determined by the left or right binding. -
Constructor Summary
ConstructorDescriptionOperator
(String name, PostfixMathCommandI pfmc, int flags) Construct a new operator with no precedence set.Operator
(String name, PostfixMathCommandI pfmc, int flags, int precedence) Constructs a new operator with a given precedence.Operator
(String name, String symbol, PostfixMathCommandI pfmc, int flags) Constructs a new operator, with a different name and symbolOperator
(String name, String symbol, PostfixMathCommandI pfmc, int flags, int precedence) Constructs a new operator, with a different name and symbol and allows a given precedence to be set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addAltSymbol
(String altSymbol) Adds an alternate symbol which can be used at parse time.Return a copy of the operator with all fields identical.List of alternative symbols which can represent the same operatorThe composite or binaryInverse operator (e.g.final int
Deprecated.final int
getFlags()
The flags for this operator.The unary inverse operator associated with a root or composite operator.int
getKey()
Deprecated.final String
getName()
returns a unique name defining this operator.final PostfixMathCommandI
getPFMC()
final int
Get the precedence of the operator.The symbol used for printing.The basic group operator associated with the inverse or composite operator.final String
returns the symbol used by this operator.final boolean
final boolean
isBinary()
final boolean
final boolean
boolean
final boolean
final boolean
final boolean
isNary()
final boolean
isPrefix()
final boolean
final boolean
final boolean
final boolean
isSuffix()
final boolean
final boolean
final boolean
final boolean
isUnary()
final boolean
final int
numArgs()
void
In a mathematical group with operator + and inverse uminus (-y) the binary inverse operator is the composition x + (-y) in other words subtraction -.final void
void
setFlag
(int flag, boolean val) Set the value of a specific flag.void
setInverseOp
(Operator inv) For composite operators like a-b which can be considered as a+(-b) there is a root operator (+) and an inverse operator (UMinus).void
setKey
(int key) Deprecated.final void
setPFMC
(PostfixMathCommandI pfmc) void
setPrecedence
(int i) Set the precedence of the operator.void
setPrintSymbol
(String printSymbol) The symbol used for printing.void
For composite operators like a-b which can be considered as a+(-b) there is a root operator (+) and an inverse operator (UMinus).final void
Sets the symbol used by the operator.returns a verbose representation of the operator and all its properties.toString()
final boolean
-
Field Details
-
NO_ARGS
public static final int NO_ARGSNo arguments to operator- See Also:
-
UNARY
public static final int UNARYUnary operators, such as -x !x ~x- See Also:
-
BINARY
public static final int BINARYBinary operators, such as x+y, x>y- See Also:
-
NARY
public static final int NARYTrinary ops such as ?: and or higher like [x,y,z,w]- See Also:
-
LEFT
public static final int LEFTLeft binding like +: 1+2+3 -> (1+2)+3- See Also:
-
RIGHT
public static final int RIGHTRight binding like =: 1=2=3 -> 1=(2=3)- See Also:
-
ASSOCIATIVE
public static final int ASSOCIATIVEAssociative operators x*(y*z) == (x*y)*z .- See Also:
-
COMMUTATIVE
public static final int COMMUTATIVECommutative operators x*y = y*x.- See Also:
-
REFLEXIVE
public static final int REFLEXIVEReflective relations x=x for all x.- See Also:
-
SYMMETRIC
public static final int SYMMETRICSymmetric relation x=y implies y=x.- See Also:
-
TRANSITIVE
public static final int TRANSITIVETransitive relations x=y and y=z implies x=z- See Also:
-
EQUIVALENCE
public static final int EQUIVALENCEEquivalence relations = reflexive, transitive and symmetric.- See Also:
-
PREFIX
public static final int PREFIXprefix operators -x- See Also:
-
SUFFIX
public static final int SUFFIXpostfix operators x%- See Also:
-
SELF_INVERSE
public static final int SELF_INVERSEself inverse operators like -(-x) !(!x)- See Also:
-
COMPOSITE
public static final int COMPOSITEcomposite operators, like a-b which is a+(-b)- See Also:
-
USE_BINDING_FOR_PRINT
public static final int USE_BINDING_FOR_PRINTFor non-commutative operators printing can be determined by the left or right binding. For example (a-b)-c is printed as a-b-c (flag set). But a/b/c could be ambiguous so (a/b)/c is printed with brackets (flag not set).- See Also:
-
NOT_IN_PARSER
public static final int NOT_IN_PARSERNon-standard operators, like array access. These will typically require the PrintRulesI interface to print and require special parsing options.- See Also:
-
TERNARY
public static final int TERNARYthree argument operators like java's cond?tval:fval- See Also:
-
name
A unique name defining the operator. Access level changed to protected in version 3.5 -
symbol
The symbol for the operator, used for printing. Access level changed to protected in version 3.5 -
printSymbol
The symbol for the operator, used for printing. Access level changed to protected in version 3.5 -
pfmc
The PostfixMathCommandI for evaluating Access level changed to protected in version 3.5 -
flags
protected int flagsFlags for type of operator Access level changed to protected in version 3.5 -
precedence
protected int precedencePrecedence of operator, 0 is most tightly bound, so prec("*") < prec("+"). Access level changed to protected in version 3.5 -
key
protected int keyKey used to identify the operator in the operator table Access level changed to protected in version 3.5
-
-
Constructor Details
-
Operator
Construct a new operator with no precedence set.- Parameters:
name
- printable name of the operatorpfmc
- postfix math command for the operatorflags
- set of flags defining the properties of the operator.
-
Operator
Constructs a new operator with a given precedence.- Parameters:
name
- printable name of the operatorpfmc
- postfix math command for the operatorflags
- set of flags defining the properties of the operatorprecedence
- operator precedence to be set
-
Operator
Constructs a new operator, with a different name and symbol- Parameters:
name
- name of operator, must be unique, used when describing operatorsymbol
- printable name of operator, used for printing equationspfmc
- postfix math command for operatorflags
- set of flags defining the properties of the operator
-
Operator
Constructs a new operator, with a different name and symbol and allows a given precedence to be set.- Parameters:
name
- name of operator, must be unique, used when describing operatorsymbol
- printable name of operator, used for printing equationspfmc
- postfix math command for operatorflags
- set of flags defining the properties of the operator.precedence
- operator precedence to be set
-
-
Method Details
-
getSymbol
returns the symbol used by this operator.- Returns:
- The symbol
-
setSymbol
Sets the symbol used by the operator. NoteJep.reinitializeComponents()
must be called after using this method.- Parameters:
sym
- the new symbol to use.
-
getName
returns a unique name defining this operator.- Returns:
- the name
-
getPrintSymbol
The symbol used for printing. If null the symbol is used instead.- Returns:
- the symbol used for printing or null if no special symbol is used
-
setPrintSymbol
The symbol used for printing. If null the symbol is used instead. -
getPFMC
-
setPFMC
-
getPrecedence
public final int getPrecedence()Get the precedence of the operator. Lower values correspond to tighter binding, that is * gives a lower value than + and 1+2*3 is 1+(2*3).- Returns:
- the precedence
-
setPrecedence
public void setPrecedence(int i) Set the precedence of the operator. Lower values correspond to tighter binding, that is * gives a lower value than + and 1+2*3 is 1+(2*3).- Parameters:
i
- the precedence
-
setDistributiveOver
-
isDistributiveOver
-
setRootOp
For composite operators like a-b which can be considered as a+(-b) there is a root operator (+) and an inverse operator (UMinus). Typically, this is set for both the inverse and the composite- Parameters:
root
- the root operator
-
setInverseOp
For composite operators like a-b which can be considered as a+(-b) there is a root operator (+) and an inverse operator (UMinus). Typically, set for the root and the composite operators.- Parameters:
inv
- the unary inverse operator
-
setBinaryInverseOp
In a mathematical group with operator + and inverse uminus (-y) the binary inverse operator is the composition x + (-y) in other words subtraction -. Typically, set for the root and the inverse.- Parameters:
inv
-
-
getRootOp
The basic group operator associated with the inverse or composite operator.- Returns:
- the root operator or null if this is the root operator or if none defined.
-
getInverseOp
The unary inverse operator associated with a root or composite operator.- Returns:
- the inverse operator or null if this is the inverse operator or if none defined.
-
getBinaryInverseOp
The composite or binaryInverse operator (e.g. subtract) associated with a root or unary inverse operator- Returns:
- the inverse operator or null if this is the inverse operator or if none defined.
-
getFlags
public final int getFlags()The flags for this operator.- Returns:
- the flags as a set of bits
-
getBinding
Deprecated.When parsing how is x+y+z interpreted. Can beOperator.LEFT
:x+y+z -> (x+y)+z
orOperator.RIGHT
:x=y=z -> x=(y=z)
. This is now deprecated- Returns:
Operator.LEFT
orOperator.RIGHT
- See Also:
-
isLeftBinding
public final boolean isLeftBinding() -
isRightBinding
public final boolean isRightBinding() -
isAssociative
public final boolean isAssociative() -
isCommutative
public final boolean isCommutative() -
isBinary
public final boolean isBinary() -
isUnary
public final boolean isUnary() -
isNary
public final boolean isNary() -
numArgs
public final int numArgs() -
isTransitive
public final boolean isTransitive() -
isSymmetric
public final boolean isSymmetric() -
isReflexive
public final boolean isReflexive() -
isEquivalence
public final boolean isEquivalence() -
isPrefix
public final boolean isPrefix() -
isSuffix
public final boolean isSuffix() -
isTernary
public final boolean isTernary() -
isComposite
public final boolean isComposite() -
isSelfInverse
public final boolean isSelfInverse() -
useBindingForPrint
public final boolean useBindingForPrint() -
notInParser
public final boolean notInParser() -
setFlag
public void setFlag(int flag, boolean val) Set the value of a specific flag. Use with care- Parameters:
flag
- flag to setval
- value for that flag
-
toFullString
returns a verbose representation of the operator and all its properties. Symbols used to represent operator are enclosed in quotes.- Returns:
- verbose string representation
-
toString
-
getKey
Deprecated.Get the key code used to identify the operator in the oldOperatorTable
.- Returns:
- the key
-
setKey
Deprecated.Key code used to identify the operator in the oldOperatorTable
.- Parameters:
key
- the key
-
getAltSymbols
List of alternative symbols which can represent the same operator- Returns:
- either a list of strings or null if there are no such symbols
-
addAltSymbol
Adds an alternate symbol which can be used at parse time. The alternate symbol added will be recognised at parse time by configurable parser the normal symbol will be used in output.
For example unicode U+002D the hyphen minus (-), U+2212 the unicode minus symbol (−), U+2013 then en-dash (–) may appear in some input to represent minus. Other possible alternative characters include U+2264 less-than or equals (≤), U+2264 greater-than or equals (≥), U+00D7 multiplication sign (×), U+00F7 division sign (÷), U+2044 fraction slash (⁄) and U+2215 division slash.
- Parameters:
altSymbol
- the symbol to add- Returns:
- true if successfully added
-
duplicate
Return a copy of the operator with all fields identical.- Returns:
- a copy of the operator
- Since:
- 4.0
-