项目作者: uestla

项目描述 :
C++ implementation of sparse matrix using CRS (Compressed Row Storage) format
高级语言: C++
项目地址: git://github.com/uestla/Sparse-Matrix.git
创建时间: 2014-03-23T23:51:49Z
项目社区:https://github.com/uestla/Sparse-Matrix

开源协议:

下载


Sparse Matrix

C++ implementation of sparse matrix using CRS format.

Buy me a Coffee

Usage

Creation

SparseMatrix comes as a template class, so we have to specify the element type.

  1. SparseMatrix::SparseMatrix<int> matrix(3); // 3×3 matrix of integers
  2. SparseMatrix::SparseMatrix<int> matrix2(4, 5); // 4×5 matrix - 4 rows, 5 columns
  3. SparseMatrix::SparseMatrix<int> matrix3(matrix); // copy constructor
  4. SparseMatrix::SparseMatrix<int> matrix4 = matrix2; // deep copy assignment

All values are now equal to <type>(), which for type int is 0.

Values

To set or get value, use methods set() and get():

  1. int val;
  2. matrix.set(-5, 2, 3); // sets -5 on 2nd row and 3rd column
  3. val = matrix.get(2, 3); // val = -5
  4. val = matrix.get(1, 1); // val = 0

When accessing invalid coordinates, InvalidCoordinatesException is thrown. Please note that rows and columns are indexed from 1.

Operations

SparseMatrix is implemented as an immutable object - all operations create new matrix instead of changing the matrix the operation is called on.

Matrix-Vector multiplication

Number of columns in the matrix has to be the same as the size of the vector, otherwise InvalidDimensionsException is thrown.

  1. SparseMatrix::SparseMatrix<int> mat(4, 5);
  2. std::vector<int> vec(5, 2);
  3. std::vector<int> result;
  4. result = mat.multiply(vec); // method
  5. result = mat * vec; // operator

Matrix-Matrix multiplication

Number of columns in the left matrix must be same as number of rows in the right matrix, otherwise InvalidDimensionsException is thrown.

  1. SparseMatrix::SparseMatrix<int> matrixA(2, 3);
  2. SparseMatrix::SparseMatrix<int> matrixB(3, 4);
  3. SparseMatrix::SparseMatrix<int> product; // will be of size 2×4
  4. product = matrixA.multiply(matrixB); // method
  5. product = matrixA * matrixB; // operator

Matrix-Matrix addition / subtraction

You can also add and subtract matrices together. Both matrices has to have same dimentions, otherwise InvalidDimensionsException is thrown.

  1. SparseMatrix::SparseMatrix<int> matrixA(4, 7);
  2. SparseMatrix::SparseMatrix<int> matrixB(4, 7);
  3. SparseMatrix::SparseMatrix<int> sum;
  4. sum = matrixA.add(matrixB); // method
  5. sum = matrixA + matrixB; // operator
  6. SparseMatrix::SparseMatrix<int> diff;
  7. diff = matrixA.subtract(matrixB); // method
  8. diff = matrixA - matrixB; // operator

Matrix-Matrix comparison

  1. SparseMatrix::SparseMatrix<int> matrixA(3);
  2. SparseMatrix::SparseMatrix<int> matrixB(3);
  3. bool areSame = matrixA == matrixB; // true

Dimensions

To access matrix dimensions use simple getters:

  1. int rows = matrix.getRowCount();
  2. int cols = matrix.getColumnCount();

Printing matrix

Basic output streaming is implemented. Note that output operator must be implemented for the element type as well.

  1. SparseMatrix::SparseMatrix<int> matrix(3, 4);
  2. matrix.set(2, 1, 1);
  3. matrix.set(7, 1, 3);
  4. matrix.set(4, 2, 2);
  5. matrix.set(1, 3, 4);
  6. std::cout << matrix << std::endl;
  7. /*
  8. 2 0 7 0
  9. 0 4 0 0
  10. 0 0 0 1
  11. */

Custom element type

If integers/floats are not enough, you can always use your own element type.

Make sure your type implements:

  • empty constructor (which represents the zero value)
  • copying constructor
  • comparison operator == so that it is possible to tell whether a value is zero-ish
  • adding operator +
  • subtracting operator -
  • product operator *
  • output operator << to be able to nicely output the matrix

You can see a simple custom element example in the tests.


Any questions / issues / pull requests / donations are very welcome! :-)