## Diagonal Matrices | Extreme Optimization Numerical Libraries for .NET Professional |

A diagonal matrix is a matrix whose only nonzero elements lie on a diagonal.
Diagonal matrices are implemented by the
DiagonalMatrix

Diagonal matrices are created using the CreateDiagonal method of the Matrix class. There are four overloads.

The first overload takes the size of the matrix as its only argument. It constructs a square matrix with the specified number of rows and columns. All elements are initially set to zero. The element type must be specified as the generic type argument. For example, for a 5x5 diagonal matrix, we have:

You can also specify both the number of rows and columns. The following creates a 5x7 matrix:

The two remaining overloads let you initialize the elements of the matrix.
The first one takes a Vector

var elements = Vector.Create(1.0, 2.0, 3.0); var t3 = Matrix.CreateDiagonal(elements); // t3 -> [[ 1 0 0 ] // [ 0 2 0 ] // [ 0 0 3 ]]

It is also possible to specify the size of the matrix. In this case, the overload has three arguments. The first two are the number of rows and columns of the matrix. The third argument is the vector on the diagonal.

var t4 = Matrix.CreateDiagonal(3, 5, elements); // t4 -> [[ 1 0 0 0 0 ] // [ 0 2 0 0 0 ] // [ 0 0 3 0 0 ]]

The length of the vector must be the same as the smallest dimension of the matrix, or a DimensionMismatchException is thrown.

The GetRow
and GetColumn
methods of a DiagonalMatrix

Copyright Â© 2004-2021,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.