%mathpiper,def="IsDiagonal"

IsDiagonal(A_IsMatrix) <--
[
	Local(i,j,m,n,result);
	m      := Length(A);
	n      := Length(A[1]);
	result := (m=n);    //    must be a square matrix
	
	i:=2;
    While(i<=m And result)
     [
	     j:=1;
	     While(j<=n And result)
	     [
		     result:= (i=j Or A[i][j] = 0);
		     j++;
	     ];
	     i++;
	 ];
	If(m=2, [ result := result And (A=Transpose(A)); ]	);
	result;
];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output



%mathpiper_docs,name="IsDiagonal",categories="User Functions;Matrices (Predicates);Predicates"
*CMD IsDiagonal --- test for a diagonal matrix
*STD
*CALL
	IsDiagonal(A)

*PARMS

{A} -- a matrix

*DESC

{IsDiagonal(A)} returns {True} if {A} is a diagonal square matrix and {False} otherwise.

*E.G.
In> IsDiagonal(Identity(5))
Result: True;
In> IsDiagonal(HilbertMatrix(5))
Result: False;
%/mathpiper_docs