Sunday, January 27, 2008

Create a diagonal matrix in Maxima

There's got to be a better way to do this! But how?
diag(d) := block([n], n : length(d), 
genmatrix(lambda([i, j],
if i = j then d[i] else 0),
n, n))$


---

January 28: Yes, there is a better way! In reply to a question on the Maxima list, Barton Willis pointed out a couple functions: diag in package diag and mat_diag which is included in the environment loaded by default:
(%i90) diag_matrix(1,2,3);
[ 1 0 0 ]
[ ]
(%o90) [ 0 2 0 ]
[ ]
[ 0 0 3 ]

1 comment:

RegnumPlayer said...

But, how to make diagonal matrix from sums of columns A.i, which is a vector, A is a nxn matrix and i is a column vector of ones, performing summation. Making diagonal nxn matrix from this vector should be trivial, however I can't find simple function that is working, maybe function naming is not intuitive and its not called diagonal ? I am leaving Maxima, since I could get not help on IRC about this.