diff options
Diffstat (limited to 'macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex')
-rw-r--r-- | macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex | 977 |
1 files changed, 977 insertions, 0 deletions
diff --git a/macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex b/macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex new file mode 100644 index 0000000000..58d9b69f84 --- /dev/null +++ b/macros/luatex/latex/luacas/doc/reference/ref_algebra/ref_algebra_classes/ref_algebra_classes.tex @@ -0,0 +1,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}
\ No newline at end of file |