From d6c61d988bfa4255baf9cdae42db59ebee38363f Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 25 May 2021 17:10:49 +0200 Subject: Added files --- src/Lecture5/notebook/7-SageAlgebra.tex | 1297 +++++++++++++++++++++++++++++++ 1 file changed, 1297 insertions(+) create mode 100644 src/Lecture5/notebook/7-SageAlgebra.tex (limited to 'src/Lecture5/notebook/7-SageAlgebra.tex') 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 @@ +\documentclass[11pt]{article} + + \usepackage[breakable]{tcolorbox} + \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour) + + \usepackage{iftex} + \ifPDFTeX + \usepackage[T1]{fontenc} + \usepackage{mathpazo} + \else + \usepackage{fontspec} + \fi + + % Basic figure setup, for now with no caption control since it's done + % automatically by Pandoc (which extracts ![](path) syntax from Markdown). + \usepackage{graphicx} + % Maintain compatibility with old templates. Remove in nbconvert 6.0 + \let\Oldincludegraphics\includegraphics + % Ensure that by default, figures have no caption (until we provide a + % proper Figure object with a Caption API and a way to capture that + % in the conversion process - todo). + \usepackage{caption} + \DeclareCaptionFormat{nocaption}{} + \captionsetup{format=nocaption,aboveskip=0pt,belowskip=0pt} + + \usepackage[Export]{adjustbox} % Used to constrain images to a maximum size + \adjustboxset{max size={0.9\linewidth}{0.9\paperheight}} + \usepackage{float} + \floatplacement{figure}{H} % forces figures to be placed at the correct location + \usepackage{xcolor} % Allow colors to be defined + \usepackage{enumerate} % Needed for markdown enumerations to work + \usepackage{geometry} % Used to adjust the document margins + \usepackage{amsmath} % Equations + \usepackage{amssymb} % Equations + \usepackage{textcomp} % defines textquotesingle + % Hack from http://tex.stackexchange.com/a/47451/13684: + \AtBeginDocument{% + \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code + } + \usepackage{upquote} % Upright quotes for verbatim code + \usepackage{eurosym} % defines \euro + \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support + \usepackage{fancyvrb} % verbatim replacement that allows latex + \usepackage{grffile} % extends the file name processing of package graphics + % to support a larger range + \makeatletter % fix for grffile with XeLaTeX + \def\Gread@@xetex#1{% + \IfFileExists{"\Gin@base".bb}% + {\Gread@eps{\Gin@base.bb}}% + {\Gread@@xetex@aux#1}% + } + \makeatother + + % The hyperref package gives us a pdf with properly built + % internal navigation ('pdf bookmarks' for the table of contents, + % internal cross-reference links, web links for URLs, etc.) + \usepackage{hyperref} + % The default LaTeX title has an obnoxious amount of whitespace. By default, + % titling removes some of it. It also provides customization options. + \usepackage{titling} + \usepackage{longtable} % longtable support required by pandoc >1.10 + \usepackage{booktabs} % table support for pandoc > 1.12.2 + \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment) + \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout) + % normalem makes italics be italics, not underlines + \usepackage{mathrsfs} + + + + % Colors for the hyperref package + \definecolor{urlcolor}{rgb}{0,.145,.698} + \definecolor{linkcolor}{rgb}{.71,0.21,0.01} + \definecolor{citecolor}{rgb}{.12,.54,.11} + + % ANSI colors + \definecolor{ansi-black}{HTML}{3E424D} + \definecolor{ansi-black-intense}{HTML}{282C36} + \definecolor{ansi-red}{HTML}{E75C58} + \definecolor{ansi-red-intense}{HTML}{B22B31} + \definecolor{ansi-green}{HTML}{00A250} + \definecolor{ansi-green-intense}{HTML}{007427} + \definecolor{ansi-yellow}{HTML}{DDB62B} + \definecolor{ansi-yellow-intense}{HTML}{B27D12} + \definecolor{ansi-blue}{HTML}{208FFB} + \definecolor{ansi-blue-intense}{HTML}{0065CA} + \definecolor{ansi-magenta}{HTML}{D160C4} + \definecolor{ansi-magenta-intense}{HTML}{A03196} + \definecolor{ansi-cyan}{HTML}{60C6C8} + \definecolor{ansi-cyan-intense}{HTML}{258F8F} + \definecolor{ansi-white}{HTML}{C5C1B4} + \definecolor{ansi-white-intense}{HTML}{A1A6B2} + \definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF} + \definecolor{ansi-default-inverse-bg}{HTML}{000000} + + % commands and environments needed by pandoc snippets + % extracted from the output of `pandoc -s` + \providecommand{\tightlist}{% + \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} + \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} + % Add ',fontsize=\small' for more characters per line + \newenvironment{Shaded}{}{} + \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} + \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} + \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} + \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} + \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} + \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} + \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} + \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} + \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} + \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} + \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} + \newcommand{\RegionMarkerTok}[1]{{#1}} + \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} + \newcommand{\NormalTok}[1]{{#1}} + + % Additional commands for more recent versions of Pandoc + \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}} + \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} + \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} + \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}} + \newcommand{\ImportTok}[1]{{#1}} + \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}} + \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} + \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} + \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}} + \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} + \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}} + \newcommand{\BuiltInTok}[1]{{#1}} + \newcommand{\ExtensionTok}[1]{{#1}} + \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}} + \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}} + \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} + \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} + + + % Define a nice break command that doesn't care if a line doesn't already + % exist. + \def\br{\hspace*{\fill} \\* } + % Math Jax compatibility definitions + \def\gt{>} + \def\lt{<} + \let\Oldtex\TeX + \let\Oldlatex\LaTeX + \renewcommand{\TeX}{\textrm{\Oldtex}} + \renewcommand{\LaTeX}{\textrm{\Oldlatex}} + % Document parameters + % Document title + \title{Algebra and Cryptography with SageMath} + \date{2021-04-23} + \author{Sebastiano Tronto - \texttt{sebastiano.tronto@uni.lu}} + + + + + +% Pygments definitions +\makeatletter +\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% + \let\PY@ul=\relax \let\PY@tc=\relax% + \let\PY@bc=\relax \let\PY@ff=\relax} +\def\PY@tok#1{\csname PY@tok@#1\endcsname} +\def\PY@toks#1+{\ifx\relax#1\empty\else% + \PY@tok{#1}\expandafter\PY@toks\fi} +\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% + \PY@it{\PY@bf{\PY@ff{#1}}}}}}} +\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} + +\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} +\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} +\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} +\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} +\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} +\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} +\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} +\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} +\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} +\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} +\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} +\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} +\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} +\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} +\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} +\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} +\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} +\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}}} +\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} + +\def\PYZbs{\char`\\} +\def\PYZus{\char`\_} +\def\PYZob{\char`\{} +\def\PYZcb{\char`\}} +\def\PYZca{\char`\^} +\def\PYZam{\char`\&} +\def\PYZlt{\char`\<} +\def\PYZgt{\char`\>} +\def\PYZsh{\char`\#} +\def\PYZpc{\char`\%} +\def\PYZdl{\char`\$} +\def\PYZhy{\char`\-} +\def\PYZsq{\char`\'} +\def\PYZdq{\char`\"} +\def\PYZti{\char`\~} +% for compatibility with earlier versions +\def\PYZat{@} +\def\PYZlb{[} +\def\PYZrb{]} +\makeatother + + + % For linebreaks inside Verbatim environment from package fancyvrb. + \makeatletter + \newbox\Wrappedcontinuationbox + \newbox\Wrappedvisiblespacebox + \newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}} + \newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}} + \newcommand*\Wrappedcontinuationindent {3ex } + \newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox} + % Take advantage of the already applied Pygments mark-up to insert + % potential linebreaks for TeX processing. + % {, <, #, %, $, ' and ": go to next line. + % _, }, ^, &, >, - and ~: stay at end of broken line. + % Use of \textquotesingle for straight quote. + \newcommand*\Wrappedbreaksatspecials {% + \def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}% + \def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}% + \def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}% + \def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}% + \def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}% + \def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}% + \def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}% + \def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}% + \def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}% + \def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}% + \def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}% + \def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}% + \def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}% + \def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}% + } + % Some characters . , ; ? ! / are not pygmentized. + % This macro makes them "active" and they will insert potential linebreaks + \newcommand*\Wrappedbreaksatpunct {% + \lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}% + \lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}% + \lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}% + \lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}% + \lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}% + \lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}% + \lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}% + \catcode`\.\active + \catcode`\,\active + \catcode`\;\active + \catcode`\:\active + \catcode`\?\active + \catcode`\!\active + \catcode`\/\active + \lccode`\~`\~ + } + \makeatother + + \let\OriginalVerbatim=\Verbatim + \makeatletter + \renewcommand{\Verbatim}[1][1]{% + %\parskip\z@skip + \sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}% + \sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}% + \def\FancyVerbFormatLine ##1{\hsize\linewidth + \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@ + \doublehyphendemerits\z@\finalhyphendemerits\z@ + \strut ##1\strut}% + }% + % If the linebreak is at a space, the latter will be displayed as visible + % space at end of first line, and a continuation symbol starts next line. + % Stretch/shrink are however usually zero for typewriter font. + \def\FV@Space {% + \nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font + \discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak} + {\kern\fontdimen2\font}% + }% + + % Allow breaks at special characters using \PYG... macros. + \Wrappedbreaksatspecials + % Breaks at punctuation characters . , ; ? ! and / need catcode=\active + \OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]% + } + \makeatother + + % Exact colors from NB + \definecolor{incolor}{HTML}{303F9F} + \definecolor{outcolor}{HTML}{D84315} + \definecolor{cellborder}{HTML}{CFCFCF} + \definecolor{cellbackground}{HTML}{F7F7F7} + + % prompt + \makeatletter + \newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep} + \makeatother + \newcommand{\prompt}[4]{ + \ttfamily\llap{{\color{#2}[#3]:\hspace{3pt}#4}}\vspace{-\baselineskip} + } + + + + % Prevent overflowing lines due to hard-to-break entities + \sloppy + % Setup hyperref package + \hypersetup{ + breaklinks=true, % so long urls are correctly broken across lines + colorlinks=true, + urlcolor=urlcolor, + linkcolor=linkcolor, + citecolor=citecolor, + } + % Slightly bigger margins than the latex defaults + + \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in} + + + +\begin{document} + + \maketitle + + + + + 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 \href{https://jupyter.org/}{Jupyter Notebook}. + +\hypertarget{the-jupyter-notebook}{% +\section{The Jupyter Notebook}\label{the-jupyter-notebook}} + +\textbf{Reference:} {[}\href{https://jupyter.org/documentation}{1}{]} + +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: \texttt{http://localhost:8888/notebooks...}). + +You can create a new notebook by clicking on +\texttt{New\ \textgreater{}\ SageMath\ 9.2}. You can also create a +Python 3 notebook to write Python code. + +Jupyter saves and reads files in the \texttt{.ipynb} format. If you +download the file for this lecture you can open it and follow the +examples interactively. + +\hypertarget{cells}{% +\subsection{Cells}\label{cells}} + +The notebook contains one or more \emph{interactive cells} that you can +run, like this one below: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{2}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{c+c1}{\PYZsh{} Exercise: modify this cell to use the print() command} +\PY{l+m+mi}{2}\PY{o}{+}\PY{l+m+mi}{2} +\PY{l+m+mi}{2}\PY{o}{/}\PY{l+m+mi}{5} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{2}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +2/5 +\end{Verbatim} +\end{tcolorbox} + + 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 \texttt{Insert} from the menu bar. + +Notice that only the last statement produces an output. You can force +anything to be written as output with the \texttt{print()} command, +which works like in Python. As an exercise, try to modify the cell above +to provide more output! + + \hypertarget{markdown}{% +\subsection{Markdown}\label{markdown}} + +\href{https://en.wikipedia.org/wiki/Markdown}{Markdown} is a simple +markup language - think of LaTeX or html, but much simpler. You can add +text to your notebook with Markdown cells by selecting +\texttt{Cell\ \textgreater{}\ Cell\ Type\ \textgreater{}\ Markdown}. + +You can also include some LaTeX code in Markdown cells, with dollar +signs \$ or align environments: + +\begin{align*} +\frac{(x+y)^2}{x+1} = \frac{x^2+y^2}{x+1} +\end{align*} + +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! + + \hypertarget{symbolic-expressions}{% +\section{Symbolic expressions}\label{symbolic-expressions}} + +\textbf{Reference:} +{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{2}{]} + +Now, let's get started with Sage. One thing you might want to do is +manipulating symbolic expressions, like the following: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{3}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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} +\PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{p}{,}\PY{n}{x}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{3}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +[x == -sqrt(6) - 1, x == sqrt(6) - 1] +\end{Verbatim} +\end{tcolorbox} + + Notice that the single \texttt{=} is part of an assignment, as in +Python: we are \emph{assigning} to the variable \texttt{f} the value +\texttt{x\^{}2\ +\ 2*x\ -\ 5\ \textgreater{}=\ 0}, which in this case is +an equation, so it contains the symbol \texttt{==}. Keep in mind the +difference between the two! + +\textbf{Exercise:} change the code above to solve the corresponding +inequality \(x^2+2x-5\geq 0\). + + \hypertarget{mathematical-variables}{% +\subsection{Mathematical variables}\label{mathematical-variables}} + +Last time we saw what \emph{variables} are in Python, and that they are +a little bit different from the \emph{Mathematical variables} that you +use in Mathematics. In Sage, both concepts are present, but they are +still distinct. For example in the cell above \texttt{f} is a variable +in the sense of computer science, while \texttt{x} is a Mathematical +variable. + +If you want to use Mathematical variables other than \texttt{x}, you +first need to \emph{declare} them with the \texttt{var()} command: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{14}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} +\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}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{14}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +[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] +\end{Verbatim} +\end{tcolorbox} + + Try removing the first line in the cell above and see what error you +get! + +Here is another example: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{16}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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} +\PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{p}{,}\PY{n}{x}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{16}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +[x == -1/2*a - 1/2*sqrt(a\^{}2 - 4*b), x == -1/2*a + 1/2*sqrt(a\^{}2 - 4*b)] +\end{Verbatim} +\end{tcolorbox} + + Some common constants are +\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{already +defined} in Sage: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{17}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{n}{pi}\PY{o}{*}\PY{n}{I}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{17}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +-1 +\end{Verbatim} +\end{tcolorbox} + + We will study symbolic expressions more in detail next time, in the +context of calculus/analysis. + + \hypertarget{basic-rings-and-fields}{% +\section{Basic rings and fields}\label{basic-rings-and-fields}} + +\textbf{References:} +{[}\href{https://doc.sagemath.org/html/en/reference/rings_standard/index.html}{3}{]} +{[}\href{https://doc.sagemath.org/html/en/reference/rings_numerical/index.html}{4}{]} +{[}\href{https://doc.sagemath.org/html/en/reference/finite_rings/index.html}{5}{]} + +As you should know, a \emph{field} is a Mathematical structure with two +operations, addition and multiplication, which respect certain rules +(distributivity, associativity, commutativity\ldots). 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 \emph{(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. + +These structures are already implemented in Sage. Some of the most +common are listed in the following table: + +\begin{longtable}[]{@{}rcl@{}} +\toprule +Mathematical object & Math symbol & Sage name \\ +\midrule +\endhead +Integers & \(\mathbb Z\) & \texttt{ZZ} \\ +Rational numbers & \(\mathbb Q\) & \texttt{QQ} \\ +Real numbers & \(\mathbb R\) & \texttt{RR} \\ +Complex numbers & \(\mathbb C\) & \texttt{CC} \\ +Integers modulo \(n\) & \(\mathbb Z/n\mathbb Z\) & +\texttt{Integers(n)} \\ +Finite fields & \(\mathbb F_p\) & GF(p) \\ +\(\dots\) & \(\dots\) & \(\dots\) \\ +\bottomrule +\end{longtable} + + If you write a number or an expression, Sage will figure out where it +``lives'', choosing the most restrictive interpretation possible. For +example \texttt{3} will be interpreted to be an integer, even if it is +also a rational number, a real number and a complex number. + + \hypertarget{parents-and-coercion}{% +\subsection{Parents and coercion}\label{parents-and-coercion}} + +\textbf{Reference:} +{[}\href{https://doc.sagemath.org/html/en/tutorial/tour_coercion.html}{6}{]} + +You can check where an object ``lives'' with the \texttt{parent()} +command. It works more or less like the Python command \texttt{type()}, +but it gives a more Mathematically inclined answer. Check the reference +link {[}6{]} above if you want more details. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{18}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{c+c1}{\PYZsh{}Edit this cell to find out the type of other objects that we used} +\PY{n}{parent}\PY{p}{(}\PY{l+m+mi}{3}\PY{o}{/}\PY{l+m+mi}{5}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{18}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +Rational Field +\end{Verbatim} +\end{tcolorbox} + + 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: + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{4}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{parent}\PY{p}{(}\PY{n}{minus\PYZus{}one}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{parent}\PY{p}{(}\PY{n}{minus\PYZus{}one\PYZus{}coerced}\PY{p}{)}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +Symbolic Ring +Integer Ring + \end{Verbatim} + + \textbf{Remark.} Notice that there is a fundamental difference between +the rings \texttt{RR} and \texttt{CC} and all the others in the table +above: the real and complex numbers are \emph{approximated}. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{1}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n+nb}{print}\PY{p}{(}\PY{n}{QQ}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{RR}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +3 +3.00000000000000 + \end{Verbatim} + + You can also choose the precision of this approximation using the +alternative name \texttt{RealField}. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{4}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n+nb}{print}\PY{p}{(}\PY{n}{RR}\PY{p}{)} +\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}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +Real Field with 53 bits of precision +Real Field with 1000 bits of precision + \end{Verbatim} + + \hypertarget{polynomial-rings}{% +\section{Polynomial rings}\label{polynomial-rings}} + +\textbf{Reference:} +{[}\href{https://doc.sagemath.org/html/en/reference/polynomial_rings/index.html}{7}{]} + +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. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{5}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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)} +\PY{n}{polring} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{5}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +Multivariate Polynomial Ring in x, y, z over Real Field with 53 bits of +precision +\end{Verbatim} +\end{tcolorbox} + + You can use as many variables as you like, and you can replace +\texttt{RR} with any ring. In the example above \texttt{polring} is just +the name of the variable (in the computer science sense) associated with +this polynomial ring. + +\hypertarget{operations-on-polynomials}{% +\subsection{Operations on polynomials}\label{operations-on-polynomials}} + +The usual Mathematical operations are available on polynomial rings, +including Euclidean division \texttt{//} and remainder \texttt{\%}. +There is also the single-slash division \texttt{/}, but the result may +not be a polynomial anymore. + +\textbf{Exercise:} use the \texttt{parent()} command to find out what +the quotient of two polynomials is. + +\textbf{Question:} what happens if you remove the first line in the cell +below? What if we used the variable \texttt{y} instead of \texttt{x}? + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{6}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{QQ}\PY{p}{[}\PY{p}{]} +\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!} +\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}{)} +\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}{)} +\PY{n}{f} \PY{o}{=} \PY{n}{p} \PY{o}{/} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{q}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{r}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +x + 1 +-4 +(x\^{}2 + 2*x - 3)/(x + 1) + \end{Verbatim} + + You can do more complex operations. Try out \texttt{roots()} and +\texttt{factor} in the cell below. + +\textbf{Remark.} Notice how the result can change substantially if you +change the base ring. + +\textbf{Remark.} +\href{https://doc.sagemath.org/html/en/reference/structure/sage/structure/factorization.html}{Factorizations} +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 +\texttt{list(factor(f))}. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{7}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{]} + +\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} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{factor}\PY{p}{(}\PY{n}{f}\PY{p}{)}\PY{p}{)} +\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)} + +\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}{]} +\PY{n}{factor}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{n}{y}\PY{o}{+}\PY{n}{x}\PY{p}{)} + +\PY{c+c1}{\PYZsh{} The following line gives an error, because the polynomial} +\PY{c+c1}{\PYZsh{} is understood to possibly have many variables:} +\PY{c+c1}{\PYZsh{}(x\PYZca{}2\PYZhy{}1).roots()} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +(t + 1) * (t\^{}2 - 3) * (t\^{}2 + 1) +[(-1, 1)] + \end{Verbatim} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{7}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +(y + 1) * x +\end{Verbatim} +\end{tcolorbox} + + \hypertarget{matrices-and-vectors}{% +\section{Matrices and vectors}\label{matrices-and-vectors}} + +\textbf{References:} +{[}\href{https://doc.sagemath.org/html/en/reference/matrices/index.html}{8}{]}, +but in particular the subections +{[}\href{https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/docs.html}{9}{]} +and +{[}\href{https://doc.sagemath.org/html/en/reference/matrices/sage/matrix/matrix2.html}{10}{]} + +In Sage you can easily manipulate matrices and vectors + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{77}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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}{)} +\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}{)} + +\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{}} +\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}{)} +\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}{)} +\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}{)} + +\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()} +\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}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +[ 1 2 3] +[ 0 0 1] +[ 4 -3 22/7] + +[1/2 0 0] +[ 7 0 0] +[ 1 1 1] + +(3/2, 21, 6) + +[ -7/2 -10 80/7] +[ 17 -4 15/7] +[ 241/7 -18/7 869/49] + +Rank of A = 3 +Rank of B = 2 + \end{Verbatim} + + \textbf{Exercise:} in the cell above, compute the determinant, inverse +and characteristic polynomial of the matrix \texttt{A}. \emph{Hint: look +at the reference {[}10{]} above (the functions are listed in alphabetic +order).} + +As for polynomials, you can specify where a matrix or a vector lives + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{57}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\PY{n}{parent}\PY{p}{(}\PY{n}{M}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{57}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +Full MatrixSpace of 2 by 2 dense matrices over Complex Field with 53 bits of +precision +\end{Verbatim} +\end{tcolorbox} + + You can also solve linear systems and compute eigenvalues and +eigenvectors of a matrix + +\textbf{Warning.} In linear algebra there are distinct concepts of +\emph{left} and \emph{right} eigenvalues (and eigenvector). The one you +know is probably that of \textbf{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\). + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{60}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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}{)} +\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} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{60}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +(0.289916349448506, 0.0241596957873755) +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{64}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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()} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{64}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +[ +(-0.3722813232690144?, Vector space of degree 2 and dimension 1 over Algebraic +Field +User basis matrix: +[ 1 -0.6861406616345072?]), +(5.372281323269015?, Vector space of degree 2 and dimension 1 over Algebraic +Field +User basis matrix: +[ 1 2.186140661634508?]) +] +\end{Verbatim} +\end{tcolorbox} + + 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. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{94}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\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}{)} +\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{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} +\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} +\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} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +[-14 2 0 -1 1 -2 -1] +[ 0 -8 0 9 -2 11 1] +[ 0 3 1 -1 1 1 221] +[ -1 2 1 -25 -10 4 0] +[ -3 0 0 2 16 -1 -2] +[ 1 -3 3 -41 1 0 0] +[ -2 1 0 0 -6 2 12] + +[ 0 9 -2] +[ 1 -1 1] + +[-14 2 0 -1 1 -2 -1] + +[-14 2 0 -1 1] +[ 1 -3 3 -41 1] +[ 0 3 1 -1 1] + \end{Verbatim} + + \textbf{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 \begin{align*} + \operatorname{det}A = \sum_{j=1}^n (-1)^ja_{0j}M_{0j} +\end{align*} 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\). + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{91}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k}{def} \PY{n+nf}{my\PYZus{}det}\PY{p}{(}\PY{n}{A}\PY{p}{)}\PY{p}{:} + \PY{k}{if} \PY{o+ow}{not} \PY{n}{A}\PY{o}{.}\PY{n}{is\PYZus{}square}\PY{p}{(}\PY{p}{)}\PY{p}{:} + \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}{)} + + \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} + + \PY{c+c1}{\PYZsh{} Continue from here!} +\end{Verbatim} +\end{tcolorbox} + + \hypertarget{number-theory}{% +\section{Number Theory}\label{number-theory}} + +\textbf{Reference:} +{[}\href{https://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/integer.html}{11}{]} + +Sage includes a large library of functions for computing with the +integers, see the link above. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{8}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{n} \PY{o}{=} \PY{l+m+mi}{123456789} +\PY{n}{m} \PY{o}{=} \PY{l+m+mi}{987654321} +\PY{n}{p} \PY{o}{=} \PY{l+m+mi}{3607} + +\PY{n+nb}{print}\PY{p}{(}\PY{n}{factor}\PY{p}{(}\PY{n}{n}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{is\PYZus{}prime}\PY{p}{(}\PY{n}{p}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{p}\PY{o}{.}\PY{n}{divides}\PY{p}{(}\PY{n}{n}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{euler\PYZus{}phi}\PY{p}{(}\PY{n}{m}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{gcd}\PY{p}{(}\PY{n}{n}\PY{p}{,} \PY{n}{m}\PY{p}{)}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{lcm}\PY{p}{(}\PY{n}{n}\PY{p}{,} \PY{n}{m}\PY{p}{)}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +3\^{}2 * 3607 * 3803 +True +True +619703040 +9 +13548070123626141 + \end{Verbatim} + + \hypertarget{primes}{% +\subsection{Primes}\label{primes}} + +\textbf{Reference:} +{[}\href{https://doc.sagemath.org/html/en/reference/sets/sage/sets/primes.html}{12}{]} + +The set of prime numbers is called \texttt{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. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{9}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{PP} \PY{o}{=} \PY{n}{Primes}\PY{p}{(}\PY{p}{)} +\PY{n+nb}{print}\PY{p}{(}\PY{n}{PP}\PY{p}{)} +\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}{)} +\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}{)} + +\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}{]} +\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}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +Set of all prime numbers: 2, 3, 5, 7, {\ldots} +31 15485867 +47 +[79, 83, 89, 97] + \end{Verbatim} + + \hypertarget{the-chinese-remainder-theorem-crt}{% +\subsection{The Chinese remainder theorem +(CRT)}\label{the-chinese-remainder-theorem-crt}} + +We say that two integers \(a\) and \(b\) are \emph{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 +\texttt{a\ \%\ n\ ==\ b\ \%\ n}. + +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 + +\begin{align*} +\begin{cases} + x \equiv a \pmod n\\ + x \equiv b \pmod m +\end{cases} +\end{align*} + +has exactly one solution modulo \(mn\). This means that there is one and +only one number \(x\) with \(0\leq x0}\) are such that +\(\gcd(n_i, n_j)=1\) for \(i\neq j\), then the system of congruences + +\begin{align*} +\begin{cases} + x \equiv a_0 \pmod {n_0}\\ + x \equiv a_1 \pmod {n_1}\\ + \dots \\ + x \equiv a_k \pmod {n_k} +\end{cases} +\end{align*} + +has exactly one solution modulo \(\prod_{i=0}^kn_i\). Use the +\texttt{crt()} function to find a solution to such a system. *Hint: +start by running the command \texttt{help(crt)}. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{127}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{c+c1}{\PYZsh{}help(crt)} +\end{Verbatim} +\end{tcolorbox} + + \hypertarget{cryptography-rsa}{% +\section{Cryptography: RSA}\label{cryptography-rsa}} + +\href{https://en.wikipedia.org/wiki/Cryptography}{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. + +A simple cryptographic protocol could be changing every letter of your +text following a fixed scheme (or \emph{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. + +\hypertarget{public-key-cryptography}{% +\subsection{Public-key cryptography}\label{public-key-cryptography}} + +Public-key cryptographic protocols, such as RSA, work like this: there +are two keys, a \emph{private} key that is only known to person A +(traditionally called Alice in every example), and a \emph{public} key +that does not need to be secret. + +The public key is used to \emph{encrypt} the message (that is to +``lock'' it, or ``hyde'' it), but one needs the private key to +\emph{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. + +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 \emph{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! + +Notice that such a protocol is \emph{asymmetric}: if Alice wanted to +send a secret to Bob in reply, Bob would need to generate a pair of keys +of his own. + +Let's see how we can do this in practice, using number theory! + +\hypertarget{rsa}{% +\subsection{RSA}\label{rsa}} + +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. + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{2}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{n}{p} \PY{o}{=} \PY{l+m+mi}{100003100019100043100057100069} +\PY{n}{q} \PY{o}{=} \PY{l+m+mi}{100144655312449572059845328443} +\PY{n}{n} \PY{o}{=} \PY{n}{p}\PY{o}{*}\PY{n}{q} +\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}{)} + +\PY{c+c1}{\PYZsh{} Use the command below to see how long it takes} +\PY{c+c1}{\PYZsh{}timeit(\PYZdq{}factor(n)\PYZdq{}, number=1, repeat=1)} +\end{Verbatim} +\end{tcolorbox} + + \begin{Verbatim}[commandchars=\\\{\}] +True True False + \end{Verbatim} + + 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! + +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. + +Of course, she does all of this using Sage! + + \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] +\prompt{In}{incolor}{105}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k}{def} \PY{n+nf}{two\PYZus{}large\PYZus{}primes}\PY{p}{(}\PY{p}{)}\PY{p}{:} + \PY{n}{p}\PY{p}{,} \PY{n}{q} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0} + \PY{c+c1}{\PYZsh{} We make sure that they are different} + \PY{k}{while} \PY{n}{p} \PY{o}{==} \PY{n}{q}\PY{p}{:} + \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}{]} + \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}{]} + \PY{k}{return} \PY{n}{p}\PY{p}{,} \PY{n}{q} + +\PY{k}{def} \PY{n+nf}{random\PYZus{}unit\PYZus{}mod}\PY{p}{(}\PY{n}{N}\PY{p}{)}\PY{p}{:} + \PY{n}{R} \PY{o}{=} \PY{n}{Integers}\PY{p}{(}\PY{n}{N}\PY{p}{)} + \PY{n}{d} \PY{o}{=} \PY{n}{R}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{)} + \PY{c+c1}{\PYZsh{} We make sure that it is invertible} + \PY{k}{while} \PY{o+ow}{not} \PY{n}{d}\PY{o}{.}\PY{n}{is\PYZus{}unit}\PY{p}{(}\PY{p}{)}\PY{p}{:} + \PY{n}{d} \PY{o}{=} \PY{n}{R}\PY{o}{.}\PY{n}{random\PYZus{}element}\PY{p}{(}\PY{p}{)} + \PY{k}{return} \PY{n}{d} + +\PY{k}{def} \PY{n+nf}{Alice\PYZus{}generate\PYZus{}keys}\PY{p}{(}\PY{p}{)}\PY{p}{:} + \PY{n}{p}\PY{p}{,} \PY{n}{q} \PY{o}{=} \PY{n}{two\PYZus{}large\PYZus{}primes}\PY{p}{(}\PY{p}{)} + \PY{n}{n} \PY{o}{=} \PY{n}{p}\PY{o}{*}\PY{n}{q} + \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!} + + \PY{n}{d} \PY{o}{=} \PY{n}{random\PYZus{}unit\PYZus{}mod}\PY{p}{(}\PY{n}{phi\PYZus{}n}\PY{p}{)} + \PY{n}{e} \PY{o}{=} \PY{n}{d}\PY{o}{\PYZca{}}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1} + \PY{k}{return} \PY{n}{n}\PY{p}{,} \PY{n}{d}\PY{p}{,} \PY{n}{e} + +\PY{n}{Alice\PYZus{}generate\PYZus{}keys}\PY{p}{(}\PY{p}{)} +\end{Verbatim} +\end{tcolorbox} + + \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0] +\prompt{Out}{outcolor}{105}{\boxspacing} +\begin{Verbatim}[commandchars=\\\{\}] +(419199544978969, 235530823946467, 80799425863927) +\end{Verbatim} +\end{tcolorbox} + + Now, how does Bob encrypt his message? Let's say he wants to send to +Alice the number \(m\) with \(1