mathsoftware

A course about LaTeX and SageMath
git clone https://git.tronto.net/mathsoftware
Download | Log | Files | Refs | README | LICENSE

7-SageAlgebra.ipynb (33627B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {},
      6    "source": [
      7     "This lecture's notes are in a different format: the presentations for the $\\LaTeX$ part were made with $\\LaTeX$, so this one is made with Sage, or rather with the [Jupyter Notebook](https://jupyter.org/).\n",
      8     "\n",
      9     "# The Jupyter Notebook\n",
     10     "**Reference:** [[1](https://jupyter.org/documentation)]\n",
     11     "\n",
     12     "The Jupyter Notebook is one of the default interfaces for SageMath, along with the command line interface. You can access it via web browser, but it is running locally on your device (notice the strange url: `http://localhost:8888/notebooks...`).\n",
     13     "\n",
     14     "You can create a new notebook by clicking on `New > SageMath 9.2`. You can also create a Python 3 notebook to write Python code.\n",
     15     "\n",
     16     "Jupyter saves and reads files in the `.ipynb` format. If you download the file for this lecture you can open it and follow the examples interactively.\n",
     17     "\n",
     18     "## Cells\n",
     19     "\n",
     20     "The notebook contains one or more *interactive cells* that you can run, like this one below:"
     21    ]
     22   },
     23   {
     24    "cell_type": "code",
     25    "execution_count": 2,
     26    "metadata": {},
     27    "outputs": [
     28     {
     29      "data": {
     30       "text/plain": [
     31        "2/5"
     32       ]
     33      },
     34      "execution_count": 2,
     35      "metadata": {},
     36      "output_type": "execute_result"
     37     }
     38    ],
     39    "source": [
     40     "# Exercise: modify this cell to use the print() command\n",
     41     "2+2\n",
     42     "2/5"
     43    ]
     44   },
     45   {
     46    "cell_type": "markdown",
     47    "metadata": {},
     48    "source": [
     49     "If you are reading this from Jupyter rather than from the pdf file, you can edit the cell above and run it again. You can also add more cells by selecting `Insert` from the menu bar.\n",
     50     "\n",
     51     "Notice that only the last statement produces an output. You can force anything to be written as output with the `print()` command, which works like in Python. As an exercise, try to modify the cell above to provide more output!"
     52    ]
     53   },
     54   {
     55    "cell_type": "markdown",
     56    "metadata": {},
     57    "source": [
     58     "## Markdown\n",
     59     "\n",
     60     "[Markdown](https://en.wikipedia.org/wiki/Markdown) is a simple markup language - think of LaTeX or html, but much simpler.\n",
     61     "You can add text to your notebook with Markdown cells by selecting `Cell > Cell Type > Markdown`.\n",
     62     "\n",
     63     "You can also include some LaTeX code in Markdown cells, with dollar signs $ or align environments:\n",
     64     "\n",
     65     "\\begin{align*}\n",
     66     "\\frac{(x+y)^2}{x+1} = \\frac{x^2+y^2}{x+1}\n",
     67     "\\end{align*}\n",
     68     "\n",
     69     "When you are done writing a Markdown cell, you can run it to see the well-formatted text. To edit the text again, double-click on the cell. Try doing it now to fix the formula above!"
     70    ]
     71   },
     72   {
     73    "cell_type": "markdown",
     74    "metadata": {},
     75    "source": [
     76     "# Symbolic expressions\n",
     77     "\n",
     78     "**Reference:** [[2](https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html)]\n",
     79     "\n",
     80     "Now, let's get started with Sage. One thing you might want to do is manipulating symbolic expressions, like the following:"
     81    ]
     82   },
     83   {
     84    "cell_type": "code",
     85    "execution_count": 3,
     86    "metadata": {},
     87    "outputs": [
     88     {
     89      "data": {
     90       "text/plain": [
     91        "[x == -sqrt(6) - 1, x == sqrt(6) - 1]"
     92       ]
     93      },
     94      "execution_count": 3,
     95      "metadata": {},
     96      "output_type": "execute_result"
     97     }
     98    ],
     99    "source": [
    100     "f =  x^2 + 2*x - 5 == 0\n",
    101     "solve(f,x)"
    102    ]
    103   },
    104   {
    105    "cell_type": "markdown",
    106    "metadata": {},
    107    "source": [
    108     "Notice that the single `=` is part of an assignment, as in Python: we are *assigning* to the variable `f` the value `x^2 + 2*x - 5 >= 0`, which in this case is an equation, so it contains the symbol `==`. Keep in mind the difference between the two!\n",
    109     "\n",
    110     "**Exercise:** change the code above to solve the corresponding inequality $x^2+2x-5\\geq 0$."
    111    ]
    112   },
    113   {
    114    "cell_type": "markdown",
    115    "metadata": {},
    116    "source": [
    117     "## Mathematical variables\n",
    118     "\n",
    119     "Last time we saw what *variables* are in Python, and that they are a little bit different from the *Mathematical variables* that you use in Mathematics. In Sage, both concepts are present, but they are still distinct. For example in the cell above `f` is a variable in the sense of computer science, while `x` is a Mathematical variable.\n",
    120     "\n",
    121     "If you want to use Mathematical variables other than `x`, you first need to *declare* them with the `var()` command:"
    122    ]
    123   },
    124   {
    125    "cell_type": "code",
    126    "execution_count": 14,
    127    "metadata": {},
    128    "outputs": [
    129     {
    130      "data": {
    131       "text/plain": [
    132        "[y == -1/2*x - 1/2*sqrt(x^2 + 2*x + 9) - 1/2, y == -1/2*x + 1/2*sqrt(x^2 + 2*x + 9) - 1/2]"
    133       ]
    134      },
    135      "execution_count": 14,
    136      "metadata": {},
    137      "output_type": "execute_result"
    138     }
    139    ],
    140    "source": [
    141     "var('y')\n",
    142     "solve(y^2 + (x+1)*y - 2 == 0, y)"
    143    ]
    144   },
    145   {
    146    "cell_type": "markdown",
    147    "metadata": {},
    148    "source": [
    149     "Try removing the first line in the cell above and see what error you get!\n",
    150     "\n",
    151     "Here is another example:"
    152    ]
    153   },
    154   {
    155    "cell_type": "code",
    156    "execution_count": 16,
    157    "metadata": {},
    158    "outputs": [
    159     {
    160      "data": {
    161       "text/plain": [
    162        "[x == -1/2*a - 1/2*sqrt(a^2 - 4*b), x == -1/2*a + 1/2*sqrt(a^2 - 4*b)]"
    163       ]
    164      },
    165      "execution_count": 16,
    166      "metadata": {},
    167      "output_type": "execute_result"
    168     }
    169    ],
    170    "source": [
    171     "var('a', 'b')\n",
    172     "f = x^2+a*x+b\n",
    173     "solve(f,x)"
    174    ]
    175   },
    176   {
    177    "cell_type": "markdown",
    178    "metadata": {},
    179    "source": [
    180     "Some common constants are [already defined](https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html) in Sage:"
    181    ]
    182   },
    183   {
    184    "cell_type": "code",
    185    "execution_count": 17,
    186    "metadata": {},
    187    "outputs": [
    188     {
    189      "data": {
    190       "text/plain": [
    191        "-1"
    192       ]
    193      },
    194      "execution_count": 17,
    195      "metadata": {},
    196      "output_type": "execute_result"
    197     }
    198    ],
    199    "source": [
    200     "e^(pi*I)"
    201    ]
    202   },
    203   {
    204    "cell_type": "markdown",
    205    "metadata": {},
    206    "source": [
    207     "We will study symbolic expressions more in detail next time, in the context of calculus/analysis."
    208    ]
    209   },
    210   {
    211    "cell_type": "markdown",
    212    "metadata": {},
    213    "source": [
    214     "# Basic rings and fields\n",
    215     "\n",
    216     "**References:** [[3](https://doc.sagemath.org/html/en/reference/rings_standard/index.html)]\n",
    217     "[[4](https://doc.sagemath.org/html/en/reference/rings_numerical/index.html)]\n",
    218     "[[5](https://doc.sagemath.org/html/en/reference/finite_rings/index.html)]\n",
    219     "\n",
    220     "As you should know, a *field* is a Mathematical structure with two operations, addition and multiplication, which respect certain rules (distributivity, associativity, commutativity...). Some examples of fields are the Rational numbers $\\mathbb Q$, the Real numbers $\\mathbb R$ and the Complex numbers $\\mathbb C$, but there are many more. As you should also know, a *(commutative) ring* is like a field, except not all elements different from $0$ need have a multiplicative inverse. For example the integers $\\mathbb Z = \\{ \\dots, -1, 0, 1, 2, \\dots\\}$ are a ring, but not a field.\n",
    221     "\n",
    222     "These structures are already implemented in Sage. Some of the most common are listed in the following table:\n",
    223     "\n",
    224     "|Mathematical object|Math symbol|Sage name|\n",
    225     "|------------------:|:---------:|:--------|\n",
    226     "|Integers|$\\mathbb Z$|`ZZ`|\n",
    227     "|Rational numbers|$\\mathbb Q$|`QQ`|\n",
    228     "|Real numbers|$\\mathbb R$|`RR`|\n",
    229     "|Complex numbers|$\\mathbb C$|`CC`|\n",
    230     "|Integers modulo $n$|$\\mathbb Z/n\\mathbb Z$|`Integers(n)`|\n",
    231     "|Finite fields|$\\mathbb F_p$|GF(p)|\n",
    232     "|$\\dots$|$\\dots$|$\\dots$|"
    233    ]
    234   },
    235   {
    236    "cell_type": "markdown",
    237    "metadata": {},
    238    "source": [
    239     "If you write a number or an expression, Sage will figure out where it \"lives\", choosing the most restrictive interpretation possible. For example `3` will be interpreted to be an integer, even if it is also a rational number, a real number and a complex number."
    240    ]
    241   },
    242   {
    243    "cell_type": "markdown",
    244    "metadata": {},
    245    "source": [
    246     "## Parents and coercion\n",
    247     "**Reference:** [[6](https://doc.sagemath.org/html/en/tutorial/tour_coercion.html)]\n",
    248     "\n",
    249     "You can check where an object \"lives\" with the `parent()` command. It works more or less like the Python command `type()`, but it gives a more Mathematically inclined answer. Check the reference link [6] above if you want more details."
    250    ]
    251   },
    252   {
    253    "cell_type": "code",
    254    "execution_count": 18,
    255    "metadata": {},
    256    "outputs": [
    257     {
    258      "data": {
    259       "text/plain": [
    260        "Rational Field"
    261       ]
    262      },
    263      "execution_count": 18,
    264      "metadata": {},
    265      "output_type": "execute_result"
    266     }
    267    ],
    268    "source": [
    269     "#Edit this cell to find out the type of other objects that we used\n",
    270     "parent(3/5)"
    271    ]
    272   },
    273   {
    274    "cell_type": "markdown",
    275    "metadata": {},
    276    "source": [
    277     "Sometimes Sage does not give you the best possible interpretation, so you can force something to be interpreted as living in a smaller ring as follows:"
    278    ]
    279   },
    280   {
    281    "cell_type": "code",
    282    "execution_count": 4,
    283    "metadata": {},
    284    "outputs": [
    285     {
    286      "name": "stdout",
    287      "output_type": "stream",
    288      "text": [
    289       "Symbolic Ring\n",
    290       "Integer Ring\n"
    291      ]
    292     }
    293    ],
    294    "source": [
    295     "minus_one = e^(pi*I)\n",
    296     "minus_one_coerced = ZZ(e^(pi*I))  # coercion\n",
    297     "print(parent(minus_one))\n",
    298     "print(parent(minus_one_coerced))"
    299    ]
    300   },
    301   {
    302    "cell_type": "markdown",
    303    "metadata": {},
    304    "source": [
    305     "**Remark.** Notice that there is a fundamental difference between the rings `RR` and `CC` and all the others in the table above: the real and complex numbers are *approximated*."
    306    ]
    307   },
    308   {
    309    "cell_type": "code",
    310    "execution_count": 1,
    311    "metadata": {},
    312    "outputs": [
    313     {
    314      "name": "stdout",
    315      "output_type": "stream",
    316      "text": [
    317       "3\n",
    318       "3.00000000000000\n"
    319      ]
    320     }
    321    ],
    322    "source": [
    323     "print(QQ(3))\n",
    324     "print(RR(3))"
    325    ]
    326   },
    327   {
    328    "cell_type": "markdown",
    329    "metadata": {},
    330    "source": [
    331     "You can also choose the precision of this approximation using the alternative name `RealField`."
    332    ]
    333   },
    334   {
    335    "cell_type": "code",
    336    "execution_count": 4,
    337    "metadata": {},
    338    "outputs": [
    339     {
    340      "name": "stdout",
    341      "output_type": "stream",
    342      "text": [
    343       "Real Field with 53 bits of precision\n",
    344       "Real Field with 1000 bits of precision\n"
    345      ]
    346     }
    347    ],
    348    "source": [
    349     "print(RR)\n",
    350     "print(RealField(prec=1000))"
    351    ]
    352   },
    353   {
    354    "cell_type": "markdown",
    355    "metadata": {},
    356    "source": [
    357     "# Polynomial rings\n",
    358     "\n",
    359     "**Reference:** [[7](https://doc.sagemath.org/html/en/reference/polynomial_rings/index.html)]\n",
    360     "\n",
    361     "If you want to work with polynomials over a certain ring it is better to use this specific construction, rather than the symbolic expressions introduced above."
    362    ]
    363   },
    364   {
    365    "cell_type": "code",
    366    "execution_count": 5,
    367    "metadata": {},
    368    "outputs": [
    369     {
    370      "data": {
    371       "text/plain": [
    372        "Multivariate Polynomial Ring in x, y, z over Real Field with 53 bits of precision"
    373       ]
    374      },
    375      "execution_count": 5,
    376      "metadata": {},
    377      "output_type": "execute_result"
    378     }
    379    ],
    380    "source": [
    381     "polring.<x,y,z> = RR[]  # Alternative: polring.<x,y,z> = PolynomialRing(RR)\n",
    382     "polring"
    383    ]
    384   },
    385   {
    386    "cell_type": "markdown",
    387    "metadata": {},
    388    "source": [
    389     "You can use as many variables as you like, and you can replace `RR` with any ring. In the example above `polring` is just the name of the variable (in the computer science sense) associated with this polynomial ring.\n",
    390     "\n",
    391     "## Operations on polynomials\n",
    392     "\n",
    393     "The usual Mathematical operations are available on polynomial rings, including Euclidean division `//` and remainder `%`. There is also the single-slash division `/`, but the result may not be a polynomial anymore.\n",
    394     "\n",
    395     "**Exercise:** use the `parent()` command to find out what the quotient of two polynomials is.\n",
    396     "\n",
    397     "**Question:** what happens if you remove the first line in the cell below? What if we used the variable `y` instead of `x`?"
    398    ]
    399   },
    400   {
    401    "cell_type": "code",
    402    "execution_count": 6,
    403    "metadata": {},
    404    "outputs": [
    405     {
    406      "name": "stdout",
    407      "output_type": "stream",
    408      "text": [
    409       "x + 1\n",
    410       "-4\n",
    411       "(x^2 + 2*x - 3)/(x + 1)\n"
    412      ]
    413     }
    414    ],
    415    "source": [
    416     "polring.<x> = QQ[]\n",
    417     "p = x^2 + 2*x - 3   # Don't forget * for multiplication!\n",
    418     "q = p // (x+1)\n",
    419     "r = p %  (x+1)\n",
    420     "f = p /  (x+1)\n",
    421     "print(q)\n",
    422     "print(r)\n",
    423     "print(f)"
    424    ]
    425   },
    426   {
    427    "cell_type": "markdown",
    428    "metadata": {},
    429    "source": [
    430     "You can do more complex operations. Try out `roots()` and `factor` in the cell below.\n",
    431     "\n",
    432     "**Remark.** Notice how the result can change substantially if you change the base ring.\n",
    433     "\n",
    434     "**Remark.** [Factorizations](https://doc.sagemath.org/html/en/reference/structure/sage/structure/factorization.html) are a particular object in Sage. They are kinda like a list, but not really. You can get a list of pairs (factor, power) with `list(factor(f))`."
    435    ]
    436   },
    437   {
    438    "cell_type": "code",
    439    "execution_count": 7,
    440    "metadata": {},
    441    "outputs": [
    442     {
    443      "name": "stdout",
    444      "output_type": "stream",
    445      "text": [
    446       "(t + 1) * (t^2 - 3) * (t^2 + 1)\n",
    447       "[(-1, 1)]\n"
    448      ]
    449     },
    450     {
    451      "data": {
    452       "text/plain": [
    453        "(y + 1) * x"
    454       ]
    455      },
    456      "execution_count": 7,
    457      "metadata": {},
    458      "output_type": "execute_result"
    459     }
    460    ],
    461    "source": [
    462     "polring_onevar.<t> = QQ[]\n",
    463     "\n",
    464     "f = t^5 + t^4 - 2*t^3 - 2*t^2 - 3*t - 3\n",
    465     "print(factor(f))\n",
    466     "print(f.roots())  # Result: list of pairs (root,multiplicity)\n",
    467     "\n",
    468     "polring_manyvar.<x,y,z> = QQ[]\n",
    469     "factor(x*y+x)\n",
    470     "\n",
    471     "# The following line gives an error, because the polynomial\n",
    472     "# is understood to possibly have many variables:\n",
    473     "#(x^2-1).roots()"
    474    ]
    475   },
    476   {
    477    "cell_type": "markdown",
    478    "metadata": {},
    479    "source": [
    480     "# Matrices and vectors\n",
    481     "\n",
    482     "**References:** [[8](https://doc.sagemath.org/html/en/reference/matrices/index.html)], but in particular the subections [[9](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/docs.html)] and [[10](https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html)]\n",
    483     "\n",
    484     "In Sage you can easily manipulate matrices and vectors"
    485    ]
    486   },
    487   {
    488    "cell_type": "code",
    489    "execution_count": 77,
    490    "metadata": {},
    491    "outputs": [
    492     {
    493      "name": "stdout",
    494      "output_type": "stream",
    495      "text": [
    496       "[   1    2    3]\n",
    497       "[   0    0    1]\n",
    498       "[   4   -3 22/7] \n",
    499       "\n",
    500       "[1/2   0   0]\n",
    501       "[  7   0   0]\n",
    502       "[  1   1   1] \n",
    503       "\n",
    504       "(3/2, 21, 6) \n",
    505       "\n",
    506       "[  -7/2    -10   80/7]\n",
    507       "[    17     -4   15/7]\n",
    508       "[ 241/7  -18/7 869/49] \n",
    509       "\n",
    510       "Rank of A = 3\n",
    511       "Rank of B = 2\n"
    512      ]
    513     }
    514    ],
    515    "source": [
    516     "A = matrix([[1,2,3],[0,0,1],[4,-3,22/7]])\n",
    517     "B = matrix([[1/2,0,0],[7,0,0],[1,1,1]])\n",
    518     "v = vector([3,4,-1])\n",
    519     "\n",
    520     "print(A, \"\\n\")  # \\n just means \"newline\"\n",
    521     "print(B, \"\\n\")\n",
    522     "print(B*v, \"\\n\")\n",
    523     "print(A^2 + 2*B - A*B, \"\\n\")\n",
    524     "\n",
    525     "print(\"Rank of A =\", rank(A)) # You can also use A.rank()\n",
    526     "print(\"Rank of B =\", rank(B))"
    527    ]
    528   },
    529   {
    530    "cell_type": "markdown",
    531    "metadata": {},
    532    "source": [
    533     "**Exercise:** in the cell above, compute the determinant, inverse and characteristic polynomial of the matrix `A`. *Hint: look at the reference [10] above (the functions are listed in alphabetic order).*\n",
    534     "\n",
    535     "As for polynomials, you can specify where a matrix or a vector lives"
    536    ]
    537   },
    538   {
    539    "cell_type": "code",
    540    "execution_count": 57,
    541    "metadata": {},
    542    "outputs": [
    543     {
    544      "data": {
    545       "text/plain": [
    546        "Full MatrixSpace of 2 by 2 dense matrices over Complex Field with 53 bits of precision"
    547       ]
    548      },
    549      "execution_count": 57,
    550      "metadata": {},
    551      "output_type": "execute_result"
    552     }
    553    ],
    554    "source": [
    555     "M = matrix(CC, [[0,1],[1,0]])\n",
    556     "parent(M)"
    557    ]
    558   },
    559   {
    560    "cell_type": "markdown",
    561    "metadata": {},
    562    "source": [
    563     "You can also solve linear systems and compute eigenvalues and eigenvectors of a matrix\n",
    564     "\n",
    565     "**Warning.** In linear algebra there are distinct concepts of *left* and *right* eigenvalues (and eigenvector). The one you know is probably that of **right** eigen-{value,vector}, that is an element $\\lambda$ of the base field and a non-zero vector $\\mathbf v$ with $A\\mathbf v=\\lambda\\mathbf v$. The other concept corresponds to the equality $\\mathbf v^TA=\\lambda \\mathbf v$."
    566    ]
    567   },
    568   {
    569    "cell_type": "code",
    570    "execution_count": 60,
    571    "metadata": {},
    572    "outputs": [
    573     {
    574      "data": {
    575       "text/plain": [
    576        "(0.289916349448506, 0.0241596957873755)"
    577       ]
    578      },
    579      "execution_count": 60,
    580      "metadata": {},
    581      "output_type": "execute_result"
    582     }
    583    ],
    584    "source": [
    585     "A = Matrix(RR, [[sqrt(59),32],[-1/4,3]])\n",
    586     "v = vector(RR, [3,0])\n",
    587     "A.solve_right(v) # Solve Ax=v. Alternative: A \\ v"
    588    ]
    589   },
    590   {
    591    "cell_type": "code",
    592    "execution_count": 64,
    593    "metadata": {},
    594    "outputs": [
    595     {
    596      "data": {
    597       "text/plain": [
    598        "[\n",
    599        "(-0.3722813232690144?, Vector space of degree 2 and dimension 1 over Algebraic Field\n",
    600        "User basis matrix:\n",
    601        "[                   1 -0.6861406616345072?]),\n",
    602        "(5.372281323269015?, Vector space of degree 2 and dimension 1 over Algebraic Field\n",
    603        "User basis matrix:\n",
    604        "[                 1 2.186140661634508?])\n",
    605        "]"
    606       ]
    607      },
    608      "execution_count": 64,
    609      "metadata": {},
    610      "output_type": "execute_result"
    611     }
    612    ],
    613    "source": [
    614     "A = Matrix(QQ, [[1,2],[3,4]])\n",
    615     "A.eigenspaces_right() # Also: A.eigenvalues(), A.eigenvectors_right()"
    616    ]
    617   },
    618   {
    619    "cell_type": "markdown",
    620    "metadata": {},
    621    "source": [
    622     "We can also extract a specific submatrix by selecting only some rows and columns, with a syntax similar to that of Python's lists. Check out more examples in the reference [9] above, and try them in the cell below."
    623    ]
    624   },
    625   {
    626    "cell_type": "code",
    627    "execution_count": 94,
    628    "metadata": {},
    629    "outputs": [
    630     {
    631      "name": "stdout",
    632      "output_type": "stream",
    633      "text": [
    634       "[-14   2   0  -1   1  -2  -1]\n",
    635       "[  0  -8   0   9  -2  11   1]\n",
    636       "[  0   3   1  -1   1   1 221]\n",
    637       "[ -1   2   1 -25 -10   4   0]\n",
    638       "[ -3   0   0   2  16  -1  -2]\n",
    639       "[  1  -3   3 -41   1   0   0]\n",
    640       "[ -2   1   0   0  -6   2  12] \n",
    641       "\n",
    642       "[ 0  9 -2]\n",
    643       "[ 1 -1  1] \n",
    644       "\n",
    645       "[-14   2   0  -1   1  -2  -1] \n",
    646       "\n",
    647       "[-14   2   0  -1   1]\n",
    648       "[  1  -3   3 -41   1]\n",
    649       "[  0   3   1  -1   1]\n"
    650      ]
    651     }
    652    ],
    653    "source": [
    654     "A = MatrixSpace(ZZ, 7).random_element()\n",
    655     "print(A, \"\\n\")\n",
    656     "print(A[1:3,2:5], \"\\n\") # Rows from 1 to 3, columns from 2 to 5\n",
    657     "print(A[0,0:], \"\\n\")    # First row, all columns\n",
    658     "print(A[[0,5,2],0:5])   # Rows 0, 5 and 2 (in this order) and columns 0 to 5"
    659    ]
    660   },
    661   {
    662    "cell_type": "markdown",
    663    "metadata": {},
    664    "source": [
    665     "**Exercise:** write a sage function that computes the determinant of an $n\\times n$ matrix $A=(a_{ij})$ using Laplace's rule by the first row, that is \n",
    666     "\\begin{align*}\n",
    667     "    \\operatorname{det}A = \\sum_{j=1}^n (-1)^ja_{0j}M_{0j}\n",
    668     "\\end{align*}\n",
    669     "where $M_{0j}$ is the determinant of the $(n-1)\\times(n-1)$ matrix obtained by removing the $0$-th row and the $j$-th column from $A$."
    670    ]
    671   },
    672   {
    673    "cell_type": "code",
    674    "execution_count": 91,
    675    "metadata": {},
    676    "outputs": [],
    677    "source": [
    678     "def my_det(A):\n",
    679     "    if not A.is_square():\n",
    680     "        print(\"Error: matrix is not square\")\n",
    681     "    \n",
    682     "    n = A.nrows()  # size of the matrix\n",
    683     "    \n",
    684     "    # Continue from here!"
    685    ]
    686   },
    687   {
    688    "cell_type": "markdown",
    689    "metadata": {},
    690    "source": [
    691     "# Number Theory\n",
    692     "\n",
    693     "**Reference:** [[11](https://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/integer.html)]\n",
    694     "\n",
    695     "Sage includes a large library of functions for computing with the integers, see the link above."
    696    ]
    697   },
    698   {
    699    "cell_type": "code",
    700    "execution_count": 8,
    701    "metadata": {},
    702    "outputs": [
    703     {
    704      "name": "stdout",
    705      "output_type": "stream",
    706      "text": [
    707       "3^2 * 3607 * 3803\n",
    708       "True\n",
    709       "True\n",
    710       "619703040\n",
    711       "9\n",
    712       "13548070123626141\n"
    713      ]
    714     }
    715    ],
    716    "source": [
    717     "n = 123456789\n",
    718     "m = 987654321\n",
    719     "p = 3607\n",
    720     "\n",
    721     "print(factor(n))\n",
    722     "print(is_prime(p))\n",
    723     "print(p.divides(n))\n",
    724     "print(euler_phi(m))\n",
    725     "print(gcd(n, m))\n",
    726     "print(lcm(n, m))"
    727    ]
    728   },
    729   {
    730    "cell_type": "markdown",
    731    "metadata": {},
    732    "source": [
    733     "## Primes\n",
    734     "\n",
    735     "**Reference:** [[12](https://doc.sagemath.org/html/en/reference/sets/sage/sets/primes.html)]\n",
    736     "\n",
    737     "The set of prime numbers is called `Primes()`. It is like an infinite list: for example you can get the one-millionth prime number or you can use this list to create other lists. You can also check what the first prime number larger than a given number is."
    738    ]
    739   },
    740   {
    741    "cell_type": "code",
    742    "execution_count": 9,
    743    "metadata": {},
    744    "outputs": [
    745     {
    746      "name": "stdout",
    747      "output_type": "stream",
    748      "text": [
    749       "Set of all prime numbers: 2, 3, 5, 7, ...\n",
    750       "31 15485867\n",
    751       "47\n",
    752       "[79, 83, 89, 97]\n"
    753      ]
    754     }
    755    ],
    756    "source": [
    757     "PP = Primes()\n",
    758     "print(PP)\n",
    759     "print(PP[10], PP[10^6])\n",
    760     "print(PP.next(44))\n",
    761     "\n",
    762     "First_Thousand_Primes = PP[0:1000]\n",
    763     "print([p for p in First_Thousand_Primes if p < 100 and p > 75])"
    764    ]
    765   },
    766   {
    767    "cell_type": "markdown",
    768    "metadata": {},
    769    "source": [
    770     "## The Chinese remainder theorem (CRT)\n",
    771     "\n",
    772     "We say that two integers $a$ and $b$ are *congruent* modulo another integer $n>0$ if they have the same remainder when divided by $n$. We denote this by $a\\equiv b\\pmod n$, or in Python/Sage syntax `a % n == b % n`.\n",
    773     "\n",
    774     "The Chinese remainder theorem states that if $a,b\\in\\mathbb Z$ and $n,m\\in \\mathbb Z_{>0}$ are such that $\\gcd(n,m)=1$ then the system of congruences\n",
    775     "\n",
    776     "\\begin{align*}\n",
    777     "\\begin{cases}\n",
    778     "    x \\equiv a \\pmod n\\\\\n",
    779     "    x \\equiv b \\pmod m\n",
    780     "\\end{cases}\n",
    781     "\\end{align*}\n",
    782     "\n",
    783     "has exactly one solution modulo $mn$. This means that there is one and only one number $x$ with $0\\leq x<mn$ such that $x\\equiv a\\pmod n$ and $x\\equiv b\\pmod m$.\n",
    784     "\n",
    785     "The procedure to find such a number is not too hard to describe (you might see it in an algebra or number theory course), but it can be a bit long. Luckily, Sage can do this for you:"
    786    ]
    787   },
    788   {
    789    "cell_type": "code",
    790    "execution_count": 10,
    791    "metadata": {},
    792    "outputs": [
    793     {
    794      "name": "stdout",
    795      "output_type": "stream",
    796      "text": [
    797       "74306 2 798\n"
    798      ]
    799     }
    800    ],
    801    "source": [
    802     "a = 2\n",
    803     "b = -1\n",
    804     "n = 172\n",
    805     "m = 799\n",
    806     "\n",
    807     "if gcd(n,m) != 1:\n",
    808     "    print(\"The numbers are not comprime, I can't solve this!\")\n",
    809     "else:\n",
    810     "    x = crt(a, b, n, m)\n",
    811     "    print(x, x%n, x%m)"
    812    ]
    813   },
    814   {
    815    "cell_type": "markdown",
    816    "metadata": {},
    817    "source": [
    818     "**Exercise.** There is a more general version of the Chinese remainder theorem which says that if $a_0, a_1, \\dots, a_k\\in\\mathbb Z$ and $n_0, n_2, \\dots, n_k\\in\\mathbb Z_{>0}$ are such that $\\gcd(n_i, n_j)=1$ for $i\\neq j$, then the system of congruences\n",
    819     "\n",
    820     "\\begin{align*}\n",
    821     "\\begin{cases}\n",
    822     "    x \\equiv a_0 \\pmod {n_0}\\\\\n",
    823     "    x \\equiv a_1 \\pmod {n_1}\\\\\n",
    824     "    \\dots \\\\\n",
    825     "    x \\equiv a_k \\pmod {n_k}\n",
    826     "\\end{cases}\n",
    827     "\\end{align*}\n",
    828     "\n",
    829     "has exactly one solution modulo $\\prod_{i=0}^kn_i$. Use the `crt()` function to find a solution to such a system.\n",
    830     "*Hint: start by running the command `help(crt)`."
    831    ]
    832   },
    833   {
    834    "cell_type": "code",
    835    "execution_count": 127,
    836    "metadata": {},
    837    "outputs": [],
    838    "source": [
    839     "#help(crt)"
    840    ]
    841   },
    842   {
    843    "cell_type": "markdown",
    844    "metadata": {},
    845    "source": [
    846     "# Cryptography: RSA\n",
    847     "\n",
    848     "[Cryptography](https://en.wikipedia.org/wiki/Cryptography) is the discipline that studies methods to communicate secrets in such a way that any unauthorized listener would not be able to understand the message.\n",
    849     "\n",
    850     "A simple cryptographic protocol could be changing every letter of your text following a fixed scheme (or *cypher*), for example by turning every A into a B, every B into a C and so on. However this is not a very secure method, for many reasons. One of them is that at some point the people who want to communicate need to agree on what method to use, and anyone listening to that conversation would be able to decypher every subsequent conversation. A public-key cryptographic protocol solves this problem.\n",
    851     "\n",
    852     "## Public-key cryptography\n",
    853     "\n",
    854     "Public-key cryptographic protocols, such as RSA, work like this: there are two keys, a *private* key that is only known to person A (traditionally called Alice in every example), and a *public* key that does not need to be secret.\n",
    855     "\n",
    856     "The public key is used to *encrypt* the message (that is to \"lock\" it, or \"hyde\" it), but one needs the private key to *decrypt* it. Imagine having two keys for your door, but one can only be used to lock it, while the other only to open it.\n",
    857     "\n",
    858     "The message exchange works like this: suppose that person B (Bob) wants to send a secret message to Alice. Then Alice secretely generates a private and a public key and sends only the public one to Bob. Now Bob encrypts the message and sends it to Alice, who can use her private key to decrypt it. Even if Eve (short for *eavesdropper*, an unauthorized listener) listens to every message exchanged, she won't be able to decypher the secret: the private key has never left Alice's house!\n",
    859     "\n",
    860     "Notice that such a protocol is *asymmetric*: if Alice wanted to send a secret to Bob in reply, Bob would need to generate a pair of keys of his own.\n",
    861     "\n",
    862     "Let's see how we can do this in practice, using number theory!\n",
    863     "\n",
    864     "## RSA\n",
    865     "\n",
    866     "As many other cryptography protocols, RSA is based on a Mathematical process that is easy to do in one direction, but very hard to invert. In this case the hard process is integer factorization, that is decomposing an integer number as a product of primes."
    867    ]
    868   },
    869   {
    870    "cell_type": "code",
    871    "execution_count": 2,
    872    "metadata": {},
    873    "outputs": [
    874     {
    875      "name": "stdout",
    876      "output_type": "stream",
    877      "text": [
    878       "True True False\n"
    879      ]
    880     }
    881    ],
    882    "source": [
    883     "p = 100003100019100043100057100069\n",
    884     "q = 100144655312449572059845328443\n",
    885     "n = p*q\n",
    886     "print(is_prime(p), is_prime(q), is_prime(p*q))\n",
    887     "\n",
    888     "# Use the command below to see how long it takes\n",
    889     "#timeit(\"factor(n)\", number=1, repeat=1)"
    890    ]
    891   },
    892   {
    893    "cell_type": "markdown",
    894    "metadata": {},
    895    "source": [
    896     "In order to generate the keys, Alice picks a number $n$ which is the product of two large primes $p$ and $q$ of more or less the same size. Finding such primes is relatively easy compared to factoring the number $n$ she obtained. Then she computes the Euler totient $\\varphi(n)=(p-1)(q-1)$ of $n$, which she can do because she knows that $n=pq$ - it would be impossible otherwise!\n",
    897     "\n",
    898     "Then Alice can compute two integers $(d,e)$ such that $de\\equiv 1\\pmod{\\varphi(n)}$. She will send the numbers $n$ and $d$ to Bob and keep $e$ secret. In this case the public key is the pair $(n,d)$, while $e$ is the private key.\n",
    899     "\n",
    900     "Of course, she does all of this using Sage!"
    901    ]
    902   },
    903   {
    904    "cell_type": "code",
    905    "execution_count": 105,
    906    "metadata": {},
    907    "outputs": [
    908     {
    909      "data": {
    910       "text/plain": [
    911        "(419199544978969, 235530823946467, 80799425863927)"
    912       ]
    913      },
    914      "execution_count": 105,
    915      "metadata": {},
    916      "output_type": "execute_result"
    917     }
    918    ],
    919    "source": [
    920     "def two_large_primes():\n",
    921     "    p, q = 0, 0\n",
    922     "    # We make sure that they are different\n",
    923     "    while p == q:\n",
    924     "        p = Primes()[randint(10^6, 2*10^6)]\n",
    925     "        q = Primes()[randint(10^6, 2*10^6)]\n",
    926     "    return p, q\n",
    927     "\n",
    928     "def random_unit_mod(N):\n",
    929     "    R = Integers(N)\n",
    930     "    d = R(0)\n",
    931     "    # We make sure that it is invertible\n",
    932     "    while not d.is_unit():\n",
    933     "        d = R.random_element()\n",
    934     "    return d\n",
    935     "\n",
    936     "def Alice_generate_keys():\n",
    937     "    p, q = two_large_primes()\n",
    938     "    n = p*q\n",
    939     "    phi_n = (p-1)*(q-1) # euler_phi(n) is slow!\n",
    940     "    \n",
    941     "    d = random_unit_mod(phi_n)\n",
    942     "    e = d^-1\n",
    943     "    return n, d, e\n",
    944     "\n",
    945     "Alice_generate_keys()"
    946    ]
    947   },
    948   {
    949    "cell_type": "markdown",
    950    "metadata": {},
    951    "source": [
    952     "Now, how does Bob encrypt his message? Let's say he wants to send to Alice the number $m$ with $1<m<n$ (In practice he would like to send her some text with emojis, or maybe a voice message; but for computers everything is a number, and there are different ways to translate any sort of information to a number. He just chooses one of the many standard methods that already exist, no cryptography is needed in this step. If the message $m$ is too long, he can split it up in some pieces and repeat the process multiple times.)\n",
    953     "\n",
    954     "Now he computes $m^d\\pmod n$ and sends it back to Alice."
    955    ]
    956   },
    957   {
    958    "cell_type": "code",
    959    "execution_count": 3,
    960    "metadata": {},
    961    "outputs": [
    962     {
    963      "data": {
    964       "text/plain": [
    965        "149461597163501"
    966       ]
    967      },
    968      "execution_count": 3,
    969      "metadata": {},
    970      "output_type": "execute_result"
    971     }
    972    ],
    973    "source": [
    974     "def Bob_encrypt(m, n, d):\n",
    975     "    R = Integers(n)\n",
    976     "    return R(m)^d  # Assume that n is large enough\n",
    977     "    \n",
    978     "message = 42424242\n",
    979     "Bob_encrypt(message, 419199544978969, 235530823946467)"
    980    ]
    981   },
    982   {
    983    "cell_type": "markdown",
    984    "metadata": {},
    985    "source": [
    986     "Since $de\\equiv 1\\pmod{\\varphi(n)}$, it follows that $(m^d)^e\\equiv m\\pmod n$ (see [Wikipedia: Euler's theorem](https://en.wikipedia.org/wiki/Euler%27s_theorem)). So for Alice it is very easy to get back the original message:"
    987    ]
    988   },
    989   {
    990    "cell_type": "code",
    991    "execution_count": 108,
    992    "metadata": {},
    993    "outputs": [
    994     {
    995      "data": {
    996       "text/plain": [
    997        "42424242"
    998       ]
    999      },
   1000      "execution_count": 108,
   1001      "metadata": {},
   1002      "output_type": "execute_result"
   1003     }
   1004    ],
   1005    "source": [
   1006     "def Alice_decrypt(m_encrypted, n, e):\n",
   1007     "    R = Integers(n)\n",
   1008     "    return R(m_encrypted)^e\n",
   1009     "\n",
   1010     "Alice_decrypt(149461597163501, 419199544978969, 80799425863927)"
   1011    ]
   1012   },
   1013   {
   1014    "cell_type": "markdown",
   1015    "metadata": {},
   1016    "source": [
   1017     "Another assumption on which RSA relies is that even if one knows $M=m^e$ and $e$, extracting the $e$-th root of $M$ modulo $n$ (and thus obtaining $m$) is very hard. Currently the best known way to do this is by factorizing $n$ first, which is considered to be a very hard problem. However, there is no proof that faster algorithms can't be devised.\n",
   1018     "\n",
   1019     "Moreover, one day we will overcome the current technological difficulties and quantum computers will be available. Quantum computers are not just \"more powerful\" than classical hardware, but they work based on completely different logical foundations and they make the factorization problem much easier to solve: for example [Shor's algorithm](https://en.wikipedia.org/wiki/Shor%27s_algorithm) takes advantage of this different logic and can factorize numbers quickly, if run on a quantum computer.\n",
   1020     "\n",
   1021     "To this day the largest number factorized with a quantum computer is $21=3\\times 7$. Nonetheless, quantum-safe cryptography protocols (i.e. based on problems that are hard to solve also with quantum computers) have already been developed."
   1022    ]
   1023   }
   1024  ],
   1025  "metadata": {
   1026   "kernelspec": {
   1027    "display_name": "SageMath 9.2",
   1028    "language": "sage",
   1029    "name": "sagemath"
   1030   },
   1031   "language_info": {
   1032    "codemirror_mode": {
   1033     "name": "ipython",
   1034     "version": 3
   1035    },
   1036    "file_extension": ".py",
   1037    "mimetype": "text/x-python",
   1038    "name": "python",
   1039    "nbconvert_exporter": "python",
   1040    "pygments_lexer": "ipython3",
   1041    "version": "3.8.5"
   1042   }
   1043  },
   1044  "nbformat": 4,
   1045  "nbformat_minor": 4
   1046 }