summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex
blob: 58d9b69f843556754624d235aa599ce0bca5bdce (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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
\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,documentation}
\usepackage{microtype}
\usepackage{makeidx}
\usepackage{fontawesome}

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

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

\definecolor{codegreen}{HTML}{49BE25}

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

\newtcolorbox{codehead}[1][]{
    enhanced,
    frame hidden,
    colback=rosegray!15,
    boxrule=0mm,
    leftrule=5mm,
    rightrule=5mm,
    boxsep=0mm,
    arc=0mm,
    outer arc=0mm,
    left=3mm,
    right=3mm,
    top=1mm,
    bottom=1mm,
    toptitle=1mm,
    bottomtitle=1mm,
    oversize,
    #1
}

\usepackage{varwidth}

\newtcolorbox{newcodehead}[2][]{
    enhanced,
    frame hidden,
    colback=rosegray!15,
    boxrule=0mm,
    leftrule=5mm,
    rightrule=5mm,
    boxsep=0mm,
    arc=0mm,
    outer arc=0mm,
    left=3mm,
    right=3mm,
    top=1mm,
    bottom=1mm,
    toptitle=1mm,
    bottomtitle=1mm,
    oversize,
    #1,
    fonttitle=\bfseries\ttfamily\footnotesize,
    coltitle=rosegray,
    attach boxed title to top text right,
    boxed title style={frame hidden,size=small,bottom=-1mm,
    interior style={fill=none,
    top color=white,
    bottom color=white}},
    title={#2}
}

\makeindex

\newcommand{\coderef}[2]{%
\begin{codehead}[sidebyside,segmentation hidden]%
    \mintinline{lua}{#1}%
    \tcblower%
    \begin{flushright}%
    \mintinline{lua}{#2}%
    \end{flushright}%
\end{codehead}%
}

\newcommand{\newcoderef}[3]{%
\begin{newcodehead}[sidebyside,segmentation hidden]{#3}%
    \mintinline{lua}{#1}%
    \tcblower%
    \begin{flushright}%
    \mintinline{lua}{#2}%
    \end{flushright}%
\end{newcodehead}%
}

\begin{document}
\setdescription{style=multiline,
        topsep=10pt,
        leftmargin=5cm,
        }

\subsection{Algebra Classes}

The algebra package contains functionality for arbitrary-precision arithmetic, polynomial arithmetic and factoring, symbolic root finding, and logarithm and trigonometric expression classes. It requires the core package to be loaded.

The abstract classes in the algebra module all inherit from the \texttt{ConstantExpression} branch in the inheritance tree:

\begin{itemize}
    \item \texttt{Ring}
    \item \texttt{EuclideanDomain}
    \item \texttt{Field}
\end{itemize}

The {\ttfamily EuclideanDomain} class is a sub-class to the {\ttfamily Ring} class, and the {\ttfamily Field} class is a sub-class to the {\ttfamily EuclideanDomain} class.

The following concrete classes inherit from the {\ttfamily Ring} class (or one of the sub-classes mentioned above). However, not all of them are proper {\ttfamily ConstantExpression}s, so some of them override the {\ttfamily isconstant()} method.

\begin{itemize}
    \item {\ttfamily Integer}
    \item {\ttfamily IntegerModN}
    \item {\ttfamily Rational}
    \item {\ttfamily PolynomialRing}
\end{itemize}

The other concrete classes in the Algebra package do not inherit from the {\ttfamily Ring} interface, instead they inherit from the {\ttfamily CompoundExpression} interface:

\begin{multicols}{2}
\begin{itemize}
    \item {\ttfamily AbsExpression}
    \item {\ttfamily Logarithm}
    \item {\ttfamily FactorialExpression}
    \item {\ttfamily SqrtExpression}
    \item {\ttfamily TrigExpression}
    \item {\ttfamily RootExpression}
    \item {\ttfamily Equation}
\end{itemize}
\end{multicols}

\newcoderef{function Integer:new(n)}{return Integer}{n number|string|Integer}
\index{Algebra!Classes!\texttt{SymbolExpression}}
\addcontentsline{toc}{subsubsection}{\ttfamily Integer}

Takes a \texttt{string}, \texttt{number}, or {\ttfamily Integer} input and constructs an \texttt{Integer} expression. The \texttt{Integer} class allows us to perform exact arithmetic on integers. Indeed, since Lua can only store integers exactly up to a certain point, it is recommended to use strings to build large integers.

\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
a = Integer(-12435)
b = Integer('-12435')
tex.print('\\[',a:tolatex(),
    '=',
    b:tolatex(),
    '\\]')
\end{minted}
\tcblower
\directlua{
    a = Integer(-12435)
    b = Integer('-12435')
    tex.print('\\[',a:tolatex(),
        '=',
        b:tolatex(),
        '\\]')
}
\end{codebox}
An {\ttfamily Integer} is a table 1-indexed by Lua numbers consisting of Lua numbers. For example:
\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
tex.print(tostring(b[1]))
\end{minted}
\tcblower
\directlua{
    tex.print(tostring(b[1]))
}
\end{codebox}
Whereas:
\begin{codebox}[]
    \begin{minted}[fontsize=\small]{lua}
c = Integer('7240531360949381947528131508')
tex.print('The first 14 digits of c:', tostring(c[1]),'. ')
tex.print('The last 14 digits of c:', tostring([2]),'.')
\end{minted}
\tcblower
\directlua{
    c = Integer('7240531360949381947528131508')
    tex.print('The first 14 digits of c:', tostring(c[1]),'. ')
    tex.print('The last 14 digits of c:', tostring(c[2]),'.')
}
\end{codebox}

The global field {\ttfamily DIGITSIZE} is set to \texttt{14} so that exact arithmetic on {\ttfamily Integer}s can be done as efficiently as possible while respecting Lua's limitations.

\subsubsection*{Fields}
{\ttfamily Integer}s have a {\ttfamily .sign} field which contains the Lua number {\ttfamily 1} or {\ttfamily -1} depending on whether \texttt{Integer} is positive or negative.
\begin{codebox}[]
    \begin{minted}[fontsize=\small]{lua}
tex.print('The sign of',tostring(b),'is:',tostring(b.sign))
\end{minted}
\tcblower
\directlua{
    tex.print('The sign of',
        tostring(b),
        'is:',
        tostring(b.sign))
}
\end{codebox}

\subsubsection*{Parsing}

The contents of the environment \mintinline{latex}{\begin{CAS}..\end{CAS}} are wrapped in the argument of a function \mintinline{lua}{CASparse()} which, among other things, seeks out digit strings intended to represent integers, and wraps those in \texttt{Integer('...')}.

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    c = 7240531360949381947528131508
\end{CAS}
\directlua{
    tex.print(tostring(c[1]))
}
\end{minted}
\tcblower
\begin{CAS}
    c = 7240531360949381947528131508
\end{CAS}
\directlua{
    tex.print(tostring(c[1]))
}
\end{codebox}

\newcoderef{function IntegerModN:new(i,n)}{return IntegerModN}{i Integer, n Integer}
\index{Algebra!Classes!\texttt{IntegerModN}}
\addcontentsline{toc}{subsubsection}{\ttfamily IntegerModN}

Takes an {\ttfamily Integer i} and {\ttfamily Integer n} and constructs an element in the ring $\mathbf{Z}/n\mathbf{Z}$, the integers modulo $n$.

\begin{codebox}[]
    \begin{minted}[fontsize=\small]{lua}
i = Integer(143)
n = Integer(57)
a = IntegerModN(i,n)
tex.print('\\[',i:tolatex(),'\\equiv',a:tolatex(true),'\\]')
\end{minted}
\tcblower
\luaexec{
    i = Integer(143)
    n = Integer(57)
    a = IntegerModN(i,n)
    tex.print('\\[',i:tolatex(),'\\equiv',a:tolatex(true),'\\]')
}
\end{codebox}

\subsubsection*{Fields}

{\ttfamily IntegerModN}s have two fields: {\ttfamily .element} and {\ttfamily .modulus}. The reduced input \texttt{i} is stored in {\ttfamily .element} while the input \texttt{n} is stored in {\ttfamily .modulus}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
tex.print(a.element:tolatex(),'\\newline')
tex.print(a.modulus:tolatex())
\end{minted}
\tcblower
\luaexec{
    tex.print(a.element:tolatex(),'\\newline')
    tex.print(a.modulus:tolatex())
}
\end{codebox}

\subsubsection*{Parsing}

The function \texttt{Mod(,)} is a shortcut for \texttt{IntegerModN(,)}:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    i = 143
    n = 57
    a = Mod(i,n)
\end{CAS}
\[\print{i}\equiv\print{a}\bmod{\print{n}}\]
\end{minted}
\tcblower
\begin{CAS}
    i = 143
    n = 57
    a = Mod(i,n)
\end{CAS}
\[ \print{i} \equiv \print{a} \bmod{\print{n}}\]
\end{codebox}

\newcoderef{function PolynomialRing:new(coefficients, symbol, degree)}{return PolynomialRing}{coefficients table<number,Ring>, symbol string|SymbolExpression, degree Integer}
\index{Algebra!Classes!\texttt{PolynomialRing}}
\addcontentsline{toc}{subsubsection}{\ttfamily PolynomialRing}

Takes a table of {\ttfamily coefficients}, not all necessarily in the same ring, and a {\ttfamily symbol} to create a polynomial in $\mathtt{R[x]}$ where $\mathtt{x}$ is {\ttfamily symbol} and $\mathtt{R}$ is the smallest {\ttfamily Ring} possible given the coefficients. If {\ttfamily degree} is omitted, it will calculate the degree of the polynomial automatically. The list can either be one-indexed or zero-indexed, but if it is one-indexed, the internal list of coefficients will still be zero-indexed.

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
  f = PolynomialRing({0,1/3,-1/2,1/6},'t')
\end{CAS}
\[ \print{f} \]
\end{minted}
\tcblower
\begin{CAS}
    f = PolynomialRing({0,1/3,-1/2,1/6},'t')
\end{CAS}
\[ \print{f} \]
\end{codebox}
The \texttt{PolynomialRing} class overwrites the \mintinline{lua}{isatomic()} and \mintinline{lua}{isconstant()} inheritances from the abstract class \texttt{ConstantExpression}.
\subsubsection*{Fields}

\begin{multicols}{2}
{\ttfamily PolynomialRing}s have several fields:
\begin{itemize}
    \item {\ttfamily f.coefficients} stores the 0-indexed table of coefficients of {\ttfamily f};
    \item {\ttfamily f.degree} stores the {\ttfamily Integer} that represents the degree of {\ttfamily f};
    \item {\ttfamily f.symbol} stores the {\ttfamily string} representing the variable or {\ttfamily symbol} of {\ttfamily f}.
    \item {\ttfamily f.ring} stores the \texttt{RingIdentifier} for the ring of coefficients.
\end{itemize}

\columnbreak

\parseshrub{f}
\bracketset{action character = @}
\begin{center}
\begin{forest}
    for tree = {font = \ttfamily,
        draw,
        rounded corners = 1pt,
        fill=gray!20,
        s sep = 1.5cm,
        l sep = 2cm}
    @\shrubresult
\end{forest}
\end{center}
\end{multicols}
For example:
\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
for i=0,f.degree:asnumber() do
  tex.print('\\[',
    f.coefficients[i]:tolatex(),
    f.symbol,
    '^{',
    tostring(i),
    '}\\]')
end
if f.ring == Rational.getring() then
  tex.print('Rational coefficients')
end
\end{minted}
\tcblower
\luaexec{
for i=0,f.degree:asnumber() do
  tex.print(
    '\\[',
    f.coefficients[i]:tolatex(),
    f.symbol,
    '^{',
    tostring(i),
    '}\\]'
  )
  end
  if f.ring == Rational.getring() then
    tex.print('Rational coefficients')
  end
}
\end{codebox}

\subsubsection*{Parsing}

The function \mintinline{lua}{Poly()} is a shortcut for \mintinline{lua}{PolynomialRing:new()}. If the second argument \texttt{symbol} is omitted, then the default is \texttt{'x'}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = Poly({0,1/3,-1/2,1/6})
\end{CAS}
\[ \print{f} \]
\end{minted}
\tcblower
\begin{CAS}
    f = Poly({0,1/3,-1/2,1/6})
\end{CAS}
\[ \print{f} \]
\end{codebox}

Alternatively, one could typeset the polynomial naturally and use the \texttt{topoly()} function. This is the same as the \texttt{topolynomial()} method except that the \texttt{autosimplify()} method is automatically called first:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x')
    f = 1/3*x - 1/2*x^2 + 1/6*x^3
    f = topoly(f)
\end{CAS}
\[ \print{f} \]
\end{minted}
\tcblower
\begin{CAS}
    vars('x')
    f = 1/3*x - 1/2*x^2 + 1/6*x^3
    f = topoly(f)
\end{CAS}
\[ \print{f} \]
\end{codebox}

\newcoderef{function Rational:new(n,d,keep)}{return Rational}{n Ring, d Ring, keep bool}
\index{Algebra!Classes!\texttt{Rational}}
\addcontentsline{toc}{subsubsection}{\ttfamily Rational}

Takes a numerator {\ttfamily n} and denominator {\ttfamily d} in the same {\ttfamily Ring} and constructs a rational expression in the field of fractions over that ring. For the integers, this is the ring of rational numbers. If the {\ttfamily keep} flag is omitted, the constructed object will be simplified to have smallest possible denominator, possibly returning an object in the original {\ttfamily Ring}. Typically, the {\ttfamily Ring} will be either {\ttfamily Integer} or {\ttfamily PolynomialRing}, so {\ttfamily Rational} can be viewed as a constructor for either a rational number or a rational function.

For example:
\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
a = Integer(6)
b = Integer(10)
c = Rational(a,b)
tex.print('\\[',c:tolatex(),'\\]')
\end{minted}
\tcblower
\luaexec{
    a = Integer(6)
    b = Integer(10)
    c = Rational(a,b)
    tex.print('\\[',c:tolatex(),'\\]')
}
\end{codebox}
But also:
\begin{codebox}
    \begin{minted}{lua}
a = Poly({Integer(2),Integer(3)})
b = Poly({Integer(4),Integer(1)})
c = Rational(a,b)
tex.print('\\[',c:tolatex(),'\\]')
\end{minted}
\tcblower
\luaexec{
a = Poly({Integer(2),Integer(3)})
b = Poly({Integer(4),Integer(1)})
c = Rational(a,b)
tex.print('\\[',c:tolatex(),'\\]')
}
\end{codebox}

\subsubsection*{Fields}

\texttt{Rational}s naturally have the two fields: \texttt{numerator}, \texttt{denominator}. These fields store precisely what you think. \texttt{Rational}s also have a \texttt{ring} field which stores the \texttt{RingIdentifier} to which the numerator and denominator belong. (This is $\mathbb{Z}$ for the rational numbers.)

If \texttt{numerator} or \texttt{denominator} are \texttt{PolynomialRing}s, then the constructed \texttt{Rational} will have an additional field: \texttt{symbol}. This stores the symbol the polynomial rings are constructed over.

\begin{codebox}[]
    \begin{minted}{lua}
if c.ring == PolynomialRing.getring() then
  tex.print('$',c:tolatex(),'$ is a Rational Function in the variable',c.symbol)
end
\end{minted}
\tcblower
\luaexec{
if c.ring == PolynomialRing.getring() then
  tex.print('$',c:tolatex(),'$ is a Rational Function in the variable',c.symbol)
end
}
\end{codebox}

\subsubsection*{Parsing}

\texttt{Raional}s are constructed naturally using the \texttt{/} operator:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    a = Poly({2,3})
    b = Poly({4,1})
    c = a/b
\end{CAS}
\[ \print{c} \]
\end{minted}
\tcblower
\begin{CAS}
    a = Poly({2,3})
    b = Poly({4,1})
    c = a/b
\end{CAS}
\[ \print{c} \]
\end{codebox}

\coderef{function AbsExpression:new(expression)}{return AbsExpression}
\addcontentsline{toc}{subsubsection}{\ttfamily AbsExpression}

Creates a new absolute value expression with the given expression.

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = Poly({1,1})
    g = Poly({-1,1})
    h = AbsExpression(f/g)
\end{CAS}
\[ h = \print{h} \]
\end{minted}
\tcblower
\begin{CAS}
    f = Poly({1,1})
    g = Poly({-1,1})
    h = AbsExpression(f/g)
\end{CAS}
\[ h = \print{h} \]
\end{codebox}

\subsubsection*{Fields}

\texttt{AbsExpression}s have only one field: \texttt{.expression}. This field simply holds the \texttt{Expression} inside the absolute value:
\begin{multicols}{2}
\begin{codebox}[]
\begin{minted}[fontsize=\small]{lua}
tex.print('\\[',
    h.expression:tolatex(),
    '\\]')
\end{minted}
\tcblower
\directlua{
    tex.print('\\[',h.expression:tolatex(),'\\]')
}
\end{codebox}
\parseshrub{h}
\bracketset{action character = @}
\begin{center}
\begin{forest}
    for tree = {font=\ttfamily,
        draw,
        rounded corners=1pt,
        fill=gray!20,
        l sep =1.5cm}
    @\shrubresult
\end{forest}
\end{center}
\end{multicols}

\subsubsection*{Parsing}

The function \mintinline{lua}{abs()} is a shortcut to \mintinline{lua}{AbsExpression:new()}. For example:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = Poly({1,1})
    g = Poly({-1,1})
    h = abs(f/g)
\end{CAS}
\[ h = \print{h} \]
\end{minted}
\tcblower
\begin{CAS}
    f = Poly({1,1})
    g = Poly({-1,1})
    h = abs(f/g)
\end{CAS}
\[ h = \print{h} \]
\end{codebox}

\newcoderef{function Logarithm:new(base,arg)}{return Logarithm}{base Expression, arg Expression}
\addcontentsline{toc}{subsubsection}{\ttfamily Logarithm}

Creates a new \texttt{Logarithm} expression with the given \texttt{base} and \texttt{arg}ument. Some basic simplification rules are known to \texttt{autosimplify()}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('b','x','y')
    f = Logarithm(b,x^y)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{minted}
\tcblower
\begin{CAS}
    vars('b','x','y')
    f = Logarithm(b,x^y)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{codebox}

\subsubsection*{Fields}

\begin{multicols}{2}
\texttt{Logarithm}s have two fields: \texttt{base} and \texttt{expression}; \texttt{base} naturally stores the base of the logarithm (i.e., the first argument of \texttt{Logarithm}) while \texttt{expression} stores the argument of the logarithm (i.e., the second argument of \texttt{Logarithm}).

\begin{center}
    \parseshrub{f}
    \bracketset{action character = @}
    \begin{forest}
        for tree = {font = \ttfamily,
            draw,
            rounded corners=1pt,
            fill = gray!20,
            s sep = 1.5cm}
        @\shrubresult
    \end{forest}
\end{center}
\end{multicols}

\subsubsection*{Parsing}

The function \mintinline{lua}{log()} is a shortcut to \texttt{Logarithm}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('b')
    f = log(b,b)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{minted}
\tcblower
\begin{CAS}
    vars('b')
    f = log(b,b)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{codebox}

There is also a \mintinline{lua}{ln()} function to shortcut \texttt{Logarithm} where the base is \texttt{e}, the natural exponent.

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = ln(e)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{minted}
\tcblower
\begin{CAS}
    f = ln(e)
\end{CAS}
\[ \print{f} = \print*{f} \]
\end{codebox}

\newcoderef{function FactorialExpression:new(expression)}{return FactorialExpression}{expression Expression}
\addcontentsline{toc}{subsubsection}{\ttfamily FactorialExpression}

Creates a new \texttt{FactorialExpression} with the given \texttt{expression}. For example:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    a = FactorialExpression(5)
\end{CAS}
\[ \print{a} \]
\end{minted}
\tcblower
\begin{CAS}
    a = FactorialExpression(5)
\end{CAS}
\[ \print{a} \]
\end{codebox}
The \texttt{evaluate()} method will compute factorials of nonnegative \texttt{Integer}s:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    a = FactorialExpression(5)
\end{CAS}
\[ \print{a} = \print{a:evaluate()} \]
\end{minted}
\tcblower
\begin{CAS}
    a = FactorialExpression(5)
\end{CAS}
\[ \print{a} = \print{a:evaluate()} \]
\end{codebox}

\subsubsection*{Fields}

\texttt{FactorialExpression}s have only one field: \texttt{expression}. This field stores the argument of \texttt{FactorialExpression()}.

\subsubsection*{Parsing}

The function \mintinline{lua}{factorial()} is a shortcut to \texttt{FactorialExpression()}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    a = factorial(5)
\end{CAS}
\[ \print{a} = \print{a:evaluate()} \]
\end{minted}
\tcblower
\begin{CAS}
    a = factorial(5)
\end{CAS}
\[ \print{a} = \print{a:evaluate()} \]
\end{codebox}

\newcoderef{function SqrtExpression:new(expression, root)}{return SqrtExpression}{expression Expression, root Integer}
\addcontentsline{toc}{subsubsection}{\ttfamily SqrtExpression}

Creates a new \texttt{SqrtExpression} with the given \texttt{expression} and \texttt{root}. Typically, \texttt{expression} is an \texttt{Integer} or \texttt{Rational}, and \texttt{SqrtExpression} is intended to represent a positive real number. If \texttt{root} is omitted, then \texttt{root} defaults to \mintinline{lua}{Integer(2)}. For example:

\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
a = SqrtExpression(Integer(8))
b = SqrtExpression(Integer(8),Integer(3))
c = a+b
tex.print('\\[',c:tolatex(),'\\]')
\end{minted}
\tcblower
\directlua{
    a = SqrtExpression(Integer(8))
b = SqrtExpression(Integer(8),Integer(3))
c = a+b
tex.print('\\[',c:tolatex(),'\\]')
}
\end{codebox}
When \texttt{expression} and \texttt{root} are of the \texttt{Integer} or \texttt{Rational} types, then \texttt{autosimplify()} does a couple things. For example, with \texttt{a,b} as above, we get:
\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
c = c:autosimplify()
tex.print('\\[',c:tolatex(),'\\]')
\end{minted}
\tcblower
\directlua{
c = c:autosimplify()
tex.print('\\[',c:tolatex(),'\\]')
}
\end{codebox}
On the other hand, if \texttt{root} or \texttt{expression} are not constants, then typically \mintinline{lua}{autosimplify()} will convert \texttt{SqrtExpression} to the appropriate \texttt{BinaryOperation}. For example:

\directlua{
    vars('x')
    a = SqrtExpression(x,Integer(3))
    b = a:autosimplify()
}

\begin{multicols}{2}
    \begin{center}
        \underline{Tree for \texttt{a}}

\parseshrub{a}
\bracketset{action character = @}
\begin{forest}
    for tree = {s sep=2cm,
        font=\ttfamily,
        draw,
        rounded corners = 1pt,
        fill=gray!20}
    @\shrubresult
\end{forest}

        \underline{Tree for \texttt{a:autosimplify()}}

\parseshrub{a:autosimplify()}
\bracketset{action character = @}
\begin{forest}
    for tree = {s sep=2cm,
        font=\ttfamily}
    @\shrubresult
\end{forest}
\end{center}
\end{multicols}

\subsubsection*{Parsing}

The function \mintinline{lua}{sqrt()} shortcuts \texttt{SqrtExpression()}:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    a = sqrt(1/9)
    b = sqrt(27/16,3)
    c = a+b
\end{CAS}
\[ \print{c} = \print*{c} \]
\end{minted}
\tcblower
\begin{CAS}
    a = sqrt(1/9)
    b = sqrt(27/16,3)
    c = a+b
\end{CAS}
\[ \print{c} = \print*{c} \]
\end{codebox}

\newcoderef{function TrigExpression:new(name,expression)}{return TrigExpression}{name string|SymbolExpression, expression Expression}
\addcontentsline{toc}{subsubsection}{\ttfamily TrigExpression}

Creates a new trig expression with the given \texttt{name} and \texttt{expression}. For example:

\begin{codebox}
    \begin{minted}[fontsize=\small]{lua}
vars('x')
f = TrigExpression('sin',x)
tex.print('\\[',f:tolatex(),'\\]')
\end{minted}
\tcblower
\directlua{
    vars('x')
    f = TrigExpression('sin',x)
    tex.print('\\[',f:tolatex(),'\\]')
}
\end{codebox}

\subsubsection*{Fields}

\begin{multicols}{2}

\texttt{TrigExpression}s have many fields:
\begin{itemize}
    \item \mintinline{lua}{TrigExpression.name} stores the string \texttt{name}, i.e. the first argument of \mintinline{lua}{TrigExpression()};
    \item \mintinline{lua}{TrigExpression.expression} stores the \texttt{Expression} \texttt{expression}, i.e. the second argument of \mintinline{lua}{TrigExpression()};
    \item and all fields inherited from \texttt{FunctionExpression} (e.g. \mintinline{lua}{TrigExpression.derivatives} which defaults to \mintinline{lua}{Integer.zero()}).
\end{itemize}

\columnbreak

\begin{center}
\parseshrub{f}
\bracketset{action character = @}
\begin{forest}
    for tree = {font = \ttfamily,
        draw,
        rounded corners = 1pt,
        fill = gray!20,
        l sep = 2cm}
    @\shrubresult
\end{forest}
\end{center}
\end{multicols}

\subsubsection*{Parsing}

The usual trigonometric functions have the anticipated shortcut names. For example:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = arctan(x^2)
\end{CAS}
\[ \print{f} \]
\end{minted}
\tcblower
\begin{CAS}
    f = arctan(x^2)
\end{CAS}
\[ \print{f} \]
\end{codebox}

\newcoderef{function RootExpression:new(expression)}{return RootExpression}{expression Expression}
\addcontentsline{toc}{subsubsection}{\ttfamily RootExpression}

Creates a new \texttt{RootExpression} with the given \texttt{expression}. The method \mintinline{lua}{RootExpression:autosimplify()} attempts to return a list of zeros of \texttt{expression}. If no such set can be found, then

\mintinline{lua}{RootExpression(expression:autosimplify())}

is returned instead. At the moment, \texttt{expression} must be a univariate polynomial of degree $0,1,2$ or $3$ in order for the \texttt{autosimplify()} method to return anything interesting. Of course, \texttt{luacas} can find roots of higher degree polynomials, but this involves more machinery/methods within the \texttt{PolynomialRing} class.

\subsubsection*{Fields}
\texttt{RootExpression}s have only one field: \texttt{.expression}. For example:
\begin{multicols}{2}
    \begin{codebox}[]
        \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    f = Poly({3,2,1})
    r = RootExpression(f)
\end{CAS}
\[ \print{r} \]
\end{minted}
\tcblower
\begin{CAS}
    f = Poly({3,2,1})
    r = RootExpression(f)
\end{CAS}
\[ \print{r} \]
\end{codebox}

\begin{center}
\parseshrub{r}
\bracketset{action character = @}
\begin{forest}
    for tree = {font = \ttfamily,
        draw,
        rounded corners=1pt,
        fill=gray!20,
        l sep = 2cm}
    @\shrubresult
\end{forest}
\end{center}
\end{multicols}

\subsubsection*{Parsing}

The function \mintinline{lua}{roots()} essentially shortcuts \texttt{RootExpression()}, but when \texttt{expression} is of the \texttt{PolynomialRing}-type, then \texttt{PolynomialRing:roots()} is returned.

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    r = roots(f)
\end{CAS}
\[ \print{r[1]} \qquad \print{r[2]} \]
\end{minted}
\tcblower
\begin{CAS}
    r = roots(f)
\end{CAS}
\[ \print{r[1]} \qquad \print{r[2]} \]
\end{codebox}


\newcoderef{function Equation:new(lhs, rhs)}{return Equation}{lhs Expression, rhs Expression}
\addcontentsline{toc}{subsubsection}{\ttfamily Equation}

Creates a new \texttt{Equation} expression with the given \texttt{lhs} (left hand side) and \texttt{rhs} (right hand side). If both sides of the equation are constants, or structurally identical, \texttt{autosimplify()} will return a boolean:

\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','y')
    f = Equation(sin(x-y),sin(x-y))
    g = f:autosimplify()
\end{CAS}
\[ \print{f} \to \print{g} \]
\end{minted}
\tcblower
\begin{CAS}
    vars('x','y')
    f = Equation(sin(x-y),sin(x-y))
    g = f:autosimplify()
\end{CAS}
\[ \print{f} \to true \]
\end{codebox}

\subsubsection*{Fields}

\begin{multicols}{2}
\texttt{Equation}s have two fields: \texttt{lhs} and \texttt{rhs}; which store the expressions on the left and right sides of the equation.

\begin{center}
    \parseshrub{f}
    \bracketset{action character = @}
    \begin{forest}
        for tree = {font = \ttfamily,
            draw,
            rounded corners=1pt,
            fill = gray!20,
            s sep = 1.5cm}
        @\shrubresult
    \end{forest}
\end{center}
\end{multicols}

\end{document}