aboutsummaryrefslogtreecommitdiff
path: root/src/Lecture5/notebook/7-SageAlgebra.tex
diff options
context:
space:
mode:
Diffstat (limited to 'src/Lecture5/notebook/7-SageAlgebra.tex')
-rw-r--r--src/Lecture5/notebook/7-SageAlgebra.tex1297
1 files changed, 1297 insertions, 0 deletions
diff --git a/src/Lecture5/notebook/7-SageAlgebra.tex b/src/Lecture5/notebook/7-SageAlgebra.tex
new file mode 100644
index 0000000..a9c6c72
--- /dev/null
+++ b/src/Lecture5/notebook/7-SageAlgebra.tex
@@ -0,0 +1,1297 @@
1\documentclass[11pt]{article}
2
3 \usepackage[breakable]{tcolorbox}
4 \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour)
5
6 \usepackage{iftex}
7 \ifPDFTeX
8 \usepackage[T1]{fontenc}
9 \usepackage{mathpazo}
10 \else
11 \usepackage{fontspec}
12 \fi
13
14 % Basic figure setup, for now with no caption control since it's done
15 % automatically by Pandoc (which extracts ![](path) syntax from Markdown).
16 \usepackage{graphicx}
17 % Maintain compatibility with old templates. Remove in nbconvert 6.0
18 \let\Oldincludegraphics\includegraphics
19 % Ensure that by default, figures have no caption (until we provide a
20 % proper Figure object with a Caption API and a way to capture that
21 % in the conversion process - todo).
22 \usepackage{caption}
23 \DeclareCaptionFormat{nocaption}{}
24 \captionsetup{format=nocaption,aboveskip=0pt,belowskip=0pt}
25
26 \usepackage[Export]{adjustbox} % Used to constrain images to a maximum size
27 \adjustboxset{max size={0.9\linewidth}{0.9\paperheight}}
28 \usepackage{float}
29 \floatplacement{figure}{H} % forces figures to be placed at the correct location
30 \usepackage{xcolor} % Allow colors to be defined
31 \usepackage{enumerate} % Needed for markdown enumerations to work
32 \usepackage{geometry} % Used to adjust the document margins
33 \usepackage{amsmath} % Equations
34 \usepackage{amssymb} % Equations
35 \usepackage{textcomp} % defines textquotesingle
36 % Hack from http://tex.stackexchange.com/a/47451/13684:
37 \AtBeginDocument{%
38 \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
39 }
40 \usepackage{upquote} % Upright quotes for verbatim code
41 \usepackage{eurosym} % defines \euro
42 \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
43 \usepackage{fancyvrb} % verbatim replacement that allows latex
44 \usepackage{grffile} % extends the file name processing of package graphics
45 % to support a larger range
46 \makeatletter % fix for grffile with XeLaTeX
47 \def\Gread@@xetex#1{%
48 \IfFileExists{"\Gin@base".bb}%
49 {\Gread@eps{\Gin@base.bb}}%
50 {\Gread@@xetex@aux#1}%
51 }
52 \makeatother
53
54 % The hyperref package gives us a pdf with properly built
55 % internal navigation ('pdf bookmarks' for the table of contents,
56 % internal cross-reference links, web links for URLs, etc.)
57 \usepackage{hyperref}
58 % The default LaTeX title has an obnoxious amount of whitespace. By default,
59 % titling removes some of it. It also provides customization options.
60 \usepackage{titling}
61 \usepackage{longtable} % longtable support required by pandoc >1.10
62 \usepackage{booktabs} % table support for pandoc > 1.12.2
63 \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
64 \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
65 % normalem makes italics be italics, not underlines
66 \usepackage{mathrsfs}
67
68
69
70 % Colors for the hyperref package
71 \definecolor{urlcolor}{rgb}{0,.145,.698}
72 \definecolor{linkcolor}{rgb}{.71,0.21,0.01}
73 \definecolor{citecolor}{rgb}{.12,.54,.11}
74
75 % ANSI colors
76 \definecolor{ansi-black}{HTML}{3E424D}
77 \definecolor{ansi-black-intense}{HTML}{282C36}
78 \definecolor{ansi-red}{HTML}{E75C58}
79 \definecolor{ansi-red-intense}{HTML}{B22B31}
80 \definecolor{ansi-green}{HTML}{00A250}
81 \definecolor{ansi-green-intense}{HTML}{007427}
82 \definecolor{ansi-yellow}{HTML}{DDB62B}
83 \definecolor{ansi-yellow-intense}{HTML}{B27D12}
84 \definecolor{ansi-blue}{HTML}{208FFB}
85 \definecolor{ansi-blue-intense}{HTML}{0065CA}
86 \definecolor{ansi-magenta}{HTML}{D160C4}
87 \definecolor{ansi-magenta-intense}{HTML}{A03196}
88 \definecolor{ansi-cyan}{HTML}{60C6C8}
89 \definecolor{ansi-cyan-intense}{HTML}{258F8F}
90 \definecolor{ansi-white}{HTML}{C5C1B4}
91 \definecolor{ansi-white-intense}{HTML}{A1A6B2}
92 \definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF}
93 \definecolor{ansi-default-inverse-bg}{HTML}{000000}
94
95 % commands and environments needed by pandoc snippets
96 % extracted from the output of `pandoc -s`
97 \providecommand{\tightlist}{%
98 \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
99 \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
100 % Add ',fontsize=\small' for more characters per line
101 \newenvironment{Shaded}{}{}
102 \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
103 \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
104 \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
105 \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
106 \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
107 \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
108 \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
109 \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
110 \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
111 \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
112 \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
113 \newcommand{\RegionMarkerTok}[1]{{#1}}
114 \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
115 \newcommand{\NormalTok}[1]{{#1}}
116
117 % Additional commands for more recent versions of Pandoc
118 \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
119 \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
120 \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
121 \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
122 \newcommand{\ImportTok}[1]{{#1}}
123 \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
124 \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
125 \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
126 \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
127 \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
128 \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
129 \newcommand{\BuiltInTok}[1]{{#1}}
130 \newcommand{\ExtensionTok}[1]{{#1}}
131 \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
132 \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
133 \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
134 \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
135
136
137 % Define a nice break command that doesn't care if a line doesn't already
138 % exist.
139 \def\br{\hspace*{\fill} \\* }
140 % Math Jax compatibility definitions
141 \def\gt{>}
142 \def\lt{<}
143 \let\Oldtex\TeX
144 \let\Oldlatex\LaTeX
145 \renewcommand{\TeX}{\textrm{\Oldtex}}
146 \renewcommand{\LaTeX}{\textrm{\Oldlatex}}
147 % Document parameters
148 % Document title
149 \title{Algebra and Cryptography with SageMath}
150 \date{2021-04-23}
151 \author{Sebastiano Tronto - \texttt{sebastiano.tronto@uni.lu}}
152
153
154
155
156
157% Pygments definitions
158\makeatletter
159\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
160 \let\PY@ul=\relax \let\PY@tc=\relax%
161 \let\PY@bc=\relax \let\PY@ff=\relax}
162\def\PY@tok#1{\csname PY@tok@#1\endcsname}
163\def\PY@toks#1+{\ifx\relax#1\empty\else%
164 \PY@tok{#1}\expandafter\PY@toks\fi}
165\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
166 \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
167\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
168
169\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
170\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
171\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
172\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
173\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
174\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
175\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
176\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
177\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
178\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
179\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
180\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
181\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
182\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
183\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
184\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
185\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
186\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
187\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
188\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
189\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
190\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
191\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
192\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
193\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
194\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
195\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
196\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
197\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
198\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
199\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
200\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
201\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
202\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit}
203\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf}
204\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
205\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
206\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
207\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
208\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
209\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
210\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
211\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
212\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
213\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
214\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
215\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
216\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
217\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
218\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
219\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
220\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
221\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
222\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
223\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
224\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
225\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
226\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
227\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
228\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
229\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
230\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
231\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
232\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
233\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
234\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
235\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
236
237\def\PYZbs{\char`\\}
238\def\PYZus{\char`\_}
239\def\PYZob{\char`\{}
240\def\PYZcb{\char`\}}
241\def\PYZca{\char`\^}
242\def\PYZam{\char`\&}
243\def\PYZlt{\char`\<}
244\def\PYZgt{\char`\>}
245\def\PYZsh{\char`\#}
246\def\PYZpc{\char`\%}
247\def\PYZdl{\char`\$}
248\def\PYZhy{\char`\-}
249\def\PYZsq{\char`\'}
250\def\PYZdq{\char`\"}
251\def\PYZti{\char`\~}
252% for compatibility with earlier versions
253\def\PYZat{@}
254\def\PYZlb{[}
255\def\PYZrb{]}
256\makeatother
257
258
259 % For linebreaks inside Verbatim environment from package fancyvrb.
260 \makeatletter
261 \newbox\Wrappedcontinuationbox
262 \newbox\Wrappedvisiblespacebox
263 \newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}}
264 \newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}}
265 \newcommand*\Wrappedcontinuationindent {3ex }
266 \newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox}
267 % Take advantage of the already applied Pygments mark-up to insert
268 % potential linebreaks for TeX processing.
269 % {, <, #, %, $, ' and ": go to next line.
270 % _, }, ^, &, >, - and ~: stay at end of broken line.
271 % Use of \textquotesingle for straight quote.
272 \newcommand*\Wrappedbreaksatspecials {%
273 \def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}%
274 \def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}%
275 \def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}%
276 \def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}%
277 \def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}%
278 \def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}%
279 \def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}%
280 \def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}%
281 \def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}%
282 \def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}%
283 \def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}%
284 \def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}%
285 \def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}%
286 \def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}%
287 }
288 % Some characters . , ; ? ! / are not pygmentized.
289 % This macro makes them "active" and they will insert potential linebreaks
290 \newcommand*\Wrappedbreaksatpunct {%
291 \lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}%
292 \lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}%
293 \lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}%
294 \lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}%
295 \lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}%
296 \lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}%
297 \lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}%
298 \catcode`\.\active
299 \catcode`\,\active
300 \catcode`\;\active
301 \catcode`\:\active
302 \catcode`\?\active
303 \catcode`\!\active
304 \catcode`\/\active
305 \lccode`\~`\~
306 }
307 \makeatother
308
309 \let\OriginalVerbatim=\Verbatim
310 \makeatletter
311 \renewcommand{\Verbatim}[1][1]{%
312 %\parskip\z@skip
313 \sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}%
314 \sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}%
315 \def\FancyVerbFormatLine ##1{\hsize\linewidth
316 \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
317 \doublehyphendemerits\z@\finalhyphendemerits\z@
318 \strut ##1\strut}%
319 }%
320 % If the linebreak is at a space, the latter will be displayed as visible
321 % space at end of first line, and a continuation symbol starts next line.
322 % Stretch/shrink are however usually zero for typewriter font.
323 \def\FV@Space {%
324 \nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
325 \discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak}
326 {\kern\fontdimen2\font}%
327 }%
328
329 % Allow breaks at special characters using \PYG... macros.
330 \Wrappedbreaksatspecials
331 % Breaks at punctuation characters . , ; ? ! and / need catcode=\active
332 \OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]%
333 }
334 \makeatother
335
336 % Exact colors from NB
337 \definecolor{incolor}{HTML}{303F9F}
338 \definecolor{outcolor}{HTML}{D84315}
339 \definecolor{cellborder}{HTML}{CFCFCF}
340 \definecolor{cellbackground}{HTML}{F7F7F7}
341
342 % prompt
343 \makeatletter
344 \newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep}
345 \makeatother
346 \newcommand{\prompt}[4]{
347 \ttfamily\llap{{\color{#2}[#3]:\hspace{3pt}#4}}\vspace{-\baselineskip}
348 }
349
350
351
352 % Prevent overflowing lines due to hard-to-break entities
353 \sloppy
354 % Setup hyperref package
355 \hypersetup{
356 breaklinks=true, % so long urls are correctly broken across lines
357 colorlinks=true,
358 urlcolor=urlcolor,
359 linkcolor=linkcolor,
360 citecolor=citecolor,
361 }
362 % Slightly bigger margins than the latex defaults
363
364 \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
365
366
367
368\begin{document}
369
370 \maketitle
371
372
373
374
375 This lecture's notes are in a different format: the presentations for
376the \(\LaTeX\) part were made with \(\LaTeX\), so this one is made with
377Sage, or rather with the \href{https://jupyter.org/}{Jupyter Notebook}.
378
379\hypertarget{the-jupyter-notebook}{%
380\section{The Jupyter Notebook}\label{the-jupyter-notebook}}
381
382\textbf{Reference:} {[}\href{https://jupyter.org/documentation}{1}{]}
383
384The Jupyter Notebook is one of the default interfaces for SageMath,
385along with the command line interface. You can access it via web
386browser, but it is running locally on your device (notice the strange
387url: \texttt{http://localhost:8888/notebooks...}).
388
389You can create a new notebook by clicking on
390\texttt{New\ \textgreater{}\ SageMath\ 9.2}. You can also create a
391Python 3 notebook to write Python code.
392
393Jupyter saves and reads files in the \texttt{.ipynb} format. If you
394download the file for this lecture you can open it and follow the
395examples interactively.
396
397\hypertarget{cells}{%
398\subsection{Cells}\label{cells}}
399
400The notebook contains one or more \emph{interactive cells} that you can
401run, like this one below:
402
403 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
404\prompt{In}{incolor}{2}{\boxspacing}
405\begin{Verbatim}[commandchars=\\\{\}]
406\PY{c+c1}{\PYZsh{} Exercise: modify this cell to use the print() command}
407\PY{l+m+mi}{2}\PY{o}{+}\PY{l+m+mi}{2}
408\PY{l+m+mi}{2}\PY{o}{/}\PY{l+m+mi}{5}
409\end{Verbatim}
410\end{tcolorbox}
411
412 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
413\prompt{Out}{outcolor}{2}{\boxspacing}
414\begin{Verbatim}[commandchars=\\\{\}]
4152/5
416\end{Verbatim}
417\end{tcolorbox}
418
419 If you are reading this from Jupyter rather than from the pdf file, you
420can edit the cell above and run it again. You can also add more cells by
421selecting \texttt{Insert} from the menu bar.
422
423Notice that only the last statement produces an output. You can force
424anything to be written as output with the \texttt{print()} command,
425which works like in Python. As an exercise, try to modify the cell above
426to provide more output!
427
428 \hypertarget{markdown}{%
429\subsection{Markdown}\label{markdown}}
430
431\href{https://en.wikipedia.org/wiki/Markdown}{Markdown} is a simple
432markup language - think of LaTeX or html, but much simpler. You can add
433text to your notebook with Markdown cells by selecting
434\texttt{Cell\ \textgreater{}\ Cell\ Type\ \textgreater{}\ Markdown}.
435
436You can also include some LaTeX code in Markdown cells, with dollar
437signs \$ or align environments:
438
439\begin{align*}
440\frac{(x+y)^2}{x+1} = \frac{x^2+y^2}{x+1}
441\end{align*}
442
443When you are done writing a Markdown cell, you can run it to see the
444well-formatted text. To edit the text again, double-click on the cell.
445Try doing it now to fix the formula above!
446
447 \hypertarget{symbolic-expressions}{%
448\section{Symbolic expressions}\label{symbolic-expressions}}
449
450\textbf{Reference:}
451{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{2}{]}
452
453Now, let's get started with Sage. One thing you might want to do is
454manipulating symbolic expressions, like the following:
455
456 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
457\prompt{In}{incolor}{3}{\boxspacing}
458\begin{Verbatim}[commandchars=\\\{\}]
459\PY{n}{f} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x} \PY{o}{\PYZhy{}} \PY{l+m+mi}{5} \PY{o}{==} \PY{l+m+mi}{0}
460\PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{p}{,}\PY{n}{x}\PY{p}{)}
461\end{Verbatim}
462\end{tcolorbox}
463
464 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
465\prompt{Out}{outcolor}{3}{\boxspacing}
466\begin{Verbatim}[commandchars=\\\{\}]
467[x == -sqrt(6) - 1, x == sqrt(6) - 1]
468\end{Verbatim}
469\end{tcolorbox}
470
471 Notice that the single \texttt{=} is part of an assignment, as in
472Python: we are \emph{assigning} to the variable \texttt{f} the value
473\texttt{x\^{}2\ +\ 2*x\ -\ 5\ \textgreater{}=\ 0}, which in this case is
474an equation, so it contains the symbol \texttt{==}. Keep in mind the
475difference between the two!
476
477\textbf{Exercise:} change the code above to solve the corresponding
478inequality \(x^2+2x-5\geq 0\).
479
480 \hypertarget{mathematical-variables}{%
481\subsection{Mathematical variables}\label{mathematical-variables}}
482
483Last time we saw what \emph{variables} are in Python, and that they are
484a little bit different from the \emph{Mathematical variables} that you
485use in Mathematics. In Sage, both concepts are present, but they are
486still distinct. For example in the cell above \texttt{f} is a variable
487in the sense of computer science, while \texttt{x} is a Mathematical
488variable.
489
490If you want to use Mathematical variables other than \texttt{x}, you
491first need to \emph{declare} them with the \texttt{var()} command:
492
493 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
494\prompt{In}{incolor}{14}{\boxspacing}
495\begin{Verbatim}[commandchars=\\\{\}]
496\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
497\PY{n}{solve}\PY{p}{(}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{o}{*}\PY{n}{y} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2} \PY{o}{==} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{y}\PY{p}{)}
498\end{Verbatim}
499\end{tcolorbox}
500
501 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
502\prompt{Out}{outcolor}{14}{\boxspacing}
503\begin{Verbatim}[commandchars=\\\{\}]
504[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 +
5059) - 1/2]
506\end{Verbatim}
507\end{tcolorbox}
508
509 Try removing the first line in the cell above and see what error you
510get!
511
512Here is another example:
513
514 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
515\prompt{In}{incolor}{16}{\boxspacing}
516\begin{Verbatim}[commandchars=\\\{\}]
517\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
518\PY{n}{f} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{+}\PY{n}{a}\PY{o}{*}\PY{n}{x}\PY{o}{+}\PY{n}{b}
519\PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{p}{,}\PY{n}{x}\PY{p}{)}
520\end{Verbatim}
521\end{tcolorbox}
522
523 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
524\prompt{Out}{outcolor}{16}{\boxspacing}
525\begin{Verbatim}[commandchars=\\\{\}]
526[x == -1/2*a - 1/2*sqrt(a\^{}2 - 4*b), x == -1/2*a + 1/2*sqrt(a\^{}2 - 4*b)]
527\end{Verbatim}
528\end{tcolorbox}
529
530 Some common constants are
531\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{already
532defined} in Sage:
533
534 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
535\prompt{In}{incolor}{17}{\boxspacing}
536\begin{Verbatim}[commandchars=\\\{\}]
537\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{n}{pi}\PY{o}{*}\PY{n}{I}\PY{p}{)}
538\end{Verbatim}
539\end{tcolorbox}
540
541 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
542\prompt{Out}{outcolor}{17}{\boxspacing}
543\begin{Verbatim}[commandchars=\\\{\}]
544-1
545\end{Verbatim}
546\end{tcolorbox}
547
548 We will study symbolic expressions more in detail next time, in the
549context of calculus/analysis.
550
551 \hypertarget{basic-rings-and-fields}{%
552\section{Basic rings and fields}\label{basic-rings-and-fields}}
553
554\textbf{References:}
555{[}\href{https://doc.sagemath.org/html/en/reference/rings_standard/index.html}{3}{]}
556{[}\href{https://doc.sagemath.org/html/en/reference/rings_numerical/index.html}{4}{]}
557{[}\href{https://doc.sagemath.org/html/en/reference/finite_rings/index.html}{5}{]}
558
559As you should know, a \emph{field} is a Mathematical structure with two
560operations, addition and multiplication, which respect certain rules
561(distributivity, associativity, commutativity\ldots). Some examples of
562fields are the Rational numbers \(\mathbb Q\), the Real numbers
563\(\mathbb R\) and the Complex numbers \(\mathbb C\), but there are many
564more. As you should also know, a \emph{(commutative) ring} is like a
565field, except not all elements different from \(0\) need have a
566multiplicative inverse. For example the integers
567\(\mathbb Z = \{ \dots, -1, 0, 1, 2, \dots\}\) are a ring, but not a
568field.
569
570These structures are already implemented in Sage. Some of the most
571common are listed in the following table:
572
573\begin{longtable}[]{@{}rcl@{}}
574\toprule
575Mathematical object & Math symbol & Sage name \\
576\midrule
577\endhead
578Integers & \(\mathbb Z\) & \texttt{ZZ} \\
579Rational numbers & \(\mathbb Q\) & \texttt{QQ} \\
580Real numbers & \(\mathbb R\) & \texttt{RR} \\
581Complex numbers & \(\mathbb C\) & \texttt{CC} \\
582Integers modulo \(n\) & \(\mathbb Z/n\mathbb Z\) &
583\texttt{Integers(n)} \\
584Finite fields & \(\mathbb F_p\) & GF(p) \\
585\(\dots\) & \(\dots\) & \(\dots\) \\
586\bottomrule
587\end{longtable}
588
589 If you write a number or an expression, Sage will figure out where it
590``lives'', choosing the most restrictive interpretation possible. For
591example \texttt{3} will be interpreted to be an integer, even if it is
592also a rational number, a real number and a complex number.
593
594 \hypertarget{parents-and-coercion}{%
595\subsection{Parents and coercion}\label{parents-and-coercion}}
596
597\textbf{Reference:}
598{[}\href{https://doc.sagemath.org/html/en/tutorial/tour_coercion.html}{6}{]}
599
600You can check where an object ``lives'' with the \texttt{parent()}
601command. It works more or less like the Python command \texttt{type()},
602but it gives a more Mathematically inclined answer. Check the reference
603link {[}6{]} above if you want more details.
604
605 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
606\prompt{In}{incolor}{18}{\boxspacing}
607\begin{Verbatim}[commandchars=\\\{\}]
608\PY{c+c1}{\PYZsh{}Edit this cell to find out the type of other objects that we used}
609\PY{n}{parent}\PY{p}{(}\PY{l+m+mi}{3}\PY{o}{/}\PY{l+m+mi}{5}\PY{p}{)}
610\end{Verbatim}
611\end{tcolorbox}
612
613 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
614\prompt{Out}{outcolor}{18}{\boxspacing}
615\begin{Verbatim}[commandchars=\\\{\}]
616Rational Field
617\end{Verbatim}
618\end{tcolorbox}
619
620 Sometimes Sage does not give you the best possible interpretation, so
621you can force something to be interpreted as living in a smaller ring as
622follows:
623
624 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
625\prompt{In}{incolor}{4}{\boxspacing}
626\begin{Verbatim}[commandchars=\\\{\}]
627\PY{n}{minus\PYZus{}one} \PY{o}{=} \PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{n}{pi}\PY{o}{*}\PY{n}{I}\PY{p}{)}
628\PY{n}{minus\PYZus{}one\PYZus{}coerced} \PY{o}{=} \PY{n}{ZZ}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{n}{pi}\PY{o}{*}\PY{n}{I}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} coercion}
629\PY{n+nb}{print}\PY{p}{(}\PY{n}{parent}\PY{p}{(}\PY{n}{minus\PYZus{}one}\PY{p}{)}\PY{p}{)}
630\PY{n+nb}{print}\PY{p}{(}\PY{n}{parent}\PY{p}{(}\PY{n}{minus\PYZus{}one\PYZus{}coerced}\PY{p}{)}\PY{p}{)}
631\end{Verbatim}
632\end{tcolorbox}
633
634 \begin{Verbatim}[commandchars=\\\{\}]
635Symbolic Ring
636Integer Ring
637 \end{Verbatim}
638
639 \textbf{Remark.} Notice that there is a fundamental difference between
640the rings \texttt{RR} and \texttt{CC} and all the others in the table
641above: the real and complex numbers are \emph{approximated}.
642
643 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
644\prompt{In}{incolor}{1}{\boxspacing}
645\begin{Verbatim}[commandchars=\\\{\}]
646\PY{n+nb}{print}\PY{p}{(}\PY{n}{QQ}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)}
647\PY{n+nb}{print}\PY{p}{(}\PY{n}{RR}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)}
648\end{Verbatim}
649\end{tcolorbox}
650
651 \begin{Verbatim}[commandchars=\\\{\}]
6523
6533.00000000000000
654 \end{Verbatim}
655
656 You can also choose the precision of this approximation using the
657alternative name \texttt{RealField}.
658
659 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
660\prompt{In}{incolor}{4}{\boxspacing}
661\begin{Verbatim}[commandchars=\\\{\}]
662\PY{n+nb}{print}\PY{p}{(}\PY{n}{RR}\PY{p}{)}
663\PY{n+nb}{print}\PY{p}{(}\PY{n}{RealField}\PY{p}{(}\PY{n}{prec}\PY{o}{=}\PY{l+m+mi}{1000}\PY{p}{)}\PY{p}{)}
664\end{Verbatim}
665\end{tcolorbox}
666
667 \begin{Verbatim}[commandchars=\\\{\}]
668Real Field with 53 bits of precision
669Real Field with 1000 bits of precision
670 \end{Verbatim}
671
672 \hypertarget{polynomial-rings}{%
673\section{Polynomial rings}\label{polynomial-rings}}
674
675\textbf{Reference:}
676{[}\href{https://doc.sagemath.org/html/en/reference/polynomial_rings/index.html}{7}{]}
677
678If you want to work with polynomials over a certain ring it is better to
679use this specific construction, rather than the symbolic expressions
680introduced above.
681
682 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
683\prompt{In}{incolor}{5}{\boxspacing}
684\begin{Verbatim}[commandchars=\\\{\}]
685\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{,}\PY{n}{z}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{RR}\PY{p}{[}\PY{p}{]} \PY{c+c1}{\PYZsh{} Alternative: polring.\PYZlt{}x,y,z\PYZgt{} = PolynomialRing(RR)}
686\PY{n}{polring}
687\end{Verbatim}
688\end{tcolorbox}
689
690 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
691\prompt{Out}{outcolor}{5}{\boxspacing}
692\begin{Verbatim}[commandchars=\\\{\}]
693Multivariate Polynomial Ring in x, y, z over Real Field with 53 bits of
694precision
695\end{Verbatim}
696\end{tcolorbox}
697
698 You can use as many variables as you like, and you can replace
699\texttt{RR} with any ring. In the example above \texttt{polring} is just
700the name of the variable (in the computer science sense) associated with
701this polynomial ring.
702
703\hypertarget{operations-on-polynomials}{%
704\subsection{Operations on polynomials}\label{operations-on-polynomials}}
705
706The usual Mathematical operations are available on polynomial rings,
707including Euclidean division \texttt{//} and remainder \texttt{\%}.
708There is also the single-slash division \texttt{/}, but the result may
709not be a polynomial anymore.
710
711\textbf{Exercise:} use the \texttt{parent()} command to find out what
712the quotient of two polynomials is.
713
714\textbf{Question:} what happens if you remove the first line in the cell
715below? What if we used the variable \texttt{y} instead of \texttt{x}?
716
717 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
718\prompt{In}{incolor}{6}{\boxspacing}
719\begin{Verbatim}[commandchars=\\\{\}]
720\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{QQ}\PY{p}{[}\PY{p}{]}
721\PY{n}{p} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x} \PY{o}{\PYZhy{}} \PY{l+m+mi}{3} \PY{c+c1}{\PYZsh{} Don\PYZsq{}t forget * for multiplication!}
722\PY{n}{q} \PY{o}{=} \PY{n}{p} \PY{o}{/}\PY{o}{/} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}
723\PY{n}{r} \PY{o}{=} \PY{n}{p} \PY{o}{\PYZpc{}} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}
724\PY{n}{f} \PY{o}{=} \PY{n}{p} \PY{o}{/} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}
725\PY{n+nb}{print}\PY{p}{(}\PY{n}{q}\PY{p}{)}
726\PY{n+nb}{print}\PY{p}{(}\PY{n}{r}\PY{p}{)}
727\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{)}
728\end{Verbatim}
729\end{tcolorbox}
730
731 \begin{Verbatim}[commandchars=\\\{\}]
732x + 1
733-4
734(x\^{}2 + 2*x - 3)/(x + 1)
735 \end{Verbatim}
736
737 You can do more complex operations. Try out \texttt{roots()} and
738\texttt{factor} in the cell below.
739
740\textbf{Remark.} Notice how the result can change substantially if you
741change the base ring.
742
743\textbf{Remark.}
744\href{https://doc.sagemath.org/html/en/reference/structure/sage/structure/factorization.html}{Factorizations}
745are a particular object in Sage. They are kinda like a list, but not
746really. You can get a list of pairs (factor, power) with
747\texttt{list(factor(f))}.
748
749 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
750\prompt{In}{incolor}{7}{\boxspacing}
751\begin{Verbatim}[commandchars=\\\{\}]
752\PY{n}{polring\PYZus{}onevar}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{t}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{QQ}\PY{p}{[}\PY{p}{]}
753
754\PY{n}{f} \PY{o}{=} \PY{n}{t}\PY{o}{\PYZca{}}\PY{l+m+mi}{5} \PY{o}{+} \PY{n}{t}\PY{o}{\PYZca{}}\PY{l+m+mi}{4} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{t}\PY{o}{\PYZca{}}\PY{l+m+mi}{3} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{t}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}} \PY{l+m+mi}{3}\PY{o}{*}\PY{n}{t} \PY{o}{\PYZhy{}} \PY{l+m+mi}{3}
755\PY{n+nb}{print}\PY{p}{(}\PY{n}{factor}\PY{p}{(}\PY{n}{f}\PY{p}{)}\PY{p}{)}
756\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{o}{.}\PY{n}{roots}\PY{p}{(}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} Result: list of pairs (root,multiplicity)}
757
758\PY{n}{polring\PYZus{}manyvar}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{,}\PY{n}{z}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{QQ}\PY{p}{[}\PY{p}{]}
759\PY{n}{factor}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{n}{y}\PY{o}{+}\PY{n}{x}\PY{p}{)}
760
761\PY{c+c1}{\PYZsh{} The following line gives an error, because the polynomial}
762\PY{c+c1}{\PYZsh{} is understood to possibly have many variables:}
763\PY{c+c1}{\PYZsh{}(x\PYZca{}2\PYZhy{}1).roots()}
764\end{Verbatim}
765\end{tcolorbox}
766
767 \begin{Verbatim}[commandchars=\\\{\}]
768(t + 1) * (t\^{}2 - 3) * (t\^{}2 + 1)
769[(-1, 1)]
770 \end{Verbatim}
771
772 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
773\prompt{Out}{outcolor}{7}{\boxspacing}
774\begin{Verbatim}[commandchars=\\\{\}]
775(y + 1) * x
776\end{Verbatim}
777\end{tcolorbox}
778
779 \hypertarget{matrices-and-vectors}{%
780\section{Matrices and vectors}\label{matrices-and-vectors}}
781
782\textbf{References:}
783{[}\href{https://doc.sagemath.org/html/en/reference/matrices/index.html}{8}{]},
784but in particular the subections
785{[}\href{https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/docs.html}{9}{]}
786and
787{[}\href{https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html}{10}{]}
788
789In Sage you can easily manipulate matrices and vectors
790
791 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
792\prompt{In}{incolor}{77}{\boxspacing}
793\begin{Verbatim}[commandchars=\\\{\}]
794\PY{n}{A} \PY{o}{=} \PY{n}{matrix}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{4}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{22}\PY{o}{/}\PY{l+m+mi}{7}\PY{p}{]}\PY{p}{]}\PY{p}{)}
795\PY{n}{B} \PY{o}{=} \PY{n}{matrix}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{1}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{]}\PY{p}{)}
796\PY{n}{v} \PY{o}{=} \PY{n}{vector}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}
797
798\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{} \PYZbs{}n just means \PYZdq{}newline\PYZdq{}}
799\PY{n+nb}{print}\PY{p}{(}\PY{n}{B}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
800\PY{n+nb}{print}\PY{p}{(}\PY{n}{B}\PY{o}{*}\PY{n}{v}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
801\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{B} \PY{o}{\PYZhy{}} \PY{n}{A}\PY{o}{*}\PY{n}{B}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
802
803\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Rank of A =}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{rank}\PY{p}{(}\PY{n}{A}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} You can also use A.rank()}
804\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Rank of B =}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{rank}\PY{p}{(}\PY{n}{B}\PY{p}{)}\PY{p}{)}
805\end{Verbatim}
806\end{tcolorbox}
807
808 \begin{Verbatim}[commandchars=\\\{\}]
809[ 1 2 3]
810[ 0 0 1]
811[ 4 -3 22/7]
812
813[1/2 0 0]
814[ 7 0 0]
815[ 1 1 1]
816
817(3/2, 21, 6)
818
819[ -7/2 -10 80/7]
820[ 17 -4 15/7]
821[ 241/7 -18/7 869/49]
822
823Rank of A = 3
824Rank of B = 2
825 \end{Verbatim}
826
827 \textbf{Exercise:} in the cell above, compute the determinant, inverse
828and characteristic polynomial of the matrix \texttt{A}. \emph{Hint: look
829at the reference {[}10{]} above (the functions are listed in alphabetic
830order).}
831
832As for polynomials, you can specify where a matrix or a vector lives
833
834 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
835\prompt{In}{incolor}{57}{\boxspacing}
836\begin{Verbatim}[commandchars=\\\{\}]
837\PY{n}{M} \PY{o}{=} \PY{n}{matrix}\PY{p}{(}\PY{n}{CC}\PY{p}{,} \PY{p}{[}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{]}\PY{p}{)}
838\PY{n}{parent}\PY{p}{(}\PY{n}{M}\PY{p}{)}
839\end{Verbatim}
840\end{tcolorbox}
841
842 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
843\prompt{Out}{outcolor}{57}{\boxspacing}
844\begin{Verbatim}[commandchars=\\\{\}]
845Full MatrixSpace of 2 by 2 dense matrices over Complex Field with 53 bits of
846precision
847\end{Verbatim}
848\end{tcolorbox}
849
850 You can also solve linear systems and compute eigenvalues and
851eigenvectors of a matrix
852
853\textbf{Warning.} In linear algebra there are distinct concepts of
854\emph{left} and \emph{right} eigenvalues (and eigenvector). The one you
855know is probably that of \textbf{right} eigen-\{value,vector\}, that is
856an element \(\lambda\) of the base field and a non-zero vector
857\(\mathbf v\) with \(A\mathbf v=\lambda\mathbf v\). The other concept
858corresponds to the equality \(\mathbf v^TA=\lambda \mathbf v\).
859
860 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
861\prompt{In}{incolor}{60}{\boxspacing}
862\begin{Verbatim}[commandchars=\\\{\}]
863\PY{n}{A} \PY{o}{=} \PY{n}{Matrix}\PY{p}{(}\PY{n}{RR}\PY{p}{,} \PY{p}{[}\PY{p}{[}\PY{n}{sqrt}\PY{p}{(}\PY{l+m+mi}{59}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{32}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{o}{/}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{]}\PY{p}{)}
864\PY{n}{v} \PY{o}{=} \PY{n}{vector}\PY{p}{(}\PY{n}{RR}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
865\PY{n}{A}\PY{o}{.}\PY{n}{solve\PYZus{}right}\PY{p}{(}\PY{n}{v}\PY{p}{)} \PY{c+c1}{\PYZsh{} Solve Ax=v. Alternative: A \PYZbs{} v}
866\end{Verbatim}
867\end{tcolorbox}
868
869 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
870\prompt{Out}{outcolor}{60}{\boxspacing}
871\begin{Verbatim}[commandchars=\\\{\}]
872(0.289916349448506, 0.0241596957873755)
873\end{Verbatim}
874\end{tcolorbox}
875
876 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
877\prompt{In}{incolor}{64}{\boxspacing}
878\begin{Verbatim}[commandchars=\\\{\}]
879\PY{n}{A} \PY{o}{=} \PY{n}{Matrix}\PY{p}{(}\PY{n}{QQ}\PY{p}{,} \PY{p}{[}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{]}\PY{p}{]}\PY{p}{)}
880\PY{n}{A}\PY{o}{.}\PY{n}{eigenspaces\PYZus{}right}\PY{p}{(}\PY{p}{)} \PY{c+c1}{\PYZsh{} Also: A.eigenvalues(), A.eigenvectors\PYZus{}right()}
881\end{Verbatim}
882\end{tcolorbox}
883
884 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
885\prompt{Out}{outcolor}{64}{\boxspacing}
886\begin{Verbatim}[commandchars=\\\{\}]
887[
888(-0.3722813232690144?, Vector space of degree 2 and dimension 1 over Algebraic
889Field
890User basis matrix:
891[ 1 -0.6861406616345072?]),
892(5.372281323269015?, Vector space of degree 2 and dimension 1 over Algebraic
893Field
894User basis matrix:
895[ 1 2.186140661634508?])
896]
897\end{Verbatim}
898\end{tcolorbox}
899
900 We can also extract a specific submatrix by selecting only some rows and
901columns, with a syntax similar to that of Python's lists. Check out more
902examples in the reference {[}9{]} above, and try them in the cell below.
903
904 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
905\prompt{In}{incolor}{94}{\boxspacing}
906\begin{Verbatim}[commandchars=\\\{\}]
907\PY{n}{A} \PY{o}{=} \PY{n}{MatrixSpace}\PY{p}{(}\PY{n}{ZZ}\PY{p}{,} \PY{l+m+mi}{7}\PY{p}{)}\PY{o}{.}\PY{n}{random\PYZus{}element}\PY{p}{(}\PY{p}{)}
908\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
909\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{:}\PY{l+m+mi}{5}\PY{p}{]}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{} Rows from 1 to 3, columns from 2 to 5}
910\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{:}\PY{p}{]}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{} First row, all columns}
911\PY{n+nb}{print}\PY{p}{(}\PY{n}{A}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{:}\PY{l+m+mi}{5}\PY{p}{]}\PY{p}{)} \PY{c+c1}{\PYZsh{} Rows 0, 5 and 2 (in this order) and columns 0 to 5}
912\end{Verbatim}
913\end{tcolorbox}
914
915 \begin{Verbatim}[commandchars=\\\{\}]
916[-14 2 0 -1 1 -2 -1]
917[ 0 -8 0 9 -2 11 1]
918[ 0 3 1 -1 1 1 221]
919[ -1 2 1 -25 -10 4 0]
920[ -3 0 0 2 16 -1 -2]
921[ 1 -3 3 -41 1 0 0]
922[ -2 1 0 0 -6 2 12]
923
924[ 0 9 -2]
925[ 1 -1 1]
926
927[-14 2 0 -1 1 -2 -1]
928
929[-14 2 0 -1 1]
930[ 1 -3 3 -41 1]
931[ 0 3 1 -1 1]
932 \end{Verbatim}
933
934 \textbf{Exercise:} write a sage function that computes the determinant
935of an \(n\times n\) matrix \(A=(a_{ij})\) using Laplace's rule by the
936first row, that is \begin{align*}
937 \operatorname{det}A = \sum_{j=1}^n (-1)^ja_{0j}M_{0j}
938\end{align*} where \(M_{0j}\) is the determinant of the
939\((n-1)\times(n-1)\) matrix obtained by removing the \(0\)-th row and
940the \(j\)-th column from \(A\).
941
942 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
943\prompt{In}{incolor}{91}{\boxspacing}
944\begin{Verbatim}[commandchars=\\\{\}]
945\PY{k}{def} \PY{n+nf}{my\PYZus{}det}\PY{p}{(}\PY{n}{A}\PY{p}{)}\PY{p}{:}
946 \PY{k}{if} \PY{o+ow}{not} \PY{n}{A}\PY{o}{.}\PY{n}{is\PYZus{}square}\PY{p}{(}\PY{p}{)}\PY{p}{:}
947 \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Error: matrix is not square}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
948
949 \PY{n}{n} \PY{o}{=} \PY{n}{A}\PY{o}{.}\PY{n}{nrows}\PY{p}{(}\PY{p}{)} \PY{c+c1}{\PYZsh{} size of the matrix}
950
951 \PY{c+c1}{\PYZsh{} Continue from here!}
952\end{Verbatim}
953\end{tcolorbox}
954
955 \hypertarget{number-theory}{%
956\section{Number Theory}\label{number-theory}}
957
958\textbf{Reference:}
959{[}\href{https://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/integer.html}{11}{]}
960
961Sage includes a large library of functions for computing with the
962integers, see the link above.
963
964 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
965\prompt{In}{incolor}{8}{\boxspacing}
966\begin{Verbatim}[commandchars=\\\{\}]
967\PY{n}{n} \PY{o}{=} \PY{l+m+mi}{123456789}
968\PY{n}{m} \PY{o}{=} \PY{l+m+mi}{987654321}
969\PY{n}{p} \PY{o}{=} \PY{l+m+mi}{3607}
970
971\PY{n+nb}{print}\PY{p}{(}\PY{n}{factor}\PY{p}{(}\PY{n}{n}\PY{p}{)}\PY{p}{)}
972\PY{n+nb}{print}\PY{p}{(}\PY{n}{is\PYZus{}prime}\PY{p}{(}\PY{n}{p}\PY{p}{)}\PY{p}{)}
973\PY{n+nb}{print}\PY{p}{(}\PY{n}{p}\PY{o}{.}\PY{n}{divides}\PY{p}{(}\PY{n}{n}\PY{p}{)}\PY{p}{)}
974\PY{n+nb}{print}\PY{p}{(}\PY{n}{euler\PYZus{}phi}\PY{p}{(}\PY{n}{m}\PY{p}{)}\PY{p}{)}
975\PY{n+nb}{print}\PY{p}{(}\PY{n}{gcd}\PY{p}{(}\PY{n}{n}\PY{p}{,} \PY{n}{m}\PY{p}{)}\PY{p}{)}
976\PY{n+nb}{print}\PY{p}{(}\PY{n}{lcm}\PY{p}{(}\PY{n}{n}\PY{p}{,} \PY{n}{m}\PY{p}{)}\PY{p}{)}
977\end{Verbatim}
978\end{tcolorbox}
979
980 \begin{Verbatim}[commandchars=\\\{\}]
9813\^{}2 * 3607 * 3803
982True
983True
984619703040
9859
98613548070123626141
987 \end{Verbatim}
988
989 \hypertarget{primes}{%
990\subsection{Primes}\label{primes}}
991
992\textbf{Reference:}
993{[}\href{https://doc.sagemath.org/html/en/reference/sets/sage/sets/primes.html}{12}{]}
994
995The set of prime numbers is called \texttt{Primes()}. It is like an
996infinite list: for example you can get the one-millionth prime number or
997you can use this list to create other lists. You can also check what the
998first prime number larger than a given number is.
999
1000 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1001\prompt{In}{incolor}{9}{\boxspacing}
1002\begin{Verbatim}[commandchars=\\\{\}]
1003\PY{n}{PP} \PY{o}{=} \PY{n}{Primes}\PY{p}{(}\PY{p}{)}
1004\PY{n+nb}{print}\PY{p}{(}\PY{n}{PP}\PY{p}{)}
1005\PY{n+nb}{print}\PY{p}{(}\PY{n}{PP}\PY{p}{[}\PY{l+m+mi}{10}\PY{p}{]}\PY{p}{,} \PY{n}{PP}\PY{p}{[}\PY{l+m+mi}{10}\PY{o}{\PYZca{}}\PY{l+m+mi}{6}\PY{p}{]}\PY{p}{)}
1006\PY{n+nb}{print}\PY{p}{(}\PY{n}{PP}\PY{o}{.}\PY{n}{next}\PY{p}{(}\PY{l+m+mi}{44}\PY{p}{)}\PY{p}{)}
1007
1008\PY{n}{First\PYZus{}Thousand\PYZus{}Primes} \PY{o}{=} \PY{n}{PP}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{:}\PY{l+m+mi}{1000}\PY{p}{]}
1009\PY{n+nb}{print}\PY{p}{(}\PY{p}{[}\PY{n}{p} \PY{k}{for} \PY{n}{p} \PY{o+ow}{in} \PY{n}{First\PYZus{}Thousand\PYZus{}Primes} \PY{k}{if} \PY{n}{p} \PY{o}{\PYZlt{}} \PY{l+m+mi}{100} \PY{o+ow}{and} \PY{n}{p} \PY{o}{\PYZgt{}} \PY{l+m+mi}{75}\PY{p}{]}\PY{p}{)}
1010\end{Verbatim}
1011\end{tcolorbox}
1012
1013 \begin{Verbatim}[commandchars=\\\{\}]
1014Set of all prime numbers: 2, 3, 5, 7, {\ldots}
101531 15485867
101647
1017[79, 83, 89, 97]
1018 \end{Verbatim}
1019
1020 \hypertarget{the-chinese-remainder-theorem-crt}{%
1021\subsection{The Chinese remainder theorem
1022(CRT)}\label{the-chinese-remainder-theorem-crt}}
1023
1024We say that two integers \(a\) and \(b\) are \emph{congruent} modulo
1025another integer \(n>0\) if they have the same remainder when divided by
1026\(n\). We denote this by \(a\equiv b\pmod n\), or in Python/Sage syntax
1027\texttt{a\ \%\ n\ ==\ b\ \%\ n}.
1028
1029The Chinese remainder theorem states that if \(a,b\in\mathbb Z\) and
1030\(n,m\in \mathbb Z_{>0}\) are such that \(\gcd(n,m)=1\) then the system
1031of congruences
1032
1033\begin{align*}
1034\begin{cases}
1035 x \equiv a \pmod n\\
1036 x \equiv b \pmod m
1037\end{cases}
1038\end{align*}
1039
1040has exactly one solution modulo \(mn\). This means that there is one and
1041only one number \(x\) with \(0\leq x<mn\) such that \(x\equiv a\pmod n\)
1042and \(x\equiv b\pmod m\).
1043
1044The procedure to find such a number is not too hard to describe (you
1045might see it in an algebra or number theory course), but it can be a bit
1046long. Luckily, Sage can do this for you:
1047
1048 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1049\prompt{In}{incolor}{10}{\boxspacing}
1050\begin{Verbatim}[commandchars=\\\{\}]
1051\PY{n}{a} \PY{o}{=} \PY{l+m+mi}{2}
1052\PY{n}{b} \PY{o}{=} \PY{o}{\PYZhy{}}\PY{l+m+mi}{1}
1053\PY{n}{n} \PY{o}{=} \PY{l+m+mi}{172}
1054\PY{n}{m} \PY{o}{=} \PY{l+m+mi}{799}
1055
1056\PY{k}{if} \PY{n}{gcd}\PY{p}{(}\PY{n}{n}\PY{p}{,}\PY{n}{m}\PY{p}{)} \PY{o}{!=} \PY{l+m+mi}{1}\PY{p}{:}
1057 \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{The numbers are not comprime, I can}\PY{l+s+s2}{\PYZsq{}}\PY{l+s+s2}{t solve this!}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
1058\PY{k}{else}\PY{p}{:}
1059 \PY{n}{x} \PY{o}{=} \PY{n}{crt}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{n}\PY{p}{,} \PY{n}{m}\PY{p}{)}
1060 \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{x}\PY{o}{\PYZpc{}}\PY{k}{n}, x\PYZpc{}m)
1061\end{Verbatim}
1062\end{tcolorbox}
1063
1064 \begin{Verbatim}[commandchars=\\\{\}]
106574306 2 798
1066 \end{Verbatim}
1067
1068 \textbf{Exercise.} There is a more general version of the Chinese
1069remainder theorem which says that if
1070\(a_0, a_1, \dots, a_k\in\mathbb Z\) and
1071\(n_0, n_2, \dots, n_k\in\mathbb Z_{>0}\) are such that
1072\(\gcd(n_i, n_j)=1\) for \(i\neq j\), then the system of congruences
1073
1074\begin{align*}
1075\begin{cases}
1076 x \equiv a_0 \pmod {n_0}\\
1077 x \equiv a_1 \pmod {n_1}\\
1078 \dots \\
1079 x \equiv a_k \pmod {n_k}
1080\end{cases}
1081\end{align*}
1082
1083has exactly one solution modulo \(\prod_{i=0}^kn_i\). Use the
1084\texttt{crt()} function to find a solution to such a system. *Hint:
1085start by running the command \texttt{help(crt)}.
1086
1087 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1088\prompt{In}{incolor}{127}{\boxspacing}
1089\begin{Verbatim}[commandchars=\\\{\}]
1090\PY{c+c1}{\PYZsh{}help(crt)}
1091\end{Verbatim}
1092\end{tcolorbox}
1093
1094 \hypertarget{cryptography-rsa}{%
1095\section{Cryptography: RSA}\label{cryptography-rsa}}
1096
1097\href{https://en.wikipedia.org/wiki/Cryptography}{Cryptography} is the
1098discipline that studies methods to communicate secrets in such a way
1099that any unauthorized listener would not be able to understand the
1100message.
1101
1102A simple cryptographic protocol could be changing every letter of your
1103text following a fixed scheme (or \emph{cypher}), for example by turning
1104every A into a B, every B into a C and so on. However this is not a very
1105secure method, for many reasons. One of them is that at some point the
1106people who want to communicate need to agree on what method to use, and
1107anyone listening to that conversation would be able to decypher every
1108subsequent conversation. A public-key cryptographic protocol solves this
1109problem.
1110
1111\hypertarget{public-key-cryptography}{%
1112\subsection{Public-key cryptography}\label{public-key-cryptography}}
1113
1114Public-key cryptographic protocols, such as RSA, work like this: there
1115are two keys, a \emph{private} key that is only known to person A
1116(traditionally called Alice in every example), and a \emph{public} key
1117that does not need to be secret.
1118
1119The public key is used to \emph{encrypt} the message (that is to
1120``lock'' it, or ``hyde'' it), but one needs the private key to
1121\emph{decrypt} it. Imagine having two keys for your door, but one can
1122only be used to lock it, while the other only to open it.
1123
1124The message exchange works like this: suppose that person B (Bob) wants
1125to send a secret message to Alice. Then Alice secretely generates a
1126private and a public key and sends only the public one to Bob. Now Bob
1127encrypts the message and sends it to Alice, who can use her private key
1128to decrypt it. Even if Eve (short for \emph{eavesdropper}, an
1129unauthorized listener) listens to every message exchanged, she won't be
1130able to decypher the secret: the private key has never left Alice's
1131house!
1132
1133Notice that such a protocol is \emph{asymmetric}: if Alice wanted to
1134send a secret to Bob in reply, Bob would need to generate a pair of keys
1135of his own.
1136
1137Let's see how we can do this in practice, using number theory!
1138
1139\hypertarget{rsa}{%
1140\subsection{RSA}\label{rsa}}
1141
1142As many other cryptography protocols, RSA is based on a Mathematical
1143process that is easy to do in one direction, but very hard to invert. In
1144this case the hard process is integer factorization, that is decomposing
1145an integer number as a product of primes.
1146
1147 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1148\prompt{In}{incolor}{2}{\boxspacing}
1149\begin{Verbatim}[commandchars=\\\{\}]
1150\PY{n}{p} \PY{o}{=} \PY{l+m+mi}{100003100019100043100057100069}
1151\PY{n}{q} \PY{o}{=} \PY{l+m+mi}{100144655312449572059845328443}
1152\PY{n}{n} \PY{o}{=} \PY{n}{p}\PY{o}{*}\PY{n}{q}
1153\PY{n+nb}{print}\PY{p}{(}\PY{n}{is\PYZus{}prime}\PY{p}{(}\PY{n}{p}\PY{p}{)}\PY{p}{,} \PY{n}{is\PYZus{}prime}\PY{p}{(}\PY{n}{q}\PY{p}{)}\PY{p}{,} \PY{n}{is\PYZus{}prime}\PY{p}{(}\PY{n}{p}\PY{o}{*}\PY{n}{q}\PY{p}{)}\PY{p}{)}
1154
1155\PY{c+c1}{\PYZsh{} Use the command below to see how long it takes}
1156\PY{c+c1}{\PYZsh{}timeit(\PYZdq{}factor(n)\PYZdq{}, number=1, repeat=1)}
1157\end{Verbatim}
1158\end{tcolorbox}
1159
1160 \begin{Verbatim}[commandchars=\\\{\}]
1161True True False
1162 \end{Verbatim}
1163
1164 In order to generate the keys, Alice picks a number \(n\) which is the
1165product of two large primes \(p\) and \(q\) of more or less the same
1166size. Finding such primes is relatively easy compared to factoring the
1167number \(n\) she obtained. Then she computes the Euler totient
1168\(\varphi(n)=(p-1)(q-1)\) of \(n\), which she can do because she knows
1169that \(n=pq\) - it would be impossible otherwise!
1170
1171Then Alice can compute two integers \((d,e)\) such that
1172\(de\equiv 1\pmod{\varphi(n)}\). She will send the numbers \(n\) and
1173\(d\) to Bob and keep \(e\) secret. In this case the public key is the
1174pair \((n,d)\), while \(e\) is the private key.
1175
1176Of course, she does all of this using Sage!
1177
1178 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1179\prompt{In}{incolor}{105}{\boxspacing}
1180\begin{Verbatim}[commandchars=\\\{\}]
1181\PY{k}{def} \PY{n+nf}{two\PYZus{}large\PYZus{}primes}\PY{p}{(}\PY{p}{)}\PY{p}{:}
1182 \PY{n}{p}\PY{p}{,} \PY{n}{q} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}
1183 \PY{c+c1}{\PYZsh{} We make sure that they are different}
1184 \PY{k}{while} \PY{n}{p} \PY{o}{==} \PY{n}{q}\PY{p}{:}
1185 \PY{n}{p} \PY{o}{=} \PY{n}{Primes}\PY{p}{(}\PY{p}{)}\PY{p}{[}\PY{n}{randint}\PY{p}{(}\PY{l+m+mi}{10}\PY{o}{\PYZca{}}\PY{l+m+mi}{6}\PY{p}{,} \PY{l+m+mi}{2}\PY{o}{*}\PY{l+m+mi}{10}\PY{o}{\PYZca{}}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{]}
1186 \PY{n}{q} \PY{o}{=} \PY{n}{Primes}\PY{p}{(}\PY{p}{)}\PY{p}{[}\PY{n}{randint}\PY{p}{(}\PY{l+m+mi}{10}\PY{o}{\PYZca{}}\PY{l+m+mi}{6}\PY{p}{,} \PY{l+m+mi}{2}\PY{o}{*}\PY{l+m+mi}{10}\PY{o}{\PYZca{}}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{]}
1187 \PY{k}{return} \PY{n}{p}\PY{p}{,} \PY{n}{q}
1188
1189\PY{k}{def} \PY{n+nf}{random\PYZus{}unit\PYZus{}mod}\PY{p}{(}\PY{n}{N}\PY{p}{)}\PY{p}{:}
1190 \PY{n}{R} \PY{o}{=} \PY{n}{Integers}\PY{p}{(}\PY{n}{N}\PY{p}{)}
1191 \PY{n}{d} \PY{o}{=} \PY{n}{R}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{)}
1192 \PY{c+c1}{\PYZsh{} We make sure that it is invertible}
1193 \PY{k}{while} \PY{o+ow}{not} \PY{n}{d}\PY{o}{.}\PY{n}{is\PYZus{}unit}\PY{p}{(}\PY{p}{)}\PY{p}{:}
1194 \PY{n}{d} \PY{o}{=} \PY{n}{R}\PY{o}{.}\PY{n}{random\PYZus{}element}\PY{p}{(}\PY{p}{)}
1195 \PY{k}{return} \PY{n}{d}
1196
1197\PY{k}{def} \PY{n+nf}{Alice\PYZus{}generate\PYZus{}keys}\PY{p}{(}\PY{p}{)}\PY{p}{:}
1198 \PY{n}{p}\PY{p}{,} \PY{n}{q} \PY{o}{=} \PY{n}{two\PYZus{}large\PYZus{}primes}\PY{p}{(}\PY{p}{)}
1199 \PY{n}{n} \PY{o}{=} \PY{n}{p}\PY{o}{*}\PY{n}{q}
1200 \PY{n}{phi\PYZus{}n} \PY{o}{=} \PY{p}{(}\PY{n}{p}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{)}\PY{o}{*}\PY{p}{(}\PY{n}{q}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{)} \PY{c+c1}{\PYZsh{} euler\PYZus{}phi(n) is slow!}
1201
1202 \PY{n}{d} \PY{o}{=} \PY{n}{random\PYZus{}unit\PYZus{}mod}\PY{p}{(}\PY{n}{phi\PYZus{}n}\PY{p}{)}
1203 \PY{n}{e} \PY{o}{=} \PY{n}{d}\PY{o}{\PYZca{}}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}
1204 \PY{k}{return} \PY{n}{n}\PY{p}{,} \PY{n}{d}\PY{p}{,} \PY{n}{e}
1205
1206\PY{n}{Alice\PYZus{}generate\PYZus{}keys}\PY{p}{(}\PY{p}{)}
1207\end{Verbatim}
1208\end{tcolorbox}
1209
1210 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1211\prompt{Out}{outcolor}{105}{\boxspacing}
1212\begin{Verbatim}[commandchars=\\\{\}]
1213(419199544978969, 235530823946467, 80799425863927)
1214\end{Verbatim}
1215\end{tcolorbox}
1216
1217 Now, how does Bob encrypt his message? Let's say he wants to send to
1218Alice the number \(m\) with \(1<m<n\) (In practice he would like to send
1219her some text with emojis, or maybe a voice message; but for computers
1220everything is a number, and there are different ways to translate any
1221sort of information to a number. He just chooses one of the many
1222standard methods that already exist, no cryptography is needed in this
1223step. If the message \(m\) is too long, he can split it up in some
1224pieces and repeat the process multiple times.)
1225
1226Now he computes \(m^d\pmod n\) and sends it back to Alice.
1227
1228 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1229\prompt{In}{incolor}{3}{\boxspacing}
1230\begin{Verbatim}[commandchars=\\\{\}]
1231\PY{k}{def} \PY{n+nf}{Bob\PYZus{}encrypt}\PY{p}{(}\PY{n}{m}\PY{p}{,} \PY{n}{n}\PY{p}{,} \PY{n}{d}\PY{p}{)}\PY{p}{:}
1232 \PY{n}{R} \PY{o}{=} \PY{n}{Integers}\PY{p}{(}\PY{n}{n}\PY{p}{)}
1233 \PY{k}{return} \PY{n}{R}\PY{p}{(}\PY{n}{m}\PY{p}{)}\PY{o}{\PYZca{}}\PY{n}{d} \PY{c+c1}{\PYZsh{} Assume that n is large enough}
1234
1235\PY{n}{message} \PY{o}{=} \PY{l+m+mi}{42424242}
1236\PY{n}{Bob\PYZus{}encrypt}\PY{p}{(}\PY{n}{message}\PY{p}{,} \PY{l+m+mi}{419199544978969}\PY{p}{,} \PY{l+m+mi}{235530823946467}\PY{p}{)}
1237\end{Verbatim}
1238\end{tcolorbox}
1239
1240 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1241\prompt{Out}{outcolor}{3}{\boxspacing}
1242\begin{Verbatim}[commandchars=\\\{\}]
1243149461597163501
1244\end{Verbatim}
1245\end{tcolorbox}
1246
1247 Since \(de\equiv 1\pmod{\varphi(n)}\), it follows that
1248\((m^d)^e\equiv m\pmod n\) (see
1249\href{https://en.wikipedia.org/wiki/Euler\%27s_theorem}{Wikipedia:
1250Euler's theorem}). So for Alice it is very easy to get back the original
1251message:
1252
1253 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1254\prompt{In}{incolor}{108}{\boxspacing}
1255\begin{Verbatim}[commandchars=\\\{\}]
1256\PY{k}{def} \PY{n+nf}{Alice\PYZus{}decrypt}\PY{p}{(}\PY{n}{m\PYZus{}encrypted}\PY{p}{,} \PY{n}{n}\PY{p}{,} \PY{n}{e}\PY{p}{)}\PY{p}{:}
1257 \PY{n}{R} \PY{o}{=} \PY{n}{Integers}\PY{p}{(}\PY{n}{n}\PY{p}{)}
1258 \PY{k}{return} \PY{n}{R}\PY{p}{(}\PY{n}{m\PYZus{}encrypted}\PY{p}{)}\PY{o}{\PYZca{}}\PY{n}{e}
1259
1260\PY{n}{Alice\PYZus{}decrypt}\PY{p}{(}\PY{l+m+mi}{149461597163501}\PY{p}{,} \PY{l+m+mi}{419199544978969}\PY{p}{,} \PY{l+m+mi}{80799425863927}\PY{p}{)}
1261\end{Verbatim}
1262\end{tcolorbox}
1263
1264 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1265\prompt{Out}{outcolor}{108}{\boxspacing}
1266\begin{Verbatim}[commandchars=\\\{\}]
126742424242
1268\end{Verbatim}
1269\end{tcolorbox}
1270
1271 Another assumption on which RSA relies is that even if one knows
1272\(M=m^e\) and \(e\), extracting the \(e\)-th root of \(M\) modulo \(n\)
1273(and thus obtaining \(m\)) is very hard. Currently the best known way to
1274do this is by factorizing \(n\) first, which is considered to be a very
1275hard problem. However, there is no proof that faster algorithms can't be
1276devised.
1277
1278Moreover, one day we will overcome the current technological
1279difficulties and quantum computers will be available. Quantum computers
1280are not just ``more powerful'' than classical hardware, but they work
1281based on completely different logical foundations and they make the
1282factorization problem much easier to solve: for example
1283\href{https://en.wikipedia.org/wiki/Shor\%27s_algorithm}{Shor's
1284algorithm} takes advantage of this different logic and can factorize
1285numbers quickly, if run on a quantum computer.
1286
1287To this day the largest number factorized with a quantum computer is
1288\(21=3\times 7\). Nonetheless, quantum-safe cryptography protocols
1289(i.e.~based on problems that are hard to solve also with quantum
1290computers) have already been developed.
1291
1292
1293 % Add a bibliography block to the postdoc
1294
1295
1296
1297\end{document}

Generated with cgit - Back to sebastiano.tronto.net