summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/reference/ref_core
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex/luacas/doc/reference/ref_core')
-rw-r--r--macros/luatex/latex/luacas/doc/reference/ref_core/ref_core.tex249
-rw-r--r--macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_classes/ref_core_classes.tex455
-rw-r--r--macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex1155
3 files changed, 1859 insertions, 0 deletions
diff --git a/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core.tex b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core.tex
new file mode 100644
index 0000000000..668eda73a1
--- /dev/null
+++ b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core.tex
@@ -0,0 +1,249 @@
+\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[
+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]{%
+\index{\currref!\texttt{#1}}%
+\begin{codehead}[sidebyside,segmentation hidden]%
+ \mintinline{lua}{#1}%
+ \tcblower%
+ \begin{flushright}%
+ \mintinline{lua}{#2}%
+ \end{flushright}%
+\end{codehead}%
+}
+
+\newcommand{\newcoderef}[3]{%
+\index{\currref!\texttt{#1}}%
+\begin{newcodehead}[sidebyside,segmentation hidden]{#3}%
+ \mintinline{lua}{#1}%
+ \tcblower%
+ \begin{flushright}%
+ \mintinline{lua}{#2}%
+ \end{flushright}%
+\end{newcodehead}%
+}
+\usetikzlibrary{shapes.multipart}
+\useforestlibrary{edges}
+
+\def\error{\color{red}}
+\def\self{\color{gray}}
+\def\call{}
+
+\begin{document}
+\thispagestyle{empty}
+
+\section{Core}
+ This section contains reference materials for the core functionality of \texttt{luacas}. The classes in this module are diagramed below according to inheritance along with the methods/functions one can call upon them.
+ \begin{itemize}
+ \item {\error\ttfamily\itshape method}: an abstract method (a method that must be implemented by a subclass to be called);
+ \item {\self\ttfamily\itshape method}: a method that returns the expression unchanged;
+ \item {\ttfamily\itshape method}: a method that is either unique, implements an abstract method, or overrides an abstract method;
+ \item {\tikz[baseline=-0.5ex]\node[fill=roseblue!50] {\ttfamily\bfseries Class};}: a concrete class.
+ \end{itemize}
+
+\forestset{
+rect/.style = {rectangle split,
+ rectangle split parts=2,
+ draw = {rosenavy,thick},
+ rounded corners = 1pt,
+ font = \ttfamily\bfseries,
+ }
+}
+\tikzset{
+ every two node part/.style={font=\ttfamily\itshape\footnotesize}
+}
+\begin{center}
+ \begin{forest}
+ for tree = {node options={align=left},
+ rect,
+ grow= south,
+ parent anchor=south,
+ child anchor=north,
+ edge = {-stealth}
+ },
+ forked edges
+ [Expression\nodepart{two}\begin{minipage}{0.5\textwidth}\vskip-0.2cm\begin{multicols}{2}
+ {\error :evaluate()} \\
+ {\error :autosimplify()} \\
+ :simplify() \\
+ :size() \\
+ {\error :subexpressions()} \\
+ {\error :setsubexpressions()} \\
+ {\error :freeof(symbol)} \\
+ {\error :substitute(map)} \\
+ {\self :expand()} \\
+ {\self :factor()} \\
+ {\self :combine()} \\
+ :getsubexpressionsrec() \\
+ {\error :isatomic()} \\
+ {\error :isconstant()} \\
+ :isrealconstant() \\
+ :iscomplexconstant() \\
+ {\error :order(other)} \\
+ {\self :topolynomial()} \\
+ {\error :tolatex()} \\
+ :type()\end{multicols}\end{minipage}
+ [AtomicExpression\nodepart{two}
+ {\self :tocompoundexpression()} \\
+ {\self :evaluate()} \\
+ {\self :autosimplify()} \\
+ :subexpressions() \\
+ {\self :setsubexpressions()} \\
+ :substitute(map) \\
+ :isatomic() \\
+ :tolatex()
+ [SymbolExpression\nodepart{two}
+ {\call :new(symbol)} \\
+ :freeof(symbol) \\
+ :isconstant() \\
+ :order(other) \\
+ :topolynomial()
+ ,fill = roseblue!50]
+ [ConstantExpression\nodepart{two}
+ :freeof(symbol)\\
+ :isconstant() \\
+ :order(other)
+ ]
+ ]
+ [CompoundExpression\nodepart{two}
+ :freeof(symbol) \\
+ :substitute(map) \\
+ :isatomic() \\
+ :isconstant()
+ [BinaryOperation\nodepart{two}
+ {\call :new(operation,expressions)} \\
+ :evaluate() \\
+ :autosimplify() (!)\\
+ :subexpressions() \\
+ :setsubexpressions() \\
+ :expand() \\
+ :factor() \\
+ :combine() \\
+ :order(other) \\
+ {:iscommutatitve()}\\
+ :topolynomial() \\
+ :tolatex()
+ , fill=roseblue!50]
+ [FunctionExpression\nodepart{two}
+ {\call :new(name,expressions)}\\
+ :evaluate()\\
+ :autosimplify()\\
+ :subexpressions()\\
+ :setsubexpressions()\\
+ :order(other)\\
+ :tolatex()
+ , fill=roseblue!50]
+ ]
+ ]
+ \end{forest}
+\end{center}
+The number of core methods should generally be kept small, since every new type of expression must implement all of these methods. The exception to this, of course, is core methods that call other core methods that provide a unified interface to expressions. For instance, {\ttfamily size()} calls {\ttfamily subexpressions()}, so it only needs to be implemented in the expression interface.
+
+All expressions should also implement the {\ttfamily \_\_tostring} and {\ttfamily \_\_eq} metamethods. Metamethods cannot be inherited using Lua, thus every expression object created by a constructor must assign a metatable to that object.
+
+\begin{itemize}
+ \item {\ttfamily \_\_tostring} provides a human-readable version of an expression for printing within Lua and exporting to external programs.
+
+ \item {\ttfamily \_\_eq} determines whether an expression is structurally identical to another expression.
+\end{itemize}
+\end{document} \ No newline at end of file
diff --git a/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_classes/ref_core_classes.tex b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_classes/ref_core_classes.tex
new file mode 100644
index 0000000000..77e80aa16f
--- /dev/null
+++ b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_classes/ref_core_classes.tex
@@ -0,0 +1,455 @@
+\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{fontawesome5}
+\usepackage{marginnote}
+
+\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}
+
+\subsection{Core Classes}
+
+There are several classes in the core module; but only some classes are concrete:
+
+\begin{multicols}{2}
+ \begin{center}
+ \underline{Abstract classes:}
+ \begin{itemize}
+ \item \texttt{Expression}
+ \item \texttt{AtomicExpression}
+ \item \texttt{CompoundExpression}
+ \item \texttt{ConstantExpression}
+ \end{itemize}
+
+ \underline{Concrete classes:}
+ \begin{itemize}
+ \item \texttt{SymbolExpression}
+ \item \texttt{BinaryOperation}
+ \item \texttt{FunctionExpression}
+ \end{itemize}
+\end{center}
+\end{multicols}
+
+The abstract classes provide a unified interface for the concrete classes (expressions) using inheritance. \emph{Every} expression in \texttt{luacas} inherits from either {\ttfamily AtomicExpression} or {\ttfamily CompoundExpression} which, in turn, inherit from {\ttfamily Expression}.
+
+\coderef{function SymbolExpression:new(string)}{return SymbolExpression}
+\index{Core!Classes!\texttt{SymbolExpression}}
+\addcontentsline{toc}{subsubsection}{\ttfamily SymbolExpression}
+
+Creates a new \texttt{SymbolExpression}. For example:
+\begin{codebox}[]
+\begin{minted}[breaklines,fontsize=\small]{lua}
+foo = SymbolExpression("bar")
+tex.sprint("The Lua variable ``foo'' is the SymbolExpression: ", foo:tolatex(),".")
+\end{minted}
+\tcblower
+\directlua{
+foo = SymbolExpression("bar")
+tex.sprint("The Lua variable 'foo' is the SymbolExpression: ", foo:tolatex(),".")
+}
+\end{codebox}
+
+\subsubsection*{Fields}
+
+\texttt{SymbolExpression}s have only one field: \texttt{symbol}. In the example above, the string \mintinline{lua}{"bar"} is stored in \mintinline{lua}{foo.symbol}.
+
+\subsubsection*{Parsing}
+
+The command \mintinline{lua}{vars()} in \texttt{test.parser} creates a new \texttt{SymbolExpression} for every string in the argument; each such \texttt{SymbolExpression} is assigned to a variable of the same name. For example:
+
+\begin{minted}{lua}
+vars('x','y')
+\end{minted}
+
+is equivalent to:
+
+\begin{minted}{lua}
+x = SymbolExpression("x")
+y = SymbolExpression("y")
+\end{minted}
+
+\newcoderef{function BinaryOperation:new(operation, expressions)}{return BinaryOperation}{operation function, expressions table<number,Expression>}
+\index{Core!Classes!\texttt{BinaryOperation}}
+\addcontentsline{toc}{subsubsection}{\ttfamily BinaryOperation}
+
+Creates a new \texttt{BinaryOperation} expression. For example:
+
+\begin{codebox}
+\begin{minted}[fontsize=\small]{lua}
+vars('x','y','z')
+w = BinaryOperation(
+ BinaryOperation.ADD,
+ {BinaryOperation(
+ BinaryOperation.MUL,
+ {x,y}
+ ),y,z}
+)
+tex.print("\\[w=",w:tolatex(),"\\]")
+\end{minted}
+\tcblower
+\directlua{
+vars('x','y','z')
+w = BinaryOperation(
+ BinaryOperation.ADD,
+ {BinaryOperation(
+ BinaryOperation.MUL,
+ {x,y}
+ ),y,z}
+)
+tex.print("\\[w=",w:tolatex(),"\\]")
+}
+\end{codebox}
+The variable \texttt{operation} must be a function \mintinline{lua}{function f(a,b)} assigned to one of the following types:
+\bgroup
+\setdescription{style=multiline,
+ topsep=10pt,
+ leftmargin=4.5cm,
+ font=\ttfamily
+ }
+\begin{description}
+ \item[BinaryOperation.ADD:] \mintinline{lua}{return a + b}
+ \item[BinaryOperation.SUB:] \mintinline{lua}{return a - b}
+ \item[BinaryOperation.MUL:] \mintinline{lua}{return a * b}
+ \item[BinaryOperation.DIV:] \mintinline{lua}{return a / b}
+ \item[BinaryOperation.IDIV:] \mintinline{lua}{return a // b}
+ \item[BinaryOperation.MOD:] \mintinline{lua}{return a % b}
+ \item[BinaryOperation.POW:] \mintinline{lua}{return a ^ b}
+\end{description}
+\egroup
+The variable \texttt{expressions} must be a table of \texttt{Expression}s index by Lua numbers.
+
+\subsubsection*{Fields}
+
+\texttt{BinaryOperation}s have the following fields: \texttt{name}, \texttt{operation}, and \texttt{expressions}. In the example above, we have:
+\begin{itemize}
+ \item the variable \texttt{expressions} is stored in \mintinline{lua}{w.expressions};
+ \item \mintinline{lua}{w.name} stores the string \mintinline{lua}{"+"}; and
+ \item \mintinline{lua}{w.operation} stores the function:
+ \begin{minted}{lua}
+BinaryOperation.ADD = function(a, b)
+ return a + b
+end
+ \end{minted}
+\end{itemize}
+
+\begin{multicols}{2}
+The entries of \texttt{w.expressions} can be used/fetched in a reasonable way:
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{latex}
+$\print{w.expressions[1]} \quad
+ \print{w.expressions[2]} \quad
+ \print{w.expressions[3]}$
+ \end{minted}
+ \tcblower
+$\print{w.expressions[1]} \quad
+ \print{w.expressions[2]} \quad
+ \print{w.expressions[3]}$
+\end{codebox}
+
+\begin{center}
+ \bracketset{action character = @}
+ \parseshrub{w}
+ \begin{forest}
+ for tree = {font = \ttfamily,
+ draw,
+ rounded corners = 1pt,
+ fill = gray!20,
+ l sep = 1.5cm,
+ s sep = 2cm}
+ @\shrubresult
+ \end{forest}
+\end{center}
+\end{multicols}
+
+\subsubsection*{Parsing}
+
+Thank goodness for this. Creating new \texttt{BinaryOperation}s isn't nearly as cumbersome as the above would indicate. Using Lua's powerful metamethods, we can parse expressions easily. For example, the construction of \texttt{w} given above can be done much more naturally using:
+\begin{codebox}
+\begin{minted}[fontsize=\small]{lua}
+vars('x','y','z')
+w = x*y+y+z
+tex.print("\\[w=", w:tolatex(), "\\]")
+\end{minted}
+\tcblower
+\directlua{
+ vars('x','y','z')
+ w = x*y+y+z
+ tex.print("\\[w=", w:tolatex(), "\\]")
+}
+\end{codebox}
+\reversemarginpar
+{\bf Warning:}\marginnote{\color{rose}\faExclamationTriangle} There are escape issues to be aware of with the operator \mintinline{latex}{%}. If you're writing custom \texttt{luacas} functions in a separate \texttt{.lua} file, then there are no issues; use \mintinline{latex}{%} with reckless abandon. But when using the operator \mintinline{latex}{%} within, say \mintinline{latex}{\begin{CAS}..\end{CAS}}, then one should write \mintinline{latex}{\%} in place of \mintinline{latex}{%}:
+
+\begin{codebox}
+\begin{minted}[breaklines,fontsize=\small]{latex}
+\begin{CAS}
+ a = 17
+ b = 5
+ c = a \% b
+\end{CAS}
+\[ \print{c} \equiv \print{a} \bmod{\print{b}} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ a = 17
+ b = 5
+ c = a \% b
+\end{CAS}
+\[ \print{c} \equiv
+ \print{a} \bmod{\print{b}} \]
+\end{codebox}
+The above escape will {\bf not} work with \mintinline{latex}{\directlua}, but it will work for \mintinline{latex}{\luaexec} from the \texttt{luacode} package. Indeed, the \texttt{luacode} package was designed (in part) to make escapes like this more manageable. Here is the equivalent code using \mintinline{latex}{\luaexec}:
+\begin{codebox}[]
+\begin{minted}[fontsize=\small]{lua}
+a = Integer(17)
+b = Integer(5)
+c = a \% b
+tex.print("\\[",c:tolatex(),"\\equiv",a:tolatex(), "\\bmod{",b:tolatex(),"} \\]")
+\end{minted}
+\tcblower
+\luaexec{
+a = Integer(17)
+b = Integer(5)
+c = a \% b
+tex.print("\\[", c:tolatex(), "\\equiv", a:tolatex(), "\\bmod{", b:tolatex(), "} \\]")
+}
+\end{codebox}
+
+\newcoderef{function FunctionExpression:new(name,expressions)}{return FunctionExpression}{name string|SymbolExpression, expressions table<number,Expression>}
+\index{Core!Classes!\texttt{FunctionExpression}}
+\addcontentsline{toc}{subsubsection}{\ttfamily FunctionExpression}
+
+Creates a generic function. For example:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+vars('x','y')
+f = FunctionExpression('f',{x,y})
+tex.print("\\[",f:tolatex(),"\\]")
+ \end{minted}
+ \tcblower
+ \luaexec{
+ vars('x','y')
+ f = FunctionExpression('f',{x,y})
+ tex.print("\\[",f:tolatex(),"\\]")
+ }
+\end{codebox}
+The variable \texttt{name} can be a string (like above), or another \texttt{SymbolExpression}. But in this case, the variable \texttt{name} just takes the value of the string \mintinline{lua}{SymbolExpression.symbol}. The variable \texttt{expressions} must be a table of \texttt{Expression}s indexed by Lua numbers.
+
+\subsubsection*{Fields}
+\texttt{FunctionExpression}s have the following fields: \texttt{name}, \texttt{expressions}, \texttt{variables}, \texttt{derivatives}. In the example above, we have:
+\begin{itemize}
+ \item the variable \texttt{name}, i.e. the string \mintinline{lua}{'f'}, is stored in \mintinline{lua}{f.name}; and
+ \item the variable \texttt{expressions}, i.e. the table \mintinline{lua}{{x,y}} is stored in \mintinline{lua}{f.expressions}.
+\end{itemize}
+
+Wait a minute, what about \texttt{variables} and \texttt{derivatives}!? The field \texttt{variables} essentially stores a copy of the variable \texttt{expressions} \textit{as long as} the entries in that table are atomic. If they aren't, then \texttt{variables} will default to $x,y,z$, or $x_1,x_2,\ldots$ if the number of variables exceeds $3$. For example:
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+vars('s','t')
+f = FunctionExpression('f',{s*s,s+t+t})
+tex.print("The variables of f are:")
+for _,symbol in ipairs(f.variables) do
+ tex.print(symbol:tolatex())
+end
+ \end{minted}
+ \tcblower
+\luaexec{
+ vars('s','t')
+ f = FunctionExpression('f',{s*s,s+t+t})
+ tex.print("The variables of f are:")
+ for _,symbol in ipairs(f.variables) do
+ tex.print(symbol:tolatex())
+ end
+}
+\end{codebox}
+The field \texttt{derivatives} is a table of \texttt{Integer}s indexed by Lua numbers whose length equals \mintinline{lua}{#o.variables}. The default value for this table is a table of (\texttt{Integer}) zeros. So for the example above, we have:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+for _,integer in ipairs(f.derivatives) do
+ if integer == Integer.zero() then
+ tex.print("I'm a zero.\\newline")
+ end
+end
+\end{minted}
+\tcblower
+\luaexec{
+ for _,integer in ipairs(f.derivatives) do
+ if integer == Integer.zero() then
+ tex.print("I'm a zero.\\newline")
+ end
+ end
+}
+\end{codebox}
+We can change the values of \texttt{variables} and \texttt{derivatives} manually (or more naturally by other gizmos found in \texttt{luacas}). For example, keeping the variables from above, we have:
+\begin{multicols}{2}
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{lua}
+f.derivatives = {Integer.one(),
+ Integer.one()}
+tex.print("\\[",
+ f:simplify():tolatex(),
+ "\\]")
+\end{minted}
+\tcblower
+\luaexec{
+ f.derivatives = {Integer.one(),Integer.one()}
+ tex.print("\\[", f:simplify():tolatex(), "\\]")
+}
+\end{codebox}
+
+\begin{center}
+\parseshrub{f}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily,
+ draw,
+ rounded corners = 1pt,
+ fill = gray!20,
+ l sep = 1.5cm,
+ s sep = 0.75cm}
+ @\shrubresult
+\end{forest}
+\end{center}
+\end{multicols}
+
+\subsubsection*{Parsing}
+
+Thank goodness for this too. The parser nested within the \LaTeX{} environment \mintinline{latex}{\begin{CAS}..\end{CAS}} allows for fairly natural function assignment; the name of the function must be declared in \mintinline{lua}{vars(...)} (or rather, as a \texttt{SymbolExpression}) beforehand:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('s','t','f')
+ f = f(s^2,s+2*t)
+ f.derivatives = {1,1}
+\end{CAS}
+\[ \print{f} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('s','t','f')
+ f = f(s^2,s+2*t)
+ f.derivatives = {1,1}
+\end{CAS}
+\[ \print{f} \]
+\end{codebox}
+
+\end{document} \ No newline at end of file
diff --git a/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex
new file mode 100644
index 0000000000..a88b5a15d8
--- /dev/null
+++ b/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex
@@ -0,0 +1,1155 @@
+\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[
+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}
+
+\subsection{Core Methods}
+
+Any of the methods below can be used within \mintinline{latex}{\begin{CAS}..\end{CAS}}. There are times when the parser or \LaTeX{} front-end allows for simpler syntax or usability.
+
+\coderef{function Expression:autosimplify()}{return Expression|table<number, Expression>}
+\index{Core!Methods!\texttt{autosimplify}}
+\addcontentsline{toc}{subsubsection}{\ttfamily autosimplify}
+
+Performs fast simplification techniques on an expression. The return depends on the type of input \texttt{Expression}. Generally, one should call {\ttfamily autosimplify()} on expressions before applying other core methods to them.
+
+Consider the code:
+
+\begin{minted}{latex}
+\begin{CAS}
+ vars('x','y','z')
+ w = x/y + y/z + z/x
+\end{CAS}
+\[ \print{w} = \print{w:autosimplify()} \]
+\end{minted}
+\begin{CAS}
+ vars('x','y','z')
+ w = x/y + y/z + z/x
+\end{CAS}
+The output is as follows:
+\[ \print{w} = \print{w:autosimplify()} \]
+It seems that \texttt{autosimplify()} did nothing; but there are significant structural differences between \texttt{w} and \texttt{w:autosimplify()}:
+
+\begin{multicols}{2}
+ \begin{center}
+ \underline{Expression tree for \texttt{w}}
+
+ \parseforest{w}
+ \bracketset{action character = @}
+ \begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+ \end{forest}
+
+ \underline{Expression tree for \texttt{w:autosimplify()}}
+
+ \parseforest{w:autosimplify()}
+ \bracketset{action character = @}
+ \begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+ \end{forest}
+\end{center}
+\end{multicols}
+
+Ironically, the \emph{autosimplified} expression tree on the right looks more complicated than the one on the left! But one of the primary functions of \texttt{autosimplify()} is to take an expression (that truly could be input in a myriad of ways) and convert that expression into something \emph{anticipatable}.
+
+For example, suppose the user inputs:
+\begin{minted}{latex}
+\begin{CAS}
+ w = x/y + (z/x+y/z)
+\end{CAS}
+\end{minted}
+\begin{CAS}
+ w = x/y + (z/x+y/z)
+\end{CAS}
+In this case, the expression trees for \texttt{w} and \texttt{w:autosimplify()}, respectively, look as follows:
+
+\begin{multicols}{2}
+\begin{center}
+\parseforest{w}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+\end{forest}
+
+\parseforest{w:autosimplify()}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+\end{forest}
+\end{center}
+\end{multicols}
+{\bf Note:} \texttt{w:autosimplify()} is exactly the same as it was before despite the different starting point. This is an essential function of \texttt{autosimplify()}.
+
+\subsubsection*{Parsing}
+
+The starred variant of the \LaTeX{} command \mintinline{latex}{\print} will automatically apply the method \texttt{autosimplify()} to its argument:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ a = x+x/2
+\end{CAS}
+\[ \print{a} = \print*{a} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ a = x+x/2
+\end{CAS}
+\[ \print{a} = \print*{a} \]
+\end{codebox}
+Alternatively, you can call \texttt{autosimplify()} directly within \mintinline{latex}{\begin{CAS}..\end{CAS}}:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ a = (x+x/2):autosimplify()
+\end{CAS}
+\[ \print{a} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ a = (x+x/2):autosimplify()
+\end{CAS}
+\[ \print{a} \]
+\end{codebox}
+
+\coderef{function Expression:evaluate()}{return Expression}
+\index{Core!Methods!\texttt{evaluate}}
+\addcontentsline{toc}{subsubsection}{\ttfamily evaluate}
+
+Attempts to apply operations found in the expression tree of \texttt{Expression}. For instance, evaluating a {\ttfamily DerivativeExpression} applies the derivative operator with respect to the {\ttfamily symbol} field to its {\ttfamily expression} field. Evaluating a {\ttfamily BinaryOperation} with its {\ttfamily operation} field set to {\ttfamily ADD} returns the sum of the numbers in the {\ttfamily expressions} field, if possible. If the expression does not represent an operation or is unable to be evaluated, calling {\ttfamily evaluate()} on an expression returns itself.
+
+For example, the code:
+\begin{minted}{latex}
+\directlua{
+ x = Integer(1)/Integer(2)
+ y = Integer(2)/Integer(3)
+ z = BinaryOperation(BinaryOperation.ADD,{x,y})
+}
+\[ \print{z} = \print{z:evaluate()}.\]
+\end{minted}
+produces:
+\directlua{
+ x = Integer(1)/Integer(2)
+ y = Integer(2)/Integer(3)
+ z = BinaryOperation(BinaryOperation.ADD,{x,y})
+}
+\[ \print{z} = \print{z:evaluate()}.\]
+
+\subsubsection*{Parsing}
+
+Arithmetic like above is actually done automatically (via the \texttt{Ring} interface):
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ x = 1/2
+ y = 2/3
+ z = x+y
+\end{CAS}
+\[ z = \print{z} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ x = 1/2
+ y = 2/3
+ z = x+y
+\end{CAS}
+\[ z = \print{z} \]
+\end{codebox}
+
+Otherwise, the \texttt{evaluate()} method will attempt to evaluate all subexpressions, and then stop there:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ y = diff(x^2+x,x)+diff(2*x,x)
+ y = y:evaluate()
+\end{CAS}
+\[ \print{y} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ y = diff(x^2+x,x)+diff(2*x,x)
+ y = y:evaluate()
+\end{CAS}
+\[ \print{y} \]
+\end{codebox}
+Whereas \texttt{autosimplify()} will return $3+2x$; indeed, the \texttt{autosimplify()} method (usually) begins by applying \texttt{evaluate()} first.
+
+\coderef{function Expression:expand()}{return Expression}
+\index{Core!Methods!\texttt{expand}}
+\addcontentsline{toc}{subsubsection}{\ttfamily expand}
+
+Expands an expression, turning products of sums into sums of products.
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x','y','z','w')
+ a = x+y
+ b = z+w
+ c = a*b
+\end{CAS}
+\[ \print{c} = \print{c:expand()} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x','y','z','w')
+ a = x+y
+ b = z+w
+ c = a*b
+\end{CAS}
+\[ \print{c} = \print{c:expand()} \]
+\end{codebox}
+
+\subsubsection*{Parsing}
+
+There is an \texttt{expand()} function in the parser; though it calls the \texttt{autosimplify()} method first. So, for example, \mintinline{lua}{expand(c)} is equivalent to \mintinline{lua}{c:autosimplify():expand()}.
+
+\coderef{function Expression:factor()}{return Expression}
+\index{Core!Methods!\texttt{factor}}
+\addcontentsline{toc}{subsubsection}{\ttfamily factor}
+
+Factors an expression, turning sums of products into products of sums. For general \texttt{Expressions} this functionality is somewhat limited. For example:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ a = x-1
+ b = a*x+a
+\end{CAS}
+\[ \print{b} = \print{b:factor()} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x','y')
+ a = x-y
+ b = a*x+a*y
+\end{CAS}
+\[ \print{b} = \print{b:factor()} \]
+\end{codebox}
+On the other hand:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x','y')
+ a = x^2-y^2
+\end{CAS}
+\[ \print{a} = \print{a:factor()} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x','y')
+ a = x^2-y^2
+\end{CAS}
+\[ \print{a} = \print{a:factor()} \]
+\end{codebox}
+
+\subsubsection*{Parsing}
+
+There is a \texttt{factor()} function in the parser that is more class-aware than the basic \texttt{:factor()} method mentioned here. For example:
+\begin{codebox}
+ \begin{minted}[fontsize = \small]{latex}
+\begin{CAS}
+ x = 12512
+\end{CAS}
+\[ \print{x:factor()} = \print{factor(x)} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ x = 12512
+\end{CAS}
+\[ \print{x:factor()} = \print{factor(x)} \]
+\end{codebox}
+
+\coderef{function Expression:freeof(symbol)}{return bool}
+\index{Core!Methods!\texttt{freeof}}
+\addcontentsline{toc}{subsubsection}{\ttfamily freeof}
+
+Determines whether or not {\ttfamily Expression} contains a particular {\ttfamily symbol} somewhere in its expression tree.
+
+The method \texttt{freeof()} is quite literal. For example:
+\begin{codebox}
+\begin{minted}[fontsize=\small]{lua}
+vars('foo','bar')
+baz = foo+bar
+if baz:freeof(foo) then
+ tex.sprint(baz:tolatex(), " is free of ",
+ foo:tolatex(),"!")
+else
+ tex.sprint(baz:tolatex(), " is bound by ",
+ foo:tolatex(),".")
+end
+\end{minted}
+\tcblower
+\directlua{
+vars('foo','bar')
+baz = foo+bar
+if baz:freeof(foo) then
+ tex.sprint(baz:tolatex(), " is free of ",
+ foo:tolatex(),"!")
+else
+ tex.sprint(baz:tolatex(), " is bound by ",
+ foo:tolatex(),".")
+end
+}
+\end{codebox}
+On the other hand, the expression tree for \mintinline{lua}{SymbolExpression("foo")} contains a single node with no edges. With nary a \mintinline{lua}{SymbolExpression("fo")} to find in such an expression tree, we have:
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+vars('foo','fo')
+if foo:freeof(fo) then
+ tex.sprint(foo:tolatex()," is free of ",
+ fo:tolatex(),"!")
+else
+ tex.sprint(foo:tolatex()," is bound by ",
+ fo:tolatex(),'.')
+end
+ \end{minted}
+ \tcblower
+ \directlua{
+vars('foo','fo')
+if foo:freeof(fo) then
+ tex.sprint(foo:tolatex()," is free of ",
+ fo:tolatex(),"!")
+else
+ tex.sprint(foo:tolatex()," is bound by ",
+ fo:tolatex(),'.')
+end
+}
+ \end{codebox}
+
+\coderef{function Expression:isatomic()}{return bool}
+\index{Core!Methods!\texttt{isatomic}}
+\addcontentsline{toc}{subsubsection}{\ttfamily isatomic}
+
+Determines whether an expression is \emph{atomic}. Typically, atomicity is measured by whether the \texttt{Expression} has any subexpression fields. So, for example, \texttt{Integer(5)} and \texttt{Integer(15)} are atomic, and so is \texttt{Integer(20)}. But:
+\begin{minted}{lua}
+BinaryOperation(BinaryOperation.ADD,
+ {Integer(5),Integer(15)})
+\end{minted}
+is non-atomic.
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+x = SymbolExpression("x")
+y = x*x+x
+if x:isatomic() then
+ tex.print(tostring(x),"is atomic;")
+end
+if not y:isatomic() then
+ tex.print(tostring(y),"is compound.")
+end
+\end{minted}
+\tcblower
+\directlua{
+x = SymbolExpression("x")
+y = x*x+x
+if x:isatomic() then
+ tex.print(tostring(x),"is atomic;")
+end
+if not y:isatomic() then
+ tex.print(tostring(y),"is compound.")
+end
+}
+\end{codebox}
+Since \texttt{SymbolExpression} inherits from \texttt{AtomicExpression}, we have that \texttt{isatomic()} is taken literally as well. For example:
+\begin{codebox}
+ \begin{minted}[fontsize=\small,breaklines]{lua}
+y = SymbolExpression("x*x+x")
+if not y:isatomic() then
+ tex.print(tostring(y),"is compound.")
+else
+ tex.print("But",tostring(y),"is atomic,
+ from a certain point of view.")
+end
+\end{minted}
+\tcblower
+\directlua{
+ y = SymbolExpression("x*x+x")
+if not y:isatomic() then
+ tex.print(tostring(y),"is compound.")
+else
+ tex.print("But",tostring(y),"is atomic,
+ from a certain point of view.")
+end
+}
+\end{codebox}
+\vskip 0.2cm
+
+\coderef{function Expression:iscomplexconstant()}{return bool}
+\index{Core!Methods!\texttt{iscomplexconstant}}
+\addcontentsline{toc}{subsubsection}{\ttfamily iscomplexconstant}
+
+Determines whether an expression is a complex number in the mathematical sense, such as $3 + \sqrt{2}i$. It's helpful to keep in mind that, oftentimes, content needs to be simplified/evaluated in order to obtain the intended results:
+
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{lua}
+a = (Integer.one() + I) ^ Integer(2)
+if a:iscomplexconstant() then
+ tex.print("$",a:tolatex(),"$ is a complex constant.")
+else
+ tex.print("$",a:tolatex(),"$ is not a complex constant.")
+end
+\end{minted}
+\tcblower
+\begin{center}
+\luaexec{
+ a = (Integer.one() + I) ^ Integer(2)
+ if a:iscomplexconstant() then
+ tex.print("$",a:tolatex(),"$ is a complex constant.")
+ else
+ tex.print("$",a:tolatex(),"$ is not a complex constant.")
+ end
+}
+\end{center}
+\end{codebox}
+While:
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{lua}
+a = (Integer.one()+I) ^ Integer(2)
+a = a:expand():simplify()
+if a:iscomplexconstant() then
+ tex.print("$",a:tolatex(),"$ is a complex constant.")
+else
+ tex.print("$",a:tolatex(),"$ is not a complex constant.")
+end
+\end{minted}
+\tcblower
+\begin{center}
+\luaexec{
+ a = (Integer.one()+I) ^ Integer(2)
+ a = a:expand():simplify()
+ if a:iscomplexconstant() then
+ tex.print("$",a:tolatex(),"$ is a complex constant.")
+ else
+ tex.print("$",a:tolatex(),"$ is not a complex constant.")
+ end
+}
+\end{center}
+\end{codebox}
+
+\coderef{function Expression:isconstant()}{return bool}
+\index{Core!Methods!\texttt{isconstant}}
+\addcontentsline{toc}{subsubsection}{\ttfamily isconstant}
+
+Determines whether an expression is atomic and contains no variables. This method is counterintuitive in some cases. For instance:
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+if not pi:isconstant() then
+ tex.print("$\\pi$ is not constant.")
+end
+\end{minted}
+\tcblower
+\luaexec{
+if not pi:isconstant() then
+ tex.print("$\\pi$ is not constant.")
+end
+}
+\end{codebox}
+This is because {\ttfamily isconstant()} is meant to check for certain autosimplification transformations that can be performed on arbitrary {\ttfamily Ring} elements but not on those constants. Use {\ttfamily isrealconstant()} for what mathematicians think of as constants.
+
+\coderef{function Expression:isrealconstant()}{return bool}
+\index{Core!Methods!\texttt{isrealconstant}}
+\addcontentsline{toc}{subsubsection}{\ttfamily isrealconstant}
+
+Determines whether an expression is a real number in the mathematical sense, such as $2$, $\sqrt{5}$, or $\sin(3)$. For example:
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{lua}
+if pi:isrealconstant() then
+ tex.print("$\\pi$ is a real constant.")
+end
+\end{minted}
+\tcblower
+\luaexec{
+ if pi:isrealconstant() then
+ tex.print("$\\pi$ is a real constant.")
+ end
+}
+\end{codebox}
+
+\coderef{function Expression:order(Expression)}{return boolean}
+\index{Core!Methods!\texttt{order}}
+\addcontentsline{toc}{subsubsection}{\ttfamily order}
+
+For the goals of autosimplification, \texttt{Expression}s must be ordered. \texttt{Expression:order(other)} method returns \mintinline{lua}{true} if \texttt{Expression} is ``less than'' \texttt{other} according to this ordering.
+
+\begin{multicols}{2}
+On certain classes, the ordering is intuitive:
+
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{lua}
+a = 4
+b = 3
+if a:order(2) then
+ tex.print(a:tolatex(),
+ "is less than",
+ b:tolatex())
+else
+ tex.print(b:tolatex(),
+ "is less than",
+ a:tolatex())
+end
+\end{minted}
+\tcblower
+\begin{CAS}
+ a = 4
+ b = 3
+ if a:order(2) then
+ tex.print(a:tolatex(),
+ "is less than",
+ b:tolatex())
+ else
+ tex.print(b:tolatex(),
+ "is less than",
+ a:tolatex())
+ end
+\end{CAS}
+\end{codebox}
+
+\columnbreak
+
+On \texttt{SymbolExpression}s, the ordering is lexigraphic:
+
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{lua}
+vars('a')
+vars('b')
+if b:order(a) then
+ tex.print(b:tolatex(),
+ "is less than",
+ a:tolatex())
+else
+ tex.print(a:tolatex(),
+ "is less than",
+ b:tolatex())
+end
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('a','b')
+ if b:order(a) then
+ tex.print(b:tolatex(),
+ "is less than",
+ a:tolatex())
+ else
+ tex.print(a:tolatex(),
+ "is less than",
+ b:tolatex())
+ end
+\end{CAS}
+\end{codebox}
+\end{multicols}
+
+Of course, inter-class comparisons can be made as well -- but these are predominantly dictated by typesetting conventions.
+
+\coderef{function Expression:setsubexpressions(subexpressions)}{return Expression}
+\index{Core!Methods!\texttt{setsubexpressions}}
+\addcontentsline{toc}{subsubsection}{\ttfamily setsubexpressions}
+
+Creates a copy of an expression with the list of subexpressions as its new subexpressions. This can reduce code duplication in other methods.
+
+
+\coderef{function Expression:simplify()}{return Expression}
+\index{Core!Methods!\texttt{simplify}}
+\addcontentsline{toc}{subsubsection}{\ttfamily simplify}
+
+Performs more extensive simplification of an expression. This may be slow, so this function is separate from autosimplification and is not called unless the user specifically directs the CAS to do so. The method aims to find an expression tree equivalent to the one given that is ``smaller'' in size as measured by the number of nodes in the expression tree.
+
+The \texttt{simplify()} method does call the \texttt{autosimplify()} method first. Here's an example of where the results of \texttt{autosimplify()} and \texttt{simplify()} differ:
+
+\begin{minted}[breaklines]{latex}
+\begin{CAS}
+ vars('x')
+ a = 1-x+0*x
+ b = 1+1*x
+ c = a*b
+\end{CAS}
+\[ \print{c} = \print{c:autosimplify()} = \print{c:simplify()}. \]
+\end{minted}
+The code above produces:
+\begin{CAS}
+ vars('x')
+ a = 1-x+0*x
+ b = 1+1*x
+ c = a*b
+\end{CAS}
+\[ \print{c} = \print{c:autosimplify()} = \print{c:simplify()}. \]
+
+\subsubsection*{Parsing}
+
+There is a \texttt{simplify()} function for those unfamiliar with Lua methods. So, for example, \mintinline{lua}{c:simplify()} is equivalent to \mintinline{lua}{simplify(c)}.
+
+\coderef{function Expression:size()}{return Integer}
+\index{Core!Methods!\texttt{size}}
+\addcontentsline{toc}{subsubsection}{\ttfamily size}
+
+Returns the number of nodes of the tree that constitutes an expression, or roughly the total number of expression objects that make up the expression.
+
+For example, consider:
+\begin{minted}{latex}
+\begin{CAS}
+ vars('x')
+ a = (1-x+0*x)
+ b = (1+1*x)
+ c = a*b
+\end{CAS}
+\end{minted}
+\begin{CAS}
+ vars('x')
+ a = (1-x+0*x)
+ b = (1+1*x)
+ c = a*b
+\end{CAS}
+Then the expression trees for \texttt{c}, \texttt{c:autosimplify()}, and \texttt{c:simplify()} are as follows:
+
+\begin{multicols}{3}
+ \begin{center}
+ \underline{Tree for \texttt{c}:}
+\parseforest{c}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+\end{forest}
+
+\columnbreak
+
+\underline{Tree for \texttt{c:autosimplify()}:}
+\parseforest{c:autosimplify()}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+\end{forest}
+
+\columnbreak
+
+\underline{Tree for \texttt{c:simplify()}:}
+\parseforest{c:simplify()}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {font = \ttfamily}
+ @\forestresult
+\end{forest}
+\end{center}
+\end{multicols}
+Accordingly, we have:
+\begin{codebox}
+ \begin{minted}[breaklines,fontsize=\small]{lua}
+tex.print("The size of \\texttt{c} is",
+ tostring(c:size()),"\\newline")
+tex.print("The size of
+ \\texttt{c:autosimplify()} is",
+ tostring(c:autosimplify():size()), "\\newline")
+tex.print("The size of
+ \\texttt{c:simplify()} is",
+ tostring(c:simplify():size()))
+ \end{minted}
+ \tcblower
+ \luaexec{
+ tex.print("The size of \\texttt{c} is", tostring(c:size()),"\\newline")
+ tex.print("The size of \\texttt{c:autosimplify()} is", tostring(c:autosimplify():size()), "\\newline")
+ tex.print("The size of \\texttt{c:simplfy()} is", tostring(c:simplify():size()))
+}
+\end{codebox}
+\vskip 0.2cm
+
+\coderef{function Expression:subexpressions()}{return table<number, Expression>}
+\index{Core!Methods!\texttt{subexpressions}}
+\addcontentsline{toc}{subsubsection}{\ttfamily subexpressions}
+
+Returns a list of all subexpressions of an expression. This gives a unified interface to the instance variables for subexpressions, which have different names across classes. For example, consider:
+\begin{codebox}
+\begin{minted}[breaklines,fontsize=\footnotesize]{latex}
+\begin{CAS}
+ vars('x','y','z')
+ a = x*y+y*z
+ b = int(sin(x),x,0,pi/2)
+\end{CAS}
+\[ a = \print{a} \quad \text{and} \quad b=\print{b}.\]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x','y','z')
+ a = x*y+y*z
+ b = int(sin(x),x,0,pi/2)
+\end{CAS}
+\[ a = \print{a} \quad \text{and} \quad b=\print{b}.\]
+\end{codebox}
+Here are the expression shrubs for \texttt{a} and \texttt{b}:
+
+\begin{multicols}{2}
+\begin{center}
+ \underline{Expression shrub for \texttt{a}}
+
+ \parseshrub{a}
+ \bracketset{action character = @}
+ \begin{forest}
+ for tree = {draw,
+ rectangle,
+ rounded corners=1pt,
+ fill=gray!10,
+ s sep = 2cm,
+ font=\ttfamily}
+ @\shrubresult
+ \end{forest}
+
+ \underline{Expression shrub for \texttt{b}}
+
+\parseshrub{b}
+\bracketset{action character = @}
+\begin{forest}
+ for tree = {draw,
+ rectangle,
+ rounded corners=1pt,
+ fill=gray!10,
+ s sep = 1cm,
+ font=\ttfamily}
+ @\shrubresult
+\end{forest}
+\end{center}
+\end{multicols}
+On the other hand:
+\begin{codebox}
+\begin{minted}[breaklines,fontsize=\small]{lua}
+for _,expr in ipairs(a:subexpressions()) do
+ tex.print("$", expr:tolatex(), "$\\quad")
+end
+\end{minted}
+\tcblower
+\luaexec{
+ for _,expr in ipairs(a:subexpressions()) do
+ tex.print("$", expr:tolatex(), "$\\quad")
+ end
+}
+\end{codebox}
+while:
+\begin{codebox}
+\begin{minted}[breaklines,fontsize=\small]{lua}
+for _,expr in ipairs(b:subexpressions()) do
+ tex.print("$", expr:tolatex(), "$\\quad")
+end
+\end{minted}
+\tcblower
+\luaexec{
+ for _,expr in ipairs(b:subexpressions()) do
+ tex.print("$",expr:tolatex(),"$\\quad")
+ end
+}
+\end{codebox}
+\vskip 0.2cm
+
+\coderef{function Expression:substitute(map)}{return Expression}
+\index{Core!Methods!\texttt{substitute}}
+\addcontentsline{toc}{subsubsection}{\ttfamily substitute}
+
+The input \texttt{map} is a table that maps expressions to expressions; the method then recursively maps each instance of an expression with its corresponding table expression. One should take care when replacing multiple compound expressions in a single command, since there is no guarantee as to the order in which subexpressions in the table are replaced.
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('foo','bar','baz')
+ qux = (foo/bar)
+ qux = qux:substitute({[foo]=bar,[bar]=baz})
+\end{CAS}
+\[ \print{qux} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('foo','bar','baz')
+ qux = (foo/bar)
+ qux = qux:substitute({[foo]=bar,[bar]=baz})
+ \end{CAS}
+ \[ \print{qux} \]
+\end{codebox}
+
+\subsubsection*{Parsing}
+
+There is a \texttt{substitute()} function with a slightly more user-friendly syntax. In particular,
+
+\mintinline{lua}{(foo/bar):substitute({[foo]=bar,[bar]=baz})}
+
+is equivalent to
+
+\mintinline{lua}{substitute({[foo]=bar,[bar]=baz}, foo/bar)}
+
+\coderef{function Expression:tolatex()}{return string}
+\index{Core!Methods!\texttt{tolatex}}
+\addcontentsline{toc}{subsubsection}{\ttfamily tolatex}
+
+Converts an expression to \LaTeX{} code. Some folks have strong feelings about how certain things are typeset. Case and point, which of these is your favorite:
+\[ \int \sin(\frac{y}{2})dy \qquad \int \sin\left( \frac{y}{2} \right)dy \qquad \int \sin\left( \frac{y}{2} \right)\, dy \qquad \int \sin\!\left( \frac{y}{2} \right)\, dy \qquad \int \sin\!\left( \frac{y}{2} \right) \mathop{\mathrm{d}y} \qquad \int \sin\mathopen{}\left( \frac{y}{2} \right) \mathop{dy} \quad ?\]
+We've tried to remain neutral:
+
+\begin{codebox}
+\begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('y')
+ f = diff(int(sin(y/2),y),y)
+\end{CAS}
+\[ \print{f} \]
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('y')
+ f = diff(int(sin(y/2),y),y)
+\end{CAS}
+\[ \print{f} \]
+\end{codebox}
+With any luck, we've pleased at least as many people as we've offended. In desperate times, one could rewrite the \texttt{tolatex()} method for any given class. Here, for example, is the \texttt{tolatex()} method as written for the \texttt{DerivativeExpression} class:
+\begin{minted}[breaklines]{lua}
+function DerivativeExpression:tolatex()
+ return '\\frac{d}{d' .. self.symbol:tolatex() .. '}\\left(' .. self.expression:tolatex() .. '\\right)'
+end
+\end{minted}
+But there are heathens that live among us who might prefer:
+\begin{minted}[breaklines]{lua}
+function DerivativeExpression:tolatex()
+ return '\\frac{\\mathrm{d}}{\\mathrm{d}' .. self.symbol:tolatex() .. '}\\left(' .. self.expression:tolatex() .. '\\right)'
+end
+\end{minted}
+If we include the above function in a separate file, say \texttt{mytex.lua}, and use:
+
+\mintinline{latex}{\directlua{dofile('mytex.lua')}}
+
+or include the above function directly into the document via \mintinline{latex}{\directlua} or \mintinline{latex}{\luaexec}, then we would get:
+\begin{codebox}
+\begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ f = DerivativeExpression(y+sin(y),y)
+\end{CAS}
+\[ \print{f} \]
+\end{minted}
+\tcblower
+\[ \frac{\mathrm{d}}{\mathrm{d}y} \left( y + \sin\mathopen{}\left(y\right) \right).\]
+\end{codebox}
+
+\subsubsection*{Parsing}
+
+The \LaTeX{} command \mintinline{latex}{\print} calls the method \mintinline{lua}{tolatex()} unto its argument and uses \mintinline{lua}{tex.print()} to display the results. The starred variant \mintinline{latex}{\print*} applies the \texttt{autosimplify()} method before applying \mintinline{lua}{tolatex()}.
+
+Additionally, one can use the \texttt{disp()} function within \mintinline{latex}{\begin{CAS}..\end{CAS}}.
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ f = DerivativeExpression(y+sin(y),y)
+ disp(f)
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ f = DerivativeExpression(y+sin(y),y)
+ disp(f)
+\end{CAS}
+\end{codebox}
+
+The function \texttt{disp} takes two optional boolean arguments both are set to \texttt{false} by default. The first optional boolean controls \emph{inline} vs \emph{display} mode; the second optional boolean controls whether the method \texttt{autosimplify()} is called before printing:
+
+\begin{multicols}{3}
+ \begin{codebox}[]
+\begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ disp(f,true)
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ disp(f,true)
+\end{CAS}
+\end{codebox}
+ \begin{codebox}[]
+\begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ disp(f,true,true)
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ disp(f,true,true)
+\end{CAS}
+\end{codebox}
+ \begin{codebox}[]
+\begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ disp(f,false,true)
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ disp(f,false,true)
+\end{CAS}
+\end{codebox}
+\end{multicols}
+
+
+\coderef{function Expression:topolynomial()}{return Expression | bool}
+\index{Core!Methods!\texttt{topolynomial}}
+\addcontentsline{toc}{subsubsection}{\ttfamily topolynomial}
+
+Attempts to cast \texttt{Expression} into a polynomial type ({\ttfamily PolynomialRing}); there are multiple outputs. The first output is \texttt{self} or \texttt{PolynomialRing}; the second output is \mintinline{lua}{false} or \mintinline{lua}{true}, respectively. \texttt{PolynomialRing} is the name of the class that codifies univariate polynomials proper.
+
+Polynomial computations tend to be significantly faster when those polynomials are stored as arrays of coefficients (as opposed to, say, when they are stored as generic \texttt{BinaryOperation}s). Hence the need for a method like \texttt{topolynomial()}.
+
+{\bf Warning:} the \texttt{topolynomial()} method expects the input to be autosimplified. For example:
+
+\begin{multicols}{2}
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f,b = f:topolynomial()
+ if b then
+ tex.print("\\[",f:tolatex(),"\\]")
+ else
+ tex.print("womp womp")
+ end
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f,b = f:topolynomial()
+ if b then
+ tex.print("\\[",f:tolatex(),"\\]")
+ else
+ tex.print("\\[ \\text{womp womp} \\]")
+ end
+\end{CAS}
+\end{codebox}
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f,b = f:autosimplify():topolynomial()
+ if b then
+ tex.print("\\[",f:tolatex(),"\\]")
+ else
+ tex.print("womp womp")
+ end
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f,b = f:autosimplify():topolynomial()
+ if b then
+ tex.print("\\[",f:tolatex(),"\\]")
+ else
+ tex.print("\\[ \\text{womp womp} \\]")
+ end
+\end{CAS}
+\end{codebox}
+\end{multicols}
+
+\subsubsection*{Parsing}
+
+There is a \mintinline{lua}{topoly()} function that applies \mintinline{lua}{:autosimplify()} automatically to the input. For example:
+\begin{codebox}[]
+ \begin{minted}[fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f = topoly(f)
+\end{CAS}
+The Lua variable \texttt{f} is the \whatis{f}: $\print{f}$.
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ f = 3+2*x+x^2
+ f = topoly(f)
+\end{CAS}
+The Lua variable \texttt{f} is the \whatis{f}: $\print{f}$.
+\end{codebox}
+
+
+\coderef{function Expression:type()}{return Expression | bool}
+\index{Core!Methods!\texttt{type}}
+\addcontentsline{toc}{subsubsection}{\ttfamily type}
+
+Returns the \mintinline{lua}{__index} field in the metatable for \texttt{Expression}. In other words, this function returns the type of \texttt{Expression}. Here's typical usage:
+
+\begin{codebox}
+ \begin{minted}[breaklines,fontsize=\small]{latex}
+\begin{CAS}
+ vars('x')
+ if x:type() == SymbolExpression then
+ tex.print(x:tolatex(), "is a SymbolExpression.")
+ end
+\end{CAS}
+\end{minted}
+\tcblower
+\begin{CAS}
+ vars('x')
+ if x:type() == SymbolExpression then
+ tex.print(x:tolatex(), "is a SymbolExpression.")
+ end
+\end{CAS}
+\end{codebox}
+
+\subsubsection*{Parsing}
+
+The \LaTeX{} command \mintinline{latex}{\whatis} can be used to print the type of \texttt{Expression}:
+
+\begin{codebox}
+ \begin{minted}[fontsize=\small]{latex}
+x is a \whatis{x}
+ \end{minted}
+ \tcblower
+ x is a \whatis{x}
+\end{codebox}
+
+Alternatively, there's a \mintinline{lua}{whatis()} function and a \mintinline{lua}{longwhatis()} function that can be called within a Lua environment (like \mintinline{latex}{\directlua} or \mintinline{latex}{\luaexec}):
+
+\begin{codebox}
+\begin{minted}[fontsize=\small]{lua}
+tex.print(whatis(x), '\\newline')
+tex.print(longwhatis(x))
+\end{minted}
+\tcblower
+\directlua{
+ tex.print(whatis(x), '\\newline')
+ tex.print(longwhatis(x))
+}
+\end{codebox}
+
+
+
+
+\end{document} \ No newline at end of file