summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex')
-rw-r--r--macros/luatex/latex/luacas/doc/reference/ref_core/ref_core_methods/ref_core_methods.tex1155
1 files changed, 1155 insertions, 0 deletions
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