Debian Codegraph @

texmf-dist/doc/lualatex/luacas/tutorial/tut.tex

raw
1 \documentclass{article}
2
3 \usepackage{standalone}
4 \usepackage{luacas}
5 \usepackage{amsmath}
6 \usepackage{amssymb}
7
8 \usepackage[margin=1in]{geometry}
9 \usepackage[shortlabels]{enumitem}
10
11 \usepackage{pgfplots}
12 \pgfplotsset{compat=1.18}
13 \usetikzlibrary{positioning,calc}
14 \usepackage{forest}
15 \usepackage{minted}
16 \usemintedstyle{pastie}
17 \usepackage[hidelinks]{hyperref}
18 \usepackage{parskip}
19 \usepackage{multicol}
20 \usepackage[most]{tcolorbox}
21 \tcbuselibrary{xparse}
22 \usepackage{microtype}
23
24 \usepackage[
25 backend=biber,
26 style=numeric,
27 ]{biblatex}
28 \addbibresource{sources.bib}
29
30 \newtcolorbox{codebox}[1][sidebyside]{
31 enhanced,skin=bicolor,
32 #1,
33 arc=1pt,
34 colframe=brown,
35 colback=brown!15,colbacklower=white,
36 boxrule=1pt,
37 notitle
38 }
39
40 \definecolor{rose}{RGB}{128,0,0}
41 \definecolor{roseyellow}{RGB}{222,205,99}
42 \definecolor{roseblue}{RGB}{167,188,214}
43 \definecolor{rosenavy}{RGB}{79,117,139}
44 \definecolor{roseorange}{RGB}{232,119,34}
45 \definecolor{rosegreen}{RGB}{61,68,30}
46 \definecolor{rosewhite}{RGB}{223,209,167}
47 \definecolor{rosebrown}{RGB}{108,87,27}
48 \definecolor{rosegray}{RGB}{84,88,90}
49
50 \begin{document}
51
52 \section{Tutorials}
53
54 Taking a cue from the phenomenal TikZ documentation, we introduce basic usage of the \texttt{luacas} package through a few informal tutorials. In the subsections that follow, we'll walk through how each of the outputs below are made using \texttt{luacas}. {\bf Crucially}, none of the computations below are ``hardcoded''; all computations are performed and printed using \texttt{luacas} to maximize portability and code reuse.
55
56 \begin{tcolorbox}[colback=rose!10,
57 colframe=rose,
58 arc=1pt,
59 frame hidden]
60 {\bf Tutorial 1:} {\itshape A limit definition of the derivative for Alice.}\vskip0.2cm
61 \small
62 \begin{CAS}
63 vars('x','h')
64 f = 2*x^3-x
65 \end{CAS}
66 Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using the limit definition of the derivative. Toward that end, we start with the appropriate difference quotient:
67 \begin{CAS}
68 subs = {[x] = x+h}
69 q = (f:substitute(subs) - f)/h
70 \end{CAS}
71 \[ \begin{aligned}
72 \print{q} &=
73 \begin{CAS}
74 q = expand(q)
75 \end{CAS}
76 \print{q}& &\text{expand/simplify} \\
77 \begin{CAS}
78 subs = {[h]=0}
79 q = q:substitute(subs)
80 \end{CAS}
81 &\xrightarrow{h\to 0} \print{q}& &\text{take limit} \\
82 &=
83 \begin{CAS}
84 q = simplify(q)
85 \end{CAS}
86 \print{q}& &\text{simplify.}
87 \end{aligned} \]
88 %So $\print{diff(f,x)} = \print*{diff(f,x)}$.
89 \end{tcolorbox}
90 \vfill
91
92 \begin{tcolorbox}[colback=rosenavy!10,
93 colframe=rosenavy,
94 arc=1pt,
95 frame hidden]
96 \small
97 {\bf Tutorial 2:} {\itshape A local max/min diagram for Bob}.
98 \vskip 0.2cm
99 Consider the function $f(x)$ defined by:
100 \begin{CAS}
101 vars('x')
102 f = x^2+2*x-2
103 g = x^2-1
104 subs = {[x] = f}
105 dh = expand(substitute(subs,g))
106 h = simplify(int(dh,x)+10)
107 \end{CAS}
108 $\displaystyle f(x) = \print{h}$.
109 \begin{multicols}{2}
110 Note that:
111 \[ f'(x) = \print{dh}.\]
112 The roots to $f'(x)=0$ equation are:
113 \begin{CAS}
114 r = roots(dh)
115 \end{CAS}
116 \[ \print{r[1]}, \quad \print{r[2]}, \quad \print{r[3]}, \quad \print{r[4]}.\]
117 Recall: $f'(x_0)$ measures the slope of the tangent line to $y=f(x)$ at $x=x_0$. The values $r$ where $f'(r)=0$ correspond to places where the slope of the tangent line to $y=f(x)$ is horizontal (see the illustration). This gives us a method for identifying locations where the graph $y=f(x)$ attains a peak (local maximum) or a valley (local minimum).
118 \directlua{
119 v = {}
120 for i=1,4 do
121 table.insert(v,simplify(substitute({[x]=r[i]},h)))
122 end
123 }
124 \columnbreak
125 \store{h}\store{dh}
126 \begin{tikzpicture}[scale=0.95]
127 \begin{axis}[legend pos = north west]
128 \addplot
129 [domain=-3.5:1.5,samples=100] {\h};
130 \addlegendentry{$f$};
131 \addplot[densely dashed]
132 [domain=-3.25:1.25,samples=100] {\dh};
133 \addlegendentry{$df/dx$};
134 \addplot[gray,dashed,thick]
135 [domain=-3.5:1.5] {0};
136 \luaexec{for i=1,4 do
137 tex.print("\\draw[fill=purple,purple]",
138 "(axis cs:{", tostring(r[i]) ,"},0) circle (1.5pt)",
139 "(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) circle (1.5pt)",
140 "(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) edge[dashed] (axis cs:{", tostring(r[i]) ,"},0);")
141 end}
142 \end{axis}
143 \end{tikzpicture}
144 \end{multicols}
145 \end{tcolorbox}
146
147 \vfill
148
149 \begin{tcolorbox}[colback=roseorange!10,
150 colframe=roseorange,
151 arc=1pt,
152 frame hidden]
153 \small
154 {\bf Tutorial 3:} {\itshape A limit definition of the derivative for Charlie.}\vskip 0.2cm
155
156 \begin{CAS}
157 vars('x','h')
158 f = x/(x^2+1)
159 \end{CAS}
160 Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using the limit definition of the derivative. Toward that end, we start with the appropriate difference quotient:
161 \begin{CAS}
162 subs = {[x] = x+h}
163 q = (f:substitute(subs) - f)/h
164 \end{CAS}
165 \directlua{
166 function Expression:mycombine()
167 local a = self.expressions[1].expressions[1].expressions[1]
168 local b = self.expressions[1].expressions[1].expressions[2]
169 local c = self.expressions[1].expressions[2].expressions[1]
170 local d = self.expressions[1].expressions[2].expressions[2]
171 local numerator = a*d-b*c
172 local denominator = self.expressions[2]*b*d
173 return numerator/denominator
174 end
175 function Expression:mysimplify()
176 local a = self.expressions[1]
177 local b = self.expressions[2]
178 a = simplify(a)
179 return a/b
180 end
181 function Expression:myfactor()
182 local a = self.expressions[1]
183 local b = self.expressions[2]
184 a = factor(a)
185 return a/b
186 end
187 }
188 \[ \begin{aligned}
189 \print{q} &=
190 \begin{CAS}
191 q = q:mycombine()
192 \end{CAS}
193 \print{q}& &\text{get a common denominator} \\
194 &=
195 \begin{CAS}
196 q = q:mysimplify()
197 \end{CAS}
198 \print{q}& &\text{simplify the numerator} \\
199 &=
200 \begin{CAS}
201 q = q:myfactor()
202 \end{CAS}
203 \print{q} & &\text{factor numerator} \\
204 &=
205 \begin{CAS}
206 q = simplify(q)
207 \end{CAS}
208 \print{q}& &\text{cancel the $h$s} \\
209 &\xrightarrow{h\to 0}
210 \begin{CAS}
211 subs = {[h] = 0}
212 q = substitute(subs,q):autosimplify()
213 \end{CAS}
214 \print{q}& &\text{take limit.}
215 \end{aligned} \]
216 \end{tcolorbox}
217 \end{document}