- All Superinterfaces:
NumericExpressionFactory
An IdealFactory provides a few services beyond those guaranteed by an
arbitrary NumericExpressionFactory.
The ideal factory produces and manipulates the following kinds of numeric expressions:
A Constant represents a concrete value. Each constant has either
integer or real type.
A Primitive expression is one which is not concrete and which is to
be treated as an atomic expression, such as a variable, from the point of
view of ideal mathematical arithmetic. Examples: symbolic constants, array
read expressions of integer or real type, tuple read expressions of integer
or real types, and function applications for functions returning integer or
real are all primitive expressions. In addition, in this factory a
Polynomial is a Primitive so that it can be treated as a
"variable" in an expression.
Any value which is the result of raising a primitive expression to a concrete
positive integer power is an instance of PrimitivePower. Any
Primitive is also a PrimitivePower by taking the exponent to
be 1.
A Monic is the product of PrimitivePowers. Any
PrimitivePower is also a Monic: it is the product of a single
primitive-power. The Constant 1 (integer or real) is also a
Monic: it is the empty product. The integer and real 1s are the only
constants which are also Monics.
A Monomial is the product of a Constant and a Monic.
Any Constant is also a Monomial by taking 1 for the monic.
Any Monic is also a Monomial by taking 1 for the constant.
A term map is a map from Monic to Monomial with the
property that a monic m in the key set maps to a monomial of the form
c*m for some non-zero constant c. A term map is a
invalid reference
SymbolicMapSymbolicExpression.
A Polynomial is a sum of the Monomial values of a term map.
Polynomial is also a sub-type of Primitive, which is a
subtype of PrimitivePower, which is a sub-type of Monic,
which is a sub-type of Monomial. In this factory, each instance
p of Polynomial satisfies all of the following:
pis the sum of at least 2 non-zero monomials- no term of
pis aPolynomial - if
phas integer type, the GCD of its coefficients is 1 and the leading coefficient is positive - if
phas real type, the leading coefficient is 1
The sum of two term maps is defined by combining the two maps by combining
two terms with the same monic by adding the coefficients. The product of two
term maps is defined in the usual way: by multiplying each element in one
with each element in the other, then combining terms with the same monic by
adding coefficients. The product of a Constant and a term map is
defined by multiplying that constant by each Monomial value in the
term map. The n-th power of a term map is the term map obtained by
multiplying the term map with itself n times.
Given any Monomial m, the term map of m is
defined as follows:
- if m is a
Polynomial, the term map is the term map of that polynomial - if m is the product of a
Constantand aPolynomial, the term map is the product of the constant and the term map of the polynomial - otherwise, the term map is the map with one entry with value m.
The expansion of a Monomial m is the term map defined
recursively as follows:
- If m is a
Primitivewhich is not aPolynomial, the expansion of m is the singleton map with value m. - If m is a
Polynomial, the expansion of m is the sum of the expansions of the terms of m. - The expansion of the product of a
Constantand aMonicis the product of the constant with the expansion of theMonic. - The expansion of a
PrimitivePowerp n is the expansion of p raised to the nth power.
In the following examples, suppose X and Y are
Primitives which are not Polynomials.
- the expansion of the
MonomialX is {X } - the expansion of the
PolynomialX+Y is {X, Y} - the expansion of the
Monomial2*(X+Y) is {2* X, 2*Y} - the expansion of the
Polynomial2*(X+ Y) + X is {3*X, Y} - the expansion of the
MonomialX2 is {X 2} - the expansion of the
Monomial(X+Y) 2 is {X2, 2*XY, Y 2
The product of two Monomials is a Monomial and is defined in
the obvious way, by multiplying primitive powers.
Suppose m1 and m2 are two
Monomials with no primitive factor in common. The sum m
1 + m2 is defined as follows. First, there is
the option of using the ordinary term map or the expansion of the two
monomials. The choice of whether or not to expand can be made using some
heuristic. In any case, the two term maps are added. The resulting term map
is factored (if possible) to produce a Monomial; the result may be a
Polynomial.
The sum of two arbitrary Monomials m1 and m
2 is defined as follows. First, the greatest common factor is
factored out, so m1=d*rr, m
2=d*r2, where d is a Monomial and
r1 and r2 are Monomials which
have no primitive factor is common. Hence m1 + m
2 = d*(r1+r2), where
the product is again monomial product and the sum r1+
r2 is a Monomial computed as described above.
A RationalExpression is the quotient of two Monomials. Any
Monomial is also a RationalExpression by taking the
denominator to be the (monomial) 1. Any RationalExpression of integer
type is also a Monomial. (The result of integer division of two
integer polynomials may be a Primitive expression with operator
SymbolicExpression.SymbolicOperator.INT_DIVIDE.)
A relational numeric expression will always be in one of the following forms:
0<m0≤mm<0m≤00=p0≠p
m is a Monic and p is a
Primitive.
Reductions: 0<x2y iff 0<y. Hence we can
assume all powers are 1. Furthermore 0<xy iff ((0<x ∧
0<y) ∨ (0<−x ∧ 0<−y)). This
can be used to reduce everything to Primitives, but unfortunately the
size of the formula is exponential in the number of factors. A heuristic
could be used to determine whether to expand.
Equality and inequality reductions are easier because xy=0 iff ( x=0 ∨ y=0), which does not involve an expansion in formula size. Similarly, xy≠0 iff (x≠0 ∧ y≠0).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PrimitivePower[]An empty array (i.e., one of length 0) ofPrimitivePower.static final Monomial[]An empty array (i.e., one of length 0) ofMonomial. -
Method Summary
Modifier and TypeMethodDescriptionadd(NumericExpression arg0, NumericExpression arg1) Returns a symbolic expression which is the result of adding arg1 from arg0.addMonomials(Monomial[] monomials) Computes the sum of a non-empty set ofMonomials of the same type.addMonomials(Monomial m1, Monomial m2) Computes the sum of any twoMonomials of the same type.Monomial[]addTermMaps(Monomial[] map1, Monomial[] map2) Computes the sum of two term maps as a term map.divide(NumericExpression arg0, NumericExpression arg1) Returns a symbolic expression which is the result of dividing arg0 by arg1.divideIntegerMonomials(Monomial numerator, Monomial denominator) Divides twoMonomials of integer type.expression(SymbolicExpression.SymbolicOperator operator, SymbolicType numericType, SymbolicObject... arguments) Returns an expression, given the operator, type, and array of argumentsfactorTermMap(Monomial[] terms) Computes aMonomialwhich is equivalent to the sum of the terms in the given term map.getConcreteExponent(RationalExpression exponent) Given the exponent in a potential power expression, this method computes a concrete integer that can be factored out of that exponent so that the exponent is in canonical form.intConstant(int value) Returns an integerConstantwrapping a Javaintvalue.isNonnegative(RationalExpression rational) Given a rational expressionrationalreturns an expression equivalent to 0≤rational.Computes an expression equivalent to "monomial != 0".isPositive(RationalExpression rational) Given a rational expressionrationalreturns an expression equivalent to 0<rational.Computes boolean expression equivalent to m=0.modulo(NumericExpression arg0, NumericExpression arg1) Returns a symbolic expression which represents arg0 modulo arg1.monic(SymbolicType type, PrimitivePower[] factorSet) Returns a (possibly trivial) monic as specified.TheComparatoronMonics.dev.civl.sarl.util.KeySetFactory<Primitive, PrimitivePower> Given aMonicreturns theMonicobtained by removing some of thePrimitivePowerfactors according to the givenmask.multiply(NumericExpression arg0, NumericExpression arg1) Returns a symbolic expression which is the result of multiplying the two given symbolic exprssions.multiplyConstantMonomial(Constant constant, Monomial monomial) Monomial[]multiplyConstantTermMap(Constant constant, Monomial[] map) Computes the term map obtained by multiplying the givenConstantwith every term in a given term map.multiplyMonomials(Monomial m1, Monomial m2) Computes the product of any twoMonomials of the same type.Monomial[]multiplyTermMaps(Monomial[] map1, Monomial[] map2) Returns the products of the two term maps as a term map.ntRationalExpression(Monomial numerator, Monomial denominator) Constructs new instance ofNTRationalExpression.one(SymbolicType type) Returns either the integer number one (1) or the real number 1 (1.0).Returns anIntObjectwrapping the int 1.Monomial[]oneTermMap(SymbolicType type) Returns awith a single entry mapping the monicinvalid reference
SymbolicMapOneto itself.polynomial(SymbolicType type, Monomial[] terms) Produces the result of summing theMonomials of a term map as aPolynomial.What is the purpose of the polynomial factory.power(NumericExpression arg0, NumericExpression arg1) General power operator: e^b.Monomial[]powerTermMap(SymbolicType type, Monomial[] map, NumberObject exponent) Raises a term map to the given power, returning the result as a term map.primitivePower(Primitive primitive, NumberObject exponent) subtract(NumericExpression arg0, NumericExpression arg1) Returns a symbolic expression which is the result of subtracting arg1 and arg0.zero(SymbolicType type) Returns either the integer constant 0 or the real constant 0, according to the giventype.zeroInt()Returns a symbolic expression of integer type with the value of 0zeroReal()Returns a symbolic expression of real type with the value of 0Methods inherited from interface dev.civl.sarl.expr.IF.NumericExpressionFactory
booleanFactory, cast, ceil, comparator, equals, expand, extractNumber, floor, init, lessThan, lessThanEquals, max, min, minus, neq, notLessThan, notLessThanEquals, number, number, numberFactory, objectFactory, oneInt, oneReal, power, roundToZero, symbolicConstant, typeFactory
-
Field Details
-
emptyTermList
An empty array (i.e., one of length 0) ofMonomial. A useful constant. -
emptyPPList
An empty array (i.e., one of length 0) ofPrimitivePower. A useful constant.
-
-
Method Details
-
polynomialFactory
What is the purpose of the polynomial factory. Is it to create only non-trivial polynomials: expressions where operator is + and all arguments are instances of Monomial? Or is it to create all instances of Polynomial???- Returns:
-
monicFactory
dev.civl.sarl.util.KeySetFactory<Primitive,PrimitivePower> monicFactory() -
monicComparator
Comparator<Monic> monicComparator()TheComparatoronMonics. This places some well-defined total order on the set of all instances ofMonic.- Returns:
- the comparator on
Monics
-
oneIntObject
IntObject oneIntObject()Returns anIntObjectwrapping the int 1.- Returns:
- the integer 1 as an
IntObject
-
intConstant
Returns an integerConstantwrapping a Javaintvalue.- Parameters:
value- any Javaint- Returns:
- the integer
Constantwrapping the givenvalue
-
zeroInt
Constant zeroInt()Description copied from interface:NumericExpressionFactoryReturns a symbolic expression of integer type with the value of 0- Specified by:
zeroIntin interfaceNumericExpressionFactory- Returns:
-
zeroReal
Constant zeroReal()Description copied from interface:NumericExpressionFactoryReturns a symbolic expression of real type with the value of 0- Specified by:
zeroRealin interfaceNumericExpressionFactory- Returns:
-
zero
Returns either the integer constant 0 or the real constant 0, according to the giventype.- Parameters:
type- either aSymbolicIntegerTypeor aSymbolicRealType- Returns:
- a value zero of the specified type
- See Also:
-
constant
-
one
Returns either the integer number one (1) or the real number 1 (1.0). The choice is made according to the giventype. In either case, the object returned is an instance of bothConstantandMonic, because one is the emptyMonic.- Parameters:
type- either aSymbolicIntegerTypeor aSymbolicRealType- Returns:
- the number 1 as a symbolic expression
-
primitivePower
- Parameters:
primitive- the base, a non-nullnumeric primitive of integer or real typeexponent- the exponent, is aNumberObjectwhich must represent a non-negative integer- Returns:
- a
PrimitivePowerexpression representing raisingprimitiveto the powerexponent
-
getConcreteExponent
Given the exponent in a potential power expression, this method computes a concrete integer that can be factored out of that exponent so that the exponent is in canonical form. Specifically, if the exponent has form p/q, and p=c*m, where p and q are
Monomials and c is aConstant, and c=n/d, where n and d areIntegerNumbers, this method returns n. Note that n may be positive or negative. It will only be 0 ifexponentis 1.The
exponentcan be safely divided by the integer returned by this method. If this method is given e and returns n, then the power expression can be rewritten as aPrimitivePowerwith primitive POWER(x,e/n) and exponent n, if n is positive, or as the rational expression 1/PrimitivePower[POWER(x,e/n), -n], if n is negative.- Parameters:
exponent- a non-nullrational expression of integer or real type- Returns:
- an concrete positive integer
nwhich can be factored out fromexponent
-
monic
Returns a (possibly trivial) monic as specified. If the given monic map is empty, this returns 1 (an instance ofOneof the appropriate type). If the monic map has a single entry, this returns the value for that entry, which is aPrimitivePower. Otherwise, returns a non-trivial monic (instance ofNTMonic).- Parameters:
type- either integer or real typefactorSet- a monic map with any number of entries; this maps a primitive to a power of that primitive; all keys and values must have type consistent withtype- Returns:
- instance of
Moniccorresponding to arguments as described above - See Also:
-
monicMask
Given aMonicreturns theMonicobtained by removing some of thePrimitivePowerfactors according to the givenmask. Themaskis an array whose length is the number ofPrimitivePowerfactors inmonic. Atruemask entry indicates the corresponding factor should be kept; afalseentry indicates the corresponding factor should be removed. -
monomial
Returns aMonomialwhich is the product of the givenconstantand the givenmonic. The two arguments must have the same type. Ifconstantis 1, theMonomialreturned may be an instance ofMonic; ifconstantis 0, theMonomialreturned may be an instance ofConstant(representing 0). This method relieves the use of having to figure out exactly which kind of object to create to represent the product of aConstantand aMonic. -
multiplyMonomials
Computes the product of any twoMonomials of the same type. -
addMonomials
Computes the sum of any twoMonomials of the same type. -
addMonomials
Computes the sum of a non-empty set ofMonomials of the same type. The result produced by this method may differ from that produced by repeated applications of the binary method#addMonomials(Monomial, Monomial)}. Example:invalid @link
{@link {@link(xy + x) + z = x(y+1) + z // two binary additions xy + x + z = xy + x + z // one invocation of this method
- Parameters:
monomials- an array of positive length consisting ofMonomials which all have the same type- Returns:
- an expression representing the sum of the monomials
-
multiplyConstantMonomial
-
isZero
Computes boolean expression equivalent to m=0.- Parameters:
monomial- a non-nullMonomial- Returns:
- a boolean expression equivalent to
monomial=0.
-
isNonZero
Computes an expression equivalent to "monomial != 0".- Parameters:
monomial- any non-nullMonomial- Returns:
- an expression equivalent to
monomial!= 0
-
divideIntegerMonomials
Divides two
Monomials of integer type. This will always return aMonomial, never a non-monomialRationalExpression. In the worst case (if the denominator does not evenly divide the numerator), the result will be a primitive expression (NumericPrimitive) in which the operator isSymbolicExpression.SymbolicOperator.INT_DIVIDE.Note on integer division: assume all terms positive. (ad)/(bd) = a/b
- Parameters:
numerator- polynomial of integer typedenominator- polynomial of integer type- Returns:
- result of division as
Monomial, which might be a new primitive expression
-
modulo
Returns a symbolic expression which represents arg0 modulo arg1. The two given expressions must have the integer type. What happens for negative integers is unspecified. Since expressions of integer type are always instances ofMonomial, this method signature is refined to returnMonomial.- Specified by:
moduloin interfaceNumericExpressionFactory- Parameters:
arg0- a symbolic expression of integer typearg1- a symbolic expression of integer type- Returns:
- arg0 % arg1
-
oneTermMap
Returns a
with a single entry mapping the monicinvalid reference
SymbolicMapOneto itself. The type ofOnewill be the giventype.A term map represents a set of
Monomials, which are considered to be the terms in a sum. TheMonomials are indexed by their correspondingMonics for efficient look-up. An entry in a term map is an ordered pair of the form (m,c*m), where m is aMonicand c is a non-0Constant.- Parameters:
type- either aSymbolicIntegerTypeor aSymbolicRealType- Returns:
- the term map consisting of a single term, one
-
addTermMaps
Computes the sum of two term maps as a term map. The sum is defined in the obvious way: the coefficient associated to a monic is the sum of the coefficients associated to that monic in the given maps, where the absence of a monic in a map is understood to be 0 (sparse representation). If the sum is 0, the entry is removed from the result, to maintain the sparse representation.- Parameters:
map1- a non-nullterm mapmap2- a non-nullinvalid input: '<'/code term map of the same type asmap1- Returns:
- a term map which represents the sum of the two given maps
- See Also:
-
multiplyTermMaps
Returns the products of the two term maps as a term map. The product is roughly an O(n^2) operation, where n is the length of each term map. It is defined in the usual way: each term in the first map is multiplied with every term in the second map, and the results are summed.- Parameters:
map1- a non-nullterm mapmap2- a non-nullinvalid input: '<'/code term map of the same type asmap1- Returns:
- a term map which represents the sum of the two given maps
- See Also:
-
multiplyConstantTermMap
Computes the term map obtained by multiplying the givenConstantwith every term in a given term map.- Parameters:
constant- a non-nullConstantmap- a term map of the same type asconstant- Returns:
- the term map obtained by multiplying
constantwith every term inmap
-
powerTermMap
Raises a term map to the given power, returning the result as a term map. This is the same as multiplying the term map with itselfexponenttimes. Theexponentis aNumberObjectrepresenting a non-negative integer. The type must be provided in casemapis empty. Otherwise, themapmust have the typetype.- Parameters:
type- the type of the givenmapand resultmap- a non-nullterm mapexponent- aNumberObjectrepresenting a non-negative integer- Returns:
- the result of multiplying
mapwith itselfexponenttimes
-
factorTermMap
Computes a
Monomialwhich is equivalent to the sum of the terms in the given term map. The goal is to attempt to factor thePolynomialas much as practical.Pre-condition:
mapsis non-empty. -
polynomial
Produces the result of summing theMonomials of a term map as aPolynomial.- Parameters:
type- the type ofterms(needed in casetermsis empty)terms- a non-nullterm map- Returns:
- the result of summing the terms in the term map
-
ntRationalExpression
Constructs new instance ofNTRationalExpression. Nothing is checked.Preconditions: numerator is not 0. If real type, denominator has degree at least 1 and leading coefficient 1. The numerator and denominator have no common factors in their factorizations.
- Parameters:
numerator- the polynomial to use as numeratordenominator- the polynomial to use as denominator- Returns:
- rational expression p/q
-
isPositive
Given a rational expressionrationalreturns an expression equivalent to 0<rational. This method will perform basic simplifications; for example, ifrationalis concrete, this method will return a concrete boolean expression (either "true" or "false").- Parameters:
rational- a non-nullinstance ofRationalExpression- Returns:
- an expression equivalent to 0<
rational
-
isNonnegative
Given a rational expressionrationalreturns an expression equivalent to 0≤rational. This method will perform basic simplifications; for example, ifrationalis concrete, this method will return a concrete boolean expression (either "true" or "false").- Parameters:
rational- a non-nullinstance ofRationalExpression- Returns:
- an expression equivalent to 0≤
rational
-
power
Description copied from interface:NumericExpressionFactoryGeneral power operator: e^b. Both e and b are numeric expressions.- Specified by:
powerin interfaceNumericExpressionFactory- Parameters:
arg0- the base expression in the power expressionarg1- the exponent in the power expression
-
expression
RationalExpression expression(SymbolicExpression.SymbolicOperator operator, SymbolicType numericType, SymbolicObject... arguments) Description copied from interface:NumericExpressionFactoryReturns an expression, given the operator, type, and array of arguments- Specified by:
expressionin interfaceNumericExpressionFactory- Parameters:
operator- A SymbolicOperatornumericType- A SymbolicTypearguments- array of arguments- Returns:
- Returns a NumericExpression
-
add
Description copied from interface:NumericExpressionFactoryReturns a symbolic expression which is the result of adding arg1 from arg0. The two given expressions must have the same (numeric) type: either both integers, or both real.- Specified by:
addin interfaceNumericExpressionFactory- Parameters:
arg0- a symbolic expression of a numeric typearg1- a symbolic expression of the same numeric type- Returns:
- arg0+arg1
-
subtract
Description copied from interface:NumericExpressionFactoryReturns a symbolic expression which is the result of subtracting arg1 and arg0. The two given expressions must have the same (numeric) type: either both integers, or both real.- Specified by:
subtractin interfaceNumericExpressionFactory- Parameters:
arg0- a symbolic expression of a numeric typearg1- a symbolic expression of the same numeric type- Returns:
- arg0-arg1
-
multiply
Description copied from interface:NumericExpressionFactoryReturns a symbolic expression which is the result of multiplying the two given symbolic exprssions. The two given expressions must have the same (numeric) type: either both integers, or both real.- Specified by:
multiplyin interfaceNumericExpressionFactory- Parameters:
arg0- a symbolic expression of a numeric typearg1- a symbolic expression of the same numeric type- Returns:
- arg0 * arg1, the product of arg0 and arg1.
-
divide
Description copied from interface:NumericExpressionFactoryReturns a symbolic expression which is the result of dividing arg0 by arg1. The two given expressions must have the same (numeric) type: either both integers, or both real. In the integer case, division is interpreted as "integer division", which rounds towards 0.- Specified by:
dividein interfaceNumericExpressionFactory- Parameters:
arg0- a symbolic expression of a numeric typearg1- a symbolic expression of the same numeric type- Returns:
- arg0 / arg1
-