COOMatrix#
-
class numpy::COOMatrix#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use COOMatrix
COOMatrix obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_SPARSE_COO.H:67 |
|
|
NP_SPARSE_COO.H:73 |
Operators#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
T |
NP_SPARSE_COO.H:232 |
Construction#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
COOMatrix |
NP_SPARSE_COO.H:96 |
Array Creation#
Shape Manipulation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
COOMatrix<T> |
NP_SPARSE_COO.H:376 |
Statistics#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_SPARSE_COO.H:319 |
Sorting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_SPARSE_COO.H:284 |
Math Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_SPARSE_COO.H:187 |
|
|
void |
NP_SPARSE_COO.H:173 |
I/O#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
CSRMatrix<T> |
NP_SPARSE_COO.H:222 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
NP_SPARSE_COO.H:262 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
NP_SPARSE_COO.H:401 |
|
|
COOMatrix<T> |
NP_SPARSE_COO.H:387 |
|
|
const std::vector<sparse_index_t>& |
NP_SPARSE_COO.H:270 |
|
|
std::vector<sparse_index_t>& |
NP_SPARSE_COO.H:274 |
|
|
size_t |
NP_SPARSE_COO.H:254 |
|
|
double |
NP_SPARSE_COO.H:257 |
|
|
size_t |
NP_SPARSE_COO.H:255 |
|
|
void |
NP_SPARSE_COO.H:432 |
|
|
void |
NP_SPARSE_COO.H:422 |
|
|
void |
NP_SPARSE_COO.H:355 |
|
|
const std::vector<sparse_index_t>& |
NP_SPARSE_COO.H:269 |
|
|
std::vector<sparse_index_t>& |
NP_SPARSE_COO.H:273 |
|
|
size_t |
NP_SPARSE_COO.H:253 |
|
|
const std::vector<T>& |
NP_SPARSE_COO.H:268 |
|
|
std::vector<T>& |
NP_SPARSE_COO.H:272 |
Code Examples#
The following examples are extracted from the test suite.
from_dense (np_test_3_all.cpp:19939)
19929 std::cout << "========= CSR Matrix - basic functionality =======================";
19930
19931 numpy::NDArray<double> dense({ 4, 4 });
19932 dense.setElementAt({ 0, 0 }, 1.0);
19933 dense.setElementAt({ 0, 3 }, 2.0);
19934 dense.setElementAt({ 1, 1 }, 3.0);
19935 dense.setElementAt({ 2, 2 }, 4.0);
19936 dense.setElementAt({ 3, 0 }, 5.0);
19937 dense.setElementAt({ 3, 3 }, 6.0);
19938
19939 auto csr = numpy::sparse::CSRMatrix<double>::from_dense(dense);
19940
19941 if (csr.rows() != 4 || csr.cols() != 4 || csr.nnz() != 6) {
19942 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : Incorrect matrix dimensions";
19943 throw std::runtime_error("np_test_sparse_csr_basic failed: dimension mismatch");
19944 }
19945
19946 if (!csr.check_format()) {
19947 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : CSR format validation failed";
19948 throw std::runtime_error("np_test_sparse_csr_basic failed: invalid format");
19949 }
diagonal (np_test_2_all.cpp:11408)
11398 void np_test_linalg_missing_diagonal() {
11399 std::cout << "========= diagonal: extract diagonal from matrix =======================";
11400
11401 numpy::NDArray<double> mat({ 3, 4 });
11402 for (size_t i = 0; i < 3; ++i) {
11403 for (size_t j = 0; j < 4; ++j) {
11404 mat.setElementAt({ i, j }, static_cast<double>(i * 4 + j));
11405 }
11406 }
11407
11408 auto diag = numpy::diagonal(mat);
11409 if (diag.getShape()[0] != 3) {
11410 std::cout << " [FAIL] : diagonal shape incorrect";
11411 throw std::runtime_error("diagonal test failed: wrong shape");
11412 }
11413
11414 // Check diagonal values (0, 5, 10)
11415 if (std::abs(diag.getElementAt({ 0 }) - 0.0) > 1e-10 ||
11416 std::abs(diag.getElementAt({ 1 }) - 5.0) > 1e-10 ||
11417 std::abs(diag.getElementAt({ 2 }) - 10.0) > 1e-10) {
11418 std::cout << " [FAIL] : diagonal values incorrect";
identity (np_test_1_all.cpp:24002)
23992 throw std::runtime_error(description);
23993 }
23994 if (!((eye_offset.getShape() == std::vector<size_t>{3, 4}))) {
23995 std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((eye_offset.getShape() == std::vector<size_t>{3, 4}))";
23996 std::cout << std::string("[FAIL] ") + description << std::endl;
23997 throw std::runtime_error(description);
23998 }
23999 // std::cout << "[OK] eye function with optional parameters works correctly\n";
24000
24001 // Test identity
24002 auto identity_result = identity<int>(4);
24003 if (!((identity_result.getShape() == std::vector<size_t>{4, 4}))) {
24004 std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((identity_result.getShape() == std::vector<size_t>{4, 4}))";
24005 std::cout << std::string("[FAIL] ") + description << std::endl;
24006 throw std::runtime_error(description);
24007 }
24008 // std::cout << "[OK] identity function has correct signature\n";
24009
24010 // Test diag with different use cases
24011 auto vector1d = createInt32Array({ 3 }, 5);
24012 auto matrix2d = createInt32Array({ 3, 3 }, 0);
transpose (np_test_2_all.cpp:4973)
4963 }
4964
4965 /**
4966 * Test matrix properties and methods
4967 */
4968 void testMatrixProperties() {
4969 std::cout << "========= testMatrixProperties =======================";
4970
4971 // Test transpose
4972 numpy::Matrix<double> m("1 2 3; 4 5 6");
4973 auto mt = m.transpose();
4974 assert_test(mt.rows() == 3, "Transpose rows");
4975 assert_test(mt.cols() == 2, "Transpose cols");
4976 assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element");
4977 assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element");
4978
4979 // Test trace for square matrix
4980 numpy::Matrix<double> square("1 2; 3 4");
4981 double tr = square.trace();
4982 assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace");
add_entry (np_test_3_all.cpp:19991)
19981 }
19982
19983 // ============================================================================
19984 // COO MATRIX TESTS
19985 // ============================================================================
19986
19987 void np_test_sparse_coo_basic() {
19988 std::cout << "========= COO Matrix - basic functionality =======================";
19989
19990 numpy::sparse::COOMatrix<double> coo(4, 4);
19991 coo.add_entry(0, 0, 1.0);
19992 coo.add_entry(0, 3, 2.0);
19993 coo.add_entry(1, 1, 3.0);
19994 coo.add_entry(2, 2, 4.0);
19995 coo.add_entry(3, 0, 5.0);
19996 coo.add_entry(3, 3, 6.0);
19997
19998 if (coo.rows() != 4 || coo.cols() != 4 || coo.nnz() != 6) {
19999 std::cout << " [FAIL] : in np_test_sparse_coo_basic() : Incorrect matrix dimensions";
20000 throw std::runtime_error("np_test_sparse_coo_basic failed");
20001 }
to_csr (np_test_3_all.cpp:20025)
20015 void np_test_sparse_conversion() {
20016 std::cout << "========= format conversion - COO to CSR =======================";
20017
20018 numpy::sparse::COOMatrix<double> coo(5, 5);
20019 coo.add_entry(0, 0, 1.0);
20020 coo.add_entry(1, 1, 2.0);
20021 coo.add_entry(2, 2, 3.0);
20022 coo.add_entry(3, 3, 4.0);
20023 coo.add_entry(4, 4, 5.0);
20024
20025 auto csr = coo.to_csr();
20026
20027 if (csr.rows() != 5 || csr.cols() != 5 || csr.nnz() != 5) {
20028 std::cout << " [FAIL] : in np_test_sparse_conversion() : Conversion produced wrong dimensions";
20029 throw std::runtime_error("np_test_sparse_conversion failed");
20030 }
20031
20032 if (!csr.check_format()) {
20033 std::cout << " [FAIL] : in np_test_sparse_conversion() : Converted CSR format invalid";
20034 throw std::runtime_error("np_test_sparse_conversion failed");
20035 }
check_format (np_test_3_all.cpp:19946)
19936 dense.setElementAt({ 3, 0 }, 5.0);
19937 dense.setElementAt({ 3, 3 }, 6.0);
19938
19939 auto csr = numpy::sparse::CSRMatrix<double>::from_dense(dense);
19940
19941 if (csr.rows() != 4 || csr.cols() != 4 || csr.nnz() != 6) {
19942 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : Incorrect matrix dimensions";
19943 throw std::runtime_error("np_test_sparse_csr_basic failed: dimension mismatch");
19944 }
19945
19946 if (!csr.check_format()) {
19947 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : CSR format validation failed";
19948 throw std::runtime_error("np_test_sparse_csr_basic failed: invalid format");
19949 }
19950
19951 auto reconstructed = csr.to_dense();
19952 for (size_t i = 0; i < 4; ++i) {
19953 for (size_t j = 0; j < 4; ++j) {
19954 if (std::abs(dense.getElementAt({ i, j }) - reconstructed.getElementAt({ i, j })) > 1e-12) {
19955 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : Roundtrip conversion mismatch";
19956 throw std::runtime_error("np_test_sparse_csr_basic failed");
clone (np_test_1_all.cpp:24942)
24932 }
24933
24934 // Test reproducibility
24935 numpy::random::PCG64DXSM rng2(11111);
24936 if (rng2.next_uint64() != val) {
24937 std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible";
24938 throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24939 }
24940
24941 // Test clone
24942 auto cloned = rng.clone();
24943 if (cloned->name() != "PCG64DXSM") {
24944 std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect";
24945 throw std::runtime_error("np_test_bitgen_pcg64dxsm failed");
24946 }
24947
24948 std::cout << " -> tests passed" << std::endl;
24949 }
24950
24951 // ============================================================================
24952 // PHILOX TESTS
cols (np_test_2_all.cpp:4911)
4901 /**
4902 * Test basic matrix construction and properties
4903 */
4904 void testMatrixBasics() {
4905 std::cout << "========= testMatrixBasics =======================";
4906
4907 // Test construction from dimensions
4908 numpy::Matrix<double> m1(3, 3);
4909 assert_test(m1.rows() == 3, "Matrix rows");
4910 assert_test(m1.cols() == 3, "Matrix cols");
4911 assert_test(m1.isSquare(), "Matrix is square");
4912
4913 // Test construction from 2D vector
4914 std::vector<std::vector<int>> data = { {1, 2, 3}, {4, 5, 6} };
4915 numpy::Matrix<int> m2(data);
4916 assert_test(m2.rows() == 2, "Matrix from vector rows");
4917 assert_test(m2.cols() == 3, "Matrix from vector cols");
4918 assert_test(m2(0, 0) == 1, "Matrix element access");
4919 assert_test(m2(1, 2) == 6, "Matrix element access");
nnz (np_test_3_all.cpp:19941)
19931 numpy::NDArray<double> dense({ 4, 4 });
19932 dense.setElementAt({ 0, 0 }, 1.0);
19933 dense.setElementAt({ 0, 3 }, 2.0);
19934 dense.setElementAt({ 1, 1 }, 3.0);
19935 dense.setElementAt({ 2, 2 }, 4.0);
19936 dense.setElementAt({ 3, 0 }, 5.0);
19937 dense.setElementAt({ 3, 3 }, 6.0);
19938
19939 auto csr = numpy::sparse::CSRMatrix<double>::from_dense(dense);
19940
19941 if (csr.rows() != 4 || csr.cols() != 4 || csr.nnz() != 6) {
19942 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : Incorrect matrix dimensions";
19943 throw std::runtime_error("np_test_sparse_csr_basic failed: dimension mismatch");
19944 }
19945
19946 if (!csr.check_format()) {
19947 std::cout << " [FAIL] : in np_test_sparse_csr_basic() : CSR format validation failed";
19948 throw std::runtime_error("np_test_sparse_csr_basic failed: invalid format");
19949 }
19950
19951 auto reconstructed = csr.to_dense();
rows (np_test_2_all.cpp:4910)
4900 namespace test_matrix {
4901
4902 /**
4903 * Test basic matrix construction and properties
4904 */
4905 void testMatrixBasics() {
4906 std::cout << "========= testMatrixBasics =======================";
4907
4908 // Test construction from dimensions
4909 numpy::Matrix<double> m1(3, 3);
4910 assert_test(m1.rows() == 3, "Matrix rows");
4911 assert_test(m1.cols() == 3, "Matrix cols");
4912 assert_test(m1.isSquare(), "Matrix is square");
4913
4914 // Test construction from 2D vector
4915 std::vector<std::vector<int>> data = { {1, 2, 3}, {4, 5, 6} };
4916 numpy::Matrix<int> m2(data);
4917 assert_test(m2.rows() == 2, "Matrix from vector rows");
4918 assert_test(m2.cols() == 3, "Matrix from vector cols");
4919 assert_test(m2(0, 0) == 1, "Matrix element access");
4920 assert_test(m2(1, 2) == 6, "Matrix element access");
values (np_test_1_all.cpp:15133)
15123 std::cout << " -> tests passed" << std::endl;
15124 }
15125 else {
15126 std::cout << "[FAIL] Where function failed" << std::endl;
15127 errors++;
15128 }
15129
15130 // Test 8: Clip function
15131 std::cout << "========= test_clip_function =======================" ;
15132
15133 numpy::NDArray<double> values({ 5 });
15134 values.setElementAt({ 0 }, -2.0);
15135 values.setElementAt({ 1 }, -1.0);
15136 values.setElementAt({ 2 }, 0.5);
15137 values.setElementAt({ 3 }, 2.0);
15138 values.setElementAt({ 4 }, 5.0);
15139 double min_val = -1.0;
15140 double max_val = 3.0;
15141
15142 auto clip_result = numpy::clip(values, min_val, max_val);
values (np_test_1_all.cpp:15133)
15123 std::cout << " -> tests passed" << std::endl;
15124 }
15125 else {
15126 std::cout << "[FAIL] Where function failed" << std::endl;
15127 errors++;
15128 }
15129
15130 // Test 8: Clip function
15131 std::cout << "========= test_clip_function =======================" ;
15132
15133 numpy::NDArray<double> values({ 5 });
15134 values.setElementAt({ 0 }, -2.0);
15135 values.setElementAt({ 1 }, -1.0);
15136 values.setElementAt({ 2 }, 0.5);
15137 values.setElementAt({ 3 }, 2.0);
15138 values.setElementAt({ 4 }, 5.0);
15139 double min_val = -1.0;
15140 double max_val = 3.0;
15141
15142 auto clip_result = numpy::clip(values, min_val, max_val);