I know the calc
package can perform infix-notation arithmetic in LaTeX... but I want more!
I'd like to perform (not necessarily infix-notation) linear-algebra operations such as scalar multiplication, matrix addition and product in LaTeX, and then print the result in an array
or in some matrix environment from amsmath
.
Why would I want to do that in LaTeX directly? Why do I not simply use some linear-algebra software, such as Matlab, Mathematica, etc.?
Well, suppose I want to walk my readers through a detailed linear-algebra calculation with many numerical examples. Of course, I could perform all the steps manually first and then hardcode the result of each step in my input file. However, this approach
- is prone to errors (LaTeX'
array
s are not very user-friendly to typeset), - lacks maintainability (I may decide to change the data in my example, which means I have to modify everything that follows).
Hence my question: Is there a way of easily performing linear-algebra operations in LaTeX?
Ideally, I would like to
mimick Matlab's syntax for defining matrices (using commas as column separators and semicolons as end-of-row characters), performing operations on them, extracting sub matrices, etc.. The syntax could be something like the following:
\let\A{1,2,3;4,5,6} \let\b{1;0;0} \let\c\matrixprod{\A,\b} \let\d\submatrix{\c}{(2,1)}
have a command that typesets a "matrix object" in an
array
ormatrix
environment, e.g.\typesetmatrix[bmatrix]{A}
be able to perform operations on matrices of arbitrary--albeit relatively small--dimensions (edit: not just 2x2 and 3x3 as in the
calculator
package).
Is that currently possible with some package? If not, I'm considering rolling up my sleeves and implementing something, but this would probably prove quite difficult, and I would like to avoid reinventing the wheel :)
Edit about other operations that would be useful:
- diag (extraction of the diagonal of a square matrix)
- trace
- determinant
- norm(s)
- condition number(s)
- inverse
Even more advanced matrix operations that would be awesome, but probably tough to implement:
- eigenvalues & eigenvectors,
- QR, least squares etc.
- SVD,
- other common matrix factorisations.
l3fp
. Currently, this package (l3fp
) evaluates floating point expressions (expandably). If I can find a good way of defining data types forl3fp
, and if I can convince the other members of the LaTeX3 team, it will be possible to write a package that adds matrix operations, letting us make use of the already existing parser: for instance,\fpa_set:Nn \A { matrix(1,2;4,5) } \fpa_set:Nn \B { 1 + \A + \A * \A / 2 - exp(\A) }
.l3pf
accessible to mere mortals somewhere (GitHub perhaps)?