Debian Codegraph @

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

raw
1 \documentclass{article}
2
3 \usepackage{luacas}
4 \usepackage{amsmath}
5 \usepackage{amssymb}
6
7 \usepackage[margin=1in]{geometry}
8 \usepackage[shortlabels]{enumitem}
9
10 \usepackage{pgfplots}
11 \pgfplotsset{compat=1.18}
12 \usetikzlibrary{positioning,calc}
13 \usepackage{forest}
14 \usepackage{minted}
15 \usemintedstyle{pastie}
16 \usepackage[hidelinks]{hyperref}
17 \usepackage{parskip}
18 \usepackage{multicol}
19 \usepackage[most]{tcolorbox}
20 \tcbuselibrary{xparse}
21 \usepackage{microtype}
22
23 \definecolor{rose}{RGB}{128,0,0}
24 \definecolor{roseyellow}{RGB}{222,205,99}
25 \definecolor{roseblue}{RGB}{167,188,214}
26 \definecolor{rosenavy}{RGB}{79,117,139}
27 \definecolor{roseorange}{RGB}{232,119,34}
28 \definecolor{rosegreen}{RGB}{61,68,30}
29 \definecolor{rosewhite}{RGB}{223,209,167}
30 \definecolor{rosebrown}{RGB}{108,87,27}
31 \definecolor{rosegray}{RGB}{84,88,90}
32
33 \usepackage[
34 backend=biber,
35 style=numeric,
36 ]{biblatex}
37 \addbibresource{sources.bib}
38
39 \newtcolorbox{codebox}[1][sidebyside]{
40 enhanced,skin=bicolor,
41 #1,
42 arc=1pt,
43 colframe=brown,
44 colback=brown!15,colbacklower=white,
45 boxrule=1pt,
46 notitle
47 }
48
49 \begin{document}
50
51 \subsection{Tutorial 3: Adding Functionality}
52
53 Charlie, like Alice and Bob, is also teaching calculus. Charlie likes Alice's examples and wants to try something similar. But Charlie would like to do more involved examples using rational functions. Accordingly, Charlie copy-and-pastes Alice's code:
54 \begin{CAS}
55 vars('x','h')
56 f = 1/(x^2+1)
57 subs = {[x]=x+h}
58 q = (substitute(subs,f)-f)/h
59 q = expand(q)
60 \end{CAS}
61 \begin{minted}{latex}
62 \begin{CAS}
63 vars('x','h')
64 f = 1/(x^2+1)
65 subs = {[x]=x+h}
66 q = (substitute(subs,f)-f)/h
67 q = expand(q)
68 \end{CAS}
69 \end{minted}
70 Unfortunately, \mintinline{latex}{\[ q=\print{q} \]} produces:
71 \[ q = \print{q} \]
72 The \mintinline{lua}{simplify()} command doesn't seem to help either! What Charlie truly needs is to combine terms, i.e., Charlie needs to find a \emph{common denominator}. They're horrified to learn that no such functionality exists in this burgeoning package.
73
74 So what's Charlie to do? They could put a feature request in, but they're concerned that the schlubs in charge of managing the package won't get around to it until who-knows-when. So Charlie decides to take matters into their own hands. Besides, looking for that silver lining, they'll learn a little bit about how \texttt{luacas} is structured.
75
76 At the heart of any CAS is the idea of an \texttt{Expression}. Mathematically speaking, an \texttt{Expression} is a rooted tree. Luckily, this tree can be drawn using the (wonderful) \texttt{forest} package. In particular, the command \mintinline{latex}{\parseforest{q}} will scan the contents of the expression \texttt{q} and parse the results into a form compatible with the \texttt{forest} package; those results are saved in a macro named \mintinline{latex}{\forestresult}.
77
78 \tcbsidebyside[
79 sidebyside adapt=right,
80 enhanced,skin=bicolor,
81 arc=1pt,
82 colframe=brown,
83 colback=brown!15,colbacklower=white,
84 boxrule=1pt,
85 notitle,
86 frame hidden
87 ]{
88 \inputminted[
89 firstline = 1,
90 lastline = 13,
91 breaklines,
92 fontsize=\small
93 ]
94 {latex}
95 {demotut3.dat}}
96 {\parseforest{q}
97 \bracketset{action character = @}
98 \begin{forest}
99 for tree = {
100 font=\ttfamily,
101 rectangle,
102 rounded corners=1pt
103 },
104 where level=0{%
105 fill=orange!25
106 }{},
107 @\forestresult
108 \end{forest}}
109
110 The root of the tree above is \texttt{ADD} since $q$ is, at its heart, the addition of two other expressions. Charlie wonders how they might check to see if a mystery \texttt{Expression} is an \texttt{ADD}? But this is putting the cart before the horse; Charlie should truly wonder how to check for the \emph{type} of \texttt{Expression} -- then they can worry about other attributes.
111
112 Charlie can print the \texttt{Expression} type directly into their document using the \mintinline{latex}{\whatis} command:
113
114 \begin{codebox}
115 \inputminted[
116 firstline = 15,
117 lastline = 18,
118 breaklines,
119 fontsize=\small
120 ]
121 {latex}
122 {demotut3.dat}
123 \tcblower
124 \begin{CAS}
125 r = diff(q,x,h)
126 \end{CAS}
127 \whatis{q} vs \whatis{r}
128 \end{codebox}
129
130 So \texttt{q} is a \texttt{BinaryOperation}? This strikes Charlie as a little strange. On the other hand, \texttt{q} is the result of a binary operation applied to two other expressions; so perhaps this makes a modicum of sense.
131
132 At any rate, Charlie now knows, according to \texttt{luacas}, that \texttt{q} is of the \texttt{Expression}-type \whatis{q}. The actual operator that's used to form \texttt{q} is stored in the attribute \mintinline{lua}{q.operation}:
133
134 \tcbsidebyside[
135 sidebyside adapt=right,
136 enhanced,skin=bicolor,
137 arc=1pt,
138 colframe=brown,
139 colback=brown!15,colbacklower=white,
140 boxrule=1pt,
141 notitle,
142 frame hidden
143 ]{
144 \inputminted[
145 firstline = 20,
146 lastline = 22,
147 breaklines,
148 fontsize=\small
149 ]
150 {latex}
151 {demotut3.dat}}
152 {
153 \luaexec{
154 if q.operation == BinaryOperation.ADD then
155 tex.sprint("I'm an \\texttt{ADD}")
156 end
157 }}
158
159 Of course, different \texttt{Expression} types have different attributes. For example, being a \texttt{DiffExpression}, \texttt{r} has the attribute \texttt{r.degree}:
160
161 \tcbsidebyside[
162 sidebyside adapt=right,
163 enhanced,skin=bicolor,
164 arc=1pt,
165 colframe=brown,
166 colback=brown!15,colbacklower=white,
167 boxrule=1pt,
168 notitle,
169 frame hidden
170 ]{
171 \inputminted[
172 firstline = 24,
173 lastline = 24,
174 breaklines,
175 fontsize=\small
176 ]
177 {latex}
178 {demotut3.dat}}
179 {
180 \luaexec{
181 tex.print("I'm an order", r.degree, "derivative.")
182 }}
183
184 \texttt{BinaryOperation}s have several attributes, but the most important attribute for Charlie's purposes is \texttt{q.expressions}. In this case, \texttt{q.expressions} is a table with two entries; those two entries are precisely the \texttt{Expressions} whose sum forms \texttt{q}. In particular,
185
186 \mintinline{latex}{\[ \print{q.expressions[1]} \qquad \text{and} \qquad \print{q.expressions[2]} \]}
187
188 produces:
189 \[ \print{q.expressions[1]} \qquad\text{and} \qquad \print{q.expressions[2]} \]
190
191 The expression \texttt{q.expressions[1]} is another \texttt{BinaryOperation}. Instead of printing the entire expression tree (as we've done above), Charlie might be interested in the commands \mintinline{latex}{\parseshrub} and \mintinline{latex}{\shrubresult}:
192
193 \tcbsidebyside[
194 sidebyside adapt=right,
195 enhanced,skin=bicolor,
196 arc=1pt,
197 colframe=brown,
198 colback=brown!15,colbacklower=white,
199 boxrule=1pt,
200 notitle,
201 frame hidden
202 ]{\small
203 \inputminted[
204 firstline = 26,
205 lastline = 30,
206 breaklines
207 ]
208 {latex}
209 {demotut3.dat}}
210 {\parseshrub{q.expressions[1]}
211 \bracketset{action character = @}
212 \begin{forest}
213 for tree = {draw,rectangle,rounded corners=1pt,fill=lightgray!20,font=\ttfamily, s sep=1.5cm}
214 @\shrubresult
215 \end{forest}
216 }
217
218 The ``shrub'' is essentially the first level of the ``forest'', but with some extra information concerning attributes. For contrast, here's the result of \mintinline{latex}{\parseshrub} and \mintinline{latex}{\shrubresult} applied to \texttt{r}, the \texttt{DiffExpression} defined above.
219
220 \tcbsidebyside[
221 sidebyside adapt=right,
222 enhanced,skin=bicolor,
223 arc=1pt,
224 colframe=brown,
225 colback=brown!15,colbacklower=white,
226 boxrule=1pt,
227 notitle,
228 frame hidden
229 ]{\small
230 \inputminted[
231 firstline = 40,
232 lastline = 46,
233 breaklines
234 ]
235 {latex}
236 {demotut3.dat}}
237 {\parseshrub{r}
238 \bracketset{action character = @}
239 \begin{forest}
240 for tree = {draw,rectangle,rounded corners=1pt,
241 fill=lightgray!20,font=\ttfamily, s sep=1.5cm}
242 @\shrubresult
243 \end{forest}
244 }
245 The attribute \mintinline{lua}{r.degree} returns the size of the table stored in \mintinline{lua}{r.symbols} which, in turn, records the variables (and order from left-to-right) with which to differentiate the expression stored in \mintinline{lua}{r.expression}.
246
247 Now that Charlie knows the basics of how \texttt{luacas} is structured, they're ready to try their hand at adding some functionality.
248
249
250 \begin{multicols}{2}
251 First, Charlie decides to up the complexity of their expression {\ttfamily f} so that they have something more general to work with:
252 \begin{CAS}
253 vars('x','h')
254 f = x/(x^2+1)
255 subs = {[x]=x+h}
256 q = (substitute(subs,f)-f)/h
257 \end{CAS}
258 \begin{minted}{latex}
259 \begin{CAS}
260 vars('x','h')
261 f = x/(x^2+1)
262 subs = {[x]=x+h}
263 q = (substitute(subs,f)-f)/h
264 \end{CAS}
265 \end{minted}
266 Next, Charlie decides to print the un\texttt{expand}ed expression tree for $q$ to help give them a clear view (see right).
267
268 \begin{center}
269 \parseforest{q}
270 \bracketset{action character = @}
271 \begin{forest}
272 for tree = {
273 font=\ttfamily}
274 @\forestresult
275 \end{forest}
276 \end{center}
277 \end{multicols}
278
279 Charlie now wants to write their own function for combining expressions like this into a single denominator. It's probably best that Charlie writes this function in a separate file, say \texttt{myfile.lua}. Like most functions in \texttt{luacas}, Charlie defines this function as a \emph{method} applied to an \texttt{Expression}:
280
281 \begin{minted}[linenos]{lua}
282 function Expression:mycombine()
283 \end{minted}
284
285 Next, Charlie declares some local variables to identify appropriate numerators and denominators:
286 \begin{minted}[linenos,firstnumber=2]{lua}
287 local a = self.expressions[1].expressions[1].expressions[1]
288 local b = self.expressions[1].expressions[1].expressions[2]
289 local c = self.expressions[1].expressions[2].expressions[1]
290 local d = self.expressions[1].expressions[2].expressions[2]
291 \end{minted}
292
293 So, for example, $a = x+h$, $b = (x+h)^2+1$, and so on. Charlie now forms the numerator and denominator, and returns the function:
294
295 \begin{minted}[linenos,firstnumber=6]{lua}
296 local numerator = a*d-b*c
297 local denominator = self.expressions[2]*b*d
298 return numerator/denominator
299 end
300 \end{minted}
301
302 \luaexec{
303 function Expression:mycombine()
304 local a = self.expressions[1].expressions[1].expressions[1]
305 local b = self.expressions[1].expressions[1].expressions[2]
306 local c = self.expressions[1].expressions[2].expressions[1]
307 local d = self.expressions[1].expressions[2].expressions[2]
308 local numerator = a*d-b*c
309 local denominator = self.expressions[2]*b*d
310 return numerator/denominator
311 end
312 }
313 Now Charlie only needs to ensure that \texttt{myfile.lua} is in a location visible to their TeX installation (e.g. in the current working folder). Charlie can then produce the following:
314
315 \begin{codebox}
316 \begin{minted}[fontsize=\small]{latex}
317 \directlua{dofile('myfile.lua')}
318 \begin{CAS}
319 q = q:mycombine()
320 \end{CAS}
321 \[ \print{q} \]
322 \end{minted}
323 \tcblower
324 \begin{CAS}
325 q = q:mycombine()
326 \end{CAS}
327 \[ \print{q}\]
328 \end{codebox}
329
330 Charlie wants to simplify the numerator (but not the denominator). So they decide to write another function in \texttt{myfile.lua} that does precisely this:
331
332 \begin{minted}[linenos,firstnumber = 11]{lua}
333 function Expression:mysimplify()
334 local a = self.expressions[1]
335 local b = self.expressions[2]
336 a = simplify(a)
337 return a/b
338 end
339 \end{minted}
340 \luaexec{
341 function Expression:simplifynum()
342 local a = self.expressions[1]
343 local b = self.expressions[2]
344 a = simplify(a)
345 return a/b
346 end
347 }
348 Now Charlie has:
349 \begin{codebox}
350 \begin{minted}[fontsize=\small]{latex}
351 \begin{CAS}
352 q = q:mysimplify()
353 \end{CAS}
354 \[ \print{q} \]
355 \end{minted}
356 \tcblower
357 \begin{CAS}
358 q = q:simplifynum()
359 \end{CAS}
360 \[ \print{q} \]
361 \end{codebox}
362 Finally, Charlie wants to factor the numerator. So Charlie writes the following final function to \texttt{myfile.lua}:
363 \begin{minted}[linenos,firstnumber = 18]{lua}
364 function Expression:myfactor()
365 local a = self.expressions[1]
366 local b = self.expressions[2]
367 a = factor(a)
368 return a/b
369 end
370 \end{minted}
371
372 \luaexec{
373 function Expression:factornum()
374 local a = self.expressions[1]
375 local b = self.expressions[2]
376 a = factor(a)
377 return a/b
378 end
379 }
380 \begin{codebox}
381 \begin{minted}[fontsize=\small]{latex}
382 After factoring the numerator:
383 \begin{CAS}
384 q = q:myfactor()
385 \end{CAS}
386 \[ \print{q} \]
387 And then simplifying:
388 \begin{CAS}
389 q = simplify(q)
390 \end{CAS}
391 \[ \print{q} \]
392 \end{minted}
393 \tcblower
394 After factoring the numerator:
395 \begin{CAS}
396 q = q:factornum()
397 \end{CAS}
398 \[ \print{q} \]
399 And then simplifying:
400 \begin{CAS}
401 q = simplify(q)
402 \end{CAS}
403 \[ \print{q} \]
404 \end{codebox}
405 Armed with their custom functions \texttt{mycombine}, \texttt{mysimplify}, and \texttt{myfactor}, Charlie can write examples just like Alice's examples, but using rational functions instead.
406
407 Of course, the schlubs that manage this package feel for Charlie, and recognize that there are other situations in which folks may want to combine a sum of rational expressions into a single rational expression. Accordingly, there is indeed a \texttt{combine} command included in \texttt{luacas} that performs this task:
408 \begin{codebox}
409 \begin{minted}[fontsize=\small]{latex}
410 \begin{CAS}
411 vars('x','y','z')
412 a = y/z
413 b = z/x
414 c = x/y
415 d = combine(a+b+c)
416 \end{CAS}
417 \[ \print{a+b+c} = \print{d} \]
418 \end{minted}
419 \tcblower
420 \begin{CAS}
421 vars('x','y','z')
422 a = y/z
423 b = z/x
424 c = x/y
425 d = combine(a+b+c)
426 \end{CAS}
427 \[ \print{a+b+c} = \print{d} \]
428 \end{codebox}
429
430 Here's Charlie's complete code (but using \mintinline{latex}{\directlua}) instead:
431
432 \begin{codebox}[frame hidden,breakable]
433 \begin{minted}[breaklines,fontsize=\small]{latex}
434 \begin{CAS}
435 vars('x','h')
436 f = x/(x^2+1)
437 \end{CAS}
438 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:
439 \begin{CAS}
440 subs = {[x] = x+h}
441 q = (f:substitute(subs) - f)/h
442 \end{CAS}
443 \directlua{
444 \end{minted}
445 \end{codebox}
446 And now the Lua code:
447 \begin{codebox}[frame hidden, breakable]
448 \begin{minted}[breaklines,fontsize=\small]{lua}
449 function Expression:mycombine()
450 local a = self.expressions[1].expressions[1].expressions[1]
451 local b = self.expressions[1].expressions[1].expressions[2]
452 local c = self.expressions[1].expressions[2].expressions[1]
453 local d = self.expressions[1].expressions[2].expressions[2]
454 local numerator = a*d-b*c
455 local denominator = self.expressions[2]*b*d
456 return numerator/denominator
457 end
458 function Expression:mysimplify()
459 local a = self.expressions[1]
460 local b = self.expressions[2]
461 a = simplify(a)
462 return a/b
463 end
464 function Expression:myfactor()
465 local a = self.expressions[1]
466 local b = self.expressions[2]
467 a = factor(a)
468 return a/b
469 end
470 \end{minted}
471 \end{codebox}
472 And now back to the \LaTeX{} code:
473 \begin{codebox}[frame hidden,breakable]
474 \begin{minted}[breaklines,fontsize=\small]{latex}
475 }
476 \[ \begin{aligned}
477 \print{q} &=
478 \begin{CAS}
479 q = q:mycombine()
480 \end{CAS}
481 \print{q}& &\text{get a common denominator} \\
482 &=
483 \begin{CAS}
484 q = q:mysimplify()
485 \end{CAS}
486 \print{q}& &\text{simplify the numerator} \\
487 &=
488 \begin{CAS}
489 q = q:myfactor()
490 \end{CAS}
491 \print{q} & &\text{factor numerator} \\
492 &=
493 \begin{CAS}
494 q = simplify(q)
495 \end{CAS}
496 \print{q}& &\text{cancel the $h$s} \\
497 &\xrightarrow{h\to 0}
498 \begin{CAS}
499 subs = {[h] = 0}
500 q = substitute(subs,q):autosimplify()
501 \end{CAS}
502 \print{q}& &\text{take limit.}
503 \end{aligned} \]
504 \end{minted}
505 \end{codebox}
506 And here is Charlie's completed project:
507 \begin{tcolorbox}[colback=roseorange!10,
508 colframe=roseorange,
509 arc=1pt,
510 frame hidden]
511 {\bf Tutorial 3:} {\itshape A limit definition of the derivative for Charlie.}\vskip 0.2cm
512
513 \begin{CAS}
514 vars('x','h')
515 f = x/(x^2+1)
516 \end{CAS}
517 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:
518 \begin{CAS}
519 subs = {[x] = x+h}
520 q = (f:substitute(subs) - f)/h
521 \end{CAS}
522 \directlua{
523 function Expression:mycombine()
524 local a = self.expressions[1].expressions[1].expressions[1]
525 local b = self.expressions[1].expressions[1].expressions[2]
526 local c = self.expressions[1].expressions[2].expressions[1]
527 local d = self.expressions[1].expressions[2].expressions[2]
528 local numerator = a*d-b*c
529 local denominator = self.expressions[2]*b*d
530 return numerator/denominator
531 end
532 function Expression:mysimplify()
533 local a = self.expressions[1]
534 local b = self.expressions[2]
535 a = simplify(a)
536 return a/b
537 end
538 function Expression:myfactor()
539 local a = self.expressions[1]
540 local b = self.expressions[2]
541 a = factor(a)
542 return a/b
543 end
544 }
545 \[ \begin{aligned}
546 \print{q} &=
547 \begin{CAS}
548 q = q:mycombine()
549 \end{CAS}
550 \print{q}& &\text{get a common denominator} \\
551 &=
552 \begin{CAS}
553 q = q:mysimplify()
554 \end{CAS}
555 \print{q}& &\text{simplify the numerator} \\
556 &=
557 \begin{CAS}
558 q = q:myfactor()
559 \end{CAS}
560 \print{q} & &\text{factor numerator} \\
561 &=
562 \begin{CAS}
563 q = simplify(q)
564 \end{CAS}
565 \print{q}& &\text{cancel the $h$s} \\
566 &\xrightarrow{h\to 0}
567 \begin{CAS}
568 subs = {[h] = 0}
569 q = substitute(subs,q):autosimplify()
570 \end{CAS}
571 \print{q}& &\text{take limit.}
572 \end{aligned} \]
573 \end{tcolorbox}
574
575 \end{document}