summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/tutorial/tut3/tut3.tex
blob: 1fda71a5a0a4593b4bbafbc11505a7f26446966a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
\documentclass{article}

\usepackage{luacas}
\usepackage{amsmath}
\usepackage{amssymb}

\usepackage[margin=1in]{geometry}
\usepackage[shortlabels]{enumitem}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{positioning,calc}
\usepackage{forest}
\usepackage{minted}
\usemintedstyle{pastie}
\usepackage[hidelinks]{hyperref}
\usepackage{parskip}
\usepackage{multicol}
\usepackage[most]{tcolorbox}
    \tcbuselibrary{xparse}
\usepackage{microtype}

\definecolor{rose}{RGB}{128,0,0}
\definecolor{roseyellow}{RGB}{222,205,99}
\definecolor{roseblue}{RGB}{167,188,214}
\definecolor{rosenavy}{RGB}{79,117,139}
\definecolor{roseorange}{RGB}{232,119,34}
\definecolor{rosegreen}{RGB}{61,68,30}
\definecolor{rosewhite}{RGB}{223,209,167}
\definecolor{rosebrown}{RGB}{108,87,27}
\definecolor{rosegray}{RGB}{84,88,90}

\usepackage[
backend=biber,
style=numeric,
]{biblatex}
\addbibresource{sources.bib}

\newtcolorbox{codebox}[1][sidebyside]{
    enhanced,skin=bicolor,
    #1,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle
}

\begin{document}

\subsection{Tutorial 3: Adding Functionality}

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:
\begin{CAS}
    vars('x','h')
    f = 1/(x^2+1)
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
\end{CAS}
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = 1/(x^2+1)
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
\end{CAS}
\end{minted}
Unfortunately, \mintinline{latex}{\[ q=\print{q} \]} produces:
\[ q = \print{q} \]
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. 

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. 

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}. 

\tcbsidebyside[ 
    sidebyside adapt=right,
    enhanced,skin=bicolor,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle,
    frame hidden
    ]{
    \inputminted[ 
        firstline = 1,
        lastline = 13,
        breaklines,
        fontsize=\small
    ]
    {latex}
    {demotut3.dat}}
    {\parseforest{q}
    \bracketset{action character = @}
    \begin{forest}
        for tree = {
            font=\ttfamily,
            rectangle,
            rounded corners=1pt
        },
        where level=0{%
            fill=orange!25
        }{},
        @\forestresult
    \end{forest}}

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. 

Charlie can print the \texttt{Expression} type directly into their document using the \mintinline{latex}{\whatis} command:

\begin{codebox}
    \inputminted[ 
        firstline = 15,
        lastline = 18,
        breaklines,
        fontsize=\small
    ]
    {latex}
    {demotut3.dat}
    \tcblower
    \begin{CAS}
        r = diff(q,x,h)
    \end{CAS}
    \whatis{q} vs \whatis{r}
\end{codebox}

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. 

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}:

\tcbsidebyside[ 
    sidebyside adapt=right,
    enhanced,skin=bicolor,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle,
    frame hidden
    ]{
    \inputminted[ 
        firstline = 20,
        lastline = 22,
        breaklines,
        fontsize=\small
    ]
    {latex}
    {demotut3.dat}}
    {
    \luaexec{
    if q.operation == BinaryOperation.ADD then 
        tex.sprint("I'm an \\texttt{ADD}")
    end
    }}

Of course, different \texttt{Expression} types have different attributes. For example, being a \texttt{DiffExpression}, \texttt{r} has the attribute \texttt{r.degree}:

\tcbsidebyside[ 
    sidebyside adapt=right,
    enhanced,skin=bicolor,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle,
    frame hidden
    ]{
    \inputminted[ 
        firstline = 24,
        lastline = 24,
        breaklines,
        fontsize=\small
    ]
    {latex}
    {demotut3.dat}}
    {
    \luaexec{
        tex.print("I'm an order", r.degree, "derivative.")
    }}

\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, 

\mintinline{latex}{\[ \print{q.expressions[1]} \qquad \text{and} \qquad \print{q.expressions[2]} \]}

produces:
\[ \print{q.expressions[1]} \qquad\text{and} \qquad \print{q.expressions[2]} \] 

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}:

\tcbsidebyside[ 
    sidebyside adapt=right,
    enhanced,skin=bicolor,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle,
    frame hidden
    ]{\small
    \inputminted[ 
        firstline = 26,
        lastline = 30,
        breaklines
    ]
    {latex}
    {demotut3.dat}}
    {\parseshrub{q.expressions[1]}
     \bracketset{action character = @}
        \begin{forest}
            for tree = {draw,rectangle,rounded corners=1pt,fill=lightgray!20,font=\ttfamily, s sep=1.5cm}
            @\shrubresult
    \end{forest}
}

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. 

\tcbsidebyside[ 
    sidebyside adapt=right,
    enhanced,skin=bicolor,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle,
    frame hidden
    ]{\small
    \inputminted[ 
        firstline = 40,
        lastline = 46,
        breaklines
    ]
    {latex}
    {demotut3.dat}}
    {\parseshrub{r}
    \bracketset{action character = @}
    \begin{forest}
        for tree = {draw,rectangle,rounded corners=1pt,
            fill=lightgray!20,font=\ttfamily, s sep=1.5cm}
        @\shrubresult
    \end{forest}
}
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}.

Now that Charlie knows the basics of how \texttt{luacas} is structured, they're ready to try their hand at adding some functionality.


\begin{multicols}{2}
First, Charlie decides to up the complexity of their expression {\ttfamily f} so that they have something more general to work with:
\begin{CAS}
    vars('x','h')
    f = x/(x^2+1)
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
\end{CAS}
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = x/(x^2+1)
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
\end{CAS}
\end{minted}
Next, Charlie decides to print the un\texttt{expand}ed expression tree for $q$ to help give them a clear view (see right). 

\begin{center}
\parseforest{q}
\bracketset{action character = @}
\begin{forest}
    for tree = {
        font=\ttfamily}
    @\forestresult
\end{forest}
\end{center}
\end{multicols}

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}:

\begin{minted}[linenos]{lua}
function Expression:mycombine()
\end{minted}

Next, Charlie declares some local variables to identify appropriate numerators and denominators:
\begin{minted}[linenos,firstnumber=2]{lua}
    local a = self.expressions[1].expressions[1].expressions[1]
    local b = self.expressions[1].expressions[1].expressions[2]
    local c = self.expressions[1].expressions[2].expressions[1]
    local d = self.expressions[1].expressions[2].expressions[2]
\end{minted}

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:

\begin{minted}[linenos,firstnumber=6]{lua}
    local numerator = a*d-b*c
    local denominator = self.expressions[2]*b*d
    return numerator/denominator
end
\end{minted}

\luaexec{
    function Expression:mycombine()
        local a = self.expressions[1].expressions[1].expressions[1]
        local b = self.expressions[1].expressions[1].expressions[2]
        local c = self.expressions[1].expressions[2].expressions[1]
        local d = self.expressions[1].expressions[2].expressions[2]
        local numerator = a*d-b*c
        local denominator = self.expressions[2]*b*d
        return numerator/denominator
    end
}
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:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\directlua{dofile('myfile.lua')}
\begin{CAS}
    q = q:mycombine()
\end{CAS}
\[ \print{q} \]         
    \end{minted}
    \tcblower
    \begin{CAS}
        q = q:mycombine()
    \end{CAS}
    \[ \print{q}\]
\end{codebox}

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:

\begin{minted}[linenos,firstnumber = 11]{lua}
function Expression:mysimplify()
    local a = self.expressions[1]
    local b = self.expressions[2]
    a = simplify(a)
    return a/b 
end
\end{minted}
\luaexec{
function Expression:simplifynum()
    local a = self.expressions[1]
    local b = self.expressions[2]
    a = simplify(a)
    return a/b 
end
}
Now Charlie has:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    q = q:mysimplify()
\end{CAS}
\[ \print{q} \] 
\end{minted}
\tcblower
\begin{CAS}
    q = q:simplifynum()
\end{CAS}
\[ \print{q} \] 
\end{codebox}
Finally, Charlie wants to factor the numerator. So Charlie writes the following final function to \texttt{myfile.lua}:
\begin{minted}[linenos,firstnumber = 18]{lua}
function Expression:myfactor()
    local a = self.expressions[1]
    local b = self.expressions[2]
    a = factor(a)
    return a/b
end
\end{minted}

\luaexec{
function Expression:factornum()
    local a = self.expressions[1]
    local b = self.expressions[2]
    a = factor(a)
    return a/b
end
}
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
After factoring the numerator:
\begin{CAS}
    q = q:myfactor()
\end{CAS}
\[ \print{q} \] 
And then simplifying:
\begin{CAS}
    q = simplify(q)
\end{CAS}
\[ \print{q} \] 
\end{minted}
\tcblower
After factoring the numerator:
\begin{CAS}
    q = q:factornum()
\end{CAS}
\[ \print{q} \] 
And then simplifying:
\begin{CAS}
    q = simplify(q)
\end{CAS}
\[ \print{q} \] 
\end{codebox}
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. 

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:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','y','z')
    a = y/z
    b = z/x
    c = x/y
    d = combine(a+b+c)
\end{CAS}
\[ \print{a+b+c} = \print{d} \] 
\end{minted}
\tcblower
\begin{CAS}
    vars('x','y','z')
    a = y/z
    b = z/x
    c = x/y
    d = combine(a+b+c)
\end{CAS}
\[ \print{a+b+c} = \print{d} \] 
\end{codebox}

Here's Charlie's complete code (but using \mintinline{latex}{\directlua}) instead:

\begin{codebox}[frame hidden,breakable]
\begin{minted}[breaklines,fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x/(x^2+1)
\end{CAS}
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:
\begin{CAS}
    subs = {[x] = x+h}
    q = (f:substitute(subs) - f)/h
\end{CAS}
\directlua{
\end{minted}
\end{codebox}
And now the Lua code:
\begin{codebox}[frame hidden, breakable]
\begin{minted}[breaklines,fontsize=\small]{lua}
    function Expression:mycombine()
        local a = self.expressions[1].expressions[1].expressions[1]
        local b = self.expressions[1].expressions[1].expressions[2]
        local c = self.expressions[1].expressions[2].expressions[1]
        local d = self.expressions[1].expressions[2].expressions[2]
        local numerator = a*d-b*c
        local denominator = self.expressions[2]*b*d
        return numerator/denominator
    end
    function Expression:mysimplify()
        local a = self.expressions[1]
        local b = self.expressions[2]
        a = simplify(a)
        return a/b 
    end
    function Expression:myfactor()
        local a = self.expressions[1]
        local b = self.expressions[2]
        a = factor(a)
        return a/b
    end
\end{minted}
\end{codebox}
And now back to the \LaTeX{} code:
\begin{codebox}[frame hidden,breakable]
\begin{minted}[breaklines,fontsize=\small]{latex}
}
\[ \begin{aligned} 
    \print{q} &=
    \begin{CAS}
        q = q:mycombine()
    \end{CAS}
    \print{q}& &\text{get a common denominator} \\ 
    &= 
    \begin{CAS}
        q = q:mysimplify()
    \end{CAS}
    \print{q}& &\text{simplify the numerator} \\ 
    &= 
    \begin{CAS}
        q = q:myfactor()
    \end{CAS}
    \print{q} & &\text{factor numerator} \\ 
    &=
    \begin{CAS}
        q = simplify(q)
    \end{CAS}
    \print{q}& &\text{cancel the $h$s} \\ 
    &\xrightarrow{h\to 0}
    \begin{CAS}
        subs = {[h] = 0}
        q = substitute(subs,q):autosimplify()
    \end{CAS}
    \print{q}& &\text{take limit.}
\end{aligned} \] 
\end{minted}
\end{codebox}
And here is Charlie's completed project:
\begin{tcolorbox}[colback=roseorange!10,
    colframe=roseorange,
    arc=1pt,
    frame hidden]
{\bf Tutorial 3:} {\itshape A limit definition of the derivative for Charlie.}\vskip 0.2cm

\begin{CAS}
    vars('x','h')
    f = x/(x^2+1)
\end{CAS}
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:
\begin{CAS}
    subs = {[x] = x+h}
    q = (f:substitute(subs) - f)/h
\end{CAS}
\directlua{
    function Expression:mycombine()
        local a = self.expressions[1].expressions[1].expressions[1]
        local b = self.expressions[1].expressions[1].expressions[2]
        local c = self.expressions[1].expressions[2].expressions[1]
        local d = self.expressions[1].expressions[2].expressions[2]
        local numerator = a*d-b*c
        local denominator = self.expressions[2]*b*d
        return numerator/denominator
    end
    function Expression:mysimplify()
        local a = self.expressions[1]
        local b = self.expressions[2]
        a = simplify(a)
        return a/b 
    end
    function Expression:myfactor()
        local a = self.expressions[1]
        local b = self.expressions[2]
        a = factor(a)
        return a/b
    end
}
\[ \begin{aligned} 
    \print{q} &=
    \begin{CAS}
        q = q:mycombine()
    \end{CAS}
    \print{q}& &\text{get a common denominator} \\ 
    &= 
    \begin{CAS}
        q = q:mysimplify()
    \end{CAS}
    \print{q}& &\text{simplify the numerator} \\ 
    &= 
    \begin{CAS}
        q = q:myfactor()
    \end{CAS}
    \print{q} & &\text{factor numerator} \\ 
    &=
    \begin{CAS}
        q = simplify(q)
    \end{CAS}
    \print{q}& &\text{cancel the $h$s} \\ 
    &\xrightarrow{h\to 0}
    \begin{CAS}
        subs = {[h] = 0}
        q = substitute(subs,q):autosimplify()
    \end{CAS}
    \print{q}& &\text{take limit.}
\end{aligned} \] 
\end{tcolorbox}

\end{document}