# what is the meaning of R.<z> = PolynomialRing( K, sparse=True ) and R.<z> = PolynomialRing( K, sparse=false) )

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Meaning of following syntax K is the base field R.<z> = PolynomialRing( K, sparse=True ) R.<z> = PolynomialRing( K, sparse=True )

edit retag close merge delete

Sort by » oldest newest most voted

Let's see the doc :

* "sparse" -- bool (default: False), whether or not elements are
sparse


As far as I understand it, a sparse polynomial stores only the non-zero coefficients (less storage, more computing), whereas a non-sparse polynomial stores all its coefficients (more storage, less computing). That can be critical when you have to work with polynomials having few coefficients of very high order...

Those polynomials are compatible :

sage: K=GF(17)
sage: R.<z>=PolynomialRing(K,sparse=False)
sage: r1=z^17-z^13+1
sage: r1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17
sage: S.<z>=PolynomialRing(K,sparse=True)
sage: r1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17
sage: s1=S(copy(r1))
sage: s1.parent()
Sparse Univariate Polynomial Ring in z over Finite Field of size 17


The use of a copy of r1 allows to keep r1's parent set :

sage: r1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17


One can compute on these two "compatible" sets :

sage: t1=r1-2*s1
sage: t1
16*z^17 + z^13 + 16
sage: t1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17
sage: r1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17
sage: s1.parent()
Sparse Univariate Polynomial Ring in z over Finite Field of size 17


BTW, the use of copy does not seem necessary (but ISTR that I've been hoisted by this...) :

sage: s2=S(r1)
sage: r1.parent()
Univariate Polynomial Ring in z over Finite Field of size 17
sage: s2.parent()
Sparse Univariate Polynomial Ring in z over Finite Field of size 17


HTH,

more

There is no mathematical difference, but an algorithmic one: in the nonsparse case, a polynomial is represented as a list of its coefficients up to its degree, in the sparse case, it only stores the nonzero coefficient (but in a different data strutcure to also store the corresponding degrees, like a dictionary, or a list of tuples). The size of the corresponding objects and the algorithms to deal with them are different.

Let us construct some polynomials in both parents and look and save them:

sage: R.<z> = PolynomialRing(ZZ, sparse=False)
sage: save(z^1000,'/tmp/nosparse_atom.sobj')
sage: save(sum(z^k for k in range(1001)),'/tmp/nosparse_sum.sobj')
sage: R.<z> = PolynomialRing(ZZ, sparse=True)
sage: save(z^1000,'/tmp/sparse_atom.sobj')
sage: save(sum(z^k for k in range(1001)),'/tmp/sparse_sum.sobj')


then, let us do ls -l /tmp in a terminal and see their respective size:

nosparse_atom.sobj 2082
nosparse_sum.sobj 2081
sparse_atom.sobj 590
sparse_sum.sobj 4009


As you can see, in the nonsparse case, both polynomials have about the same size in memory (storing the coefficient 1 is the same as storing the coefficient 0). In the sparse case, a lot of memory is saved in storing only the pair (1000,1), while for the sum, the object is about twice the size since both degrees and coefficients have tobe stored.

more