%mathpiper,def="Diagonal"

//
// Diagonal: return a vector with the diagonal elements of the matrix
//
Function("Diagonal",{A})
[
	Local(result,i,n);
	n:=Length(A);
	result:=ZeroVector(n);
	For(i:=1,i<=n,i++)
	[
		result[i] := A[i][i];
	];
	result;
];

%/mathpiper



%mathpiper_docs,name="Diagonal",categories="User Functions;Linear Algebra"
*CMD Diagonal --- extract the diagonal from a matrix
*STD
*CALL
	Diagonal(A)

*PARMS

{A} -- matrix

*DESC

This command returns a vector of the diagonal components
of the matrix {A}.


*E.G.

In> Diagonal(5*Identity(4))
Result: {5,5,5,5};
In> Diagonal(HilbertMatrix(3))
Result: {1,1/3,1/5};

*SEE DiagonalMatrix, IsDiagonal
%/mathpiper_docs