Debian Codegraph @

texmf-dist/doc/lualatex/lualinalg/lualinalg.tex

raw
1 \documentclass{article}
2 \usepackage{listings,color,parskip,booktabs,longtable,array,
3 hyperref,multirow,multicol,url,amsmath,amssymb,framed,lualinalg,tikz,tikz-3dplot}
4 \usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
5 \usetikzlibrary{calc,3d,arrows}
6 \hypersetup{colorlinks,urlcolor=blue}
7 \lstset{frame=none,
8 language=[LaTeX]{TeX},
9 aboveskip=3mm,
10 belowskip=3mm,
11 showstringspaces=false,
12 columns=flexible,
13 basicstyle={\ttfamily},
14 numbers=none,
15 numberstyle=\tiny\color{gray},
16 stringstyle=\color{mauve},
17 breaklines=true,
18 breakatwhitespace=true,
19 tabsize=1,
20 upquote=true
21 }
22 \begin{document}
23 \title{The lualinalg Package}
24 \author{Chetan Shirore\thanks{Email id: [email protected]} \space and Ajit Kumar}
25 \maketitle
26
27 \section{Introduction}
28
29 The \verb|lualinalg| package is developed to perform operations on vectors and matrices defined over the field of real or complex numbers inside LaTeX documents. It provides flexible ways for defining and displaying vectors and matrices. No particular environment of LaTeX is required to use commands in the package. The package is written in Lua, and tex file is to be compiled with the LuaLaTeX engine. The time required for calculations is not an issue while compiling with LuaLaTeX. There is no need to install Lua on the user's system as TeX distributions (TeXLive or MikTeX) come bundled with LuaLaTeX. It may also save users' efforts to copy vectors and matrices from other software (which may not be in latex-compatible format) and to use them in a tex file. The vectors and matrices of reasonable size can be handled with ease. The package can be modified or extended by writing custom Lua programs (Section \ref{customuse}).
30
31 The package supports fractions; numerators and denominators must be integers. A fraction can be specified with the Lua function: \verb|lfrac|. This function has the syntax \verb|lfrac(n,d,mode)|: \(n\) is an integer and \(d\) is a non-zero integer. The mode is optional. It can be \verb|fracs| or \verb|nofracs|. The default mode is \verb|fracs|. If fractions are input, the package will display vectors and matrices in fraction mode wherever possible. The package does not attempt to convert floats into fractions. If fractions are expected, then the input should contain fractions. If fractions are input and answers are expected in numbers, the mode can be specified as \verb|nofracs|.
32
33 The Lua function \verb|lcomplex| defines the complex numbers. It has the syntax \verb|lcomplex(x,y)|, where \(x\) is a real part, and \(y\) is an imaginary part. \(x\) and \(y\) can also be fractions (numerators and denominators should be integers). The package has a command \verb|\imUnit| which provides typesetting for the imaginary unit. Its default value is \verb|\mathrm{i}|. It can be redefined. For example, one can redefine it as \verb|\renewcommand{\imUnit}{j}}|.
34
35
36 \section{Installation and License}
37 The installation of the \verb|lualinalg| package is similar to the plain latex package, where the \verb|.sty| file is in the LaTeX directory of the texmf tree. The package can be included with \verb|\usepackage{lualinalg}| command in the preamble of the LaTeX document.
38
39 The \verb|lualinalg| package is released under the LaTeX Project Public License v1.3c or later. The complete license text is available at \url{http://www.latex-project.org/lppl.txt}. It is developed in Lua. Lua is available as a certified open-source software. Its license is simple and liberal, which is compatible with GPL. The package makes use of \verb|complex.lua| file which is available on \url{https://github.com/davidm/lua-matrix/blob/master/lua/complex.lua}. It is available under the same licensing as that of Lua. The package also loads the \href{https://ctan.org/pkg/luamaths}{luamaths} package, which is available under the LaTeX Project Public License v1.3c or later. This package is loaded to use the standard mathematical functions and for computations on real numbers while performing operations on vectors and matrices.
40
41 \section{Defining vectors and performing operations on vectors}
42 \subsection{Defining Vectors} Vectors are defined with the \verb|\vectornew| command.
43 \begin{lstlisting}
44 \vectorNew{vector name}{coordinates}
45 \end{lstlisting}
46
47 This command has two compulsory arguments: \verb|vector name| and \verb|coordinates|. Coordinates of vectors are enclosed in curly braces. A comma separates coordinates. The following are a few valid ways of defining vectors.
48 \begin{lstlisting}
49 \vectorNew{v1}{{1,2,3,4,5,6}}
50 \vectorNew{v2}{{3,6,lcomplex(6,6)}}
51 \end{lstlisting}
52 The standard vector of dimension \(n \) with \(i^{th}\) coordinate \(1\) and zero vectors can also be produced by using the command \verb|\vectorNew|. For example, the following commands
53 \begin{lstlisting}
54 \vectorNew{e_1}{3,'e',1}
55 \(e_1=\left(\vectorPrint{e}\right)\)
56 \vectorNew{O}{3,'zero'}
57 \(O=\left(\vectorPrint{O}\right)\)
58 \end{lstlisting}
59 output to \vectorNew{e_1}{3,'e',1}
60 \(e_1=\left(\vectorPrint{e_1}\right)\)
61 \vectorNew{O}{3,'zero'}
62 \(O=\left(\vectorPrint{O}\right)\)
63 \subsection{Commands for operations on vectors}
64 Table \ref{tbl:luavector} lists commands for operations on vectors.
65 \begin{longtable}{m{7cm}m{8.2cm}}
66 \toprule
67 \multicolumn{1}{c}{\textcolor{blue}{Command Format}} & \multicolumn{1}{c}{\textcolor{blue}{Description}} \\
68 \toprule
69 \begin{lstlisting}
70 \vectorPrint[truncate]{vector}
71 \end{lstlisting} & Prints vector. Accepts one \emph{optional} argument: \verb|truncate|. It specifies the number of digits up to which vector coordinates must be truncated. The value of \verb|truncate| may be 0,1,2,\ldots\\
72 \midrule
73 \begin{lstlisting}
74 \vectorGetCoordinate{vector}{i}
75 \end{lstlisting}
76 &
77 Gives the \(i\)th coordinate of vector. \\
78 \midrule
79 \begin{lstlisting}
80 \vectorSetCoordinate
81 {vector}{i}{val}
82 \end{lstlisting}
83 &
84 Sets the \(i\)th coordinate of vector as \verb|val|. \\
85 \midrule
86 \begin{lstlisting}
87 \vectorCopy{v}{w}
88 \end{lstlisting}&
89 Defines a new vector \(v\) obtained by copying coordinates of vector \(w\). \\
90 \midrule
91
92 \begin{lstlisting}
93 \vectorEql{v}{w}
94 \end{lstlisting}&
95 Returns true if \(v = w\), otherwise returns false. \\
96 \midrule
97
98 \begin{lstlisting}
99 \vectorAdd{vector}{v1}{v2}
100 \end{lstlisting}&
101 Defines a new vector as the addition of vectors \(v1\) and \(v2\). Both vectors \(v1\) and \(v2\) should be of the same dimension. The addition is done coordinate-wise.\\
102 \midrule
103 \begin{lstlisting}
104 \vectorSub{vector}{v1}{v2}
105 \end{lstlisting}&
106 Defines a new vector as the subtraction of vectors \(v1\) and \(v2\). Both vectors \(v1\) and \(v2\) should be of the same dimension. The subtraction is done coordinate-wise. \\
107 \midrule
108 \begin{lstlisting}
109 \vectorMulNum{vector}{v}{num}
110 \end{lstlisting}&
111 Defines a new vector obtained by multiplying each coordinate of a vector by number \verb|num|. It can be a real or complex number (scalar). \\
112 \midrule
113 \begin{lstlisting}
114 \vectorDot{v}{w}
115 \end{lstlisting}&
116 Gives the dot product of two vectors: \(v\) and \(w\). If \(v=\left(v_1, \ldots, v_n \right)\) and \(w =\left(w_1, \ldots, w_n \right)\) are defined over the field of real numbers, then it is evaluated as \(v_1\cdot w_1 + \cdots + v_n \cdot w_n\). If they are defined over the field of complex numbers, then it is evaluated as \(v_1\cdot \bar{w_1} + \cdots + v_n \cdot \bar{w_n}\). \(\bar{w_i}\) denotes the complex conjugate of complex number \(w_i\). \\
117 \midrule
118 \begin{lstlisting}
119 \vectorCross{vector}{v}{w}
120 \end{lstlisting}&
121 Defines a new vector obtained by taking the cross product of vectors \(v\) and \(w\) of dimension 3. If \(v=\left(v_1, v_2, v_3 \right)\) and \(w =\left(w_1, w_2, w_3 \right)\), then the cross product of these two vectors is the vector \((v_2w_3-v_3w_2, v_3w_1-v_1w_3,\) \( v_1w_2-v_2w_1 ) \). \\
122 \midrule
123 \begin{lstlisting}
124 \vectorSumNorm{v}
125 \end{lstlisting}&
126 Calculates the sum norm of a vector \(v\). If \(v=\left(v_1, \ldots, v_n \right)\) then it is given by \(|v_1| + \cdots + |v_n| \). \\
127 \midrule
128 \begin{lstlisting}
129 \vectorEuclidNorm{v}
130 \end{lstlisting}&
131 Calculates the Euclidean norm of a vector \(v\). If \(v=\left(v_1, \ldots, v_n \right)\) then it is given by \(\sqrt{|v_1|^2 + \cdots + |v_n|^2} \). \\
132 \midrule
133 \begin{lstlisting}
134 \vectorpNorm{v}
135 \end{lstlisting}&
136 Calculates the \(p\) \((p > 1)\) norm of a vector \(v\). If \(v=\left(v_1, \ldots, v_n \right)\) then it is given by \(\sqrt[\leftroot{-2}\uproot{2} p]{|v_1|^p + \cdots + |v_n|^p} \). \\
137 \midrule
138 \begin{lstlisting}
139 \vectorSupNorm{v}
140 \end{lstlisting}&
141 Calculates the sup norm of a vector \(v\). If \(v=\left(v_1, \ldots, v_n \right)\) then it is given by \(\max\{|v_1| , \ldots , |v_n| \} \). \\
142 \midrule
143 \begin{lstlisting}
144 \vectorCreateRandom{v}{n}{a}{b}
145 \end{lstlisting}&
146 Creates a new vector \(v\) of dimension \(n\) with coordinates as random numbers from the interval \([a,b]\). \\
147 \midrule
148 \begin{lstlisting}
149 \vectorOp{vector}{expression}
150 \end{lstlisting}&
151 Defines a new vector obtained by evaluating an expression. The expression supports all standard operations such as \(+,-,* \). \\
152 \midrule
153 \begin{lstlisting}
154 \vectorGetAngle{v}{w}
155 \end{lstlisting}&
156 Gives the angle between two vectors \(v\) and \(w\) in radians. If \(v\) and \(w \) are defined over the field of real numbers, then it is evaluated as \(\cos^{-1}\left(\frac{v \cdot w}{|v| |w|}\right)\). If they are defined over the field of complex numbers, then it is evaluated as \(\cos^{-1}\left(\frac{\Re (v \cdot w) }{|v| |w|}\right)\). Here \(v \cdot w\) denotes the dot product of vectors \(v\) and \(w\), \(\Re (v \cdot w)\) denotes real part of the dot product \(v \cdot w\), and \(|v|\) and \(|w|\) denote Euclidean norms of vectors \(v\) and \(w\) respectively. \\
157 \midrule
158 \begin{lstlisting}
159 \vectorParse{vector}
160 \end{lstlisting}&
161 Parses the coordinates of a vector defined over the field of real numbers. The command helps to plot vectors with different packages. \\
162 \midrule
163 \begin{lstlisting}
164 \vectorGramSchmidt[brckt,
165 truncate]{list of vectors}
166 \end{lstlisting} & Performs Gram Schmidt orthogonalisation process on a list of vectors. Accepts two \emph{optional} arguments: \verb|brckt| and \verb|truncate|. The \verb|brckt| is type of parenthesis to be used for displaying vectors. It can be `round', `square' or `curly'. The truncate is number of digits up to which vector coordinates are to be truncated. The value of \verb|truncate| can be 0,1,2,\ldots\\
167 \midrule
168 \begin{lstlisting}
169 \vectorGramSchmidtSteps[brckt,
170 truncate]{list of vectors}
171 \end{lstlisting} & Performs Gram Schmidt orthogonalisation process on a list of vectors in a step-by-step manner. Accepts two \emph{optional} arguments: \verb|brckt| and \verb|truncate|. The \verb|brckt| is type of parenthesis to be used for displaying vectors. It can be `round', `square' or `curly'. The truncate is number of digits up to which vector coordinates are to be truncated. The value of \verb|truncate| can be 0,1,2,\ldots\\
172 \bottomrule \\
173 \caption{Commands for operations on vectors}
174 \label{tbl:luavector}
175 \end{longtable}
176
177 \subsection{Illustrations of commands for operations on vectors}
178 The following commands define vectors \(v,w,x,\) and \(y\).
179 \begin{lstlisting}
180 \vectorNew{v}{{1,2,lcomplex(3,3)}}
181 \vectorNew{w}{{3,6,lcomplex(6,6)}}
182 \vectorNew{x}{{1.12345678,6,lcomplex(6,6)}}
183 \vectorNew{y}{{1,2,3}}
184 \end{lstlisting}
185 Table \ref{tbl:illluavector} illustrates various operations on vectors \(v,w,x\) and \(y\).
186 \vectorNew{v}{{1,2,lcomplex(3,3)}}
187 \vectorNew{w}{{3,6,lcomplex(6,6)}}
188 \vectorNew{x}{{1.12345678,6,lcomplex(6,6)}}
189 \vectorNew{y}{{1,2,3}}
190 \begin{longtable}{lp{6.5cm}}
191 \toprule
192 Commands & Output Produced\\
193 \toprule
194 \begin{lstlisting}
195 \(v=\left(\vectorPrint{v}\right)\)
196 \(w=\left(\vectorPrint{w}\right)\)
197 \end{lstlisting} &
198 \(v=\left(\vectorPrint{v}\right)\) \newline
199 \(w=\left(\vectorPrint{w}\right)\)
200 \\
201 \midrule
202 \begin{lstlisting}
203 \(x=\left(\vectorPrint
204 [truncate=3]{x}\right)\)
205 \end{lstlisting} &
206 \(x=\left(\vectorPrint
207 [truncate=3]{x}\right)\)
208 \\
209 \midrule
210 \begin{lstlisting}
211 third coordinate of vector
212 \(v = \vectorGetCoordinate{v}{3}\)
213 \end{lstlisting} &
214 third coordinate of vector
215 \(v = \vectorGetCoordinate{v}{3}\)
216 \\
217 \midrule
218 \begin{lstlisting}
219 \(\vectorCopy{z}{w}\)
220 \(z = \left(\vectorPrint{z}\right)\)
221 \end{lstlisting} &
222 \(\vectorCopy{z}{w}\)
223 \(z = \left(\vectorPrint{z}\right)\)
224 \\
225 \midrule
226 \begin{lstlisting}
227 \vectorEql{z}{w}
228 \end{lstlisting} &
229 \vectorEql{z}{w}
230 \\
231 \midrule
232 \begin{lstlisting}
233 new third coordinate of vector
234 \(z = \vectorSetCoordinate{z}{3}{9.3}\)
235 \(z=\left(\vectorPrint{z}\right)\)
236 \end{lstlisting} &
237 new third coordinate of vector
238 \(z = \vectorSetCoordinate{z}{3}{9.3}\) \newline
239 \(z=\left(\vectorPrint{z}\right)\)
240 \\
241 \midrule
242 \begin{lstlisting}
243 \vectorAdd{v1}{v}{w}
244 \(v1 = v+w =\left(\vectorPrint{v1}\right)\)
245 \end{lstlisting} &
246 \vectorAdd{v1}{v}{w}
247 \(v1 = v+w =\left(\vectorPrint{v1}\right)\)
248 \\
249 \midrule
250 \begin{lstlisting}
251 \vectorSub{v2}{v}{w}
252 \(v2 = v-w =\left(\vectorPrint{v2}\right)\)
253 \end{lstlisting} &
254 \vectorSub{v2}{v}{w}
255 \(v2 = v-w =\left(\vectorPrint{v2}\right)\)
256 \\
257 \midrule
258 \begin{lstlisting}
259 \vectorMulNum{v3}{v}{complex('3+i')}
260 \(v3 = 3v =\left(\vectorPrint{v3}\right)\)
261 \end{lstlisting} &
262 \vectorMulNum{v3}{v}{complex('3+i')}
263 \(v3 = 3v =\left(\vectorPrint{v3}\right)\)
264 \\
265 \midrule
266 \begin{lstlisting}
267 \(v \cdot w =\vectorDot{v}{w}\)
268 \end{lstlisting} &
269 \(v \cdot w =\vectorDot{v}{w}\)
270 \\
271 \midrule
272 \begin{lstlisting}
273 \vectorCross{v4}{v}{w}
274 \(v \times w =\left(\vectorPrint{v4}\right)\)
275 \end{lstlisting} &
276 \vectorCross{v4}{v}{w}
277 \(v \times w =\left(\vectorPrint{v4}\right)\)
278 \\
279 \midrule
280 \begin{lstlisting}
281 Sum norm of a vector \(v = \vectorSumNorm{v}\)
282 \end{lstlisting} &
283 Sum norm of a vector \(v = \vectorSumNorm{v}\)
284 \\
285 \midrule
286 \begin{lstlisting}
287 Euclidean norm of a vector
288 \(v = \vectorEuclidNorm{v}\)
289 \end{lstlisting} &
290 Euclidean norm of a vector
291 \(v = \vectorEuclidNorm{v}\)
292 \\
293 \midrule
294 \begin{lstlisting}
295 p norm of a vector \(v = \vectorpNorm{v}{3}\)
296 \end{lstlisting} &
297 p norm of a vector \(v = \vectorpNorm{v}{3}\)
298 \\
299 \midrule
300 \begin{lstlisting}
301 Sup norm of a vector \(v = \vectorSupNorm{v}\)
302 \end{lstlisting} &
303 Sup norm of a vector \(v = 4.2426406871193\)
304 \\
305 \midrule
306 \begin{lstlisting}
307 \vectorCreateRandom{v5}{3}{9}{90}
308 \(v5 =\left(\vectorPrint{v5}\right)\)
309 \end{lstlisting} &
310 \vectorCreateRandom{v5}{3}{9}{90}
311 \(v5 =\left(\vectorPrint{v5}\right)\)
312 \\
313 \midrule
314 \begin{lstlisting}
315 \vectorOp{v6}{v+w-2*v}
316 \(v6 =\left(\vectorPrint{v6}\right)\)
317 \end{lstlisting} &
318 \vectorOp{v6}{v+w-2*v}
319 \(v6 =\left(\vectorPrint{v6}\right)\)
320 \\
321 \midrule
322 \begin{lstlisting}
323 angle between vector \(v\) and \(w\) is
324 \( \vectorGetAngle{v}{w}\).
325 \end{lstlisting} &
326 angle between vector \(v\) and \(w\) is
327 \( \vectorGetAngle{v}{w}\).
328 \\
329 \midrule
330 \begin{lstlisting}
331 \vectorParse{y}
332 \end{lstlisting} &
333 \vectorParse{y}
334 \\
335 \bottomrule \\
336 \caption{Illustration of commands for operations on vectors}
337 \label{tbl:illluavector}
338 \end{longtable}
339 The package has commands for performing Gram Schmidt Orthogonalisation process. It can also produce the computations in a step-by step manner.
340 \begin{framed}
341 \begin{lstlisting}[label={code:luavecgs}, caption={Gram Schmidt Orthogonalisation process in the lualinalgpackage}]
342 \vectorNew{v1}{{1,2,3}}
343 \vectorNew{v2}{{4,5,6}}
344 \vectorNew{v3}{{7,8,90}}
345 \[v1=\left(\vectorPrint{v1}\right)\]
346 \[v2=\left(\vectorPrint{v2}\right)\]
347 \[v3=\left(\vectorPrint{v3}\right)\]
348 Gram Schmidt on \(v1,v2,v3\): \vectorGramSchmidt[brckt=round,truncate=3]{{'v1','v2','v3'}}
349 \vectorGramSchmidtSteps[brckt=round,truncate=3]{{'v1','v2','v3'}}
350 \end{lstlisting}
351 \end{framed}
352 Listing \ref{code:luavecgs} outputs the following.
353 \begin{framed}
354 \vectorNew{v1}{{1,2,3}}
355 \vectorNew{v2}{{4,5,6}}
356 \vectorNew{v3}{{7,8,90}}
357 \[v1=\left(\vectorPrint{v1}\right)\]
358 \[v2=\left(\vectorPrint{v2}\right)\]
359 \[v3=\left(\vectorPrint{v3}\right)\]
360 Gram Schmidt on \(v1,v2,v3\): \vectorGramSchmidt[brckt=round,truncate=3]{{'v1','v2','v3'}}
361 \vectorGramSchmidtSteps[brckt=round,truncate=3]{{'v1','v2','v3'}}
362 \end{framed}
363
364 In addition to \verb|\mathRound|, the command \verb|complexRound| is also available. It has the following syntax.
365 \begin{verbatim}
366 \complexRound{complex number}{number of decimal places}
367 \end{verbatim}
368
369 This command has two compulsory arguments. The complex number and number of decimal places to which number should be rounded off. For example,
370 \verb| \complexRound{lcomplex(3.3333666, 6.777666)}{3}| outputs to \(\complexRound{lcomplex(3.3333666, 6.777666)}{3}\). This command can be nested with other commands in the package.
371
372 \subsection{Plotting vectors}
373 The \verb|lualinalg| package can be used with other packages that have facility to plot vectors defined over the field of real numbers in 2 or 3 dimensions. Listing \ref{code:luavecplot} illustrates plotting of vectors in 2-D plane by using \verb|lualinalg| and \verb|tikz| package.
374 \begin{lstlisting}[label={code:luavecplot}, caption={Plotting vectors in 2-dimensions with the lualinalg and tikz packages}]
375 \tdplotsetmaincoords{0}{0}
376 \begin{tikzpicture}[scale=1,
377 tdplot_main_coords,
378 axis/.style={->,blue,thick},
379 vector/.style={-stealth,red,very thick},
380 vector guide/.style={dashed,red,thick}]
381 \vectorNew{o}{{0,0}}
382 \vectorNew{e1}{{4,0}}
383 \vectorNew{e2}{{0,4}}
384 \vectorNew{f}{{2,1}}
385 \vectorNew{g}{{1,2}}
386 % Axes
387 \draw [axis] \vectorParse{o}-- \vectorParse{e1} node [below left] {$x$};
388 \draw [axis] \vectorParse{o}-- \vectorParse{e2} node [right] {$y$};
389 % Plotting Vectors
390 \draw [vector] \vectorParse{o} --\vectorParse{f};
391 \draw [vector] \vectorParse{o} --\vectorParse{g};
392 \vectorOp{h}{f+g}
393 \draw [vector] \vectorParse{o} --\vectorParse{h};
394 \draw [vector,dashed,black] \vectorParse{f} --\vectorParse{h};
395 \draw [vector,dashed,black] \vectorParse{g} --\vectorParse{h};
396 % Labels
397 \node [below right] at \vectorParse{f} {$f$};
398 \node [above left] at \vectorParse{g} {$g$};
399 \node [above left] at \vectorParse{h} {$f+g$};
400 \draw[vector guide, black] \vectorParse{h} -- (\vectorGetCoordinate{h}{1},0) node [below] {$x=\vectorGetCoordinate{h}{1}$};
401 \draw[vector guide, black] \vectorParse{h} -- (0,\vectorGetCoordinate{h}{2}) node [left] {$y=\vectorGetCoordinate{h}{2}$};
402 \end{tikzpicture}
403 \end{lstlisting}
404 Listing \ref{code:luavecplot} produces figure \ref{fig:2dvecplot}.
405 \begin{figure}
406 \centering
407 \tdplotsetmaincoords{0}{0}
408 \begin{tikzpicture}[scale=1,
409 tdplot_main_coords,
410 axis/.style={->,blue,thick},
411 vector/.style={-stealth,red,very thick},
412 vector guide/.style={dashed,red,thick}]
413 \vectorNew{o}{{0,0}}
414 \vectorNew{e1}{{4,0}}
415 \vectorNew{e2}{{0,4}}
416 \vectorNew{f}{{2,1}}
417 \vectorNew{g}{{1,2}}
418 % Axes
419 \draw [axis] \vectorParse{o}-- \vectorParse{e1} node [below left] {$x$};
420 \draw [axis] \vectorParse{o}-- \vectorParse{e2} node [right] {$y$};
421 % Plotting Vectors
422 \draw [vector] \vectorParse{o} --\vectorParse{f};
423 \draw [vector] \vectorParse{o} --\vectorParse{g};
424 \vectorOp{h}{f+g}
425 \draw [vector] \vectorParse{o} --\vectorParse{h};
426 \draw [vector,dashed,black] \vectorParse{f} --\vectorParse{h};
427 \draw [vector,dashed,black] \vectorParse{g} --\vectorParse{h};
428 % Labels
429 \node [below right] at \vectorParse{f} {$f$};
430 \node [above left] at \vectorParse{g} {$g$};
431 \node [above left] at \vectorParse{h} {$f+g$};
432 \draw[vector guide, black] \vectorParse{h} -- (\vectorGetCoordinate{h}{1},0) node [below] {$x=\vectorGetCoordinate{h}{1}$};
433 \draw[vector guide, black] \vectorParse{h} -- (0,\vectorGetCoordinate{h}{2}) node [left] {$y=\vectorGetCoordinate{h}{2}$};
434 \end{tikzpicture}
435 \caption{Plotting 2-D Vectors with lualinalg and tikz packages}
436 \label{fig:2dvecplot}
437 \end{figure}
438
439
440 Listing \ref{code:luavecplot2} illustrates plotting of vectors in 3-D plane by using \verb|lualinalg| and \verb|tikz| package.
441 \begin{lstlisting}[label={code:luavecplot2}, caption={Plotting vectors in 3-dimensions with the lualinalg and tikz packages}]
442 \documentclass{article}
443 \usepackage{tikz,tikz-3dplot,lualinalg}
444 \begin{document}
445 \tdplotsetmaincoords{60}{120}
446 \begin{tikzpicture}[scale=1,
447 tdplot_main_coords,
448 axis/.style={->,blue,thick},
449 vector/.style={-stealth,red,very thick},
450 vector guide/.style={dashed,red,thick}]
451 \vectorNew{o}{{0,0,0}}
452 \vectorNew{e1}{{3,0,0}}
453 \vectorNew{e2}{{0,5,0}}
454 \vectorNew{e3}{{0,0,4}}
455 \vectorNew{f}{{2,2,0}}
456 \vectorNew{g}{{-1,2,2}}
457 % Axes
458 \draw [axis] \vectorParse{o}-- \vectorParse{e1} node [below left] {$x$};
459 \draw [axis] \vectorParse{o}-- \vectorParse{e2} node [right] {$y$};
460 \draw [axis] \vectorParse{o}-- \vectorParse{e3} node [above] {$z$};
461 % Plotting Vectors
462 \draw [vector] \vectorParse{o} --\vectorParse{f};
463 \draw [vector] \vectorParse{o} --\vectorParse{g};
464 \vectorOp{h}{f+g}
465 \draw [vector] \vectorParse{o} --\vectorParse{h};
466 % Labels
467 \node [below right] at \vectorParse{f} {$f$};
468 \node [above left] at \vectorParse{g} {$g$};
469 \node [right] at \vectorParse{h} {$f+g$};
470 \draw[vector guide, black] \vectorParse{h} -- (\vectorGetCoordinate{h}{1},0,0) node [left] {$x=\vectorGetCoordinate{h}{1}$};
471 \draw[vector guide, black] \vectorParse{h} -- (0,\vectorGetCoordinate{h}{2},0) node [below] {$y=\vectorGetCoordinate{h}{2}$};
472 \draw[vector guide, black] \vectorParse{h} -- (0,0,\vectorGetCoordinate{h}{3}) node [left] {$z=\vectorGetCoordinate{h}{3}$};
473 \end{tikzpicture}
474 \end{document}
475 \end{lstlisting}
476
477 Listing \ref{code:luavecplot2} produces figure \ref{fig:3dvecplot}.
478 \begin{figure}
479 \centering
480 \tdplotsetmaincoords{60}{120}
481 \begin{tikzpicture}[scale=1,
482 tdplot_main_coords,
483 axis/.style={->,blue,thick},
484 vector/.style={-stealth,red,very thick},
485 vector guide/.style={dashed,red,thick}]
486 \vectorNew{o}{{0,0,0}}
487 \vectorNew{e1}{{3,0,0}}
488 \vectorNew{e2}{{0,5,0}}
489 \vectorNew{e3}{{0,0,4}}
490 \vectorNew{f}{{2,2,0}}
491 \vectorNew{g}{{-1,2,2}}
492 % Axes
493 \draw [axis] \vectorParse{o}-- \vectorParse{e1} node [below left] {$x$};
494 \draw [axis] \vectorParse{o}-- \vectorParse{e2} node [right] {$y$};
495 \draw [axis] \vectorParse{o}-- \vectorParse{e3} node [above] {$z$};
496 % Plotting Vectors
497 \draw [vector] \vectorParse{o} --\vectorParse{f};
498 \draw [vector] \vectorParse{o} --\vectorParse{g};
499 \vectorOp{h}{f+g}
500 \draw [vector] \vectorParse{o} --\vectorParse{h};
501 % Labels
502 \node [below right] at \vectorParse{f} {$f$};
503 \node [above left] at \vectorParse{g} {$g$};
504 \node [right] at \vectorParse{h} {$f+g$};
505 \draw[vector guide, black] \vectorParse{h} -- (\vectorGetCoordinate{h}{1},0,0) node [left] {$x=\vectorGetCoordinate{h}{1}$};
506 \draw[vector guide, black] \vectorParse{h} -- (0,\vectorGetCoordinate{h}{2},0) node [below] {$y=\vectorGetCoordinate{h}{2}$};
507 \draw[vector guide, black] \vectorParse{h} -- (0,0,\vectorGetCoordinate{h}{3}) node [left] {$z=\vectorGetCoordinate{h}{3}$};
508 \end{tikzpicture}
509 \caption{Plotting 3-D Vectors with lualinalg and tikz packages}
510 \label{fig:3dvecplot}
511 \end{figure}
512
513 \section{Defining matrices and operations on matrices}
514 Matrices are defined with the \verb|\matrixNew| command.
515 \begin{lstlisting}
516 \matrixNew{matrix name}{row entries}
517 \end{lstlisting}
518 This command has two compulsory arguments: \verb|matrix name| and \verb|row entries|. Each row of the matrix is enclosed in curly brackets. A comma separates numbers in rows. Rows are also separated by a comma. The whole matrix is then enclosed in curly brackets. The following are a few valid ways of defining matrices.
519
520 \begin{lstlisting}
521 \def\n{{{1,2,3},{4,5,6},{7,8,lcomplex(9,3)}}}
522 \def\s{{{1,2,3},{4,5,6},{7,8,10}}}
523 \matrixNew{m}{\n}
524 \matrixNew{n}{\s}
525 % an alternative way
526 \matrixNew{m}{{{1,2,3},{4,5,6},{7,8,lcomplex(9,3)}}}
527 \matrixNew{n}{{{1,2,3},{4,5,6},{7,8,10}}}
528 \end{lstlisting}
529
530 The identity and zero matrices can be defined as well by using the \verb|\matrixNew| command. For example, the following commands
531 \begin{lstlisting}
532 \matrixNew{mtx}{3,'I'}
533 I = \(\matrixPrint{mtx}\)
534 \matrixNew{O}{3,4,'zero'}
535 O = \(\matrixPrint{O}\)
536 \end{lstlisting}
537 output to
538 \matrixNew{mtx}{3,'I'}
539 I = \(\matrixPrint{mtx}\)
540 \matrixNew{O}{3,4,'zero'}
541 O = \(\matrixPrint{O}\)
542 \subsection{Commands for operations on matrices}
543 Table \ref{tbl:luamtxcmd} lists all commands for operations on matrices in the \verb|lualinalg| package.
544 \begin{longtable}{m{7cm}m{7.5cm}}
545 \toprule
546 \multicolumn{1}{c}{\textcolor{blue}{Command Format}} & \multicolumn{1}{c}{\textcolor{blue}{Description}} \\
547 \toprule
548 \multicolumn{2}{c}{Printing Matrices}\\
549 \midrule
550 \begin{lstlisting}
551 \matrixPrint[type,truncate]{mtx}
552 \end{lstlisting} & Prints matrix. Accepts two \emph{optional} arguments: \verb|type| and \verb|truncate|. The \verb|type| may be one of the values \verb|pmatrix,bmatrix, vmatrix,Vmatrix|.
553 The default type is \verb|bmatrix|. The \verb|truncate| specifies the number of digits up to which matrix entries are to be truncated. The value of \verb|truncate| may be 0,1,2,\ldots.\\
554 \midrule
555 \multicolumn{2}{c}{Some parameters of defined matrices} \\
556 \midrule
557 \begin{lstlisting}
558 \matrixNumRows{matrix}
559 \end{lstlisting} &
560 Gives the number of rows in a matrix.\\
561 \midrule
562 \begin{lstlisting}
563 \matrixNumCols{matrix}
564 \end{lstlisting}&
565 Gives the number of columns in a matrix.\\
566 \midrule
567 \begin{lstlisting}
568 \matrixGetElement{matrix}{i}{j}
569 \end{lstlisting}
570 &
571 Gives an entry of matrix in the \(i\)th row and the \(j\)th column. \\
572 \midrule
573 \multicolumn{2}{c}{Algebraic operations on matrices} \\
574 \midrule
575 \begin{lstlisting}
576 \matrixAdd{matrix}{m1}{m2}
577 \end{lstlisting}&
578 Defines a new matrix as the addition of matrices m1 and m2. The second matrix may have more rows and\textbackslash or columns.\\
579 \midrule
580 \begin{lstlisting}
581 \matrixSub{matrix}{m1}{m2}
582 \end{lstlisting}&
583 Defines a new matrix as the subtraction of matrices m1 and m2. The second matrix may have more rows and\textbackslash or columns.\\
584 \midrule
585 \begin{lstlisting}
586 \matrixMulNum{matrix}{number}{m1}
587 \end{lstlisting}&
588 Defines a new matrix obtained by multiplying each entry of matrix m1 by s real or complex number. \\
589 \midrule
590 \begin{lstlisting}
591 \matrixMul{matrix}{m1}{m2}
592 \end{lstlisting}&
593 Defines a new matrix obtained by multiplying matrix m1 by matrix m2. The number of rows in matrix m2 must equal the number of columns in matrix m1.\\
594 \midrule
595 \begin{lstlisting}
596 \matrixPow{matrix}{m1}{power}
597 \end{lstlisting}&
598 Defines a new matrix obtained by taking the \(i\)th power of matrix m1 (multiplying matrix m1 \(i\) times with itself). \\
599 \midrule
600 \begin{lstlisting}
601 \matrixInvert{matrix}{matrix1}
602 \end{lstlisting}&
603 Defines a new matrix obtained by taking the inverse of matrix1. It throws an error if matrix is not invertible. \\
604 \midrule
605 \begin{lstlisting}
606 \matrixTrace{matrix}
607 \end{lstlisting}&
608 Gives the trace (sum of diagonal entries) of a square matrix. It throws an error if the matrix is not square. \\
609 \midrule
610
611 \begin{lstlisting}
612 \matrixConjugate{matrix}{m1}
613 \end{lstlisting}&
614 Defines a new matrix obtained by taking the complex conjugate of each entry of matrix m1. \\
615 \midrule
616 \begin{lstlisting}
617 \matrixConjugateT{matrix}{m1}
618 \end{lstlisting}&
619 Defines a new matrix obtained by taking the transpose of matrix m1 and then the complex conjugate of each matrix entry. \\
620 \midrule
621 \begin{lstlisting}
622 \matrixNormOne{matrix}
623 \end{lstlisting}&
624 Calculates the norm1 of a matrix. For matrix \(A\) of size \(m \times n\), it is given by
625 \[ \|A\|_{1}=\max _{1 \leqslant j \leqslant n} \sum_{i=1}^{m}\left|a_{i j}\right| \] \\
626 \midrule
627 \begin{lstlisting}
628 \matrixNormInfty{matrix}
629 \end{lstlisting}&
630 Calculates the infinity norm of a matrix. For matrix \(A\) of size \(m \times n\), it is given by
631 \[ \|A\|_{\infty}=\max _{1 \leqslant i \leqslant m} \sum_{j=1}^{n}\left|a_{i j}\right| \] \\
632 \midrule
633 \begin{lstlisting}
634 \matrixNormMax{matrix}
635 \end{lstlisting}&
636 Calculates the max norm of a matrix. For matrix \(A\) of size \(m \times n\), it is given by
637 \[ \|A\|_{\max }=\max _{i,j}\left|a_{i j}\right| \] \\
638 \midrule
639 \begin{lstlisting}
640 \matrixNormF{matrix}
641 \end{lstlisting}&
642 Calculates the Frobenius norm of a matrix. For matrix \(A\) of size \(m \times n\), it is given by
643 \[\|A\|_{F}=\sqrt{\sum_{i=1}^{m} \sum_{j=1}^{n}\left|a_{i j}\right|^{2}} \] \\
644 \midrule
645 \begin{lstlisting}
646 \matrixRank{matrix}
647 \end{lstlisting}&
648 Gives the rank of matrix \(m\). It also supports matrices of complex numbers. \\
649 \midrule
650 \begin{lstlisting}
651 \matrixDet{matrix}
652 \end{lstlisting}&
653 Gives the determinant of matrix \(m\). It also supports matrices of complex numbers. \\
654 \midrule
655 \begin{lstlisting}
656 \matrixTranspose{matrix}{m1}
657 \end{lstlisting}&
658 Defines a new matrix obtained by taking the transpose of matrix m1. \\
659 \midrule
660 \begin{lstlisting}
661 \matrixSetElement{matrix}{i}{j}{val}
662 \end{lstlisting}
663 &
664 Set entry of a matrix in the \(i\)th row and \(j\)th column as \verb|val|. \\
665 \midrule
666 \begin{lstlisting}
667 \matrixSubmatrix{sm}{m}{i}{j}{k}{l}
668 \end{lstlisting}&
669 Defines a new matrix sm obtained by taking a submatrix of matrix m. Here \(i,j\) denotes the start row and start column, and \(k,l\) denotes the end row and end column for obtaining the submatrix. \\
670 \midrule
671 \begin{lstlisting}
672 \matrixConcatH{matrix}{m1}{m2}
673 \end{lstlisting}&
674 Defines a new matrix obtained by augmenting matrix m1 with matrix m2 horizontally. \\
675 \midrule
676 \begin{lstlisting}
677 \matrixConcatV{matrix}{m1}{m2}
678 \end{lstlisting}&
679 Defines a new matrix obtained by augmenting matrix m1 with matrix m2 vertically. \\
680 \midrule
681 \begin{lstlisting}
682 \matrixOp{matrix}{expression}
683 \end{lstlisting}&
684 Defines a new matrix obtained by evaluating an expression. The expression supports all standard operations such as \(+,*, \) \^ \ . \\
685 \midrule
686 \begin{lstlisting}
687 \matrixCopy{matrix}{matrix1}
688 \end{lstlisting}&
689 Defines a new matrix obtained by copying values from matrix1. \\
690 \midrule
691
692 \begin{lstlisting}
693 \matrixEql{m1}{m2}
694 \end{lstlisting}&
695 Returns true if m1 = m2, otherwise returns false. \\
696 \midrule
697
698 \begin{lstlisting}
699 \matrixCreateRandom
700 {m}{i}{j}{k}{l}
701 \end{lstlisting}&
702 Creates a new matrix m with random numbers. Here \(i, j\) denotes the number of rows and columns, and \(k, l\) denotes the start and end integers between which random numbers are generated. \\
703 \midrule
704 \multicolumn{2}{c}{Elementary row operations on matrices} \\
705 \midrule
706 \begin{lstlisting}
707 \matrixSwapRows{mtx}{m1}{i}{j}
708 \end{lstlisting}&
709 Defines a new matrix mtx obtained by swapping the \(i\)th and \(j\)th rows of matrix m1. \\
710 \midrule
711 \begin{lstlisting}
712 \matrixMulRow{matrix}{m}{i}{no}
713 \end{lstlisting}&
714 Defines a new matrix obtained by multiplying the \(i\)th row of matrix1 by a real or complex number. \\
715 \midrule
716 \begin{lstlisting}
717 \matrixMulAddRow{mtx}{m}{i}{no}{j}
718 \end{lstlisting}&
719 Defines a new matrix mtx obtained by multiplying the \(i\)th row of matrix1 by a real or complex number and adding it to the \(j\)th row. \\
720 \midrule
721 \multicolumn{2}{c}{Elementary column operations on matrices} \\
722 \midrule
723 \begin{lstlisting}
724 \matrixSwapCols{mtx}{m}{i}{j}
725 \end{lstlisting}&
726 Defines a new matrix mtx obtained by swapping the \(i\)th and \(j\)th columns of matrix m. \\
727 \midrule
728 \begin{lstlisting}
729 \matrixMulCol{matrix}{m}{i}{no}
730 \end{lstlisting}&
731 Defines a new matrix obtained by multiplying the \(i\)th column of matrix1 by a real or complex number. \\
732 \midrule
733 \begin{lstlisting}
734 \matrixMulAddCol{mtx}{m}{i}{no}{j}
735 \end{lstlisting}&
736 Defines a new matrix mtx obtained by multiplying the \(i\)th column of matrix1 by a real or complex number and adding it to the \(j\)th column. \\
737 \midrule
738 \multicolumn{2}{c}{Reduced row echelon form of matrix} \\
739 \midrule
740 \begin{lstlisting}
741 \matrixRREF{matrix}{matrix1}
742 \end{lstlisting}&
743 Defines a new matrix obtained by taking the reduced row echelon form of matrix1. It supports matrices of complex numbers as well. \\
744 \midrule
745 \begin{lstlisting}
746 \matrixRREFSteps[type,truncate]
747 {matrix}
748 \end{lstlisting}&
749 Obtains reduced row echelon form of matrix in a step-by-step manner. The command has two optional parameters \verb|type| and \verb|truncate|. It supports matrices with complex numbers as well. \verb|type| may be one of the values \verb|pmatrix,bmatrix, vmatrix,Vmatrix|.
750 The default type is \verb|bmatrix|. \verb|truncate| specifies number of digits up to which matrix entries are to be truncated. \verb|truncate| may be 0,1,2,\ldots. \\
751 \midrule
752 \multicolumn{2}{c}{Gauss-Jordan Elimination} \\
753 \midrule
754 \begin{lstlisting}
755 \matrixGaussJordan{matrix}
756 {m1}{m2}
757 \end{lstlisting}&
758 Defines new matrix obtained by performing Gauss-Jordan elimination on augmented matrix \(m1|m2\). \\
759 \midrule
760 \begin{lstlisting}
761 \matrixGaussJordanSteps[type,truncate]
762 {matrix}{m1}{m2}
763 \end{lstlisting}&
764 Defines new matrix obtained by performing Gauss-Jordan elimination on augmented matrix \(m1|m2\) in a step-by-step manner. The command has two optional parameters \verb|type| and \verb|truncate|. \verb|type| may be one of the values \verb|pmatrix, bmatrix, vmatrix,| \verb |Vmatrix|. The default type is \verb|bmatrix|. \verb|truncate| specifies number of digits up to which matrix entries are to be truncated. \verb|truncate| may be 0,1,2,\ldots. \\
765 \bottomrule \\
766 \caption{Commands for operations on matrices}
767 \label{tbl:luamtxcmd}
768 \end{longtable}
769
770 \subsection{Illustrations of matrix operations}
771 The following commands define matrices \(m,n,\) and \(r\).
772 \begin{lstlisting}
773 \def\r{{{1,2,3},{4,5,6},{7,8,lcomplex(9,3)}}}
774 \def\s{{{1,2,3},{4,5,6},{7,8,10}}}
775 \def\t{{{1,2,3},{4,5,6},{7,8,9}}}
776 \def\u{{{1},{2},{3}}}
777 \def\z{{{lfrac(1,2),lcomplex(2,3),3},{4,5,6},{7,8,9}}}
778
779 \matrixNew{m}{\r}
780 \matrixNew{n}{\s}
781 \matrixNew{p}{\t}
782 \matrixNew{q}{\u}
783 \matrixNew{r}{\z}
784
785 \end{lstlisting}
786
787 \def\r{{{1,2,3},{4,5,6},{7,8,lcomplex(9,3)}}}
788 \def\s{{{1,2,3},{4,5,6},{7,8,10}}}
789 \def\t{{{1,2,3},{4,5,6},{7,8,9}}}
790 \def\u{{{1},{2},{3}}}
791 \def\z{{{lfrac(1,2),lcomplex(2,3),3},{4,5,6},{7,8,9}}}
792
793 \matrixNew{m}{\r}
794 \matrixNew{n}{\s}
795 \matrixNew{p}{\t}
796 \matrixNew{q}{\u}
797 \matrixNew{r}{\z}
798 Table \ref{tbl:illluamatrix} illustrates various operations on matrices \(m,n,p,\) and \(q\).
799 \begin{center}
800 \begin{longtable}{lc}
801 \toprule
802 Commands & Output Produced\\
803 \toprule
804 \multicolumn{2}{c}{Printing matrices}\\
805 \midrule
806 \begin{lstlisting}
807 \(m=\matrixPrint{m}\)
808 \end{lstlisting} &
809 \(m=\matrixPrint{m}\)
810 \\
811 \midrule
812 \begin{lstlisting}
813 \(m=\matrixPrint[type=pmatrix]{m}\)
814 \end{lstlisting} &
815 \(m=\matrixPrint[type=pmatrix]{m}\)
816 \\
817 \midrule
818 \multicolumn{2}{c}{Some parameters of defined matrices}\\
819 \midrule
820 \begin{lstlisting}
821 No. or rows in matrix \(m
822 = \matrixNumRows{m}\)
823 \end{lstlisting} &
824 No. or rows in matrix \(m
825 = \matrixNumRows{m}\)\\
826 \midrule
827 \begin{lstlisting}
828 No. or columns in matrix \(m
829 = \matrixNumCols{m}\)
830 \end{lstlisting} &
831 No. or columns in matrix \(m
832 = \matrixNumCols{m}\)\\
833 \midrule
834 \begin{lstlisting}
835 Element of matrix \(m\) at \((3,3) =
836 \matrixGetElement{m}{3}{3}\)
837 \end{lstlisting} &
838 Element of matrix \(m\) at \((3,3) =
839 \matrixGetElement{m}{3}{3}\)\\
840 \midrule
841 \multicolumn{2}{c}{Algebraic operations on matrices}\\
842 \midrule
843 \begin{lstlisting}
844 \matrixAdd{m1}{m}{p}
845 \(m1 = \matrixPrint{m1}\)
846 \end{lstlisting} &
847 \matrixAdd{m1}{m}{p}
848 \matrixAdd{m1}{m}{p}
849 \(m1 = \matrixPrint{m1}\)\\
850 \midrule
851 \begin{lstlisting}
852 \matrixSub{m2}{m}{p}
853 \(m2 = \matrixPrint{m2}\)
854 \end{lstlisting} &
855 \matrixSub{m2}{m}{p}
856 \(m2 = \matrixPrint{m2}\)\\
857 \midrule
858 \begin{lstlisting}
859 \matrixMulNum{m3}{3}{m}
860 \(m3 = \matrixPrint{m3}\)
861 \end{lstlisting} &
862 \matrixMulNum{m3}{3}{m}
863 \(m3 = \matrixPrint{m3}\)\\
864 \midrule
865 \begin{lstlisting}
866 \matrixMul{m4}{m}{p}
867 \(m4 = \matrixPrint{m4}\)
868 \end{lstlisting} &
869 \matrixMul{m4}{m}{p}
870 \(m4 = \matrixPrint{m4}\)\\
871 \midrule
872 \begin{lstlisting}
873 \matrixPow{m5}{m}{2}
874 \(m5 = \matrixPrint{m5}\)
875 \end{lstlisting} &
876 \matrixPow{m5}{m}{2}
877 \(m5 = \matrixPrint{m5}\)\\
878 \midrule
879 \begin{lstlisting}
880 \matrixInvert{m6}{m}
881 \(m6 = \matrixPrint[truncate=2]{m6}\)
882 \end{lstlisting} &
883 \matrixInvert{m6}{m}
884 \(m6 = \matrixPrint[truncate=2]{m6}\)\\
885 \midrule
886
887 \begin{lstlisting}
888 Rank of matrix \(m =\matrixRank{m}\)
889
890 \end{lstlisting} &
891 Rank of matrix \(m =\matrixRank{m}\)\\
892 \midrule
893 \begin{lstlisting}
894 Determinant of matrix \(m =\matrixDet{m}\)
895 \end{lstlisting} &
896 Determinant of matrix \(m =\matrixDet{m}\)\\
897 \midrule
898 \begin{lstlisting}
899 \matrixTranspose{m7}{m}
900 \(m7 = \matrixPrint{m7}\)
901 \end{lstlisting} &
902 \matrixTranspose{m7}{m}
903 \(m7 = \matrixPrint{m7}\)\\
904 \midrule
905 \begin{lstlisting}
906 \matrixSetElement{n}{3}{3}{300}
907 \(n= \matrixPrint{n}\)
908 \end{lstlisting} &
909 \matrixSetElement{n}{3}{3}{300}
910 \(n= \matrixPrint{n}\)\\
911 \midrule
912
913 \begin{lstlisting}
914 \matrixSubmatrix{m8}{m}{1}{2}{2}{3}
915 \(m8 = \matrixPrint{m8}\)
916 \end{lstlisting} &
917 \matrixSubmatrix{m8}{m}{1}{2}{2}{3}
918 \(m8 = \matrixPrint{m8}\)\\
919 \midrule
920 \begin{lstlisting}
921 \matrixConcatH{m9}{m}{q}
922 \(m9= \matrixPrint{m9}\)
923 \end{lstlisting} &
924 \matrixConcatH{m9}{m}{q}
925 \(m9= \matrixPrint{m9}\)\\
926 \midrule
927 \begin{lstlisting}
928 \matrixConcatV{m10}{m}{n}
929 \(m10= \matrixPrint{m10}\)
930 \end{lstlisting} &
931 \matrixConcatV{m10}{m}{n}
932 \(m10= \matrixPrint{m10}\)\\
933 \midrule
934 \begin{lstlisting}
935 \matrixOp{m11}{m*m+2*m}
936 \(\matrixPrint[truncate=4]{m11}\)
937 \end{lstlisting} &
938 \matrixOp{m11}{m*m+2*m}
939 \(\matrixPrint[truncate=4]{m11}\)\\
940 \midrule
941 \begin{lstlisting}
942 \matrixCopy{m12}{m}
943 \(m12 = \matrixPrint{m12}\)
944 \end{lstlisting} &
945 \matrixCopy{m12}{m}
946 \(m12 = \matrixPrint{m12}\)\\
947 \midrule
948 \begin{lstlisting}
949 \matrixEql{m12}{m}
950 \end{lstlisting} &
951 \matrixEql{m12}{m}\\
952 \midrule
953 \begin{lstlisting}
954 trace of matrix \( m = \matrixTrace{m}\)
955 \end{lstlisting} &
956 trace of matrix \( m = \matrixTrace{m}\) \\
957
958 \midrule
959 \begin{lstlisting}
960 \matrixConjugate{mc}{m}
961 \(mc = \matrixPrint{mc}\)
962 \end{lstlisting} &
963 \matrixConjugate{mc}{m}
964 \(mc = \matrixPrint{mc}\)\\
965 \midrule
966
967 \begin{lstlisting}
968 \matrixConjugateT{mct}{m}
969 \(mct = \matrixPrint{mct}\)
970 \end{lstlisting} &
971 \matrixConjugateT{mct}{m}
972 \(mct = \matrixPrint{mct}\)\\
973 \midrule
974
975 \begin{lstlisting}
976 \(\matrixNormOne{m}\)
977 \end{lstlisting} &
978 \(\matrixNormOne{m}\) \\
979
980 \midrule
981 \begin{lstlisting}
982 \(\matrixNormInfty{m}\)
983 \end{lstlisting} &
984 \(\matrixNormInfty{m}\) \\
985
986 \midrule
987 \begin{lstlisting}
988 \(\matrixNormMax{m}\)
989 \end{lstlisting} &
990 \(\matrixNormMax{m}\) \\
991
992 \midrule
993 \begin{lstlisting}
994 \(\matrixNormF{m}\)
995 \end{lstlisting} &
996 \(\matrixNormF{m}\) \\
997
998 \midrule
999 \multicolumn{2}{c}{Elementary row operations on matrices}\\
1000 \midrule
1001 \begin{lstlisting}
1002 \matrixSwapRows{m13}{m}{2}{3}
1003 \(m13 = \matrixPrint{m13}\)
1004 \end{lstlisting} &
1005 \matrixSwapRows{m13}{m}{2}{3}
1006 \(m13 = \matrixPrint{m13}\)\\
1007 \midrule
1008 \begin{lstlisting}
1009 \matrixMulRow{m14}{m}{3}{300}
1010 \(m14 = \matrixPrint{m14}\)
1011 \end{lstlisting} &
1012 \matrixMulRow{m14}{m}{3}{300}
1013 \(m14 = \matrixPrint{m14}\)\\
1014 \midrule
1015 \begin{lstlisting}
1016 \matrixMulAddRow{m15}{m}{2}{10}{3}
1017 \(m15 = \matrixPrint{m15}\)
1018 \end{lstlisting} &
1019 \matrixMulAddRow{m15}{m}{2}{10}{3}
1020 \(m15 = \matrixPrint{m15}\)\\
1021 \midrule
1022 \multicolumn{2}{c}{Elementary column operations on matrices}\\
1023 \midrule
1024 \begin{lstlisting}
1025 \matrixSwapCols{m16}{m}{2}{3}
1026 \(m16 = \matrixPrint{m16}\)
1027 \end{lstlisting} &
1028 \matrixSwapCols{m16}{m}{2}{3}
1029 \(m16 = \matrixPrint{m16}\)\\
1030 \midrule
1031 \begin{lstlisting}
1032 \matrixMulCol{m17}{m}{3}{300}
1033 \(m17 = \matrixPrint{m17}\)
1034 \end{lstlisting} &
1035 \matrixMulCol{m17}{m}{3}{300}
1036 \(m17 = \matrixPrint{m17}\)\\
1037 \midrule
1038
1039 \begin{lstlisting}
1040 \matrixMulAddCol{m18}{m}{2}{10}{3}
1041 \(m18 = \matrixPrint{m18}\)
1042 \end{lstlisting} &
1043 \matrixMulAddCol{m18}{m}{2}{10}{3}
1044 \(m18 = \matrixPrint{m18}\)\\
1045 \midrule
1046
1047 \multicolumn{2}{c}{Reduced row echelon form of a matrix}\\
1048 \midrule
1049 \begin{lstlisting}
1050 \matrixRREF{m19}{p}
1051 \(m19 = \matrixPrint{m19}\)
1052 \end{lstlisting} &
1053 \matrixRREF{m19}{p}
1054 \(m19 = \matrixPrint{m19}\)\\
1055 \midrule
1056 \begin{lstlisting}
1057 \matrixRREF{m20}{m}
1058 \(m20 = \matrixPrint{m20}\)
1059 \end{lstlisting} &
1060 \matrixRREF{m20}{m}
1061 \(m20 = \matrixPrint{m20}\)\\
1062 \bottomrule \\
1063 \caption{Illustration of commands for operations on matrices}
1064 \label{tbl:illluamatrix}
1065 \end{longtable}
1066 \end{center}
1067 The package has command \verb|\matrixRREFSteps| to produce step-by-step computation of reduced row echelon form of a matrix. The command \verb|\matrixRREFSteps{r}| outputs the following.
1068 \renewcommand*{\arraystretch}{1.5}
1069 \begin{framed}
1070 \matrixRREFSteps{r}
1071 \end{framed}
1072 \renewcommand*{\arraystretch}{1.0}
1073 The command \verb|\matrixGaussJordan| is used to obtain Gauss-Jordan elimination of an augmented matrix.
1074 \begin{lstlisting}
1075 \def\a{{{lfrac(1,2),1,1},{2,-1,-1},{1,-1,1}}}
1076 \def\b{{{3},{3},{9}}}
1077 \matrixNew{S}{\a}
1078 \matrixNew{T}{\b}
1079 \matrixConcatH{W}{S}{T}
1080 $$W = \matrixPrint{W}$$
1081 \matrixGaussJordan{U}{S}{T}
1082 $$U = \matrixPrint{U}$$
1083 \end{lstlisting}
1084 The above code produces the following output.
1085 \def\a{{{lfrac(1,2),1,1},{2,-1,-1},{1,-1,1}}}
1086 \def\b{{{3},{3},{9}}}
1087 \matrixNew{S}{\a}
1088 \matrixNew{T}{\b}
1089 \matrixConcatH{W}{S}{T}
1090 \renewcommand{\arraystretch}{1.5}
1091 \begin{framed}
1092 $$W = \matrixPrint{W}$$
1093 \matrixGaussJordan{U}{S}{T}
1094 $$U = \matrixPrint{U}$$
1095 \end{framed}
1096 The package also has a command \verb|\matrixGaussJordanSteps| to produce step-by-step computation of Gauss-Jordan elimination of an augmented matrix. The command \verb|\matrixGaussJordanSteps{S}{T}| produces the following output.
1097 \begin{framed}
1098 \matrixGaussJordanSteps{S}{T}
1099 \end{framed}
1100 \renewcommand{\arraystretch}{1.0}
1101 \section{Customized usage}\label{customuse}
1102 The commands available in the package can be used for performing further operations on vectors and matrices. The command \verb|\vectorAdd| can be extended to add more than two vectors. The latex document (listing \ref{code:custluavec}) provides some instances of such usage.
1103 \begin{lstlisting}[label={code:custluavec}, caption={Customized usage of the lualinalg package}]
1104 \documentclass{article}
1105 \usepackage{lualinalg}
1106 \begin{document}
1107 \begin{luacode*}
1108 function sumcoordinates(v1)
1109 local sum = 0
1110 for i = 1,#v1 do
1111 sum = sum + v1[i]
1112 end
1113 return sum
1114 end
1115
1116 function vector.addmulti(...)
1117 p=table.pack(...)
1118 s=vector(p[1])
1119 for i=2,#p do
1120 s=vector.add(s,vector(p[i]))
1121 end
1122 return s
1123 end
1124 \end{luacode*}
1125 \vectorNew{v}{{1,2,lcomplex(3,3)}}
1126 The sum of coordinates of vector
1127 \(v = \directlua{tex.sprint(tostring( sumcoordinates(vectors['v'])))}\).
1128
1129 \newcommand\vectorAddmulti[2]{%
1130 \directlua{%
1131 vectors['#1'] = vector.addmulti(#2)
1132 }%
1133 }
1134 \vectorNew{w}{{3,6,lcomplex(6,6)}}
1135 \vectorNew{x}{{9,12,lcomplex(12,12)}}
1136 \vectorAddmulti{y}{vectors['v'],vectors['w'],vectors['x']}
1137 The sum of vectors \(v,w \text{ and } x =\left( \vectorPrint{y} \right)\).
1138 \end{document}
1139 \end{lstlisting}
1140 The latex document (listing \ref{code:custluavec}) outputs the following on compilation.
1141 \begin{framed}
1142 The sum of coordinates of vector \(v = 6 + 3\mathrm{i}\).
1143
1144 The sum of vectors \(v,w \text{ and } x = \left(13, 20, 21 + 21\mathrm{i} \right) \).
1145 \end{framed}
1146 The command \verb|\matrixAdd| can be extended to add more than two matrices. The latex document (listing \ref{code:custluamtx}) provides some instances of such usage.
1147
1148 \begin{lstlisting}[label={code:custluamtx}, caption={Customized usage of the lualinalg package}]
1149 \documentclass{article}
1150 \usepackage{lualinalg}
1151 \begin{document}
1152 \begin{luacode}
1153 function squareDiagEntries(m1)
1154 if #m1 ~= #m1[1] then error( "matrix not square") end
1155 local sum = 0
1156 for i = 1,#m1 do
1157 for j = 1,#m1[1] do
1158 if i == j then sum = sum + (m1[i][j])^2 end
1159 end
1160 end
1161 return complex.round(sum)
1162 end
1163
1164 function matrix.addmulti(...)
1165 p=table.pack(...)
1166 s=matrix(p[1])
1167 for i=2,#p do
1168 s=matrix.add(s,matrix(p[i]))
1169 end
1170 return s
1171 end
1172 \end{luacode}
1173
1174 \def\r{{{1,2,3},{4,5,6},{7,8,lcomplex(9,3)}}}
1175 \matrixNew{m}{\r}
1176 The sum of squares of diagonal entries of matrix
1177 \(m = \directlua{tex.sprint(tostring(squareDiagEntries(matrices['m'])))}\).
1178
1179 \def\s{{{1,2,3},{4,5,lcomplex(6,6)}}}
1180 \def\t{{{10,20,30},{40,50,lcomplex(60,60)}}}
1181 \def\u{{{100,200,300},{400,500,lcomplex(600,600)}}}
1182 \matrixNew{m1}{\s}
1183 \matrixNew{m2}{\t}
1184 \matrixNew{m3}{\u}
1185 \newcommand\matrixAddmulti[2]{%
1186 \directlua{%
1187 matrices['#1'] = matrix.addmulti(#2)
1188 }%
1189 }
1190 \matrixAddmulti{m4}{matrices['m1'],matrices['m2'],matrices['m3']}
1191 The sum of matrices \(m1,m2 \text{ and } m3 = \matrixPrint{m4}\).
1192 \end{document}
1193 \end{lstlisting}
1194
1195 The latex document (listing \ref{code:custluamtx}) outputs the following on compilation.
1196 \begin{framed}
1197 The sum of squares of diagonal entries of matrix \(m = 98 + 54\mathrm{i}\).
1198
1199 The sum of matrices \(m1,m2 \text{ and } m3 =\begin{bmatrix} 111 & 222 & 333 \\ 444 & 555 & 666+666\mathrm{i} \end{bmatrix} \).
1200 \end{framed}
1201
1202
1203 \end{document}