AUTHORS:
- David Harvey (2006-09-20): changed FormalSum not to derive from “list” anymore, because that breaks new Element interface
- Nick Alexander (2006-12-06): added test cases.
- William Stein (2006, 2009): wrote the first version in 2006, documented it in 2009.
coefficients in the given ring.
FormalSum(list of pairs (coeff, number)) – create a formal sum
EXAMPLES:
sage: A = FormalSum([(1, 2/3)]); A
2/3
sage: B = FormalSum([(3, 1/5)]); B
3*1/5
sage: -B
-3*1/5
sage: A + B
3*1/5 + 2/3
sage: A - B
-3*1/5 + 2/3
sage: B*3
9*1/5
sage: 2*A
2*2/3
sage: list(2*A + A)
[(3, 2/3)]
TESTS:
sage: R = FormalSums(QQ)
sage: loads(dumps(R)) == R
True
sage: a = R(2/3) + R(-5/7); a
-5/7 + 2/3
sage: loads(dumps(a)) == a
True
A formal sum over a ring.
EXAMPLES:
sage: a = FormalSum([(1,3),(2,5)]); a
3 + 2*5
sage: b = FormalSum([(1,3),(2,7)]); b
3 + 2*7
sage: a < b
True
sage: b < a
False
sage: a == 1
False
sage: b == 0
False
EXAMPLES:
sage: v = FormalSum([(1,2), (5, 1000), (-3, 7)]); v
2 - 3*7 + 5*1000
sage: v[0]
(1, 2)
sage: v[1]
(-3, 7)
sage: v[2]
(5, 1000)
sage: list(v)
[(1, 2), (-3, 7), (5, 1000)]
x – object
parent – FormalSums(R) module (default: FormalSums(ZZ))
coefficients into base ring, which can speed up constructing a formal sum.
combine common terms
EXAMPLES:
sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)])
4*2/3 - 5*7
sage: a = FormalSum([(1,2/3), (3,2/3), (-5, 7)], reduce=False); a
2/3 + 3*2/3 - 5*7
sage: a.reduce()
sage: a
4*2/3 - 5*7
sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)], parent=FormalSums(GF(5)))
4*2/3
Notice below that the coefficient 5 doesn’t get reduced modulo 5:
sage: FormalSum([(1,2/3), (3,2/3), (-5, 7)], parent=FormalSums(GF(5)), check=False)
4*2/3 - 5*7
EXAMPLES:
sage: for z in FormalSum([(1,2), (5, 1000), (-3, 7)]): print z
(1, 2)
(-3, 7)
(5, 1000)
EXAMPLES:
sage: v = FormalSum([(1,2), (5, 1000), (-3, 7)]); v
2 - 3*7 + 5*1000
sage: len(v)
3
EXAMPLES:
sage: bool(FormalSum([(1,3/7),(-2,5)]))
True
sage: bool(FormalSums(QQ)(0))
False
sage: bool(FormalSums(QQ)(1))
True
EXAMPLES:
sage: FormalSum([(1,3/7),(2,5/8)]) + FormalSum([(1,3/7),(-2,5)]) # indirect doctest
2*3/7 + 2*5/8 - 2*5
EXAMPLES:
sage: latex(FormalSum([(1,2), (5, 8/9), (-3, 7)]))
5\cdot rac{8}{9} + 2 - 3\cdot 7
EXAMPLES:
sage: -3*FormalSum([(1,3/7),(-2,5)])
-3*3/7 + 6*5
EXAMPLES:
sage: -FormalSum([(1,3),(2,5)])
-3 - 2*5
EXAMPLES:
sage: a = FormalSum([(1,2/3), (-3,4/5), (7,Mod(2,3))])
sage: a # random, since comparing Mod(2,3) and rationals ill-defined
sage: a._repr_() # random
'2/3 - 3*4/5 + 7*2'
EXAMPLES:
sage: FormalSum([(1,3/7),(-2,5)])*(-3)
-3*3/7 + 6*5
EXAMPLES:
sage: a = FormalSum([(-2,3), (2,3)], reduce=False); a
-2*3 + 2*3
sage: a.reduce()
sage: a
0
Return the R-module of finite formal sums with coefficients in R.
EXAMPLES:
sage: FormalSums()
Abelian Group of all Formal Finite Sums over Integer Ring
sage: FormalSums(ZZ[sqrt(2)])
Abelian Group of all Formal Finite Sums over Order in Number Field in sqrt2 with defining polynomial x^2 - 2
sage: FormalSums(GF(9,'a'))
Abelian Group of all Formal Finite Sums over Finite Field in a of size 3^2
Make a formal sum in self from the element x.
EXAMPLES:
sage: FormalSums(ZZ) < FormalSums(QQ)
True
sage: FormalSums(QQ) == FormalSums(QQ)
True
sage: FormalSums(QQ) > FormalSums(ZZ)
True
EXAMPLES:
sage: FormalSums(ZZ)
Abelian Group of all Formal Finite Sums over Integer Ring
sage: FormalSums(GF(7))
Abelian Group of all Formal Finite Sums over Finite Field of size 7
EXAMPLES:
sage: FormalSums(ZZ)._an_element_impl()
1
sage: FormalSums(QQ)._an_element_impl()
1/2*1
sage: QQ.an_element()
1/2
EXAMPLES:
sage: P = FormalSum([(1,2/3)]).parent() sage: P._coerce_impl(2) 2
EXAMPLES:
sage: FormalSums(ZZ).base_extend(GF(7))
Abelian Group of all Formal Finite Sums over Finite Field of size 7
EXAMPLES:
sage: A = FormalSums(RR).get_action(RR); A # indirect doctest
Right scalar multiplication by Real Field with 53 bits of precision on Abelian Group of all Formal Finite Sums over Real Field with 53 bits of precision
sage: A = FormalSums(ZZ).get_action(QQ); A
Right scalar multiplication by Rational Field on Abelian Group of all Formal Finite Sums over Rational Field
with precomposition on left by Call morphism:
From: Abelian Group of all Formal Finite Sums over Integer Ring
To: Abelian Group of all Formal Finite Sums over Rational Field
sage: A = FormalSums(QQ).get_action(ZZ); A
Right scalar multiplication by Integer Ring on Abelian Group of all Formal Finite Sums over Rational Field