Je suis gras ou italique
Je peux taper des maths: ∫π0cos(x)dx
Le langage Sage est une variante de Python
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.
a = 1
a = graphs.PetersenGraph()
a
graphs.PerkelGraph()
Perkel Graph: Graph on 57 vertices (use the .plot() method to plot)
Quelques structures algébriques prédéfinies en Sage
QQ
Rational Field
RR
Real Field with 53 bits of precision
MonRR = RealField(prec=100)
MonRR
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
3/5
3/5
ZZ
Integer Ring
a = 3
type(a)
<class 'sage.rings.integer.Integer'>
type(3/5)
<class 'sage.rings.rational.Rational'>
a = 3
b = 5
(a/b).numerator()
3
c = a/b
c.numerator().abs()
3
Accéder à l'aide
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
c.ceil()
1
Parents et éléments
ZZ
Integer Ring
type(ZZ)
<class 'sage.rings.integer_ring.IntegerRing_class'>
3 in ZZ
True
3/5 in ZZ
False
a = 3
a.parent()
Integer Ring
ZZ.an_element()
1
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
a = GF(11)(2)
b = GF(13)(3)
a, b
(2, 3)
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'
a.parent()
Finite Field of size 11
b.parent()
Finite Field of size 13
k = GF(11)
k
Finite Field of size 11
a = 3
b = 2/5
a.parent()
Integer Ring
b.parent()
Rational Field
c = a + b
c
17/5
c.parent()
Rational Field
d = (3/4) + 0.55
d
1.30000000000000
d.parent()
Real Field with 53 bits of precision
a = GF(11)(2)
b = GF(13)(4)
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'
c = ZZ(a)
c
2
c.parent()
Integer Ring
ZZ(a) + ZZ(b)
6
La variable prédéfinie x
du Symbolic ring
x
x
y
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-47-9063a9f0e032> in <module>() ----> 1 y NameError: name 'y' is not defined
parent(x)
Symbolic Ring
p = x^2 + 3*x + 4
p
x^2 + 3*x + 4
3^10
59049
3**10
59049
p
x^2 + 3*x + 4
Affichage LaTeX
%display latex
p
%display plain
p
x^2 + 3*x + 4
a = p^2
a
(x^2 + 3*x + 4)^2
a.expand()
x^4 + 6*x^3 + 17*x^2 + 24*x + 16
Anneaux de polynômes
A.<x> = QQ[]
A
Univariate Polynomial Ring in x over Rational Field
x
x
parent(x) is A
True
Syntaxe longue
B = QQ['y']
y = B.gen()
B
Univariate Polynomial Ring in y over Rational Field
y
y
C.<z> = PolynomialRing(QQ)
C
Univariate Polynomial Ring in z over Rational Field
z in C
True
A == B
False
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'
A.<x,y,z> = QQ[]
A
Multivariate Polynomial Ring in x, y, z over Rational Field
x + y
x + y
p = x^2 + 3*x + 5
p
x^2 + 3*x + 5
p^2
x^4 + 6*x^3 + 19*x^2 + 30*x + 25
factor(10403)
101 * 103
factor(p^2)
(x^2 + 3*x + 5)^2
A.<x> = QQ[]
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
(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)
P = P1*P2
P
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
P.degree()
11
P.leading_coefficient()
1
P.list()
[2, 3, -7, 2, 9, -13, -2, 9, -5, -2, 3, 1]
P1 // P2
x + 1
P1 % P2
-x^4 + x^3 - x + 1
P1.quo_rem(P2)
(x + 1, -x^4 + x^3 - x + 1)
gcd(P1, P2)
x^4 - x^3 + x - 1
P1.gcd(P2)
x^4 - x^3 + x - 1
P.factor()
(x + 2) * (x - 1)^2 * (x + 1)^2 * (x^2 + 3*x + 1) * (x^2 - x + 1)^2
P(2)
3564
A.<x> = ZZ[]
P = x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
P
x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
P.factor()
x * (x^2 + 1) * (x^2 + x + 1) * (x^6 - x^4 + 2*x^3 - x^2 + 1)
B.<x> = ZZ[i][]
B
Univariate Polynomial Ring in x over Order in Number Field in I with defining polynomial x^2 + 1
P1 = B(P)
P1
x^11 + x^10 + x^9 + 2*x^8 + 2*x^6 + 2*x^4 + x^3 + x^2 + x
P1.parent()
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.
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:
QQ[i]
Number Field in I with defining polynomial x^2 + 1
P2 = P.change_ring(QQ[i])
P2.parent()
Univariate Polynomial Ring in x over Number Field in I with defining polynomial x^2 + 1
P2.factor()
(x - I) * x * (x + I) * (x^2 + x + 1) * (x^6 - x^4 + 2*x^3 - x^2 + 1)
P.change_ring(GF(2)).factor()
x * (x + 1)^8 * (x^2 + x + 1)
Ceci semble être un bug (temporaire) de la version déployée sur ce serveur
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
P.parent()
Univariate Polynomial Ring in x over Integer Ring
P1 = P.change_ring(ZZ[i])
P.parent()
Univariate Polynomial Ring in x over Integer Ring
P1.parent()
Univariate Polynomial Ring in x over Order in Number Field in I with defining polynomial x^2 + 1