up previous next
DiagonalMat

matrix with given diagonal

Syntax
DiagonalMat(L:LIST):MAT

Description
This function return the diagonal matrix whose diagonal are the elements of the list L.

Example
DiagonalMat([3,4,5]);
Mat([
  [3, 0, 0],
  [0, 4, 0],
  [0, 0, 5]
])
------------------------------- 

-- fast implementation for high powers of a diagonal matrix
Define PowerDiag(M, Exp)
  If Not IsDiagonal(M) Then Error("PowerDiag: matrix must be diagonal"); EndIf;
  Return DiagonalMat([ M[I,I]^Exp | I In 1..Len(M)]);
EndDefine;

PowerDiag(Identity(3), 200000000);
Mat([
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]
])
-------------------------------


See Also