aboutsummaryrefslogtreecommitdiff
path: root/src/Lecture6/notebook/8-SageCalculus.tex
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Lecture6/notebook/8-SageCalculus.tex1456
1 files changed, 1456 insertions, 0 deletions
diff --git a/src/Lecture6/notebook/8-SageCalculus.tex b/src/Lecture6/notebook/8-SageCalculus.tex
new file mode 100644
index 0000000..c1defa2
--- /dev/null
+++ b/src/Lecture6/notebook/8-SageCalculus.tex
@@ -0,0 +1,1456 @@
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{Calculus and more with SageMath}
150 \author{Sebastiano Tronto - \texttt{sebastiano.tronto@uni.lu}}
151 \date{2021-05-07}
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 \hypertarget{symbolic-expressions}{%
376\section{Symbolic expressions}\label{symbolic-expressions}}
377
378\textbf{Reference:}
379{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{1}{]}
380
381Last time we saw the basics of symbolic expressions: * How to define and
382manipulate symbolic expressions * How to introduce new variables (in the
383Mathematical sense) with \texttt{var()} * How to solve equations and
384inequalities * Some of the Mathematical constants that are included in
385Sage, and how to approximate them using \texttt{n()}
386
387Here are some examples to remind you of these basic things:
388
389 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
390\prompt{In}{incolor}{2}{\boxspacing}
391\begin{Verbatim}[commandchars=\\\{\}]
392\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{z}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{} Define new variables (x is already defined by Sage)}
393\PY{n}{f} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{pi}
394\PY{n}{g} \PY{o}{=} \PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{y} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2} \PY{o}{\PYZgt{}} \PY{l+m+mi}{0}
395\PY{n+nb}{print}\PY{p}{(} \PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{o}{==}\PY{l+m+mi}{0}\PY{p}{,} \PY{n}{x}\PY{p}{)} \PY{p}{)}
396\PY{n+nb}{print}\PY{p}{(} \PY{n}{solve}\PY{p}{(}\PY{n}{z}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}} \PY{n}{f}\PY{p}{,} \PY{n}{z}\PY{p}{)} \PY{p}{)}
397\PY{n+nb}{print}\PY{p}{(} \PY{n}{solve}\PY{p}{(}\PY{n}{g}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{p}{)}
398\PY{n+nb}{print}\PY{p}{(} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{pi} \PY{o}{+} \PY{n}{e}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{is approximately}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{n}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{pi} \PY{o}{+} \PY{n}{e}\PY{p}{)} \PY{p}{)}
399\end{Verbatim}
400\end{tcolorbox}
401
402 \begin{Verbatim}[commandchars=\\\{\}]
403[
404x == -sqrt(-pi),
405x == sqrt(-pi)
406]
407[
408z == -sqrt(pi + x\^{}2),
409z == sqrt(pi + x\^{}2)
410]
411[[y < -2], [y > 1]]
4122*pi + e is approximately 9.00146713563863
413 \end{Verbatim}
414
415 Now we will see some more details about solving equations and
416manipulating their solutions.
417
418 \hypertarget{solving-equations-and-inequalities}{%
419\subsection{Solving equations and
420inequalities}\label{solving-equations-and-inequalities}}
421
422\textbf{Reference}
423{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{1}{]}
424for the details of \texttt{solve()} and \texttt{find\_root()},
425{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/relation.html\#solving}{2}{]}
426for examples.
427
428Other than equations and inequalities, we can also solve systems: it is
429enough to give Sage a list of expressions and a list of variables with
430respect to which we want to solve. For example the system
431
432\begin{align*}
433 \begin{cases}
434 x + y = 2 \\
435 2x - y = 6
436 \end{cases}
437\end{align*}
438
439Can be solved as
440
441 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
442\prompt{In}{incolor}{40}{\boxspacing}
443\begin{Verbatim}[commandchars=\\\{\}]
444\PY{n}{solve}\PY{p}{(}\PY{p}{[}\PY{n}{x}\PY{o}{+}\PY{n}{y} \PY{o}{==} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x} \PY{o}{\PYZhy{}} \PY{n}{y} \PY{o}{==} \PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{]}\PY{p}{)}
445\end{Verbatim}
446\end{tcolorbox}
447
448 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
449\prompt{Out}{outcolor}{40}{\boxspacing}
450\begin{Verbatim}[commandchars=\\\{\}]
451[[x == (8/3), y == (-2/3)]]
452\end{Verbatim}
453\end{tcolorbox}
454
455 \textbf{Exercise.} Find the intersection of the circle of radius \(2\)
456centered in the origin and the parabula of equation \(y=x^2-2x^2+1\).
457
458 \hypertarget{the-set-of-solutions}{%
459\subsubsection{The set of solutions}\label{the-set-of-solutions}}
460
461One would expect the result of \texttt{solve()} to be a list of
462solutions, but it is actually a list of expressions (technically it is
463not a list but a different type of Python collection, but this is not so
464important)
465
466 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
467\prompt{In}{incolor}{37}{\boxspacing}
468\begin{Verbatim}[commandchars=\\\{\}]
469\PY{n}{solutions} \PY{o}{=} \PY{n}{solve}\PY{p}{(}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{l+m+mi}{9} \PY{o}{==} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{x}\PY{p}{)}
470\PY{n}{solutions}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} \PY{c+c1}{\PYZsh{} This is the expression \PYZsq{}x == \PYZhy{}3\PYZsq{}}
471\end{Verbatim}
472\end{tcolorbox}
473
474 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
475\prompt{Out}{outcolor}{37}{\boxspacing}
476\begin{Verbatim}[commandchars=\\\{\}]
477x == -3
478\end{Verbatim}
479\end{tcolorbox}
480
481 To read the actual solution without the \texttt{x\ ==} part you can use
482the \texttt{rhs()} or \texttt{lhs()} functions, which can be applied to
483any expression containing a relation operator (like \texttt{==},
484\texttt{\textless{}}, \texttt{\textgreater{}=}\ldots) and return the
485\emph{right hand side} and \emph{left hand side} of the expression,
486respectively
487
488 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
489\prompt{In}{incolor}{41}{\boxspacing}
490\begin{Verbatim}[commandchars=\\\{\}]
491\PY{n}{f} \PY{o}{=} \PY{n}{x} \PY{o}{==} \PY{l+m+mi}{2}
492\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{rhs:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{f}\PY{o}{.}\PY{n}{rhs}\PY{p}{(}\PY{p}{)}\PY{p}{)}
493\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{lhs:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{f}\PY{o}{.}\PY{n}{lhs}\PY{p}{(}\PY{p}{)}\PY{p}{)}
494\end{Verbatim}
495\end{tcolorbox}
496
497 \begin{Verbatim}[commandchars=\\\{\}]
498rhs: 2
499lhs: x
500 \end{Verbatim}
501
502 When you solve an inequality or a system, the set of solutions can be
503more complicated to describe. In this case the result is a list
504containing lists of expressions that have to be \texttt{True} at the
505same time. It is easier to explain with an example:
506
507 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
508\prompt{In}{incolor}{38}{\boxspacing}
509\begin{Verbatim}[commandchars=\\\{\}]
510\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Simple inequality:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{solve}\PY{p}{(}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{l+m+mi}{9} \PY{o}{\PYZgt{}} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{)}
511\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{System of inequalities:}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{solve}\PY{p}{(}\PY{p}{[}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{l+m+mi}{9} \PY{o}{\PYZgt{}} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{x} \PY{o}{\PYZlt{}} \PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{)}
512\end{Verbatim}
513\end{tcolorbox}
514
515 \begin{Verbatim}[commandchars=\\\{\}]
516Simple inequality: [[x < -3], [x > 3]]
517System of inequalities:
518 [
519[3 < x, x < 6],
520[x < -3]
521]
522 \end{Verbatim}
523
524 In the last example (system of inequalities), Sage is telling us that
525the system \begin{align*}
526 \begin{cases}
527 x^2-9 > 9 \\
528 x < 6
529 \end{cases}
530\end{align*} has two solutions: * \(x\) is between \(3\) and \(6\); *
531\(x\) is less than \(-3\).
532
533Since in Sage (and in Python) expressions can have at most on relational
534operator like \texttt{\textless{}}, the first solution requires two
535expressions to be described. Hence the ``list of lists''.
536
537 \textbf{Exercise.} In the first exercise you were asked to solve a
538system of equations, but some of its solutions were complex numbers.
539Select only the real solutions and print them as pairs \((x,y)\).
540
541 When solving a system of equations (not inequalities), you can use the
542option \texttt{solution\_dict=True} to have the solutions arranged as a
543\emph{dictionary}, which is a type of Python collection that we did not
544treat in this course
545
546 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
547\prompt{In}{incolor}{44}{\boxspacing}
548\begin{Verbatim}[commandchars=\\\{\}]
549\PY{n}{solve}\PY{p}{(}\PY{p}{[}\PY{n}{x}\PY{o}{+}\PY{n}{y} \PY{o}{==} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x} \PY{o}{\PYZhy{}} \PY{n}{y} \PY{o}{==} \PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{]}\PY{p}{,} \PY{n}{solution\PYZus{}dict}\PY{o}{=}\PY{k+kc}{True}\PY{p}{)}
550\end{Verbatim}
551\end{tcolorbox}
552
553 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
554\prompt{Out}{outcolor}{44}{\boxspacing}
555\begin{Verbatim}[commandchars=\\\{\}]
556[\{x: 8/3, y: -2/3\}]
557\end{Verbatim}
558\end{tcolorbox}
559
560 \hypertarget{alternative-method-for-real-roots-find_root}{%
561\subsubsection{\texorpdfstring{Alternative method for real roots:
562\texttt{find\_root()}}{Alternative method for real roots: find\_root()}}\label{alternative-method-for-real-roots-find_root}}
563
564The \texttt{solve()} method is very useful when solving \emph{symbolic}
565equations, for example when you have two variables and you want to solve
566for one of them in terms of the other. However, it does not always find
567explicit solutions.
568
569When you want to find an explicit, even if approximate, solution, it can
570be better to use \texttt{find\_root()}. This function works
571\emph{numerically}, which means that it finds an approximation of the
572root. It only works for real solutions and you need to specify an
573interval where you want the root to be searched:
574
575 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
576\prompt{In}{incolor}{52}{\boxspacing}
577\begin{Verbatim}[commandchars=\\\{\}]
578\PY{n}{f} \PY{o}{=} \PY{n}{e}\PY{o}{\PYZca{}}\PY{n}{x} \PY{o}{+} \PY{n}{x} \PY{o}{\PYZhy{}} \PY{l+m+mi}{10}
579\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Using solve():}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{solve}\PY{p}{(}\PY{n}{f}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{)}
580\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Using find\PYZus{}root():}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{f}\PY{o}{.}\PY{n}{find\PYZus{}root}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{100}\PY{p}{)}\PY{p}{)}
581\end{Verbatim}
582\end{tcolorbox}
583
584 \begin{Verbatim}[commandchars=\\\{\}]
585Using solve():
586 [
587x == -e\^{}x + 10
588]
589Using find\_root(): 2.070579904980303
590 \end{Verbatim}
591
592 \hypertarget{evaluating-functions}{%
593\subsection{Evaluating functions}\label{evaluating-functions}}
594
595If an expression contains only one variable you can evaluate it easily,
596even if it is not a function.
597
598 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
599\prompt{In}{incolor}{21}{\boxspacing}
600\begin{Verbatim}[commandchars=\\\{\}]
601\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
602\PY{n}{f} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{l+m+mi}{3}
603\PY{n}{g} \PY{o}{=} \PY{n}{x} \PY{o}{\PYZgt{}} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}
604
605\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}
606\PY{n+nb}{print}\PY{p}{(}\PY{n}{g}\PY{p}{(}\PY{l+m+mi}{3}\PY{o}{+}\PY{n}{y}\PY{p}{)}\PY{p}{)}
607\end{Verbatim}
608\end{tcolorbox}
609
610 \begin{Verbatim}[commandchars=\\\{\}]
6111
612y + 3 > (y + 3)\^{}2
613 \end{Verbatim}
614
615 If an expression contains more than one variable, you can specify a
616value for each of them and they will be substituted in alphabetic order.
617You can also specify a value only for some of the variables.
618
619 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
620\prompt{In}{incolor}{38}{\boxspacing}
621\begin{Verbatim}[commandchars=\\\{\}]
622\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{z}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
623
624\PY{n}{f} \PY{o}{=} \PY{n}{y}\PY{o}{*}\PY{n}{z}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}} \PY{n}{y} \PY{o}{==} \PY{n}{z}
625\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)}\PY{p}{)}
626\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{n}{z}\PY{o}{=}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}
627\end{Verbatim}
628\end{tcolorbox}
629
630 \begin{Verbatim}[commandchars=\\\{\}]
631-2 == 0
6323*y == 2
633 \end{Verbatim}
634
635 \hypertarget{symbolic-computations}{%
636\subsection{Symbolic computations}\label{symbolic-computations}}
637
638Sage can understand and simplify symbolic expressions such as sums
639(finite or infinite) and products. In the following cell, we compute the
640following sums using the
641\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.sum}{\texttt{sum()}}
642function:
643
644\begin{align*}
645 \begin{array}{llcc}
646 (1) & \sum_{k=0}^nk &=&\frac{n^2+n}{2}\\
647 (2) & \sum_{k=0}^nk^4 &=&\frac{6n^5+15n^4+10n^3-n}{30}\\
648 (3) & \sum_{k=0}^n\binom nk &=& 2^n\\
649 (4) & \sum_{k=0}^\infty \frac1{k^2} &=& \frac{\pi^2}{6}
650 \end{array}
651\end{align*}
652
653 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
654\prompt{In}{incolor}{22}{\boxspacing}
655\begin{Verbatim}[commandchars=\\\{\}]
656\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{k}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{n}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} \PY{c+c1}{\PYZsh{} Remember to declare all variables}
657
658\PY{n}{s} \PY{o}{=} \PY{p}{[}\PY{p}{]}
659\PY{n}{s}\PY{o}{.}\PY{n}{append}\PY{p}{(} \PY{n+nb}{sum}\PY{p}{(}\PY{n}{k}\PY{p}{,} \PY{n}{k}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{n}\PY{p}{)} \PY{p}{)}
660\PY{n}{s}\PY{o}{.}\PY{n}{append}\PY{p}{(} \PY{n+nb}{sum}\PY{p}{(}\PY{n}{k}\PY{o}{\PYZca{}}\PY{l+m+mi}{4}\PY{p}{,} \PY{n}{k}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{n}\PY{p}{)} \PY{p}{)}
661\PY{n}{s}\PY{o}{.}\PY{n}{append}\PY{p}{(} \PY{n+nb}{sum}\PY{p}{(}\PY{n}{binomial}\PY{p}{(}\PY{n}{n}\PY{p}{,}\PY{n}{k}\PY{p}{)}\PY{p}{,} \PY{n}{k}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{n}\PY{p}{)} \PY{p}{)}
662\PY{n}{s}\PY{o}{.}\PY{n}{append}\PY{p}{(} \PY{n+nb}{sum}\PY{p}{(}\PY{l+m+mi}{1}\PY{o}{/}\PY{n}{k}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{,} \PY{n}{k}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{n}{infinity}\PY{p}{)} \PY{p}{)}
663
664\PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{s}\PY{p}{)}\PY{p}{)}\PY{p}{:}
665 \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{(}\PY{l+s+si}{\PYZob{}\PYZcb{}}\PY{l+s+s2}{) }\PY{l+s+si}{\PYZob{}\PYZcb{}}\PY{l+s+s2}{\PYZdq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{i}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{,} \PY{n}{s}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{)}\PY{p}{)}
666\end{Verbatim}
667\end{tcolorbox}
668
669 \begin{Verbatim}[commandchars=\\\{\}]
670(1) 1/2*n\^{}2 + 1/2*n
671(2) 1/5*n\^{}5 + 1/2*n\^{}4 + 1/3*n\^{}3 - 1/30*n
672(3) 2\^{}n
673(4) 1/6*pi\^{}2
674 \end{Verbatim}
675
676 An alternative notation is \texttt{expression.sum(k,\ a,\ b)}. There is
677an analogous
678\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.prod}{\texttt{prod()}}
679for products.
680
681 Sometimes Sage tries to keep an expression in its original form without
682expanding out sums and products. To change this behavior you can use the
683\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.expand}{\texttt{expand()}}
684function:
685
686 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
687\prompt{In}{incolor}{30}{\boxspacing}
688\begin{Verbatim}[commandchars=\\\{\}]
689\PY{n}{f} \PY{o}{=} \PY{p}{(}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}} \PY{p}{(}\PY{n}{x}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{)}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}
690\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{p}{)}
691\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{o}{.}\PY{n}{expand}\PY{p}{(}\PY{p}{)}\PY{p}{)}
692\end{Verbatim}
693\end{tcolorbox}
694
695 \begin{Verbatim}[commandchars=\\\{\}]
696(x + 1)\^{}2 - (x - 1)\^{}2
6974*x
698 \end{Verbatim}
699
700 \hypertarget{the-symbolic-ring}{%
701\subsubsection{The Symbolic Ring}\label{the-symbolic-ring}}
702
703\textbf{Reference:}
704{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/ring.html}{3}{]}
705
706The symbolic expressions that we have seen so far live in a ring called
707\emph{symbolic ring} and denoted by \texttt{SR} in Sage. This ring works
708like the ring \texttt{ZZ} of integers or \texttt{RR} of reals numbers.
709In particular, you can define matrices and other objects using it as a
710``basis''.
711
712 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
713\prompt{In}{incolor}{45}{\boxspacing}
714\begin{Verbatim}[commandchars=\\\{\}]
715\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{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
716
717\PY{n}{M} \PY{o}{=} \PY{n}{matrix}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{n}{c}\PY{p}{,}\PY{n}{d}\PY{p}{]}\PY{p}{]}\PY{p}{)}
718\PY{n+nb}{print}\PY{p}{(}\PY{n}{M}\PY{o}{.}\PY{n}{determinant}\PY{p}{(}\PY{p}{)}\PY{p}{)}
719
720\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{SR}\PY{p}{[}\PY{p}{]}
721\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}{a}\PY{o}{*}\PY{n}{x} \PY{o}{+} \PY{n}{a}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}
722\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{o}{.}\PY{n}{roots}\PY{p}{(}\PY{p}{)}\PY{p}{)}
723\end{Verbatim}
724\end{tcolorbox}
725
726 \begin{Verbatim}[commandchars=\\\{\}]
727-b*c + a*d
728[(-a, 2)]
729 \end{Verbatim}
730
731 \textbf{Exercise.} Compute the eigenvalues of the matrix \begin{align*}
732\begin{pmatrix}
733\cos \alpha & \sin \alpha\\
734-\sin\alpha & \cos \alpha
735\end{pmatrix}
736\end{align*}
737
738 \hypertarget{calculus}{%
739\section{Calculus}\label{calculus}}
740
741\textbf{Reference:}
742{[}\href{https://doc.sagemath.org/html/en/reference/calculus/index.html}{4}{]}
743for an overview, but most functions are described in
744{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html}{1}{]}
745
746 \hypertarget{limits-and-series}{%
747\subsection{Limits and series}\label{limits-and-series}}
748
749\textbf{References:}
750{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/calculus.html\#sage.calculus.calculus.limit}{5}{]}
751for limits,
752{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.series}{6}{]}
753for series
754
755You can compute limits
756
757 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
758\prompt{In}{incolor}{54}{\boxspacing}
759\begin{Verbatim}[commandchars=\\\{\}]
760\PY{n}{f} \PY{o}{=} \PY{n}{sin}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{o}{/}\PY{n}{x}
761\PY{c+c1}{\PYZsh{} print(f(0)) \PYZsh{} This one gives an error}
762\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{limit}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)} \PY{p}{)}
763
764\PY{n+nb}{print}\PY{p}{(} \PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{p}{)}\PY{p}{)}\PY{o}{.}\PY{n}{limit}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{n}{infinity}\PY{p}{)} \PY{p}{)}
765\end{Verbatim}
766\end{tcolorbox}
767
768 \begin{Verbatim}[commandchars=\\\{\}]
7691
7700
771 \end{Verbatim}
772
773 \textbf{Exercise.} Compute the constant \(e\) using a limit.
774
775 You can also specify a direction for the limit. If you don't, Sage
776assumes that you want to take a two-sided limit.
777
778 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
779\prompt{In}{incolor}{55}{\boxspacing}
780\begin{Verbatim}[commandchars=\\\{\}]
781\PY{n}{f} \PY{o}{=} \PY{n+nb}{abs}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{o}{/}\PY{n}{x} \PY{c+c1}{\PYZsh{} 1 if x\PYZgt{}0, \PYZhy{}1 if x\PYZlt{}0}
782\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{limit}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} undefined}
783\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{limit}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{,} \PY{n+nb}{dir}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{+}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} \PY{p}{)}
784\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{limit}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{,} \PY{n+nb}{dir}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{\PYZhy{}}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} \PY{p}{)}
785\end{Verbatim}
786\end{tcolorbox}
787
788 \begin{Verbatim}[commandchars=\\\{\}]
789und
7901
791-1
792 \end{Verbatim}
793
794 There is also the alternative notation \texttt{limit(f,\ x,\ dir)} which
795does the same as \texttt{f.limit(x,\ dir)}.
796
797 You can also compute series expansions up to any order. \textbf{Watch
798out:} the notation uses \texttt{==} instead of \texttt{=} as
799\texttt{limit()} does.
800
801 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
802\prompt{In}{incolor}{56}{\boxspacing}
803\begin{Verbatim}[commandchars=\\\{\}]
804\PY{n}{f} \PY{o}{=} \PY{n}{e}\PY{o}{\PYZca{}}\PY{n}{x}
805\PY{n}{g} \PY{o}{=} \PY{n}{sin}\PY{p}{(}\PY{n}{x}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{cos}\PY{p}{(}\PY{n}{x}\PY{p}{)}
806\PY{n}{h} \PY{o}{=} \PY{n}{log}\PY{p}{(}\PY{n}{x}\PY{p}{)}
807
808\PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{o}{.}\PY{n}{series}\PY{p}{(}\PY{n}{x}\PY{o}{==}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)}
809\PY{n+nb}{print}\PY{p}{(}\PY{n}{g}\PY{o}{.}\PY{n}{series}\PY{p}{(}\PY{n}{x}\PY{o}{==}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{7}\PY{p}{)}\PY{p}{)}
810\PY{n+nb}{print}\PY{p}{(}\PY{n}{h}\PY{o}{.}\PY{n}{series}\PY{p}{(}\PY{n}{x}\PY{o}{==}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)}
811\end{Verbatim}
812\end{tcolorbox}
813
814 \begin{Verbatim}[commandchars=\\\{\}]
8151 + 1*x + 1/2*x\^{}2 + Order(x\^{}3)
816(-2) + 1*x + 1*x\^{}2 + (-1/6)*x\^{}3 + (-1/12)*x\^{}4 + 1/120*x\^{}5 + 1/360*x\^{}6 +
817Order(x\^{}7)
8181*(x - 1) + (-1/2)*(x - 1)\^{}2 + Order((x - 1)\^{}3)
819 \end{Verbatim}
820
821 \hypertarget{derivatives}{%
822\subsection{Derivatives}\label{derivatives}}
823
824\textbf{References:}
825{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.derivative}{7}{]}
826and
827{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/functional.html\#sage.calculus.functional.derivative}{8}{]}
828for derivatives,
829{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/functions.html\#sage.calculus.functions.jacobian}{9}{]}
830for the Jacobian matrix and
831{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html\#sage.symbolic.expression.Expression.hessian}{10}{]}
832for the Hessian.
833
834 When computing derivatives, you need to specify with respect to which
835variables you want to derive, except in case there is only one.
836
837 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
838\prompt{In}{incolor}{57}{\boxspacing}
839\begin{Verbatim}[commandchars=\\\{\}]
840\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{y}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
841\PY{n+nb}{print}\PY{p}{(} \PY{p}{(}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{+}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{4}\PY{p}{)}\PY{o}{.}\PY{n}{derivative}\PY{p}{(}\PY{n}{y}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} Alternative: derivative(f, y)}
842\PY{n+nb}{print}\PY{p}{(} \PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{3}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{2}\PY{p}{)}\PY{o}{.}\PY{n}{derivative}\PY{p}{(}\PY{p}{)} \PY{p}{)}
843\end{Verbatim}
844\end{tcolorbox}
845
846 \begin{Verbatim}[commandchars=\\\{\}]
8478*y\^{}3
8486*x\^{}2 - 1
849 \end{Verbatim}
850
851 You can also compute higher order derivatives:
852
853 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
854\prompt{In}{incolor}{58}{\boxspacing}
855\begin{Verbatim}[commandchars=\\\{\}]
856\PY{n+nb}{print}\PY{p}{(} \PY{p}{(}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{3}\PY{p}{)}\PY{o}{.}\PY{n}{derivative}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{x}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} Same as (x\PYZca{}3).derivative(x, 2)}
857
858\PY{n}{f} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{7}\PY{o}{*}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{4}\PY{o}{*}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{3} \PY{o}{+} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{5} \PY{o}{+} \PY{n}{y} \PY{o}{+} \PY{l+m+mi}{2}
859\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{derivative}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} Twice in x, once in y}
860\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{derivative}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{l+m+mi}{4}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} 4 times in x, twice in y}
861\end{Verbatim}
862\end{tcolorbox}
863
864 \begin{Verbatim}[commandchars=\\\{\}]
8656*x
86684*x\^{}5*y + 10*y\^{}4 + 24*x\^{}2*y
8671680*x\^{}3 + 48
868 \end{Verbatim}
869
870 Jacobian and Hessian matrices are also easy to compute:
871
872 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
873\prompt{In}{incolor}{59}{\boxspacing}
874\begin{Verbatim}[commandchars=\\\{\}]
875\PY{n}{f} \PY{o}{=} \PY{p}{(}\PY{o}{\PYZhy{}}\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}{*}\PY{n}{y}\PY{p}{,} \PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{3}\PY{p}{,} \PY{n}{x}\PY{o}{+}\PY{n}{y}\PY{o}{+}\PY{n}{x}\PY{o}{*}\PY{n}{y}\PY{p}{)}
876\PY{n+nb}{print}\PY{p}{(} \PY{n}{jacobian}\PY{p}{(}\PY{n}{f}\PY{p}{,} \PY{p}{[}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{]}\PY{p}{)}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}} \PY{p}{)}
877
878\PY{n}{g} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{x}\PY{o}{*}\PY{n}{y} \PY{o}{+} \PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{3} \PY{o}{\PYZhy{}}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{x}\PY{o}{*}\PY{n}{y}\PY{o}{\PYZca{}}\PY{l+m+mi}{2} \PY{o}{\PYZhy{}}\PY{l+m+mi}{3}
879\PY{n+nb}{print}\PY{p}{(} \PY{n}{g}\PY{o}{.}\PY{n}{hessian}\PY{p}{(}\PY{p}{)} \PY{p}{)}
880\end{Verbatim}
881\end{tcolorbox}
882
883 \begin{Verbatim}[commandchars=\\\{\}]
884[-2*x + 2*y 2*x]
885[ 0 3*y\^{}2]
886[ y + 1 x + 1]
887
888[ 2 -4*y + 1]
889[ -4*y + 1 -4*x + 6*y]
890 \end{Verbatim}
891
892 \emph{Note:} the notation \texttt{f.jacobian({[}x,y{]})} is also valid,
893but only if you specify that \texttt{f} is vector by declaring it as
894\texttt{f\ =\ vector({[}...{]})}.
895
896 \hypertarget{integrals}{%
897\subsection{Integrals}\label{integrals}}
898
899\textbf{References:}
900{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/integration/integral.html}{11}{]}
901for symbolic integration and
902{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/integration.html}{12}{]}
903for numerical methods.
904
905You should remember from high school or from your first
906calculus/analysis course that derivatives are easy, but integrals are
907hard. When using a computer software to solve your integrals, you have
908two choices:
909
910\begin{enumerate}
911\def\labelenumi{\arabic{enumi}.}
912\tightlist
913\item
914 You can try to compute a primitive function exactly, and then (if you
915 are computing a definite integral) substitute the endpoints of your
916 integration interval to get the result. We can call this
917 \emph{symbolic integration}.
918\item
919 You can get an \emph{approximated} result with a \emph{numerical
920 method}. This method always gives some kind of result, but it cannot
921 be used to compute indefinite integrals.
922\end{enumerate}
923
924Sage can do both of these things, although people that work in numerical
925analysis and use often the second method tend to prefer other programs,
926such as Matlab (or its open-source clone Octave).
927
928 \hypertarget{symbolic-integration}{%
929\subsubsection{Symbolic integration}\label{symbolic-integration}}
930
931Symbolic integrals work more or less like derivatives. You must specify
932an integration variable, but the endpoints of the integration interval
933are optional. If they are not given you get an indefinite integral.
934
935 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
936\prompt{In}{incolor}{60}{\boxspacing}
937\begin{Verbatim}[commandchars=\\\{\}]
938\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}{)}
939\PY{n}{f} \PY{o}{=} \PY{n}{x} \PY{o}{+} \PY{n}{sin}\PY{p}{(}\PY{n}{x}\PY{p}{)}
940\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{integral}\PY{p}{(}\PY{n}{x}\PY{p}{)} \PY{p}{)} \PY{c+c1}{\PYZsh{} Alternative: integral(f, x)}
941\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{integral}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{10}\PY{p}{,} \PY{l+m+mi}{10}\PY{p}{)} \PY{p}{)}
942\PY{n+nb}{print}\PY{p}{(} \PY{n}{f}\PY{o}{.}\PY{n}{integral}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)} \PY{p}{)}
943\end{Verbatim}
944\end{tcolorbox}
945
946 \begin{Verbatim}[commandchars=\\\{\}]
9471/2*x\^{}2 - cos(x)
9480
949-1/2*a\^{}2 + 1/2*b\^{}2 + cos(a) - cos(b)
950 \end{Verbatim}
951
952 Your endpoints can also be \(\pm\infty\):
953
954 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
955\prompt{In}{incolor}{61}{\boxspacing}
956\begin{Verbatim}[commandchars=\\\{\}]
957\PY{n+nb}{print}\PY{p}{(} \PY{n}{integral}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{n}{infinity}\PY{p}{)} \PY{p}{)}
958\PY{n+nb}{print}\PY{p}{(} \PY{n}{integral}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{n}{infinity}\PY{p}{,} \PY{n}{infinity}\PY{p}{)} \PY{p}{)}
959\end{Verbatim}
960\end{tcolorbox}
961
962 \begin{Verbatim}[commandchars=\\\{\}]
9631
964sqrt(pi)
965 \end{Verbatim}
966
967 The last function is also an example of an integral that perhaps you
968might want to compute numerically. In fact:
969
970 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
971\prompt{In}{incolor}{65}{\boxspacing}
972\begin{Verbatim}[commandchars=\\\{\}]
973\PY{n+nb}{print}\PY{p}{(} \PY{n}{integral}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{n}{x}\PY{p}{)} \PY{p}{)}
974\PY{n+nb}{print}\PY{p}{(} \PY{n}{integral}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)} \PY{p}{)}
975\end{Verbatim}
976\end{tcolorbox}
977
978 \begin{Verbatim}[commandchars=\\\{\}]
9791/2*sqrt(pi)*erf(x)
9801/2*sqrt(pi)*erf(2) - 1/2*sqrt(pi)*erf(1)
981 \end{Verbatim}
982
983 Here \texttt{erf(x)} denotes the
984\href{https://en.wikipedia.org/wiki/Error_function}{error function}.
985
986 \hypertarget{numerical-integration}{%
987\subsubsection{Numerical integration}\label{numerical-integration}}
988
989In order to get an explicit value for the computations above, we can use
990a \emph{numerical} method.
991
992The word ``numerical'' does not have much to do with numbers, but it
993refers to the fact that we are trying to compute explicit results rather
994than symbolic or algebraic ones.
995\href{https://en.wikipedia.org/wiki/Numerical_analysis}{Numerical
996analysis} is the branch of mathematics that studies methods to
997approximate computations over the real or complex numbers. With these
998methods there is usually a trade-off between speed and precision.
999
1000The Sage function
1001\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/integration.html\#sage.calculus.integration.numerical_integral}{\texttt{numerical\_integral()}}
1002takes as a parameter a real-valued one-variable function and the
1003integration endpoints, and it returns both an approximate value for the
1004integral and an error estimate.
1005
1006 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1007\prompt{In}{incolor}{40}{\boxspacing}
1008\begin{Verbatim}[commandchars=\\\{\}]
1009\PY{n}{numerical\PYZus{}integral}\PY{p}{(}\PY{n}{e}\PY{o}{\PYZca{}}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}
1010\end{Verbatim}
1011\end{tcolorbox}
1012
1013 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1014\prompt{Out}{outcolor}{40}{\boxspacing}
1015\begin{Verbatim}[commandchars=\\\{\}]
1016(0.13525725794999466, 1.5016572202374808e-15)
1017\end{Verbatim}
1018\end{tcolorbox}
1019
1020 The result above means, in symbols \begin{align*}
1021\int_1^2 e^{-x^2}\mathrm dx = 0.13525725794999466 \pm 1.5016572202374808\times 10^{-15}
1022\end{align*}
1023
1024There is also a
1025\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/integration.html\#sage.calculus.integration.monte_carlo_integral}{\texttt{monte\_carlo\_integral()}}
1026method for functions with more than one variable.
1027
1028 \textbf{Exercise.} Compute the area of the ellipse of equation
1029\(y^2+\left(\frac x3\right)^2=1\).
1030
1031 \hypertarget{differential-equations}{%
1032\subsection{Differential equations}\label{differential-equations}}
1033
1034\textbf{Reference:}
1035{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/desolvers.html}{13}{]}
1036
1037A
1038\href{https://en.wikipedia.org/wiki/Differential_equation}{differential
1039equation} is an equation involving an unknwon function and its
1040derivatives. They can be of two kinds: \emph{ordinary} differential
1041equations
1042(\href{https://en.wikipedia.org/wiki/Ordinary_differential_equation}{ODE})
1043and \emph{partial} differential equations
1044(\href{https://en.wikipedia.org/wiki/Partial_differential_equation}{PDE}).
1045The latter involve multivariate functions and their partial derivatives.
1046
1047Differential equations are in general hard to solve \emph{exactly} (or
1048\emph{symbolically}): even a simple equation of the form \(f'(x)=g(x)\),
1049where \(g(x)\) is someknown function, requires solving the integral
1050\(\int g(x)\mathrm{d}x\) in order to find \(f\), which as we know is not
1051always easy!
1052
1053Theoretical results on differential equations usually ensure the
1054existence and/or uniquess of a solution under certain conditions, but in
1055general they do not give a way to solve them. There exits many methods
1056to find approximate solutions, and some of them are implemented in Sage
1057as well (see
1058{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/desolvers.html}{13}{]}).
1059However we will focus on the simple ODEs that can be solved exactly.
1060
1061Let's start with a simple example. Let's find all functions \(f(x)\)
1062such that \(f'(x)=f(x)\). In order to do so, we need to use the
1063\texttt{function()} construct, which allows us to define an ``unknwon''
1064function inside Sage, like we define variables with \texttt{var()}.
1065
1066 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1067\prompt{In}{incolor}{4}{\boxspacing}
1068\begin{Verbatim}[commandchars=\\\{\}]
1069\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{x}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1070\PY{n}{function}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{f}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1071\PY{n}{equation} \PY{o}{=} \PY{n}{derivative}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{)} \PY{o}{==} \PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}
1072\PY{n}{desolve}\PY{p}{(}\PY{n}{equation}\PY{p}{,} \PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{)} \PY{c+c1}{\PYZsh{} f is the unknown function}
1073\end{Verbatim}
1074\end{tcolorbox}
1075
1076 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1077\prompt{Out}{outcolor}{4}{\boxspacing}
1078\begin{Verbatim}[commandchars=\\\{\}]
1079\_C*e\^{}x
1080\end{Verbatim}
1081\end{tcolorbox}
1082
1083 As you can expect, they are all the functions \(Ce^x\) for some constant
1084\(C\). The constant \(C\) plays the same role as the constant in the
1085solution of an integral, but in this case Sage writes it explicitly.
1086
1087We can also specify \emph{initial conditions} for our function. For
1088example we can impose that \(f(0)=3\) as follows:
1089
1090 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1091\prompt{In}{incolor}{5}{\boxspacing}
1092\begin{Verbatim}[commandchars=\\\{\}]
1093\PY{n}{desolve}\PY{p}{(}\PY{n}{equation}\PY{p}{,} \PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{)}
1094\end{Verbatim}
1095\end{tcolorbox}
1096
1097 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1098\prompt{Out}{outcolor}{5}{\boxspacing}
1099\begin{Verbatim}[commandchars=\\\{\}]
11003*e\^{}x
1101\end{Verbatim}
1102\end{tcolorbox}
1103
1104 You can also solve \emph{second order} equations, that is equations
1105where the second derivative also appears. In this case if you want to
1106specify an initial condition you should write the triple of values
1107\((x_0, f(x_0), f'(x_0))\).
1108
1109 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1110\prompt{In}{incolor}{6}{\boxspacing}
1111\begin{Verbatim}[commandchars=\\\{\}]
1112\PY{n}{equation} \PY{o}{=} \PY{n}{derivative}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{n}{x}\PY{o}{*}\PY{n}{derivative}\PY{p}{(}\PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{)} \PY{o}{==} \PY{l+m+mi}{1}
1113\PY{n}{desolve}\PY{p}{(}\PY{n}{equation}\PY{p}{,} \PY{n}{f}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)}\PY{p}{)}
1114\end{Verbatim}
1115\end{tcolorbox}
1116
1117 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1118\prompt{Out}{outcolor}{6}{\boxspacing}
1119\begin{Verbatim}[commandchars=\\\{\}]
1120-1/2*I*sqrt(2)*sqrt(pi)*integrate(erf(1/2*I*sqrt(2)*x)*e\^{}(-1/2*x\^{}2), x)
1121\end{Verbatim}
1122\end{tcolorbox}
1123
1124 \textbf{Exercise.} Use Sage to find out the functions \(f(x)\) that
1125satisfy \begin{align*}
1126 \begin{array}{rlcrl}
1127 (A) &
1128 \begin{cases}
1129 f(0) &= 1\\
1130 f'(0) &= 0\\
1131 f''(x) &= -f(x)
1132 \end{cases}
1133 & \qquad \qquad &
1134 (B) &
1135 \begin{cases}
1136 f(0) &= 0\\
1137 f'(0) &= 1\\
1138 f''(x) &= -f(x)
1139 \end{cases}
1140 \end{array}
1141\end{align*}
1142
1143 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1144\prompt{In}{incolor}{ }{\boxspacing}
1145\begin{Verbatim}[commandchars=\\\{\}]
1146
1147\end{Verbatim}
1148\end{tcolorbox}
1149
1150 \hypertarget{a-real-world-example}{%
1151\subsubsection{A real-world example}\label{a-real-world-example}}
1152
1153Differential equations have countless applications in Science, so it
1154would be a shame not to see at least a simple one.
1155
1156Consider an object moving with constant acceleration \(a\). Its velocity
1157at time \(t\) is described by the formula \(v(t) = v(0) + at\). For
1158example an object falling from the sky has acceleration
1159\(g\sim 9.8 m/s^2\) towards the ground, so its velocity is
1160\(v(t) = -gt\).
1161
1162However in the real world you need to take into account the air's
1163resistance, which depends (among other things) on the velocity of the
1164object. In this case the acceleration \(a(t)\) is not constant anymore,
1165and it satisfies an equation of the form \(a(t)=-g -kv(t)\), where \(k\)
1166is some constant that may depend on the shape and mass of the object (in
1167practice it may be more complicated than this).
1168
1169Since the acceleration is the derivative of the velocity, we have a
1170differential equation \begin{align*}
1171 v'(t) = -g -kv(t)
1172\end{align*} and we can try to solve it with Sage!
1173
1174 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1175\prompt{In}{incolor}{7}{\boxspacing}
1176\begin{Verbatim}[commandchars=\\\{\}]
1177\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{t}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1178\PY{n}{function}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{v}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1179\PY{n}{g} \PY{o}{=} \PY{l+m+mf}{9.8}
1180\PY{n}{k} \PY{o}{=} \PY{l+m+mf}{1.5}
1181\PY{n}{conditions} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)} \PY{c+c1}{\PYZsh{} Start with velocity 0}
1182\PY{n}{desolve}\PY{p}{(}\PY{n}{derivative}\PY{p}{(}\PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{)} \PY{o}{==} \PY{o}{\PYZhy{}}\PY{n}{g} \PY{o}{\PYZhy{}}\PY{n}{k}\PY{o}{*}\PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{,} \PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{,} \PY{n}{conditions}\PY{p}{)}
1183\end{Verbatim}
1184\end{tcolorbox}
1185
1186 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1187\prompt{Out}{outcolor}{7}{\boxspacing}
1188\begin{Verbatim}[commandchars=\\\{\}]
1189-98/15*(e\^{}(3/2*t) - 1)*e\^{}(-3/2*t)
1190\end{Verbatim}
1191\end{tcolorbox}
1192
1193 If you want to solve this equation symbolically (that is, keeping \(g\)
1194and \(k\) in symbols) you need to specify that \(t\) is the
1195\emph{independent variable} of the equation:
1196
1197 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1198\prompt{In}{incolor}{10}{\boxspacing}
1199\begin{Verbatim}[commandchars=\\\{\}]
1200\PY{n}{var}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{t}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{g}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{k}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1201\PY{n}{function}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{v}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
1202\PY{n}{conditions} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)} \PY{c+c1}{\PYZsh{} Start with velocity 0}
1203\PY{n}{desolve}\PY{p}{(}\PY{n}{derivative}\PY{p}{(}\PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{)} \PY{o}{==} \PY{o}{\PYZhy{}}\PY{n}{g} \PY{o}{\PYZhy{}}\PY{n}{k}\PY{o}{*}\PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{,} \PY{n}{v}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{,} \PY{n}{conditions}\PY{p}{,} \PY{n}{ivar}\PY{o}{=}\PY{n}{t}\PY{p}{)}
1204\end{Verbatim}
1205\end{tcolorbox}
1206
1207 \begin{tcolorbox}[breakable, size=fbox, boxrule=.5pt, pad at break*=1mm, opacityfill=0]
1208\prompt{Out}{outcolor}{10}{\boxspacing}
1209\begin{Verbatim}[commandchars=\\\{\}]
1210-(g*e\^{}(k*t) - g)*e\^{}(-k*t)/k
1211\end{Verbatim}
1212\end{tcolorbox}
1213
1214 \hypertarget{basic-data-analysis-and-visualization}{%
1215\section{Basic data analysis and
1216visualization}\label{basic-data-analysis-and-visualization}}
1217
1218\hypertarget{statistics}{%
1219\subsection{Statistics}\label{statistics}}
1220
1221\textbf{References:}
1222{[}\href{https://doc.sagemath.org/html/en/reference/stats/sage/stats/basic_stats.html}{14}{]}
1223
1224Sage includes the most basic functions for statistical analysis.
1225
1226 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1227\prompt{In}{incolor}{20}{\boxspacing}
1228\begin{Verbatim}[commandchars=\\\{\}]
1229\PY{n}{L} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{6}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{4}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{4}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}
1230
1231\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Values:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{L}\PY{p}{)}
1232
1233\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Mean:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{mean}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}
1234\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Median:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{median}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}
1235\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Mode:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{mode}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}
1236
1237\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Standard deviation:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{std}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}
1238\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Variance:}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+se}{\PYZbs{}t}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{variance}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}
1239
1240\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Moving average (5):}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{moving\PYZus{}average}\PY{p}{(}\PY{n}{L}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)}\PY{p}{)}
1241\end{Verbatim}
1242\end{tcolorbox}
1243
1244 \begin{Verbatim}[commandchars=\\\{\}]
1245Values: [1, 2, 3, 3, -6, -2, 4, -1, 0, 2, 3, -4, 0]
1246Mean: 5/13
1247Median: 1
1248Mode: [3]
1249Standard deviation: 2*sqrt(29/13)
1250Variance: 116/13
1251Moving average (5): [3/5, 0, 2/5, -2/5, -1, 3/5, 8/5, 0, 1/5]
1252 \end{Verbatim}
1253
1254 You can also compare your data to a probability distribution, see
1255\href{https://doc.sagemath.org/html/en/reference/probability/sage/probability/probability_distribution.html}{this
1256page}. If you need to do more advanced statistics you should consider
1257using \href{https://www.r-project.org/}{R}; you can also use it inside
1258Sage.
1259
1260 \hypertarget{plotting}{%
1261\subsection{Plotting}\label{plotting}}
1262
1263\textbf{Reference:}
1264{[}\href{https://doc.sagemath.org/html/en/reference/plotting/index.html}{15}{]},
1265more specifically the subsection
1266{[}\href{https://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html}{16}{]}.
1267
1268Some Sage objects can be plotted:
1269
1270 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1271\prompt{In}{incolor}{21}{\boxspacing}
1272\begin{Verbatim}[commandchars=\\\{\}]
1273\PY{n}{f} \PY{o}{=} \PY{n}{sin}\PY{p}{(}\PY{n}{x}\PY{p}{)}
1274\PY{n}{plot}\PY{p}{(}\PY{n}{f}\PY{p}{)}
1275\end{Verbatim}
1276\end{tcolorbox}
1277
1278
1279\prompt{Out}{outcolor}{21}{}
1280
1281 \begin{center}
1282 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_75_0.png}
1283 \end{center}
1284 { \hspace*{\fill} \\}
1285
1286
1287 Sage's plotting functions are based on Python's
1288\href{https://matplotlib.org/}{matplotlib}.
1289
1290You can give a number of options to adjust the aspect of your plot, see
1291\href{https://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html\#sage.plot.plot.plot}{here}.
1292Let's see some of them:
1293
1294 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1295\prompt{In}{incolor}{67}{\boxspacing}
1296\begin{Verbatim}[commandchars=\\\{\}]
1297\PY{n}{f} \PY{o}{=} \PY{n}{sin}\PY{p}{(}\PY{n}{x}\PY{p}{)}
1298\PY{n}{plot}\PY{p}{(}\PY{n}{f}\PY{p}{,}
1299 \PY{o}{\PYZhy{}}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{pi}\PY{p}{,} \PY{l+m+mi}{2}\PY{o}{*}\PY{n}{pi}\PY{p}{,} \PY{c+c1}{\PYZsh{} bounds for x}
1300 \PY{n}{ymin} \PY{o}{=} \PY{o}{\PYZhy{}}\PY{l+m+mf}{0.7}\PY{p}{,} \PY{n}{ymax} \PY{o}{=} \PY{l+m+mf}{0.7}\PY{p}{,} \PY{c+c1}{\PYZsh{} bounds for y}
1301 \PY{n}{color} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{red}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,}
1302 \PY{n}{title} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{The sin function}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,}
1303 \PY{p}{)}
1304\end{Verbatim}
1305\end{tcolorbox}
1306
1307
1308\prompt{Out}{outcolor}{67}{}
1309
1310 \begin{center}
1311 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_77_0.png}
1312 \end{center}
1313 { \hspace*{\fill} \\}
1314
1315
1316 Some of the options are not described precisely in Sage's documentation,
1317but you can find them on
1318\href{https://matplotlib.org/stable/contents.html}{matplotlib's
1319documentation}. You can find many examples online for adjusting your
1320plot as you like!
1321
1322 If you need to plot more than one object at the time, you can sum two
1323plots and show them together with \texttt{show()}:
1324
1325 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1326\prompt{In}{incolor}{36}{\boxspacing}
1327\begin{Verbatim}[commandchars=\\\{\}]
1328\PY{n}{cosine} \PY{o}{=} \PY{n}{plot}\PY{p}{(}\PY{n}{cos}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{n}{x}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{n}{pi}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{,}\PY{n}{pi}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{red}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
1329\PY{n}{exponential} \PY{o}{=} \PY{n}{plot}\PY{p}{(}\PY{n}{exp}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{n}{x}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mf}{0.5}\PY{p}{)}\PY{p}{)}
1330
1331\PY{n}{show}\PY{p}{(}\PY{n}{cosine} \PY{o}{+} \PY{n}{exponential}\PY{p}{)}
1332\end{Verbatim}
1333\end{tcolorbox}
1334
1335 \begin{center}
1336 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_80_0.png}
1337 \end{center}
1338 { \hspace*{\fill} \\}
1339
1340 Finally, there are other types of plots that you can use, like
1341\href{https://doc.sagemath.org/html/en/reference/plotting/sage/plot/scatter_plot.html\#sage.plot.scatter_plot.scatter_plot}{scatter
1342plots} and
1343\href{https://doc.sagemath.org/html/en/reference/plotting/sage/plot/bar_chart.html\#sage.plot.bar_chart.bar_chart}{bar
1344charts}. You can also add
1345\href{https://doc.sagemath.org/html/en/reference/plotting/sage/plot/text.html\#sage.plot.text.text}{text}
1346to your plot:
1347
1348 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1349\prompt{In}{incolor}{53}{\boxspacing}
1350\begin{Verbatim}[commandchars=\\\{\}]
1351\PY{n}{b} \PY{o}{=} \PY{n}{bar\PYZus{}chart}\PY{p}{(}\PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{)}
1352\PY{n}{s} \PY{o}{=} \PY{n}{scatter\PYZus{}plot}\PY{p}{(}\PY{p}{[}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{)}\PY{p}{]}\PY{p}{,}
1353 \PY{n}{marker} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{*}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{c+c1}{\PYZsh{} symbol}
1354 \PY{n}{markersize} \PY{o}{=} \PY{l+m+mi}{100}\PY{p}{,}
1355 \PY{n}{edgecolor} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{black}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,}
1356 \PY{n}{facecolor} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{red}\PY{l+s+s2}{\PYZdq{}}
1357 \PY{p}{)}
1358\PY{n}{t} \PY{o}{=} \PY{n}{text}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{wow, such plot!}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{black}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{20}\PY{p}{)}
1359\PY{n}{show}\PY{p}{(}\PY{n}{b} \PY{o}{+} \PY{n}{s} \PY{o}{+} \PY{n}{t}\PY{p}{)}
1360\end{Verbatim}
1361\end{tcolorbox}
1362
1363 \begin{center}
1364 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_82_0.png}
1365 \end{center}
1366 { \hspace*{\fill} \\}
1367
1368 \hypertarget{interpolation}{%
1369\subsection{Interpolation}\label{interpolation}}
1370
1371\textbf{References:}
1372{[}\href{https://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/polynomial_ring.html\#sage.rings.polynomial.polynomial_ring.PolynomialRing_field.lagrange_polynomial}{17}{]}
1373and
1374{[}\href{https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/interpolation.html}{18}{]}.
1375
1376When you need to work with a discrete set of data, like measurements of
1377real-world quantities, it can be useful to visualize a ``smoothed out''
1378version of this data, for example by plotting a function that
1379approximates it.
1380
1381One way to do so is finding the lowest-degree polynomial that passes
1382through all your points. This is called
1383\href{https://en.wikipedia.org/wiki/Lagrange_polynomial}{Lagrange
1384Polynomial}.
1385
1386 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1387\prompt{In}{incolor}{65}{\boxspacing}
1388\begin{Verbatim}[commandchars=\\\{\}]
1389\PY{n}{points} \PY{o}{=} \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}{2}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mf}{1.5}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} \PY{p}{]}
1390\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{QQ}\PY{p}{[}\PY{p}{]} \PY{c+c1}{\PYZsh{} you need to specify a polynomial ring}
1391\PY{n}{lp} \PY{o}{=} \PY{n}{polring}\PY{o}{.}\PY{n}{lagrange\PYZus{}polynomial}\PY{p}{(}\PY{n}{points}\PY{p}{)}
1392\PY{n}{show}\PY{p}{(}\PY{n}{scatter\PYZus{}plot}\PY{p}{(}\PY{n}{points}\PY{p}{,} \PY{n}{facecolor}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{red}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
1393 \PY{o}{+} \PY{n}{plot}\PY{p}{(}\PY{n}{lp}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{)} \PY{c+c1}{\PYZsh{} slightly different notation for polynomials}
1394 \PY{o}{+} \PY{n}{text}\PY{p}{(}\PY{n}{lp}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{black}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
1395 \PY{p}{)}
1396\end{Verbatim}
1397\end{tcolorbox}
1398
1399 \begin{center}
1400 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_84_0.png}
1401 \end{center}
1402 { \hspace*{\fill} \\}
1403
1404 One can compute the Lagrange Polynomial over any base ring, and it has
1405the advantage that it is a very ``nice'' function (continuous and
1406differentiable as much as you like, with easily computable derivatives
1407and primitives).
1408
1409However, it does not always give you good approximation of your data:
1410
1411 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1412\prompt{In}{incolor}{2}{\boxspacing}
1413\begin{Verbatim}[commandchars=\\\{\}]
1414\PY{n}{R} \PY{o}{=} \PY{p}{[}\PY{n}{x}\PY{o}{/}\PY{l+m+mi}{10} \PY{k}{for} \PY{n}{x} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{]}
1415\PY{n}{L} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{o}{/}\PY{p}{(}\PY{l+m+mi}{1}\PY{o}{+}\PY{l+m+mi}{25}\PY{o}{*}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m+mi}{2}\PY{p}{)} \PY{k}{for} \PY{n}{x} \PY{o+ow}{in} \PY{n}{R}\PY{p}{]}
1416\PY{n}{points} \PY{o}{=} \PY{p}{[}\PY{p}{(}\PY{n}{R}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{,} \PY{n}{L}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{)} \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}\PY{p}{]}
1417\PY{n}{polring}\PY{o}{.}\PY{o}{\PYZlt{}}\PY{n}{x}\PY{o}{\PYZgt{}} \PY{o}{=} \PY{n}{RR}\PY{p}{[}\PY{p}{]}
1418\PY{n}{lp} \PY{o}{=} \PY{n}{polring}\PY{o}{.}\PY{n}{lagrange\PYZus{}polynomial}\PY{p}{(}\PY{n}{points}\PY{p}{)}
1419
1420\PY{n}{show}\PY{p}{(}\PY{n}{plot}\PY{p}{(}\PY{n}{lp}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mf}{0.82}\PY{p}{,} \PY{l+m+mf}{0.72}\PY{p}{)} \PY{o}{+} \PY{n}{scatter\PYZus{}plot}\PY{p}{(}\PY{n}{points}\PY{p}{)}\PY{p}{)}
1421\end{Verbatim}
1422\end{tcolorbox}
1423
1424 \begin{center}
1425 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_86_0.png}
1426 \end{center}
1427 { \hspace*{\fill} \\}
1428
1429 This particular example is called
1430\href{https://en.wikipedia.org/wiki/Runge\%27s_phenomenon}{Runge's
1431phenomenon}. For a better approximation you can use a
1432\href{https://en.wikipedia.org/wiki/Spline_(mathematics)}{spline}, which
1433is a \emph{piecewise} polynomial function:
1434
1435 \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
1436\prompt{In}{incolor}{90}{\boxspacing}
1437\begin{Verbatim}[commandchars=\\\{\}]
1438\PY{n}{show}\PY{p}{(}\PY{n}{plot}\PY{p}{(}\PY{n}{spline}\PY{p}{(}\PY{n}{points}\PY{p}{)}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{)} \PY{o}{+} \PY{n}{scatter\PYZus{}plot}\PY{p}{(}\PY{n}{points}\PY{p}{)}\PY{p}{)}
1439\end{Verbatim}
1440\end{tcolorbox}
1441
1442 \begin{center}
1443 \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_88_0.png}
1444 \end{center}
1445 { \hspace*{\fill} \\}
1446
1447 A detailed explanation of splines is a good topic for a course of
1448numerical analysis. For this course it is enough that you know that they
1449exist and they can be plotted.
1450
1451
1452 % Add a bibliography block to the postdoc
1453
1454
1455
1456\end{document}

Generated with cgit - Back to sebastiano.tronto.net