covfefe
covfefe

Reputation: 2675

Difference between Matrix, SparseMatrix, and DenseMatrix in MathNet Numerics?

I understand that some matrices have a lot of data, while others have mainly 0's or are empty. But what is the advantage of creating a SparseMatrix object to hold a sparsely populated matrix over creating a DenseMatrix object to hold a sparsely populated matrix? They both seem to offer more or less the same operations as far as methods go.

I'm also wondering when you would use a Matrix object to hold data -- as in are there any advantages or situations where this would be preferred over using the other two.

Upvotes: 0

Views: 4143

Answers (2)

Christoph Rüegg
Christoph Rüegg

Reputation: 4736

For small matrices (e.g. less than 1000x1000) dense matrices work well. But in practice there are a lot of problems where much larger matrices are needed, but where almost all values are zero (often with non-zero values close to the diagonal). With sparse matrices it is possible to handle very large matrices in cases where the dense structure is unfeasible (because it needs too much memory or is way to expensive to compute with CPU-time wise).

Note that as of today the Math.NET Numerics direct matrix decomposition methods are optimized for dense matrices only; use iterative solvers for sparse data instead.

Regarding types, in Math.NET Numerics v3 the hierarchy for double-valued matrices is as follows:

Matrix<double>
  |- Double.Matrix
       |- Double.DenseMatrix
       |- Double.SparseMatrix
       |- Double.DiagonalMatrix

With Matrix<T> I refer to the full type MathNet.Numerics.LinearAlgebra.Matrix<T>, with Double.Matrix to MathNet.Numerics.LinearAlgebra.Double.Matrix, etc.

  • Matrix<double>: always declare all variables, properties and arguments using this generic type only. Indeed, in most cases this is the only type needed in user code.
  • Double.Matrix: do not use
  • Double.DenseMatrix: use for creating a dense matrix only - if you do not wish to use the builder (Matrix<double>.Build.Dense...)
  • Double.SparseMatrix: use for creating a sparse matrix only - if you do not wish to use the builder
  • Double.DiagonalMatrix: use for creating a diagonal matrix only - if you do not wish to use the builder

Upvotes: 5

etr
etr

Reputation: 1247

They each are optimized for that specific use. For example sparse matrix uses CSR format.

Compressed sparse row (CSR or CRS)

CSR is effectively identical to the Yale Sparse Matrix format, except that the column array is normally stored ahead of the row index array. I.e. CSR is (val, col_ind, row_ptr), where val is an array of the (left-to-right, then top-to-bottom) non-zero values of the matrix; col_ind is the column indices corresponding to the values; and, row_ptr is the list of value indexes where each row starts. The name is based on the fact that row index information is compressed relative to the COO format. One typically uses another format (LIL, DOK, COO) for construction. This format is efficient for arithmetic operations, row slicing, and matrix-vector products. See scipy.sparse.csr_matrix.

See wiki for more info.

Upvotes: 1

Related Questions