OpenDreamKit / MitM-Sage

SageMath utilities for the Math-in-the-Middle integration approach (clone of https://gl.mathhub.info/ODK/Sage/ for binder usage)
2 stars 3 forks source link

Exporting polynomials #1

Open florian-rabe opened 6 years ago

florian-rabe commented 6 years ago

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 commented 6 years ago

+1 to polynomial ring import and export (at some point we will want to handle the term order, but we can do without it for now).

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.

+1; remains to decide whether to keep ETuples as keys of the dictionary, or plain tuples of ints.

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

[2]@nthiery What is the canonical function to build a polynomial from a polynomial ring and a dictionary of coefficients?

sage: R = PolynomialRing(QQ, 'x,y,z')
sage: p = R.random_element()
sage: p.dict()
{(0, 0, 0): -1/5, (0, 1, 1): -5, (1, 0, 1): 1}
sage: t = p.dict()
sage: R(t)
x*z - 5*y*z - 1/5
florian-rabe commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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.