## Matrix Basics | Extreme Optimization Numerical Libraries for .NET Professional |

Matrices come in many shapes and sizes. When the matrix exhibits a definite structure, calculations can often be speeded up by several orders of magnitude. Storage requirements may also be significantly reduced. It is therefore useful to define different matrix types to take advantage of these improvements.

The **Extreme Optimization Numerical Libraries for .NET**
include classes for dense matrices, upper- and lower-triangular matrices,
symmetric or Hermitian matrices, and sparse matrices.

The Matrix

Class | Description |
---|---|

A standard dense matrix. All elements can take on any value. This is the most common type. | |

A matrix whose elements above or below the main diagonal are all zero. | |

A matrix whose elements are symmetrical about the main diagonal. A symmetric matrix is equal to its transpose. | |

A matrix whose elements are mostly zero. Only the nonzero elements are stored. | |

A matrix that represents a range or slice of another matrix. |

The Matrix

Some matrix types put restrictions on the elements that can be changed. For example, you can only modify the
non-zero elements of a
TriangularMatrix

A DenseMatrix

There are three ways to make a copy of a matrix.

The ShallowCopy method creates a derived matrix that is a memberwise clone of the original. The new matrix has the same type and dimensions as the original matrix. When you change the value of an element of this matrix, the value of the corresponding element in the original matrix changes as well, and vice versa.

The Clone method, which implements the ICloneable interface, creates a stand-alone matrix that is a complete copy of the original. This method creates a matrix of the same type as the original, and makes its own copy of the element storage. When you change the value of an element of this matrix, the value of the corresponding element in the original matrix does not change. Likewise, if you change the value of an element of the original matrix, the value of the corresponding element in the copied matrix does not change.

You can convert a shallow copy to a full copy by calling the CloneData method. This method ensures that an instance has its own copy of the data. After a call to this method, any changes to the elements of this matrix will only affect this instance.

// Create a vector, a shallow copy, and a clone: Matrix m = Matrix.Create(2, 2); m[1,1] = 2; Matrix mShallowCopy = m.ShallowCopy(); Matrix mClone = m.Clone(); // This prints '2': Console.WriteLine("m[1,1] = {0}", m[1,1]); // Now change the value of this component. m[1,1] = -1; // Component changed. This prints '-1': Console.WriteLine("m[1,1] = {0}", m[1,1]); // Shallow copy changed, also. This prints '-1': Console.WriteLine("mShallowCopy[1,1] = {0}", mShallowCopy[1,1]); // Clone is left unchanged. This prints '2': Console.WriteLine("mClone[1,1] = {0}", mClone[1,1]);

The third way to copy a vector is by calling the vector's ToDenseMatrix method. This method returns a
stand-alone Matrix

You can copy the elements of a matrix to another, previously created vector using the CopyTo method. Care should be taken that you don't try to modify elements that are read-only. Any such attempt will result in a ComponentReadOnlyException.

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.