summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/tutorial/tut.tex
blob: 3e12ec919d659a4b5d2f781df00b5938521fec39 (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
\documentclass{article}

\usepackage{standalone}
\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}

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

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

\begin{document}

\section{Tutorials}

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. 

\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
\small
\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}
\vfill

\begin{tcolorbox}[colback=rosenavy!10,
    colframe=rosenavy,
    arc=1pt,
    frame hidden]
    \small
    {\bf Tutorial 2:} {\itshape A local max/min diagram for Bob}.
    \vskip 0.2cm
    Consider the function $f(x)$ defined by:
    \begin{CAS}
        vars('x')
        f = x^2+2*x-2
        g = x^2-1
        subs = {[x] = f}
        dh = expand(substitute(subs,g))
        h = simplify(int(dh,x)+10)
    \end{CAS}
    $\displaystyle f(x) = \print{h}$.
\begin{multicols}{2}
    Note that: 
    \[ f'(x) = \print{dh}.\] 
    The roots to $f'(x)=0$ equation are:
    \begin{CAS}
        r = roots(dh)
    \end{CAS}
    \[ \print{r[1]}, \quad \print{r[2]}, \quad \print{r[3]}, \quad \print{r[4]}.\] 
    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). 
    \directlua{
    v = {}
    for i=1,4 do 
        table.insert(v,simplify(substitute({[x]=r[i]},h)))
    end
    }
    \columnbreak 
    \store{h}\store{dh}
    \begin{tikzpicture}[scale=0.95]
        \begin{axis}[legend pos = north west]
            \addplot 
            [domain=-3.5:1.5,samples=100] {\h};
            \addlegendentry{$f$};
            \addplot[densely dashed] 
            [domain=-3.25:1.25,samples=100] {\dh};
            \addlegendentry{$df/dx$};
            \addplot[gray,dashed,thick]
            [domain=-3.5:1.5] {0};
            \luaexec{for i=1,4 do 
                tex.print("\\draw[fill=purple,purple]",
                    "(axis cs:{", tostring(r[i]) ,"},0) circle (1.5pt)",
                    "(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) circle (1.5pt)",
                    "(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) edge[dashed] (axis cs:{", tostring(r[i]) ,"},0);")
                end}
        \end{axis}
    \end{tikzpicture}
\end{multicols}
\end{tcolorbox}

\vfill

\begin{tcolorbox}[colback=roseorange!10,
    colframe=roseorange,
    arc=1pt,
    frame hidden]
    \small
{\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}