Show / Hide Table of Contents

Class SparseMatrix<T>

A square matrix that uses a sparse storage method with doubly-linked elements.

Inheritance
object
SparseMatrix<T>
Implements
ISparseMatrix<T>
IMatrix<T>
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
Namespace: SpiceSharp.Algebra
Assembly: SpiceSharp.dll
Syntax
public class SparseMatrix<T> : ISparseMatrix<T>, IMatrix<T>
Type Parameters
Name Description
T

The base value type.

Remarks

The elements in row and column with index 0 are considered trashcan elements. They should all map on the same element.

The matrix automatically expands size if necessary.

Constructors

SparseMatrix()

Initializes a new instance of the SparseMatrix<T> class.

Declaration
public SparseMatrix()

SparseMatrix(int)

Initializes a new instance of the SparseMatrix<T> class.

Declaration
public SparseMatrix(int size)
Parameters
Type Name Description
int size

The matrix size.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if size is negative.

Properties

ElementCount

Gets the number of elements in the matrix.

Declaration
public int ElementCount { get; }
Property Value
Type Description
int

The element count.

this[MatrixLocation]

Gets or sets the value at the specified location.

Declaration
public T this[MatrixLocation location] { get; set; }
Parameters
Type Name Description
MatrixLocation location

The location.

Property Value
Type Description
T

The value.

this[int, int]

Gets or sets the value at the specified row and column.

Declaration
public T this[int row, int column] { get; set; }
Parameters
Type Name Description
int row

The row index.

int column

The column index.

Property Value
Type Description
T

The value.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if row or column is not positive.

Size

Gets the size of the matrix.

Declaration
public int Size { get; }
Property Value
Type Description
int

The matrix size.

Methods

Clear()

Clears the matrix of any elements. The size of the matrix becomes 0.

Declaration
public void Clear()

FindDiagonalElement(int)

Finds the ISparseMatrixElement<T> on the diagonal.

Declaration
public ISparseMatrixElement<T> FindDiagonalElement(int index)
Parameters
Type Name Description
int index

The index.

Returns
Type Description
ISparseMatrixElement<T>

The matrix element if it exists; otherwise null.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if index is negative.

FindElement(MatrixLocation)

Finds a pointer to the matrix element at the specified row and column. If the element doesn't exist, null is returned.

Declaration
public Element<T> FindElement(MatrixLocation location)
Parameters
Type Name Description
MatrixLocation location

The matrix location.

Returns
Type Description
Element<T>

The matrix element if it exists; otherwise null.

GetElement(MatrixLocation)

Gets a pointer to the matrix element at the specified row and column. If the element doesn't exist, it is created.

Declaration
public Element<T> GetElement(MatrixLocation location)
Parameters
Type Name Description
MatrixLocation location

The matrix location.

Returns
Type Description
Element<T>

The matrix element.

GetFirstInColumn(int)

Gets the first non-default ISparseMatrixElement<T> in the specified column.

Declaration
public ISparseMatrixElement<T> GetFirstInColumn(int column)
Parameters
Type Name Description
int column

The column index.

Returns
Type Description
ISparseMatrixElement<T>

The matrix element.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if column is negative.

GetFirstInRow(int)

Gets the first non-default ISparseMatrixElement<T> in the specified row.

Declaration
public ISparseMatrixElement<T> GetFirstInRow(int row)
Parameters
Type Name Description
int row

The row index.

Returns
Type Description
ISparseMatrixElement<T>

The matrix element.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if row is negative.

GetLastInColumn(int)

Gets the last non-default ISparseMatrixElement<T> in the specified column.

Declaration
public ISparseMatrixElement<T> GetLastInColumn(int column)
Parameters
Type Name Description
int column

The column index.

Returns
Type Description
ISparseMatrixElement<T>

The matrix element.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if column is negative.

GetLastInRow(int)

Gets the last non-default ISparseMatrixElement<T> in the specified row.

Declaration
public ISparseMatrixElement<T> GetLastInRow(int row)
Parameters
Type Name Description
int row

The row index.

Returns
Type Description
ISparseMatrixElement<T>

The matrix element.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if row is negative.

RemoveElement(MatrixLocation)

Removes a matrix element at the specified row and column. If the element doesn't exist, this method returns false.

Declaration
public bool RemoveElement(MatrixLocation location)
Parameters
Type Name Description
MatrixLocation location

The location.

Returns
Type Description
bool

true if the element was removed; otherwise, false.

Reset()

Resets all elements in the matrix to their default value.

Declaration
public void Reset()

SwapColumns(int, int)

Swaps two columns in the matrix.

Declaration
public void SwapColumns(int column1, int column2)
Parameters
Type Name Description
int column1

The first column index.

int column2

The second column index.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if column1 or column2 is not greater than 0.

SwapRows(int, int)

Swaps two rows in the matrix.

Declaration
public void SwapRows(int row1, int row2)
Parameters
Type Name Description
int row1

The first row index.

int row2

The second row index.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if row1 or row2 is not greater than 0.

ToString()

Returns a string that represents this instance.

Declaration
public override string ToString()
Returns
Type Description
string

A string that represents this instance.

Overrides
object.ToString()

Implements

ISparseMatrix<T>
IMatrix<T>

Extension Methods

Utility.ThrowIfNull<T>(T, string)
In this article
Back to top Generated by DocFX