Saturday, February 2, 2008

Printing a matrix in Maxima, again

My adventure continues:
printMatrix(a) := block([as, body],
as : matrixmap(lambda([x],
printf(false, "~a", x)), a),
body: simplode(maplist(lambda([row],
simplode(row, " & ")),
as),
sconc(" ", "\\\\", newline)),
sconc("\\left(\\begin{array}{",
simplode(makelist("r", i, 1,
length(a[1]))), "}",
newline,
body,
newline,
"\\end{array}\\right)"))$
There's just got to be a function in Maxima somewhere that already does this. (There's also just got to be a function that creates a list based on repeating an element. makelist(x, i, 1, n) does the job though.)

(%i42) printMatrix(diag_matrix(1,2,3));
(%o42) \left(\begin{array}{rrr}
1 & 0 & 0 \\
0 & 2 & 0 \\
0 & 0 & 3
\end{array}\right)


Also in the spirit of duplicating functionality that's just gotta be out there: Escape HTML characters. I use this pretty often. Maybe I should make it spiffier.

No comments: