Skip to content

Instantly share code, notes, and snippets.

@defeo
Last active October 17, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defeo/a79b7f7e31c1973bfcff1d95aab84269 to your computer and use it in GitHub Desktop.
Save defeo/a79b7f7e31c1973bfcff1d95aab84269 to your computer and use it in GitHub Desktop.
MA2-ace TD 4
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Éléments de syntaxe Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Les variables ne sont pas **typées**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 13\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'toto'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = \"toto\"\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<type 'str'>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Le bloc `if`\n",
"\n",
"Pas d'accolades `{` en Python : c'est l'**indentation** (espaceement à gauche) qui délimite les blocs. La convention veut que chaque nouveau bloc imbriqué ajoute 4 espaces d'indentation."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"15\n"
]
}
],
"source": [
"if x <= 14:\n",
" print \"Hello\"\n",
" x += 5\n",
" print x\n",
"elif x == 15:\n",
" print x\n",
"else:\n",
" print x - 20"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Une condition plus complexe avec des connecteurs logiques et des multi-comparaisons"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Oui\n"
]
}
],
"source": [
"if x is not None and 5 < x < 100 and (True or False):\n",
" print \"Oui\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boucle `while`"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"76\n",
"77\n",
"78\n",
"79\n",
"80\n",
"81\n",
"82\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"98\n",
"99\n",
"100\n"
]
}
],
"source": [
"x = 75\n",
"while x < 100:\n",
" x += 1\n",
" print x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Les variables de la boucle `while` ne sont pas locales à la boucle"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### *Listes* et boucle `for`"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n",
"12\n",
"20\n",
"25\n"
]
}
],
"source": [
"L = [1, 2, 10, 15]\n",
"for a in L:\n",
" b = a + 10\n",
" print b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Encore une preuve du fait que les variables des boucles ne sont pas locales"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(15, 25)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a, b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On peut imbrique les blocs, bien sûr"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"10\n",
"5\n"
]
}
],
"source": [
"for x in L:\n",
" if x > 10:\n",
" print x - 10\n",
" else:\n",
" print x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fonctions"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def tot(a, b):\n",
" c = a + b\n",
" return c"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = tot(14, 5)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Les variables d'une fonction sont (généralement) locales à la fonction"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'c' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-15-2cd6ee2c70b0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'c' is not defined"
]
}
],
"source": [
"c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Puisque l'opérateur `+` est *polymorphique* (il fonctionne sur plusieurs types), la fonction `tot` l'est aussi"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9.30000000000000"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tot(5.2, 4.1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tot(\"Hello \", 'world')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mais cela ne veut pas dire qu'on peut additionner tout et n'importe quoi: les conversions de type ne sont pas implicite en Python (mais elle le sont parfois en Sage)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand parent(s) for +: '<type 'str'>' and 'Integer Ring'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-37d325c0ab11>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m13\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-12-957743e6310d>\u001b[0m in \u001b[0;36mtot\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/local/SageMath-8.0/src/sage/rings/integer.pyx\u001b[0m in \u001b[0;36msage.rings.integer.Integer.__add__ (/local/SageMath-8.0/src/build/cythonized/sage/rings/integer.c:10936)\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1604\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1605\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1606\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcoercion_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbin_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mleft\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mright\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moperator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1607\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1608\u001b[0m \u001b[0mcpdef\u001b[0m \u001b[0m_add_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mright\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/local/SageMath-8.0/src/sage/structure/coerce.pyx\u001b[0m in \u001b[0;36msage.structure.coerce.CoercionModel_cache_maps.bin_op (/local/SageMath-8.0/src/build/cythonized/sage/structure/coerce.c:10448)\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1102\u001b[0m \u001b[0;31m# We should really include the underlying error.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1103\u001b[0m \u001b[0;31m# This causes so much headache.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1104\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mbin_op_exception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1105\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1106\u001b[0m \u001b[0mcpdef\u001b[0m \u001b[0mcanonical_coercion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand parent(s) for +: '<type 'str'>' and 'Integer Ring'"
]
}
],
"source": [
"tot(\"Hello\", 13)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Hello13'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tot(\"Hello\", str(13))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Hello13'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tot(\"Hello\", '13')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plus sur les listes"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A = [1, 2, 3] + [100, 11, 5]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 100, 11, 5]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[3]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.index(100)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[3]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"A.append(354)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 100, 11, 5, 354]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[3, 100]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[2:4]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 100]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[1:5:2]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"354"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[-1]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 100, 11]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A[:-2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Classes\n",
"\n",
"La méthode spéciale `__init__` est le *constructeur* de la classe"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Point():\n",
" def __init__(self, x, y):\n",
" self.x = x\n",
" self.y = y\n",
" def montrer(self):\n",
" print \"(\" + str(self.x) + \",\" + str(self.y) + \")\" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Le constructeur est appelé implicitement à la création d'un objet"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"p = Point(10, 15)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.x"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.y"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10,15)\n"
]
}
],
"source": [
"p.montrer()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Les champs des classes sont *dynamiques*: on peut en ajouter à souhait"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"p.c = 56"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"56"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.c"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10,15)\n"
]
}
],
"source": [
"p.montrer()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<__main__.Point instance at 0x7f2cab07f5a8>"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On peut ajouter aussi des méthodes à une classe déjà définie"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def affichage(self):\n",
" print \"Dans __repr__\"\n",
" # L'opérateur % sur les chaines de caractères est simlaire au printf en C\n",
" return \"(%d, %d)\" % (self.x, self.y)\n",
"Point.__repr__ = affichage"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = Point(10, 15)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"La méthode spéciale `__repr__` modifie la façon dont les objets s'affichent"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dans __repr__\n"
]
},
{
"data": {
"text/plain": [
"(10, 15)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercice 6.1"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def euclidepol (A,B):\n",
" A0 = A; A1 = B\n",
" S0 = 1; S1 = 0\n",
" T0 = 0; T1 = 1\n",
" while A1 != 0:\n",
" Q = A0//A1\n",
" U = A1; A1 = A0 - Q*A1; A0 = U\n",
" U = S1; S1 = S0 - Q*S1; S0 = U\n",
" U = T1; T1 = T0 - Q*T1; T0 = U\n",
" return (A0,S0,T0)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"A.<X> = QQ[]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a, b = X^2-1, (X-1)^2\n",
"g, u, v = euclidepol(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2*X - 2"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1, -1)"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u, v"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u*a + v*b == g"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercice 6.2"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def irreducible(k, n):\n",
" A.<x> = k[]\n",
" p = x^n\n",
" while not p.is_irreducible():\n",
" p = A.random_element(n)\n",
" return p.monic()"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x^3 - 3/2*x^2 - 45/2*x + 3/4"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"irreducible(QQ, 3)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x^100 + 3*x^99 + 6*x^98 + 4*x^97 + 5*x^96 + 2*x^95 + x^94 + 6*x^93 + 2*x^92 + 2*x^91 + 2*x^90 + 2*x^89 + x^88 + 2*x^87 + x^86 + x^85 + 4*x^84 + x^83 + 3*x^82 + 6*x^81 + 3*x^80 + 2*x^79 + 4*x^78 + 5*x^77 + x^76 + 6*x^75 + 5*x^74 + 5*x^73 + x^72 + 2*x^71 + 6*x^70 + 3*x^69 + 4*x^68 + 3*x^67 + 3*x^66 + 5*x^64 + 4*x^62 + 4*x^61 + 2*x^60 + 4*x^59 + 5*x^57 + 5*x^56 + 2*x^55 + 4*x^54 + 2*x^53 + 2*x^52 + 2*x^51 + 2*x^50 + 6*x^49 + 5*x^48 + x^47 + 5*x^46 + 5*x^44 + 3*x^43 + 3*x^42 + 3*x^41 + 4*x^39 + 2*x^38 + 3*x^36 + 6*x^35 + 3*x^34 + 6*x^33 + 4*x^32 + 5*x^31 + x^30 + 2*x^29 + 4*x^28 + 5*x^27 + 2*x^26 + 2*x^25 + 3*x^24 + 3*x^23 + 2*x^22 + 3*x^21 + 4*x^20 + 2*x^19 + 2*x^18 + 5*x^16 + 5*x^15 + 2*x^14 + 3*x^13 + 5*x^12 + 4*x^11 + 2*x^9 + 4*x^8 + 6*x^7 + 2*x^6 + 6*x^4 + 5*x^3 + 2*x^2 + 6*x + 6"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"irreducible(GF(7), 100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercice 6.3"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def lexico_irred(p, n):\n",
" A.<x> = GF(p)[]\n",
" P = x^n\n",
" while not P.is_irreducible():\n",
" # print P\n",
" for i in range(n):\n",
" if P[i] != p-1:\n",
" P += x^i\n",
" break\n",
" for j in range(i):\n",
" P -= P[j] * x^j\n",
" return P"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x^10 + 2*x^2 + 1"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lexico_irred(3, 10)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x^21 + 2*x + 1"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lexico_irred(101, 21)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 8.0",
"language": "",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment