{"id":20766705,"url":"https://github.com/uestla/sparse-matrix","last_synced_at":"2025-08-13T21:38:17.397Z","repository":{"id":15316697,"uuid":"18046722","full_name":"uestla/Sparse-Matrix","owner":"uestla","description":"C++ implementation of sparse matrix using CRS (Compressed Row Storage) format","archived":false,"fork":false,"pushed_at":"2020-08-08T18:21:32.000Z","size":98,"stargazers_count":92,"open_issues_count":8,"forks_count":47,"subscribers_count":7,"default_branch":"master","last_synced_at":"2023-03-12T17:15:39.065Z","etag":null,"topics":["cpp","crs","matrix","sparse"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uestla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-23T23:51:49.000Z","updated_at":"2023-03-08T09:11:02.000Z","dependencies_parsed_at":"2022-08-25T20:00:22.023Z","dependency_job_id":null,"html_url":"https://github.com/uestla/Sparse-Matrix","commit_stats":null,"previous_names":[],"tags_count":9,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uestla%2FSparse-Matrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uestla%2FSparse-Matrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uestla%2FSparse-Matrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uestla%2FSparse-Matrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uestla","download_url":"https://codeload.github.com/uestla/Sparse-Matrix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225030493,"owners_count":17409896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","crs","matrix","sparse"],"created_at":"2024-11-17T11:25:40.474Z","updated_at":"2024-11-17T11:25:41.009Z","avatar_url":"https://github.com/uestla.png","language":"C++","readme":"# Sparse Matrix\n\nC++ implementation of sparse matrix using [CRS format](http://netlib.org/linalg/html_templates/node91.html#SECTION00931100000000000000).\n\n[![Buy me a Coffee](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=KWQJ7VTXZMZLS)\n\n\n## Usage\n\n### Creation\n\nSparseMatrix comes as a template class, so we have to specify the element type.\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e matrix(3); // 3×3 matrix of integers\nSparseMatrix::SparseMatrix\u003cint\u003e matrix2(4, 5); // 4×5 matrix - 4 rows, 5 columns\n\nSparseMatrix::SparseMatrix\u003cint\u003e matrix3(matrix); // copy constructor\nSparseMatrix::SparseMatrix\u003cint\u003e matrix4 = matrix2; // deep copy assignment\n```\n\nAll values are now equal to `\u003ctype\u003e()`, which for type `int` is `0`.\n\n### Values\n\nTo set or get value, use methods `set()` and `get()`:\n\n```cpp\nint val;\nmatrix.set(-5, 2, 3); // sets -5 on 2nd row and 3rd column\nval = matrix.get(2, 3); // val = -5\nval = matrix.get(1, 1); // val = 0\n```\n\nWhen accessing invalid coordinates, `InvalidCoordinatesException` is thrown. Please note that **rows and columns are indexed from 1**.\n\n### Operations\n\nSparseMatrix is implemented as an immutable object - all operations create new matrix instead of changing the matrix the operation is called on.\n\n#### Matrix-Vector multiplication\n\nNumber of columns in the matrix has to be the same as the size of the vector, otherwise `InvalidDimensionsException` is thrown.\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e mat(4, 5);\nstd::vector\u003cint\u003e vec(5, 2);\n\nstd::vector\u003cint\u003e result;\nresult = mat.multiply(vec); // method\nresult = mat * vec; // operator\n```\n\n#### Matrix-Matrix multiplication\n\nNumber of columns in the left matrix must be same as number of rows in the right matrix, otherwise `InvalidDimensionsException` is thrown.\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e matrixA(2, 3);\nSparseMatrix::SparseMatrix\u003cint\u003e matrixB(3, 4);\n\nSparseMatrix::SparseMatrix\u003cint\u003e product; // will be of size 2×4\nproduct = matrixA.multiply(matrixB); // method\nproduct = matrixA * matrixB; // operator\n```\n\n#### Matrix-Matrix addition / subtraction\n\nYou can also add and subtract matrices together. Both matrices has to have same dimentions, otherwise `InvalidDimensionsException` is thrown.\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e matrixA(4, 7);\nSparseMatrix::SparseMatrix\u003cint\u003e matrixB(4, 7);\n\nSparseMatrix::SparseMatrix\u003cint\u003e sum;\nsum = matrixA.add(matrixB); // method\nsum = matrixA + matrixB; // operator\n\nSparseMatrix::SparseMatrix\u003cint\u003e diff;\ndiff = matrixA.subtract(matrixB); // method\ndiff = matrixA - matrixB; // operator\n```\n\n#### Matrix-Matrix comparison\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e matrixA(3);\nSparseMatrix::SparseMatrix\u003cint\u003e matrixB(3);\n\nbool areSame = matrixA == matrixB; // true\n```\n\n#### Dimensions\n\nTo access matrix dimensions use simple getters:\n\n```cpp\nint rows = matrix.getRowCount();\nint cols = matrix.getColumnCount();\n```\n\n#### Printing matrix\n\nBasic output streaming is implemented. Note that output operator must be implemented for the element type as well.\n\n```cpp\nSparseMatrix::SparseMatrix\u003cint\u003e matrix(3, 4);\nmatrix.set(2, 1, 1);\nmatrix.set(7, 1, 3);\nmatrix.set(4, 2, 2);\nmatrix.set(1, 3, 4);\n\nstd::cout \u003c\u003c matrix \u003c\u003c std::endl;\n\n/*\n2 0 7 0\n0 4 0 0\n0 0 0 1\n*/\n```\n\n### Custom element type\n\nIf integers/floats are not enough, you can always use your own element type.\n\nMake sure your type implements:\n\n* empty constructor (which represents the zero value)\n* copying constructor\n* comparison `operator ==` so that it is possible to tell whether a value is zero-ish\n* adding `operator +`\n* subtracting `operator -`\n* product `operator *`\n* output `operator \u003c\u003c` to be able to nicely output the matrix\n\nYou can see a simple custom element example in [the tests](tests/cases/custom-type.cpp).\n\n\n-----------\n\nAny questions / issues / pull requests / [donations](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=KWQJ7VTXZMZLS) are very welcome! :-)\n","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=KWQJ7VTXZMZLS"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuestla%2Fsparse-matrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuestla%2Fsparse-matrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuestla%2Fsparse-matrix/lists"}