Matrix ====== .. cpp:class:: numpy::Matrix numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Matrix Matrix obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``Matrix(size_trows, size_tcols)`` - NP_MATRIX.H:51 - :ref:`View ` * - ``Matrix(const std::vector>&data)`` - NP_MATRIX.H:58 - :ref:`View ` * - ``Matrix(const NDArray&array)`` - NP_MATRIX.H:84 - :ref:`View ` * - ``Matrix(const std::string &matlab_string)`` - NP_MATRIX.H:92 - :ref:`View ` * - ``Matrix(const Matrix &other)`` - NP_MATRIX.H:97 - :ref:`View ` Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Matrix & operator=(const Matrix &other)`` - Matrix & - NP_MATRIX.H:100 - * - ``T operator()(size_trow, size_tcol)`` - T - NP_MATRIX.H:163 - * - ``Matrix operator\*(const Matrix&other)`` - Matrix - NP_MATRIX.H:180 - * - ``Matrix operator+(const Matrix&other)`` - Matrix - NP_MATRIX.H:187 - * - ``Matrix operator-(const Matrix&other)`` - Matrix - NP_MATRIX.H:194 - * - ``Matrix operator^(intpower)`` - Matrix - NP_MATRIX.H:201 - * - ``Matrix operator\*(const T &scalar)`` - Matrix - NP_MATRIX.H:224 - * - ``operator NDArray()`` - - NP_MATRIX.H:354 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Matrix eye(size_tn)`` - Matrix - NP_MATRIX.H:361 - :ref:`View ` * - ``Matrix ones(size_trows, size_tcols)`` - Matrix - NP_MATRIX.H:372 - :ref:`View ` * - ``Matrix zeros(size_trows, size_tcols)`` - Matrix - NP_MATRIX.H:385 - :ref:`View ` Shape Manipulation ------------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Matrix transpose()`` - Matrix - NP_MATRIX.H:112 - :ref:`View ` Linear Algebra -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T det()`` - T - NP_MATRIX.H:246 - :ref:`View ` * - ``Matrix inv()`` - Matrix - NP_MATRIX.H:281 - :ref:`View ` * - ``Matrix matmul(const Matrix&other)`` - Matrix - NP_MATRIX.H:234 - :ref:`View ` * - ``Matrix pinv()`` - Matrix - NP_MATRIX.H:314 - :ref:`View ` * - ``T trace()`` - T - NP_MATRIX.H:267 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Matrix H()`` - Matrix - NP_MATRIX.H:121 - :ref:`View ` * - ``Matrix I()`` - Matrix - NP_MATRIX.H:140 - :ref:`View ` * - ``size_t cols()`` - size_t - NP_MATRIX.H:155 - :ref:`View ` * - ``void ensureTwoDimensional()`` - void - NP_MATRIX.H:32 - * - ``bool isSingular()`` - bool - NP_MATRIX.H:326 - * - ``bool isSquare()`` - bool - NP_MATRIX.H:321 - :ref:`View ` * - ``void parseMatlabString(const std::string &str)`` - void - NP_MATRIX.H:393 - * - ``void print()`` - void - NP_MATRIX.H:349 - :ref:`View ` * - ``size_t rows()`` - size_t - NP_MATRIX.H:154 - :ref:`View ` * - ``void setElement(size_trow, size_tcol, const T &value)`` - void - NP_MATRIX.H:170 - :ref:`View ` * - ``size_t size()`` - size_t - NP_MATRIX.H:156 - :ref:`View ` * - ``std::string toString()`` - std::string - NP_MATRIX.H:339 - :ref:`View ` * - ``void validateSquare(const std::string &operation)`` - void - NP_MATRIX.H:38 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-matrix-matrix-0: .. dropdown:: Matrix (np_test_5_all.cpp:8563) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8553 :emphasize-lines: 11 mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { throw std::runtime_error("Missing matrix dimensions in toString()"); } std::cout << " -> tests passed" << std::endl; } void np_test_toString_matrix_identity() { std::cout << "========= Matrix::toString() identity matrix ======================="; numpy::Matrix mat = numpy::Matrix::eye(4); .. _example-matrix-matrix-1: .. dropdown:: Matrix (np_test_5_all.cpp:8563) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8553 :emphasize-lines: 11 mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { throw std::runtime_error("Missing matrix dimensions in toString()"); } std::cout << " -> tests passed" << std::endl; } void np_test_toString_matrix_identity() { std::cout << "========= Matrix::toString() identity matrix ======================="; numpy::Matrix mat = numpy::Matrix::eye(4); .. _example-matrix-matrix-2: .. dropdown:: Matrix (np_test_5_all.cpp:8563) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8553 :emphasize-lines: 11 mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { throw std::runtime_error("Missing matrix dimensions in toString()"); } std::cout << " -> tests passed" << std::endl; } void np_test_toString_matrix_identity() { std::cout << "========= Matrix::toString() identity matrix ======================="; numpy::Matrix mat = numpy::Matrix::eye(4); .. _example-matrix-matrix-3: .. dropdown:: Matrix (np_test_5_all.cpp:8563) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8553 :emphasize-lines: 11 mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { throw std::runtime_error("Missing matrix dimensions in toString()"); } std::cout << " -> tests passed" << std::endl; } void np_test_toString_matrix_identity() { std::cout << "========= Matrix::toString() identity matrix ======================="; numpy::Matrix mat = numpy::Matrix::eye(4); .. _example-matrix-matrix-4: .. dropdown:: Matrix (np_test_5_all.cpp:8563) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8553 :emphasize-lines: 11 mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { throw std::runtime_error("Missing matrix dimensions in toString()"); } std::cout << " -> tests passed" << std::endl; } void np_test_toString_matrix_identity() { std::cout << "========= Matrix::toString() identity matrix ======================="; numpy::Matrix mat = numpy::Matrix::eye(4); .. _example-matrix-eye-5: .. dropdown:: eye (np_test_1_all.cpp:23980) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23970 :emphasize-lines: 11 // Test geomspace auto geomspace_result = geomspace(1.0, 16.0, 5); if (!(geomspace_result.getShape()[0] == 5)) { std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geomspace_result.getShape()[0] == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] geomspace function has correct signature\n"; // Test eye with optional parameters auto eye_square = eye(3); auto eye_rect = eye(3, 4); auto eye_offset = eye(3, 4, 1); if (!((eye_square.getShape() == std::vector{3, 3}))) { std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((eye_square.getShape() == std::vector{3, 3}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!((eye_rect.getShape() == std::vector{3, 4}))) { std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((eye_rect.getShape() == std::vector{3, 4}))"; .. _example-matrix-ones-6: .. dropdown:: ones (np_test_1_all.cpp:319) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 309 :emphasize-lines: 11 //int_range.printArray(); auto reshaped = int_range.reshapeArray({ 3, 2 }); // std::cout << "Reshaped to 2x2 matrix:"; //reshaped.printArray(); auto zeros = createInt32Zeros({ 2, 3 }); // std::cout << "zeros (2,3):"; //zeros.printArray(); auto ones = createInt32Ones({ 2, 3 }); // std::cout << "ones (2,3):"; //ones.printArray(); // std::cout << "Addition of zeros and ones:" << std::endl; auto sum = zeros.addArrays(ones); //sum.printArray(); // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl; // std::cout << "Mean: " << sum.meanArray() << std::endl; // std::cout << std::endl << "Type introspection with get_typename():" << std::endl; .. _example-matrix-zeros-7: .. dropdown:: zeros (np_test_1_all.cpp:316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 306 :emphasize-lines: 11 auto int_range = NDArray::createRange(1, 12, 2); // std::cout << "Int32 range array (1 to 10, step 2):" << std::endl; //int_range.printArray(); auto reshaped = int_range.reshapeArray({ 3, 2 }); // std::cout << "Reshaped to 2x2 matrix:"; //reshaped.printArray(); auto zeros = createInt32Zeros({ 2, 3 }); // std::cout << "zeros (2,3):"; //zeros.printArray(); auto ones = createInt32Ones({ 2, 3 }); // std::cout << "ones (2,3):"; //ones.printArray(); // std::cout << "Addition of zeros and ones:" << std::endl; auto sum = zeros.addArrays(ones); //sum.printArray(); // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl; .. _example-matrix-transpose-8: .. dropdown:: transpose (np_test_2_all.cpp:4973) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4963 :emphasize-lines: 11 } /** * Test matrix properties and methods */ void testMatrixProperties() { std::cout << "========= testMatrixProperties ======================="; // Test transpose numpy::Matrix m("1 2 3; 4 5 6"); auto mt = m.transpose(); assert_test(mt.rows() == 3, "Transpose rows"); assert_test(mt.cols() == 2, "Transpose cols"); assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element"); assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element"); // Test trace for square matrix numpy::Matrix square("1 2; 3 4"); double tr = square.trace(); assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace"); .. _example-matrix-det-9: .. dropdown:: det (np_test_2_all.cpp:4985) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4975 :emphasize-lines: 11 assert_test(mt.cols() == 2, "Transpose cols"); assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element"); assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element"); // Test trace for square matrix numpy::Matrix square("1 2; 3 4"); double tr = square.trace(); assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace"); // Test determinant for 2x2 matrix double det = square.det(); assert_test(std::abs(det - (-2.0)) < 1e-10, "Matrix determinant 2x2"); // Test matrix inverse for 2x2 auto inv = square.inv(); assert_test(std::abs(inv(0, 0) - (-2.0)) < 1e-10, "Matrix inverse 0,0"); assert_test(std::abs(inv(0, 1) - 1.0) < 1e-10, "Matrix inverse 0,1"); assert_test(std::abs(inv(1, 0) - 1.5) < 1e-10, "Matrix inverse 1,0"); assert_test(std::abs(inv(1, 1) - (-0.5)) < 1e-10, "Matrix inverse 1,1"); // Verify A * A^-1 = I .. _example-matrix-inv-10: .. dropdown:: inv (np_test_2_all.cpp:4989) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4979 :emphasize-lines: 11 // Test trace for square matrix numpy::Matrix square("1 2; 3 4"); double tr = square.trace(); assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace"); // Test determinant for 2x2 matrix double det = square.det(); assert_test(std::abs(det - (-2.0)) < 1e-10, "Matrix determinant 2x2"); // Test matrix inverse for 2x2 auto inv = square.inv(); assert_test(std::abs(inv(0, 0) - (-2.0)) < 1e-10, "Matrix inverse 0,0"); assert_test(std::abs(inv(0, 1) - 1.0) < 1e-10, "Matrix inverse 0,1"); assert_test(std::abs(inv(1, 0) - 1.5) < 1e-10, "Matrix inverse 1,0"); assert_test(std::abs(inv(1, 1) - (-0.5)) < 1e-10, "Matrix inverse 1,1"); // Verify A * A^-1 = I auto identity_check = square * inv; assert_test(std::abs(identity_check(0, 0) - 1.0) < 1e-10, "Inverse verification diagonal"); assert_test(std::abs(identity_check(1, 1) - 1.0) < 1e-10, "Inverse verification diagonal"); assert_test(std::abs(identity_check(0, 1)) < 1e-10, "Inverse verification off-diagonal"); .. _example-matrix-matmul-11: .. dropdown:: matmul (np_test_1_all.cpp:15423) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15413 :emphasize-lines: 11 // Verify reconstruction: A = U * S * Vt // First create diagonal matrix from S NDArray S_diag({ 2, 2 }); for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 2; ++j) { S_diag.setElementAt({ i, j }, (i == j) ? svd_result.S.getElementAt({ i }) : 0.0); } } auto US = matmul(svd_result.U, S_diag); auto reconstructed = matmul(US, svd_result.Vt); // std::cout << "Reconstructed matrix (U * S * Vt):" << std::endl; //reconstructed.printArray(); // Check reconstruction error double max_error = 0.0; for (size_t i = 0; i < 3; ++i) { for (size_t j = 0; j < 2; ++j) { double error = std::abs(matrix.getElementAt({ i, j }) - reconstructed.getElementAt({ i, j })); .. _example-matrix-pinv-12: .. dropdown:: pinv (np_test_1_all.cpp:15553) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15543 :emphasize-lines: 11 matrix.setElementAt({ 0, 0 }, 1.0); matrix.setElementAt({ 0, 1 }, 2.0); matrix.setElementAt({ 1, 0 }, 2.0); matrix.setElementAt({ 1, 1 }, 4.0); matrix.setElementAt({ 2, 0 }, 3.0); matrix.setElementAt({ 2, 1 }, 6.0); // std::cout << "Original matrix (rank-deficient):" << std::endl; //matrix.printArray(); auto pseudoinv = pinv(matrix); // std::cout << "Pseudoinverse:" << std::endl; //pseudoinv.printArray(); // Test Moore-Penrose conditions // 1. A * A^+ * A = A auto AApA = matmul(matmul(matrix, pseudoinv), matrix); // std::cout << "A * A^+ * A (should equal A):" << std::endl; //AApA.printArray(); .. _example-matrix-trace-13: .. dropdown:: trace (np_test_2_all.cpp:4981) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4971 :emphasize-lines: 11 // Test transpose numpy::Matrix m("1 2 3; 4 5 6"); auto mt = m.transpose(); assert_test(mt.rows() == 3, "Transpose rows"); assert_test(mt.cols() == 2, "Transpose cols"); assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element"); assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element"); // Test trace for square matrix numpy::Matrix square("1 2; 3 4"); double tr = square.trace(); assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace"); // Test determinant for 2x2 matrix double det = square.det(); assert_test(std::abs(det - (-2.0)) < 1e-10, "Matrix determinant 2x2"); // Test matrix inverse for 2x2 auto inv = square.inv(); assert_test(std::abs(inv(0, 0) - (-2.0)) < 1e-10, "Matrix inverse 0,0"); assert_test(std::abs(inv(0, 1) - 1.0) < 1e-10, "Matrix inverse 0,1"); .. _example-matrix-h-14: .. dropdown:: H (np_test_1_all.cpp:20194) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20184 :emphasize-lines: 11 const SchurDecomposition& schur) { ValidationResult result; result.passed = false; result.max_error = 0.0; result.avg_error = 0.0; try { // Compute Q * T auto QT = matmul(schur.Q, schur.T_matrix); // Compute Q^H (conjugate transpose) NDArray QH = schur.Q.transposeArray(); // Start with transpose if constexpr (utils::is_complex_v) { QH = utils::conjugate_transpose(schur.Q); // Override with conjugate transpose if complex } // Compute reconstructed matrix: Q * T * Q^H auto reconstructed = matmul(QT, QH); // Calculate error double total_error = 0.0; .. _example-matrix-i-15: .. dropdown:: I (np_test_1_all.cpp:24644) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24634 :emphasize-lines: 11 throw std::runtime_error("np_test_eigh_3x3_diagonal failed"); } std::cout << " -> tests passed" << std::endl; } void np_test_eigh_identity() { std::cout << "========= eigh: identity matrix ======================="; // Create 3x3 identity matrix numpy::NDArray I({ 3, 3 }); for (size_t i = 0; i < 3; ++i) { for (size_t j = 0; j < 3; ++j) { I.setElementAt({ i, j }, (i == j) ? 1.0 : 0.0); } } auto result = numpy::linalg::eigh(I, true); print_vector(result.eigenvalues, "Eigenvalues"); .. _example-matrix-cols-16: .. dropdown:: cols (np_test_2_all.cpp:4911) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4901 :emphasize-lines: 11 /** * Test basic matrix construction and properties */ void testMatrixBasics() { std::cout << "========= testMatrixBasics ======================="; // Test construction from dimensions numpy::Matrix m1(3, 3); assert_test(m1.rows() == 3, "Matrix rows"); assert_test(m1.cols() == 3, "Matrix cols"); assert_test(m1.isSquare(), "Matrix is square"); // Test construction from 2D vector std::vector> data = { {1, 2, 3}, {4, 5, 6} }; numpy::Matrix m2(data); assert_test(m2.rows() == 2, "Matrix from vector rows"); assert_test(m2.cols() == 3, "Matrix from vector cols"); assert_test(m2(0, 0) == 1, "Matrix element access"); assert_test(m2(1, 2) == 6, "Matrix element access"); .. _example-matrix-issquare-17: .. dropdown:: isSquare (np_test_2_all.cpp:4912) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4902 :emphasize-lines: 11 /** * Test basic matrix construction and properties */ void testMatrixBasics() { std::cout << "========= testMatrixBasics ======================="; // Test construction from dimensions numpy::Matrix m1(3, 3); assert_test(m1.rows() == 3, "Matrix rows"); assert_test(m1.cols() == 3, "Matrix cols"); assert_test(m1.isSquare(), "Matrix is square"); // Test construction from 2D vector std::vector> data = { {1, 2, 3}, {4, 5, 6} }; numpy::Matrix m2(data); assert_test(m2.rows() == 2, "Matrix from vector rows"); assert_test(m2.cols() == 3, "Matrix from vector cols"); assert_test(m2(0, 0) == 1, "Matrix element access"); assert_test(m2(1, 2) == 6, "Matrix element access"); // Test MATLAB-style string construction .. _example-matrix-print-18: .. dropdown:: print (np_test_1_all.cpp:6395) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6385 :emphasize-lines: 11 std::vector strings = {"Alice", "Bob", "Charlie"}; auto arr2 = array<32>(strings); // std::cout << "Created from strings vector:" << std::endl; for (size_t i = 0; i < arr2.size(); ++i) { // std::cout << "arr2[" << i << "] = '" << arr2[i] << "'"; } // Test 2D array creation chararray16 arr_2d({2, 3}, "test"); // std::cout << "Created 2D chararray16 with shape {2, 3}:"; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } void testCharArrayStringOperationsCharArray() { std::cout << "========= testStringOperations ======================="; // Test string concatenation std::vector words1 = {"Hello", "Good", "Nice"}; std::vector words2 = {" World", " Day", " Work"}; .. _example-matrix-rows-19: .. dropdown:: rows (np_test_2_all.cpp:4910) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4900 :emphasize-lines: 11 namespace test_matrix { /** * Test basic matrix construction and properties */ void testMatrixBasics() { std::cout << "========= testMatrixBasics ======================="; // Test construction from dimensions numpy::Matrix m1(3, 3); assert_test(m1.rows() == 3, "Matrix rows"); assert_test(m1.cols() == 3, "Matrix cols"); assert_test(m1.isSquare(), "Matrix is square"); // Test construction from 2D vector std::vector> data = { {1, 2, 3}, {4, 5, 6} }; numpy::Matrix m2(data); assert_test(m2.rows() == 2, "Matrix from vector rows"); assert_test(m2.cols() == 3, "Matrix from vector cols"); assert_test(m2(0, 0) == 1, "Matrix element access"); assert_test(m2(1, 2) == 6, "Matrix element access"); .. _example-matrix-setelement-20: .. dropdown:: setElement (np_test_5_all.cpp:8553) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8543 :emphasize-lines: 11 // =========================== #endif // End of RecordArray tests void np_test_toString_matrix_basic() { std::cout << "========= Matrix::toString() basic ======================="; numpy::Matrix mat(3, 3); for (size_t i = 0; i < 3; ++i) { for (size_t j = 0; j < 3; ++j) { mat.setElement(i, j, static_cast(i * 3 + j)); } } std::string result = mat.toString(); // std::cout << "Result:\n" << result << std::endl; verify_not_empty(result, "Matrix toString()"); if (!verify_contains(result, "Matrix(3x3)", "Matrix")) { .. _example-matrix-size-21: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); .. _example-matrix-tostring-22: .. dropdown:: toString (np_test_1_all.cpp:6826) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6816 :emphasize-lines: 11 void testDatetime64CreationDateTime() { std::cout << "========= testDatetime64CreationDateTime ======================="; // Test default constructor datetime64 default_dt; if (!(default_dt.isNaT())) { std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl; // Test value constructor datetime64 epoch_day(0, DateTimeUnit::Day); // std::cout << "Epoch day: " << epoch_day.toString() << std::endl; // Test string constructor datetime64 date_from_string("2024-01-15"); // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl; datetime64 datetime_from_string("2024-01-15T10:30:45");