summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/tutorial/tut1/tut1.tex
blob: 1151d3e80b3efb9ff9570f28ee01cf1565a5fc32 (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
\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 1: Limit Definition of the Derivative}

Alice is teaching calculus, and she wants to give her students many examples of the dreaded \emph{limit definition of the derivative}. On the other hand, she'd like to avoid working out many examples by-hand. She decides to give \texttt{luacas} a try.

Alice can access the \texttt{luacas} program using a custom environment: \mintinline{latex}{\begin{CAS}..\end{CAS}}. The first thing Alice must do is declare variables that will be used going forward:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
\end{CAS}
\end{minted}
Alice decides that $f$, the function to be differentiated, should be $x^2$. So Alice makes this assignment with:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
\end{CAS}
\end{minted}
Now, Alice wants to use the variable $q$ to store the appropriate \emph{difference quotient} of $f$. Alice could hardcode this into $q$, but that seems to defeat the oft sought after goal of reusable code. So Alice decides to use the \texttt{substitute} command of \texttt{luacas}:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f) - f)/h
\end{CAS}
\end{minted}
Alice is curious to know if $q$ is what she thinks it is. So Alice decides to have \LaTeX{} print out the contents of $q$ within her document. For this, she uses the \mintinline{latex}{\print} command. 
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)- f)/h
\end{CAS}
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \[ \print{q} \] 
\end{codebox}
So far so good! Alice wants to expand the numerator of $q$; she finds the aptly named \texttt{expand} method helpful in this regard. Alice redefines \mintinline{lua}{q} to be \mintinline{lua}{q=expand(q)}, and prints the result to see if things worked as expected:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        q = expand(q)
    \end{CAS}
    \[ \print*{q} \] 
\end{codebox}
Alice is pleasantly surprised that the result of the expansion has been \emph{simplified}, i.e., the factors of $x^2$ and $-x^2$ cancelled each other out, and the resulting extra factor of $h$ has been cancelled out of the numerator and denominator.

Finally, Alice wants to take the limit as $h\to 0$. Now that our difference quotient has been expanded and simplified, this amounts to another substitution:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
    subs = {[h] = 0}
    q = substitute(subs,q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        subs = {[h]=0}
        q = q:substitute(subs)
    \end{CAS}
    \[ \print{q} \] 
\end{codebox}
Alice is slightly disappointed that $0+2x$ is returned and not $2x$. Alice takes a guess that there's a \mintinline{lua}{simplify} command. This does the trick: adding the line \mintinline{lua}{q = simplify(q)} before leaving the \texttt{CAS} environment returns the expected $2x$:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
    subs = {[h] = 0}
    q = substitute(subs,q)
    q = simplify(q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        q=simplify(q)
    \end{CAS}
    \[ \print{q} \] 
\end{codebox}

Alternatively, Alice could have used the \mintinline{latex}{\print*} command instead of \mintinline{latex}{\print} -- the essential difference is that \mintinline{latex}{\print*}, unlike \mintinline{latex}{\print}, automatically simplifies the content of the argument. 

Alice is pretty happy with how everything is working, but she wants to be able to typeset the individual steps of this process. Alice is therefore thrilled to learn that the \mintinline{latex}{\begin{CAS}..\end{CAS}} environment is very robust -- it can:
\begin{itemize}
    \item Be entered into and exited out of essentially anywhere within her \LaTeX{} document, for example, within \mintinline{latex}{\begin{aligned}..\end{aligned}}; and 
    \item CAS variables persist -- if Alice assigns \mintinline{lua}{f = x^2} within \mintinline{latex}{\begin{CAS}..\end{CAS}}, then the CAS remembers that \mintinline{lua}{f = x^2} the next time Alice enters the CAS environment. 
\end{itemize}
Here's Alice's completed code:
\begin{codebox}[frame hidden,breakable]
\begin{minted}[breaklines,fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
\end{CAS}
Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using thelimit definition of the derivative. Toward that end, we start with the appopriatedifference quotient:
\begin{CAS}
    subs = {[x]=x+h}
    q = (substitute(subs,f) - f)/h
\end{CAS}
\[ \begin{aligned}
    \print{q} &= 
    \begin{CAS} 
        q = expand(q) 
    \end{CAS}
    \print{q}& &\text{expand/simplify} \\
    \begin{CAS}
        subs = {[h]=0}
        q = substitute(subs,q)
    \end{CAS}
    &\xrightarrow{h\to 0} \print{q}& &\text{take limit}\\ 
    &= 
    \begin{CAS}
        q = simplify(q)
    \end{CAS}
    \print{q} & &\text{simplify.}
\end{aligned} \] 
So $\print{diff(f,x)} = \print*{diff(f,x)}$. 
\end{minted}
\end{codebox}
    
Alice can produce another example merely by changing the definition of $f$ on the third line to another polynomial:

\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = 2*x^3-x
\end{CAS}
\end{minted}
And here is Alice's completed project:
\begin{tcolorbox}[colback=rose!10,
    colframe=rose,
    arc=1pt,
    frame hidden]
{\bf Tutorial 1:} {\itshape A limit definition of the derivative for Alice.}\vskip0.2cm

\begin{CAS}
vars('x','h')
f = 2*x^3-x
\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}
\[ \begin{aligned}
\print{q} &= 
\begin{CAS} 
    q = expand(q)
\end{CAS}
\print{q}& &\text{expand/simplify} \\
\begin{CAS}
    subs = {[h]=0}
    q = q:substitute(subs)
\end{CAS}
&\xrightarrow{h\to 0} \print{q}& &\text{take limit} \\ 
&= 
\begin{CAS}
    q = simplify(q)
\end{CAS}
\print{q}& &\text{simplify.}
\end{aligned} \] 
%So $\print{diff(f,x)} = \print*{diff(f,x)}$.
\end{tcolorbox}

\end{document}