TD 1

Introduction à Jupyter

Je suis gras ou italique

Je peux taper des maths: $\int_0^\pi \cos(x) dx$

  • A
  • B
  • C

Introduction à Sage

Le langage Sage est une variante de Python

In [1]:
if 3 > 2:
    print("Hello world!")
  1+1  # Mauvaise indentation ici
else:
    print("Boooo")
  File "<ipython-input-1-b0b9ee5f3daa>", line 3
    Integer(1)+Integer(1)  # Mauvaise indentation ici
                                                     ^
IndentationError: unindent does not match any outer indentation level

Sage permet de manipuler des entiers, des chaînes de caractères, etc., mais aussi des objets mathématiques complexes.

In [2]:
a = 1
In [3]:
a = graphs.PetersenGraph()
In [4]:
a
Out[4]:
In [5]:
graphs.PerkelGraph()
Out[5]:
Perkel Graph: Graph on 57 vertices (use the .plot() method to plot)

Quelques structures algébriques prédéfinies en Sage

In [27]:
QQ
Out[27]:
Rational Field
In [28]:
RR
Out[28]:
Real Field with 53 bits of precision
In [30]:
MonRR = RealField(prec=100)
MonRR
Out[30]:
Real Field with 100 bits of precision

Sage préfère représenter les objets de façon exacte, si possible. Comparer 3/5 en Sage et en Python

In [6]:
3/5
Out[6]:
3/5
In [7]:
ZZ
Out[7]:
Integer Ring
In [8]:
a = 3
In [9]:
type(a)
Out[9]:
<class 'sage.rings.integer.Integer'>
In [10]:
type(3/5)
Out[10]:
<class 'sage.rings.rational.Rational'>
In [11]:
a = 3
In [12]:
b = 5
In [13]:
(a/b).numerator()
Out[13]:
3
In [14]:
c = a/b
In [15]:
c.numerator().abs()
Out[15]:
3

Accéder à l'aide

In [17]:
help(c.ceil)
Help on built-in function ceil:

ceil(...) method of sage.rings.rational.Rational instance
    Rational.ceil(self)
    File: sage/rings/rational.pyx (starting at line 3321)
    
            Return the ceiling of this rational number.
    
            OUTPUT: Integer
    
            If this rational number is an integer, this returns this number,
            otherwise it returns the floor of this number +1.
    
            EXAMPLES::
    
                sage: n = 5/3; n.ceil()
                2
                sage: n = -17/19; n.ceil()
                0
                sage: n = -7/2; n.ceil()
                -3
                sage: n = 7/2; n.ceil()
                4
                sage: n = 10/2; n.ceil()
                5

In [18]:
c.ceil()
Out[18]:
1

Parents et éléments

In [19]:
ZZ
Out[19]:
Integer Ring
In [20]:
type(ZZ)
Out[20]:
<class 'sage.rings.integer_ring.IntegerRing_class'>
In [21]:
3 in ZZ
Out[21]:
True
In [22]:
3/5 in ZZ
Out[22]:
False
In [23]:
a = 3
In [24]:
a.parent()
Out[24]:
Integer Ring
In [25]:
ZZ.an_element()
Out[25]:
1
In [26]:
print(list(ZZ.some_elements()))
[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10, 11, -11, 12, -12, 13, -13, 14, -14, 15, -15, 16, -16, 17, -17, 18, -18, 19, -19, 20, -20, 21, -21, 22, -22, 23, -23, 24, -24, 25, -25, 26, -26, 27, -27, 28, -28, 29, -29, 30, -30, 31, -31, 32, -32, 33, -33, 34, -34, 35, -35, 36, -36, 37, -37, 38, -38, 39, -39, 40, -40, 41, -41, 42, -42, 43, -43, 44, -44, 45, -45, 46, -46, 47, -47, 48, -48, 49, -49, 50]

Opérations entre parents incompatibles

In [27]:
a = GF(11)(2)
b = GF(13)(3)
In [28]:
a, b
Out[28]:
(2, 3)
In [29]:
a + b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-bd58363a63fc> in <module>()
----> 1 a + b

/opt/conda/lib/python3.6/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__add__ (build/cythonized/sage/structure/element.c:10980)()
   1249         # Left and right are Sage elements => use coercion model
   1250         if BOTH_ARE_ELEMENT(cl):
-> 1251             return coercion_model.bin_op(left, right, add)
   1252 
   1253         cdef long value

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10879)()
   1225         # We should really include the underlying error.
   1226         # This causes so much headache.
-> 1227         raise bin_op_exception(op, x, y)
   1228 
   1229     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for +: 'Finite Field of size 11' and 'Finite Field of size 13'
In [30]:
a.parent()
Out[30]:
Finite Field of size 11
In [31]:
b.parent()
Out[31]:
Finite Field of size 13
In [32]:
k = GF(11)
k
Out[32]:
Finite Field of size 11

Conversions

In [33]:
a = 3
In [34]:
b = 2/5
In [35]:
a.parent()
Out[35]:
Integer Ring
In [36]:
b.parent()
Out[36]:
Rational Field
In [37]:
c = a + b
c
Out[37]:
17/5
In [38]:
c.parent()
Out[38]:
Rational Field
In [39]:
d = (3/4) + 0.55
d
Out[39]:
1.30000000000000
In [40]:
d.parent()
Out[40]:
Real Field with 53 bits of precision
In [41]:
a = GF(11)(2)
b = GF(13)(4)
In [42]:
a+b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-ca730b97bf8a> in <module>()
----> 1 a+b

/opt/conda/lib/python3.6/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__add__ (build/cythonized/sage/structure/element.c:10980)()
   1249         # Left and right are Sage elements => use coercion model
   1250         if BOTH_ARE_ELEMENT(cl):
-> 1251             return coercion_model.bin_op(left, right, add)
   1252 
   1253         cdef long value

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10879)()
   1225         # We should really include the underlying error.
   1226         # This causes so much headache.
-> 1227         raise bin_op_exception(op, x, y)
   1228 
   1229     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for +: 'Finite Field of size 11' and 'Finite Field of size 13'
In [43]:
c = ZZ(a)
c
Out[43]:
2
In [44]:
c.parent()
Out[44]:
Integer Ring
In [45]:
ZZ(a) + ZZ(b)
Out[45]:
6

Polynômes

La variable prédéfinie x du Symbolic ring

In [46]:
x
Out[46]:
x
In [47]:
y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-47-9063a9f0e032> in <module>()
----> 1 y

NameError: name 'y' is not defined
In [48]:
parent(x)
Out[48]:
Symbolic Ring
In [49]:
p = x^2 + 3*x + 4
p
Out[49]:
x^2 + 3*x + 4
In [50]:
3^10
Out[50]:
59049
In [51]:
3**10
Out[51]:
59049
In [52]:
p
Out[52]:
x^2 + 3*x + 4

Affichage LaTeX

In [53]:
%display latex
In [54]:
p
Out[54]:
In [55]:
%display plain
In [56]:
p
Out[56]:
x^2 + 3*x + 4
In [57]:
a = p^2
a
Out[57]:
(x^2 + 3*x + 4)^2
In [58]:
a.expand()
Out[58]:
x^4 + 6*x^3 + 17*x^2 + 24*x + 16

Anneaux de polynômes

In [59]:
A.<x> = QQ[]
In [60]:
A
Out[60]:
Univariate Polynomial Ring in x over Rational Field
In [61]:
x
Out[61]:
x
In [62]:
parent(x) is A
Out[62]:
True

Syntaxe longue

In [66]:
B = QQ['y']
y = B.gen()
In [67]:
B
Out[67]:
Univariate Polynomial Ring in y over Rational Field
In [68]:
y
Out[68]:
y
In [69]:
C.<z> = PolynomialRing(QQ)
In [70]:
C
Out[70]:
Univariate Polynomial Ring in z over Rational Field
In [71]:
z in C
Out[71]:
True
In [72]:
A == B
Out[72]:
False
In [73]:
x + z
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-73-2d5f8479e2db> in <module>()
----> 1 x + z

/opt/conda/lib/python3.6/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__add__ (build/cythonized/sage/structure/element.c:10980)()
   1249         # Left and right are Sage elements => use coercion model
   1250         if BOTH_ARE_ELEMENT(cl):
-> 1251             return coercion_model.bin_op(left, right, add)
   1252 
   1253         cdef long value

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10879)()
   1225         # We should really include the underlying error.
   1226         # This causes so much headache.
-> 1227         raise bin_op_exception(op, x, y)
   1228 
   1229     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for +: 'Univariate Polynomial Ring in x over Rational Field' and 'Univariate Polynomial Ring in z over Rational Field'
In [74]:
A.<x,y,z> = QQ[]
In [75]:
A
Out[75]:
Multivariate Polynomial Ring in x, y, z over Rational Field
In [76]:
x + y
Out[76]:
x + y
In [77]:
p = x^2 + 3*x + 5
In [78]:
p
Out[78]:
x^2 + 3*x + 5
In [79]:
p^2
Out[79]:
x^4 + 6*x^3 + 19*x^2 + 30*x + 25
In [80]:
factor(10403)
Out[80]:
101 * 103
In [81]:
factor(p^2)
Out[81]:
(x^2 + 3*x + 5)^2

Exo 1.1

In [82]:
A.<x> = QQ[]
In [83]:
P1 = x^6 + 2*x^5 - 2*x^4 + 2*x^2 - 2*x - 1
P2 = x^5 + x^4 - 2*x^3 + x^2 + x - 2
P1, P2
Out[83]:
(x^6 + 2*x^5 - 2*x^4 + 2*x^2 - 2*x - 1, x^5 + x^4 - 2*x^3 + x^2 + x - 2)
In [84]:
P = P1*P2
P
Out[84]:
x^11 + 3*x^10 - 2*x^9 - 5*x^8 + 9*x^7 - 2*x^6 - 13*x^5 + 9*x^4 + 2*x^3 - 7*x^2 + 3*x + 2
In [85]:
P.degree()
Out[85]:
11
In [86]:
P.leading_coefficient()
Out[86]:
1
In [87]:
P.list()
Out[87]:
[2, 3, -7, 2, 9, -13, -2, 9, -5, -2, 3, 1]
In [88]:
P1 // P2
Out[88]:
x + 1
In [89]:
P1 % P2
Out[89]:
-x^4 + x^3 - x + 1
In [90]:
P1.quo_rem(P2)
Out[90]:
(x + 1, -x^4 + x^3 - x + 1)
In [91]:
gcd(P1, P2)
Out[91]:
x^4 - x^3 + x - 1
In [92]:
P1.gcd(P2)
Out[92]:
x^4 - x^3 + x - 1
In [93]:
P.factor()
Out[93]:
(x + 2) * (x - 1)^2 * (x + 1)^2 * (x^2 + 3*x + 1) * (x^2 - x + 1)^2
In [94]:
P(2)
Out[94]:
3564

Exo 1.2

In [95]:
A.<x> = ZZ[]
In [96]:
P = x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
P
Out[96]:
x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
In [97]:
P.factor()
Out[97]:
x * (x^2 + 1) * (x^2 + x + 1) * (x^6 - x^4 + 2*x^3 - x^2 + 1)
In [98]:
B.<x> = ZZ[i][]
B
Out[98]:
Univariate Polynomial Ring in x over Order in Number Field in I with defining polynomial x^2 + 1
In [99]:
P1 = B(P)
P1
Out[99]:
x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
In [100]:
P1.parent()
Out[100]:
Univariate Polynomial Ring in x over Order in Number Field in I with defining polynomial x^2 + 1

Sage ne sait pas factorizer des polynômes à coefficients dans $ℤ[i]$. C'est dommage.

In [101]:
P1.factor()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.factor (build/cythonized/sage/rings/polynomial/polynomial_element.c:40717)()
   4308             try:
-> 4309                 if R.is_finite():
   4310                     if R.characteristic() > 1<<29:

/opt/conda/lib/python3.6/site-packages/sage/rings/ring.pyx in sage.rings.ring.Ring.is_finite (build/cythonized/sage/rings/ring.c:9002)()
    915             return True
--> 916         return super(Ring, self).is_finite()
    917 

AttributeError: 'super' object has no attribute 'is_finite'

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-101-f8a86c9bd1b7> in <module>()
----> 1 P1.factor()

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.factor (build/cythonized/sage/rings/polynomial/polynomial_element.c:41468)()
   4329                 return F
   4330             except (TypeError, AttributeError):
-> 4331                 raise NotImplementedError
   4332 
   4333         return self._factor_pari_helper(G, n)

NotImplementedError: 
In [102]:
QQ[i]
Out[102]:
Number Field in I with defining polynomial x^2 + 1
In [103]:
P2 = P.change_ring(QQ[i])
In [104]:
P2.parent()
Out[104]:
Univariate Polynomial Ring in x over Number Field in I with defining polynomial x^2 + 1
In [105]:
P2.factor()
Out[105]:
(x - I) * x * (x + I) * (x^2 + x + 1) * (x^6 - x^4 + 2*x^3 - x^2 + 1)
In [106]:
P.change_ring(GF(2)).factor()
Out[106]:
x * (x + 1)^8 * (x^2 + x + 1)

Ceci semble être un bug (temporaire) de la version déployée sur ce serveur

In [107]:
P.change_ring(GF(4)).factor()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-107-a6308930077c> in <module>()
----> 1 P.change_ring(GF(Integer(4))).factor()

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.change_ring (build/cythonized/sage/rings/polynomial/polynomial_element.c:33595)()
   3243             return R(self)
   3244         else:
-> 3245             return self._parent.change_ring(R)(list(self))
   3246 
   3247     def _mpoly_dict_recursive(self, variables=None, base_ring=None):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in change_ring(self, R)
    998         from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
    999 
-> 1000         return PolynomialRing(R, names=self.variable_name(), sparse=self.is_sparse())
   1001 
   1002     def change_var(self, var):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring_constructor.py in PolynomialRing(base_ring, *args, **kwds)
    648         return _multi_variate(base_ring, names, **kwds)
    649     else:
--> 650         return _single_variate(base_ring, names, **kwds)
    651 
    652 

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring_constructor.py in _single_variate(base_ring, name, sparse, implementation, order)
    744     if implementation is not None:
    745         kwds["implementation"] = implementation
--> 746     R = constructor(base_ring, name, **kwds)
    747 
    748     for impl in implementation_names:

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in __init__(self, base_ring, name, element_class, implementation)
   2226             element_class = Polynomial_ZZ_pEX
   2227         PolynomialRing_field.__init__(self, base_ring, sparse=False, name=name,
-> 2228                                       element_class=element_class)
   2229 
   2230     @staticmethod

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in __init__(self, base_ring, name, sparse, element_class, category)
   1833                 element_class = polynomial_element_generic.Polynomial_generic_dense_field
   1834 
-> 1835         PolynomialRing_integral_domain.__init__(self, base_ring, name=name, sparse=sparse, element_class=element_class, category=category)
   1836 
   1837     def _ideal_class_(self, n=0):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in __init__(self, base_ring, name, sparse, implementation, element_class, category)
   1744                     element_class = Polynomial_integer_dense_flint
   1745         PolynomialRing_commutative.__init__(self, base_ring, name=name,
-> 1746                 sparse=sparse, element_class=element_class, category=category)
   1747         self._has_singular = can_convert_to_singular(self)
   1748 

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in __init__(self, base_ring, name, sparse, element_class, category)
   1620                 category = polynomial_default_category(base_ring.category(), 1)
   1621         PolynomialRing_general.__init__(self, base_ring, name=name,
-> 1622                 sparse=sparse, element_class=element_class, category=category)
   1623 
   1624     def quotient_by_principal_ideal(self, f, names=None):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in __init__(self, base_ring, name, sparse, element_class, category)
    291         # since we want to use PolynomialBaseringInjection.
    292         sage.algebras.algebra.Algebra.__init__(self, base_ring, names=name, normalize=True, category=category)
--> 293         self.__generator = self.element_class(self, [0,1], is_gen=True)
    294         self._populate_coercion_lists_(
    295                 #coerce_list = [base_inject],

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_zz_pex.pyx in sage.rings.polynomial.polynomial_zz_pex.Polynomial_ZZ_pEX.__init__ (build/cythonized/sage/rings/polynomial/polynomial_zz_pex.cpp:15512)()
    139                 # not do K.coerce(e) but K(e).
    140                 e = K(e)
--> 141                 d = parent._modulus.ZZ_pE(list(e.polynomial()))
    142                 ZZ_pEX_SetCoeff(self.x, i, d.x)
    143             return

/opt/conda/lib/python3.6/site-packages/sage/rings/finite_rings/element_givaro.pyx in sage.rings.finite_rings.element_givaro.FiniteField_givaroElement.polynomial (build/cythonized/sage/rings/finite_rings/element_givaro.cpp:15428)()
   1527             return PolynomialRing(K.prime_subfield(), name)(ret)
   1528         else:
-> 1529             return K.polynomial_ring()(ret)
   1530 
   1531     def _magma_init_(self, magma):

/opt/conda/lib/python3.6/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9727)()
    918         if mor is not None:
    919             if no_extra_args:
--> 920                 return mor._call_(x)
    921             else:
    922                 return mor._call_with_args(x, args, kwds)

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4554)()
    143                 print(type(C), C)
    144                 print(type(C._element_constructor), C._element_constructor)
--> 145             raise
    146 
    147     cpdef Element _call_with_args(self, x, args=(), kwds={}):

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4422)()
    138         cdef Parent C = self._codomain
    139         try:
--> 140             return C._element_constructor(x)
    141         except Exception:
    142             if print_warnings:

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_ring.py in _element_constructor_(self, x, check, is_gen, construct, **kwds)
    405         C = self.element_class
    406         if isinstance(x, (list, tuple)):
--> 407             return C(self, x, check=check, is_gen=False, construct=construct)
    408         if isinstance(x, range):
    409             return C(self, list(x), check=check, is_gen=False,

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_gf2x.pyx in sage.rings.polynomial.polynomial_gf2x.Polynomial_GF2X.__init__ (build/cythonized/sage/rings/polynomial/polynomial_gf2x.cpp:13861)()
     63         except AttributeError:
     64             pass
---> 65         Polynomial_template.__init__(self, parent, x, check, is_gen, construct)
     66 
     67     cdef get_unsafe(self, Py_ssize_t i):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_template.pxi in sage.rings.polynomial.polynomial_gf2x.Polynomial_template.__init__ (build/cythonized/sage/rings/polynomial/polynomial_gf2x.cpp:6474)()
    139                 # r += parent(e)*power
    140                 celement_pow(monomial, gen, deg, NULL, (<Polynomial_template>self)._cparent)
--> 141                 celement_mul(monomial, &(<Polynomial_template>self.__class__(parent, e)).x, monomial, (<Polynomial_template>self)._cparent)
    142                 celement_add(&self.x, &self.x, monomial, (<Polynomial_template>self)._cparent)
    143                 deg += 1

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_gf2x.pyx in sage.rings.polynomial.polynomial_gf2x.Polynomial_GF2X.__init__ (build/cythonized/sage/rings/polynomial/polynomial_gf2x.cpp:13861)()
     63         except AttributeError:
     64             pass
---> 65         Polynomial_template.__init__(self, parent, x, check, is_gen, construct)
     66 
     67     cdef get_unsafe(self, Py_ssize_t i):

/opt/conda/lib/python3.6/site-packages/sage/rings/polynomial/polynomial_template.pxi in sage.rings.polynomial.polynomial_gf2x.Polynomial_template.__init__ (build/cythonized/sage/rings/polynomial/polynomial_gf2x.cpp:7355)()
    174             self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct)
    175         else:
--> 176             x = parent.base_ring()(x)
    177             self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct)
    178 

/opt/conda/lib/python3.6/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9727)()
    918         if mor is not None:
    919             if no_extra_args:
--> 920                 return mor._call_(x)
    921             else:
    922                 return mor._call_with_args(x, args, kwds)

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4554)()
    143                 print(type(C), C)
    144                 print(type(C._element_constructor), C._element_constructor)
--> 145             raise
    146 
    147     cpdef Element _call_with_args(self, x, args=(), kwds={}):

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4422)()
    138         cdef Parent C = self._codomain
    139         try:
--> 140             return C._element_constructor(x)
    141         except Exception:
    142             if print_warnings:

/opt/conda/lib/python3.6/site-packages/sage/rings/finite_rings/integer_mod_ring.py in _element_constructor_(self, x)
   1165         """
   1166         try:
-> 1167             return integer_mod.IntegerMod(self, x)
   1168         except (NotImplementedError, PariError):
   1169             raise TypeError("error coercing to finite field")

/opt/conda/lib/python3.6/site-packages/sage/rings/finite_rings/integer_mod.pyx in sage.rings.finite_rings.integer_mod.IntegerMod (build/cythonized/sage/rings/finite_rings/integer_mod.c:4556)()
    195             return a
    196     t = modulus.element_class()
--> 197     return t(parent, value)
    198 
    199 

/opt/conda/lib/python3.6/site-packages/sage/rings/finite_rings/integer_mod.pyx in sage.rings.finite_rings.integer_mod.IntegerMod_abstract.__init__ (build/cythonized/sage/rings/finite_rings/integer_mod.c:5829)()
    364             return
    365         else:
--> 366             z = sage.rings.integer_ring.Z(value)
    367         self.set_from_mpz(z.value)
    368 

/opt/conda/lib/python3.6/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9727)()
    918         if mor is not None:
    919             if no_extra_args:
--> 920                 return mor._call_(x)
    921             else:
    922                 return mor._call_with_args(x, args, kwds)

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4554)()
    143                 print(type(C), C)
    144                 print(type(C._element_constructor), C._element_constructor)
--> 145             raise
    146 
    147     cpdef Element _call_with_args(self, x, args=(), kwds={}):

/opt/conda/lib/python3.6/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4416)()
    138         cdef Parent C = self._codomain
    139         try:
--> 140             return C._element_constructor(x)
    141         except Exception:
    142             if print_warnings:

/opt/conda/lib/python3.6/site-packages/sage/rings/integer.pyx in sage.rings.integer.Integer.__init__ (build/cythonized/sage/rings/integer.c:6013)()
    686                     mpz_set_pylong(self.value, n)
    687                 else:
--> 688                     raise TypeError("Cannot convert non-integral float to integer")
    689 
    690             elif isinstance(x, pari_gen):

TypeError: Cannot convert non-integral float to integer
In [108]:
P.parent()
Out[108]:
Univariate Polynomial Ring in x over Integer Ring
In [109]:
P1 = P.change_ring(ZZ[i])
In [110]:
P.parent()
Out[110]:
Univariate Polynomial Ring in x over Integer Ring
In [111]:
P1.parent()
Out[111]:
Univariate Polynomial Ring in x over Order in Number Field in I with defining polynomial x^2 + 1