Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Change sparsity function for dense polys + add doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrenet committed Jul 4, 2014
1 parent 7fec476 commit e89350e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
42 changes: 33 additions & 9 deletions src/sage/rings/polynomial/polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4467,18 +4467,42 @@ cdef class Polynomial(CommutativeAlgebraElement):
EXAMPLES::
sage: R.<x> = ZZ[]
sage: f = x+x^4+x^7
sage: f.sparsity()
sage: (x+x^4+x^7).sparsity()
3
TESTS::
sage: R.<x> = ZZ[]
sage: (x+x^4+x^7).sparsity()
3
sage: R(0).sparsity()
0
sage: R(1).sparsity()
1
sage: R.<x> = QQ[]
sage: (1/2*x+x^4+x^7/3).sparsity()
3
sage: R.<x> = QQbar[]
sage: (1-x+x^4+x^7).sparsity()
4
sage: R.<x> = GF(17)[]
sage: (1-x+17*x^4+x^7).sparsity()
3
sage: K.<a> = GF(3^5)
sage: R.<x> = K[]
sage: ((a^6-1)+x-a*x^4).sparsity()
3
sage: R.<x> = IntegerModRing(10)[]
sage: (1+x+10*x^2+12*x^3).sparsity()
3
sage: S = QuotientRing(R,Ideal(x^2-1))
sage: T.<t> = S[]
sage: ((9*x + 8)*t^4 + (5*x + 9)*t + x^2 + 4).sparsity()
3
sage: ((x^2+9)*t).sparsity()
0
"""
zero = self.parent().base_ring().zero_element()
l = self.list()
cdef int c
while True:
try:
if l.pop() is not zero: c+=1
except IndexError:
return c
return len([c for c in self.list() if c != zero])

def prec(self):
"""
Expand Down
33 changes: 32 additions & 1 deletion src/sage/rings/polynomial/polynomial_element_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,40 @@ def sparsity(self):
EXAMPLES::
sage: R.<x> = PolynomialRing(ZZ,sparse=True)
sage: f = x+x^4+x^7
sage: f = x+x^43+x^72
sage: f.sparsity()
3
TESTS::
sage: R.<x> = PolynomialRing(ZZ,sparse=True)
sage: (x+x^43+x^72).sparsity()
3
sage: R(0).sparsity()
0
sage: R(1).sparsity()
1
sage: R.<x> = PolynomialRing(QQ,sparse=True)
sage: (1/2*x+x^43+x^72/3).sparsity()
3
sage: R.<x> = PolynomialRing(QQbar,sparse=True)
sage: (1-x^27+x^43+x^72).sparsity()
4
sage: R.<x> = PolynomialRing(GF(17),sparse=True)
sage: (1-x^27+17*x^43+x^72).sparsity()
3
sage: K.<a> = GF(3^5)
sage: R.<x> = PolynomialRing(K,sparse=True)
sage: ((a^6-1)+x^27-a*x^43).sparsity()
3
sage: R.<x> = PolynomialRing(IntegerModRing(10),sparse=True)
sage: (1+x^17+10*x^2+12*x^3).sparsity()
3
sage: S = QuotientRing(R,Ideal(x^2-1))
sage: T.<t> = PolynomialRing(S,sparse=True)
sage: ((9*x + 8)*t^43 + (5*x + 9)*t^17 + x^2 + 4).sparsity()
3
sage: ((x^2+9)*t^78).sparsity()
0
"""
return len(self.__coeffs)

Expand Down

0 comments on commit e89350e

Please sign in to comment.