up previous next
DiagMat

matrix with given diagonal

Syntax
DiagMat(L:LIST):MAT
DiagMat(R:RING, L:LIST):MAT

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

Example
/**/  DiagMat([3,4,5]);
matrix(
[
  [3, 0, 0],
  [0, 4, 0],
  [0, 0, 5]
])

/**/ DiagMat(QQ,[5,6,7]);
matrix(
[
  [5, 0, 0],
  [0, 6, 0],
  [0, 0, 7]
])

-- 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 DiagMat([ M[I, I]^Exp | I In 1..NumRows(M) ]);
  EndDefine;

/**/ PowerDiag(IdentityMat(QQ,3), 200000000);
matrix( /*QQ*/
 [[1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]]
 )

See Also