Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting polynomials #1

Open
florian-rabe opened this issue Oct 16, 2018 · 4 comments
Open

Exporting polynomials #1

florian-rabe opened this issue Oct 16, 2018 · 4 comments

Comments

@florian-rabe
Copy link
Contributor

florian-rabe commented Oct 16, 2018

See also https://github.com/OpenDreamKit/MitM-Sage/blob/master/sage/Sage%20polynomials.ipynb

@nthiery Does the following make sense for importing/exporting polynomials?

Polynomial Rings

Export

Any object R of class
sage.rings.polynomial.multi_polynomial_ring_base.MPolynomialRing_base or
sage.rings.polynomial.polynomial_ring.PolynomialRing_general
is exported as

OMA(
OMS(sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing),
R.base_ring(),
R.variable_names()
)

Import

Works directly, i.e., we apply PolynomialRing to base ring and variable names.

Polynomials

Export

Any object p of class sage.rings.polynomial.polynomial_element.Polynomial
is exported as

OMA(
OMS(sage.rings.polynomial.polynomial_element.Polynomial),
p.parent(),
p.dict()
)
where p.parent() is the polynomial ring and p.dict() returns the map from exponent-tuples to coefficients.

Import

The symbol OMS(sage.rings.polynomial.polynomial_element.Polynomial),
is imported as the function lambda R, dict: R(dict)

@nthiery What is the canonical function to build a polynomial from a polynomial ring and a dictionary of coefficients? Edit: added the functions according to Nicolas's answer.

cc @tkw1536 @Jazzpirate

@nthiery
Copy link
Contributor

nthiery commented Oct 16, 2018 via email

@florian-rabe
Copy link
Contributor Author

@nthiery I think you misunderstood the question about the canonical function to build a polynomial.

I need a function f such that f(R, d) returns a polynomial in polynomial ring R with coefficient dictionary d.

@nthiery
Copy link
Contributor

nthiery commented Oct 17, 2018

Sorry if I have not made my point clear. Up to curyfication, R is that function:

def f(R, d):
     return R(d)

@florian-rabe
Copy link
Contributor Author

I've added the code as envisioned, but there are still some issues.

Nicolas recommends hooking directly into the pickling.
He expects the unpickling-based work flow will not work for nested objects.

The necessary code would be

from sage.rings.polynomial.multi_polynomial_ring_base import MPolynomialRing_base
from sage.rings.polynomial.polynomial_ring import PolynomialRing_general
from sage.rings.polynomial.polynomial_element import Polynomial

def pickle_polynomial(p):
   return lambda R,d: R(d), (p.parent(), p.dict())
copyreg.pickle(Polynomial, pickle_polynomial)
def pickle_polynomial_ring(p):
   return lambda br,vn: PolynomialRing(br,vn), (p.base_ring(), p.varnames())
copyreg.pickle(MPolynomialRing_base, pickle_polynomial_ring)

We should also double-check what common base class Sage polynomials have. I received different results at two different times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants