IntervalArray ============= .. cpp:class:: numpy::IntervalArray numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use IntervalArray IntervalArray obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``IntervalArray(const numpy::NDArray& left, const numpy::NDArray& right, const numpy::NDArray& mask, IntervalClosed closed = IntervalClosed::Right, bool copy = false)`` - df_interval_array.h:121 - * - ``IntervalArray(const numpy::NDArray& left, const numpy::NDArray& right, IntervalClosed closed = IntervalClosed::Right)`` - df_interval_array.h:141 - * - ``explicit IntervalArray(const std::vector>>& values, IntervalClosed closed = IntervalClosed::Right)`` - df_interval_array.h:158 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static IntervalArray from_arrays(const std::vector& left, const std::vector& right, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - df_interval_array.h:266 - * - ``static IntervalArray from_arrays(const numpy::NDArray& left, const numpy::NDArray& right, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - df_interval_array.h:287 - * - ``\* Example: from_breaks(`` - \* Example: - df_interval_array.h:221 - * - ``static IntervalArray from_breaks(const std::vector& breaks, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - df_interval_array.h:227 - * - ``static IntervalArray from_breaks(const numpy::NDArray& breaks, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - df_interval_array.h:248 - * - ``static IntervalArray from_tuples(const std::vector>& tuples, IntervalClosed closed = IntervalClosed::Right)`` - static IntervalArray - df_interval_array.h:300 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool empty() const`` - bool - df_interval_array.h:355 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray take(const numpy::NDArray& indices) const`` - IntervalArray - df_interval_array.h:1210 - :ref:`View ` Sorting ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray argsort() const`` - numpy::NDArray - df_interval_array.h:968 - :ref:`View ` Set Operations -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray unique() const`` - IntervalArray - df_interval_array.h:1053 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string() const`` - std::string - df_interval_array.h:1180 - :ref:`View ` * - ``std::vector>> to_tuples() const`` - std::vector>> - df_interval_array.h:841 - Type Handling ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``IntervalArray copy() const`` - IntervalArray - df_interval_array.h:1312 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``BooleanArray is_empty() const`` - BooleanArray - df_interval_array.h:461 - * - ``bool is_left_closed() const`` - bool - df_interval_array.h:383 - * - ``bool is_na(size_t index) const`` - bool - df_interval_array.h:561 - * - ``bool is_non_overlapping_monotonic() const`` - bool - df_interval_array.h:488 - * - ``bool is_right_closed() const`` - bool - df_interval_array.h:390 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::int64 argmax() const`` - numpy::int64 - df_interval_array.h:1025 - :ref:`View ` * - ``numpy::int64 argmin() const`` - numpy::int64 - df_interval_array.h:1001 - :ref:`View ` * - ``IntervalClosed closed() const`` - IntervalClosed - df_interval_array.h:369 - * - ``std::string closed_string() const`` - std::string - df_interval_array.h:376 - * - ``static IntervalArray concat(const std::vector& arrays)`` - static IntervalArray - df_interval_array.h:1274 - * - ``BooleanArray contains(T value) const`` - BooleanArray - df_interval_array.h:679 - :ref:`View ` * - ``BooleanArray contains(const numpy::NDArray& values) const`` - BooleanArray - df_interval_array.h:717 - :ref:`View ` * - ``size_t count() const`` - size_t - df_interval_array.h:644 - :ref:`View ` * - ``const numpy::NDArray& data_left() const`` - const numpy::NDArray& - df_interval_array.h:527 - * - ``const numpy::NDArray& data_right() const`` - const numpy::NDArray& - df_interval_array.h:534 - * - ``IntervalArray dropna() const`` - IntervalArray - df_interval_array.h:622 - * - ``dtype_type dtype() const`` - dtype_type - df_interval_array.h:320 - :ref:`View ` * - ``<< dtype().name() << "')"`` - << - df_interval_array.h:1197 - :ref:`View ` * - ``std::pair, IntervalArray> factorize() const`` - std::pair, IntervalArray> - df_interval_array.h:1107 - * - ``IntervalArray fillna(const std::pair& fill_value) const`` - IntervalArray - df_interval_array.h:603 - * - ``bool has_na() const`` - bool - df_interval_array.h:657 - * - ``std::string interval_to_string(size_t index) const`` - std::string - df_interval_array.h:1159 - * - ``oss << interval_to_string(i)`` - oss << - df_interval_array.h:1189 - * - ``BooleanArray isna() const`` - BooleanArray - df_interval_array.h:576 - * - ``numpy::NDArray left() const`` - numpy::NDArray - df_interval_array.h:401 - :ref:`View ` * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_interval_array.h:234 - * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_interval_array.h:273 - * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_interval_array.h:302 - * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_interval_array.h:1086 - * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_interval_array.h:1139 - * - ``size_t len() const`` - size_t - df_interval_array.h:362 - * - ``numpy::NDArray length() const`` - numpy::NDArray - df_interval_array.h:437 - :ref:`View ` * - ``const numpy::NDArray& mask() const`` - const numpy::NDArray& - df_interval_array.h:541 - :ref:`View ` * - ``numpy::NDArray mask_arr(std::vector`` - numpy::NDArray - df_interval_array.h:1088 - * - ``FloatingArray mid() const`` - FloatingArray - df_interval_array.h:416 - * - ``size_t nbytes() const`` - size_t - df_interval_array.h:334 - :ref:`View ` * - ``constexpr int ndim() const`` - constexpr int - df_interval_array.h:341 - :ref:`View ` * - ``numpy::NDArray new_left(std::vector`` - numpy::NDArray - df_interval_array.h:631 - * - ``numpy::NDArray new_left(std::vector`` - numpy::NDArray - df_interval_array.h:1212 - * - ``numpy::NDArray new_left(std::vector`` - numpy::NDArray - df_interval_array.h:1251 - * - ``numpy::NDArray new_left(std::vector`` - numpy::NDArray - df_interval_array.h:1288 - * - ``numpy::NDArray new_mask(std::vector`` - numpy::NDArray - df_interval_array.h:606 - * - ``numpy::NDArray new_mask(std::vector`` - numpy::NDArray - df_interval_array.h:1214 - * - ``numpy::NDArray new_mask(std::vector`` - numpy::NDArray - df_interval_array.h:1253 - * - ``numpy::NDArray new_mask(std::vector`` - numpy::NDArray - df_interval_array.h:1290 - * - ``numpy::NDArray new_right(std::vector`` - numpy::NDArray - df_interval_array.h:632 - * - ``numpy::NDArray new_right(std::vector`` - numpy::NDArray - df_interval_array.h:1213 - * - ``numpy::NDArray new_right(std::vector`` - numpy::NDArray - df_interval_array.h:1252 - * - ``numpy::NDArray new_right(std::vector`` - numpy::NDArray - df_interval_array.h:1289 - * - ``BooleanArray notna() const`` - BooleanArray - df_interval_array.h:589 - * - ``BooleanArray overlaps(T other_left, T other_right, IntervalClosed other_closed) const`` - BooleanArray - df_interval_array.h:764 - * - ``BooleanArray overlaps(T other_left, T other_right) const`` - BooleanArray - df_interval_array.h:812 - * - ``numpy::NDArray right() const`` - numpy::NDArray - df_interval_array.h:408 - :ref:`View ` * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_interval_array.h:235 - * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_interval_array.h:274 - * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_interval_array.h:303 - * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_interval_array.h:1087 - * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_interval_array.h:1140 - * - ``IntervalArray set_closed(IntervalClosed new_closed) const`` - IntervalArray - df_interval_array.h:826 - * - ``IntervalArray set_closed(const std::string& new_closed) const`` - IntervalArray - df_interval_array.h:833 - * - ``std::vector shape() const`` - std::vector - df_interval_array.h:348 - :ref:`View ` * - ``size_t size() const`` - size_t - df_interval_array.h:327 - :ref:`View ` * - ``IntervalArray slice(size_t start, size_t stop, size_t step = 1) const`` - IntervalArray - df_interval_array.h:1237 - :ref:`View ` * - ``\* Encodes the array as enumerated type (codes pointing to uniques).`` - \* Encodes the array as enumerated - df_interval_array.h:1102 - :ref:`View ` * - ``void validate_arrays()`` - void - df_interval_array.h:54 - * - ``void validate_bounds()`` - void - df_interval_array.h:81 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-intervalarray-empty-0: .. dropdown:: empty (np_test_1_all.cpp:6316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6306 :emphasize-lines: 11 } void test_data_generator_emptyBenchmarkSorting() { std::cout << "========= test_data_generator_empty ======================="; DataGenerator gen(42); // Test empty data generation std::vector data = gen.generate(0, DataPattern::RANDOM); if (!(data.empty())) { std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Empty data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-intervalarray-take-1: .. dropdown:: take (np_test_5_all.cpp:4313) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4303 :emphasize-lines: 11 for (size_t i = 0; i < 6; ++i) { arr.setElementAt({ i }, static_cast(i * 10)); // [0, 10, 20, 30, 40, 50] } // Take specific indices numpy::NDArray indices({ 3 }); indices.setElementAt({ 0 }, 5); // 50 indices.setElementAt({ 1 }, 2); // 20 indices.setElementAt({ 2 }, 0); // 0 auto result = numpy::take(arr, indices); if (result.getSize() != 3) { std::cout << " [FAIL] : in np_test_take_basic() : Wrong result size"; throw std::runtime_error("Test failed"); } if (result.getElementAt({ 0 }) != 50 || result.getElementAt({ 1 }) != 20 || result.getElementAt({ 2 }) != 0) { std::cout << " [FAIL] : in np_test_take_basic() : Wrong values extracted"; throw std::runtime_error("Test failed"); } .. _example-intervalarray-argsort-2: .. dropdown:: argsort (np_test_1_all.cpp:16841) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16831 :emphasize-lines: 11 std::cout << "========= test_argsort ======================="; // Test 1D array argsort NDArray arr1d({ 5 }); arr1d.setElementAt({ 0 }, 30); // index 0 arr1d.setElementAt({ 1 }, 10); // index 1 arr1d.setElementAt({ 2 }, 40); // index 2 arr1d.setElementAt({ 3 }, 20); // index 3 arr1d.setElementAt({ 4 }, 50); // index 4 auto indices = argsort(arr1d); // Expected order: 10(idx1), 20(idx3), 30(idx0), 40(idx2), 50(idx4) if (!(indices.getElementAt({ 0 }) == 1)) { std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 0 }) == 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(indices.getElementAt({ 1 }) == 3)) { std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 1 }) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-intervalarray-unique-3: .. dropdown:: unique (np_test_1_all.cpp:6259) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6249 :emphasize-lines: 11 if (!(data.size() == 100)) { std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Count unique values std::vector sorted_copy = data; std::sort(sorted_copy.begin(), sorted_copy.end()); auto unique_end = std::unique(sorted_copy.begin(), sorted_copy.end()); size_t unique_count = std::distance(sorted_copy.begin(), unique_end); // Should have significantly fewer unique values than total elements if (!(unique_count < data.size() / 2)) { std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unique_count < data.size() / 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Few unique data generation test passed. Unique values: " << unique_count << std::endl; .. _example-intervalarray-to_string-4: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-intervalarray-copy-5: .. dropdown:: copy (np_test_1_all.cpp:9812) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9802 :emphasize-lines: 11 //original.printArray(); // The modification should be at position (1,1) in original if (!(original.getElementAt({1, 1}) == 9999)) { std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Slice view shares memory with original"; // Test slice copy (should be independent) auto slice_copy = original.sliceArray({{2, 4}, {2, 4}}); // std::cout << "Slice copy [2:4, 2:4]:"; //slice_copy.printArray(); slice_copy.setElementAt({0, 0}, 7777); // std::cout << "After modifying slice copy at (0,0):" << std::endl; // std::cout << "Original array:" << std::endl; //original.printArray(); // std::cout << "Slice copy:" << std::endl; .. _example-intervalarray-argmax-6: .. dropdown:: argmax (np_test_3_all.cpp:20521) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20511 :emphasize-lines: 11 auto result = numpy::argmin(arr); // Should return 1 (index of (1,5)) if (result.getElementAt({0}) != 1) { throw std::runtime_error("argmin() failed: expected index 1"); } std::cout << "PASSED\n"; } void test_argmax_complex128() { std::cout << " Testing argmax() with complex128... "; numpy::NDArray arr({3}); arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0 - largest arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1 arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2 auto result = numpy::argmax(arr); // Should return 0 (index of (3,1)) if (result.getElementAt({0}) != 0) { throw std::runtime_error("argmax() failed: expected index 0"); } .. _example-intervalarray-argmin-7: .. dropdown:: argmin (np_test_3_all.cpp:20506) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20496 :emphasize-lines: 11 if (min_result.getElementAt({0}).real() != 2.0 || min_result.getElementAt({0}).imag() != 1.0) { throw std::runtime_error("min() tie-breaking failed: expected (2,1)"); } if (max_result.getElementAt({0}).real() != 2.0 || max_result.getElementAt({0}).imag() != 5.0) { throw std::runtime_error("max() tie-breaking failed: expected (2,5)"); } std::cout << "PASSED\n"; } void test_argmin_complex128() { std::cout << " Testing argmin() with complex128... "; numpy::NDArray arr({3}); arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0 arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1 - smallest arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2 auto result = numpy::argmin(arr); // Should return 1 (index of (1,5)) if (result.getElementAt({0}) != 1) { throw std::runtime_error("argmin() failed: expected index 1"); } .. _example-intervalarray-contains-8: .. dropdown:: contains (np_test_2_all.cpp:10521) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10511 :emphasize-lines: 11 auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz"); // Verify keys auto keys = npz_file.keys(); if (!(keys.size() == 3)) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("integers"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("floats"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("complex_nums"))) { .. _example-intervalarray-contains-9: .. dropdown:: contains (np_test_2_all.cpp:10521) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10511 :emphasize-lines: 11 auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz"); // Verify keys auto keys = npz_file.keys(); if (!(keys.size() == 3)) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("integers"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("floats"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("complex_nums"))) { .. _example-intervalarray-count-10: .. dropdown:: count (np_test_1_all.cpp:3616) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3606 :emphasize-lines: 11 // Create larger arrays for performance testing auto large_arr = NDArray::createOnes({100, 100}); auto broadcast_arr = NDArray::createOnes({1, 100}); // Time the operation (basic timing) auto start = std::chrono::high_resolution_clock::now(); auto result = large_arr.addArrays(broadcast_arr); auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(end - start); // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl; // Verify result shape if (!((result.getShape() == std::vector{100, 100}))) { std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector{100, 100}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(result.getElementAt({50, 50}) == 2.0)) { std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({50, 50}) == 2.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-intervalarray-dtype-11: .. dropdown:: dtype (np_test_3_all.cpp:7942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7932 :emphasize-lines: 11 if (info.max != expected_max) { std::cerr << "[FAIL] IInfo::max mismatch"; throw std::runtime_error("IInfo uint64 max test failed"); } // std::cout << "[OK] IInfo::max = " << info.max << std::endl; std::cout << " -> tests passed" << std::endl; } // ============================================================================ // dtype() Tests - DType descriptor creation // ============================================================================ void np_test_dtype_from_string_integers() { std::cout << "========= np_test_dtype_from_string_integers ======================="; // Test integer type strings if (dtype("int8") != DType::INT8) { std::cerr << "[FAIL] dtype(\"int8\") should return INT8"; throw std::runtime_error("dtype string test failed"); } .. _example-intervalarray-dtype-12: .. dropdown:: dtype (np_test_3_all.cpp:7942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7932 :emphasize-lines: 11 if (info.max != expected_max) { std::cerr << "[FAIL] IInfo::max mismatch"; throw std::runtime_error("IInfo uint64 max test failed"); } // std::cout << "[OK] IInfo::max = " << info.max << std::endl; std::cout << " -> tests passed" << std::endl; } // ============================================================================ // dtype() Tests - DType descriptor creation // ============================================================================ void np_test_dtype_from_string_integers() { std::cout << "========= np_test_dtype_from_string_integers ======================="; // Test integer type strings if (dtype("int8") != DType::INT8) { std::cerr << "[FAIL] dtype(\"int8\") should return INT8"; throw std::runtime_error("dtype string test failed"); } .. _example-intervalarray-left-13: .. dropdown:: left (np_test_1_all.cpp:21799) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21789 :emphasize-lines: 11 } void testRecFunctionsJoin() { std::cout << "========= testRecFunctionsJoin ======================="; auto dtype = std::make_shared(std::vector>{ {"key", DType::INT32}, { "data", DType::FLOAT64 } }); RecordArray left(dtype, { 3 }); left.setFieldValue({ 0 }, "key", static_cast(1)); left.setFieldValue({ 0 }, "data", static_cast(10.0)); left.setFieldValue({ 1 }, "key", static_cast(2)); left.setFieldValue({ 1 }, "data", static_cast(20.0)); left.setFieldValue({ 2 }, "key", static_cast(3)); left.setFieldValue({ 2 }, "data", static_cast(30.0)); auto dtype2 = std::make_shared(std::vector>{ {"key", DType::INT32}, { "info", DType::UNICODE32 } .. _example-intervalarray-length-14: .. dropdown:: length (np_test_1_all.cpp:7335) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7325 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void testStringTruncationStringTypes() { std::cout << "========= testStringTruncationStringTypes ======================="; // Test truncation behavior str8 small("This string is way too long for 8 characters"); // std::cout << "Long string in str8: '" << small << "'" << std::endl; // std::cout << "Length: " << small.length() << " (max: " << str8::max_size << ")" << std::endl; str16 medium("This is a medium length string that should be truncated"); // std::cout << "Medium string in str16: '" << medium << "'" << std::endl; // std::cout << "Length: " << medium.length() << " (max: " << str16::max_size << ")" << std::endl; std::cout << " -> tests passed" << std::endl; } void testStringComparisonsStringTypes() { std::cout << "========= testStringComparisonsStringTypes ======================="; .. _example-intervalarray-mask-15: .. dropdown:: mask (np_test_1_all.cpp:27691) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27681 :emphasize-lines: 11 void test_mask_rows_cols() { std::cout << "========= ma::mask_rows() and ma::mask_cols(): mask operations ==="; auto data = numpy::array({ {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }); auto mask = numpy::array({ {false, true, false}, {false, false, false} }); auto ma = numpy::ma::masked_array(data, mask); auto result_rows = numpy::ma::mask_rows(ma); auto result_cols = numpy::ma::mask_cols(ma); // std::cout << " ma::mask_rows(): row 0 fully masked: " << (result_rows.mask().getElementAt({ 0, 0 }) ? "true" : "false") << std::endl; // std::cout << " ma::mask_cols(): column 1 fully masked: " << (result_cols.mask().getElementAt({ 1, 1 }) ? "true" : "false"); std::cout << " -> tests passed"; } void test_compress_rowcols() { std::cout << "========= ma::compress_rowcols(): remove masked rows/columns ==="; auto data = numpy::array({ {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0} }); auto mask = numpy::array({ {false, false, false}, {false, true, false}, {false, false, false} }); auto ma = numpy::ma::masked_array(data, mask); .. _example-intervalarray-nbytes-16: .. dropdown:: nbytes (np_test_2_all.cpp:3547) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3537 :emphasize-lines: 11 } // Test itemsize if (!(itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test nbytes if (!(nbytes(arr) == 24 * sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test flags auto flags_info = flags(arr); if (!(flags_info.owndata == true)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.owndata == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-intervalarray-ndim-17: .. dropdown:: ndim (np_test_2_all.cpp:3526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3516 :emphasize-lines: 11 void testArrayInfo() { std::cout << "Testing array information functions...\n"; // Test basic info functions auto arr = createFloat64Array({ 2, 3, 4 }); for (size_t i = 0; i < arr.getSize(); ++i) { arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast(i)); } // Test ndim if (!(ndim(arr) == 3)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test size if (!(size(arr) == 24)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-intervalarray-right-18: .. dropdown:: right (np_test_1_all.cpp:21812) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21802 :emphasize-lines: 11 left.setFieldValue({ 1 }, "key", static_cast(2)); left.setFieldValue({ 1 }, "data", static_cast(20.0)); left.setFieldValue({ 2 }, "key", static_cast(3)); left.setFieldValue({ 2 }, "data", static_cast(30.0)); auto dtype2 = std::make_shared(std::vector>{ {"key", DType::INT32}, { "info", DType::UNICODE32 } }); RecordArray right(dtype2, { 3 }); right.setFieldValue({ 0 }, "key", static_cast(1)); right.setFieldValue({ 0 }, "info", std::string("A")); right.setFieldValue({ 1 }, "key", static_cast(2)); right.setFieldValue({ 1 }, "info", std::string("B")); right.setFieldValue({ 2 }, "key", static_cast(4)); right.setFieldValue({ 2 }, "info", std::string("D")); // std::cout << "Left array:" << std::endl; //left.printArray(); .. _example-intervalarray-shape-19: .. dropdown:: shape (np_test_1_all.cpp:3751) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3741 :emphasize-lines: 11 } for (size_t j = 0; j < 3; ++j) { arr3.setElementAt({0, j}, static_cast((j + 1) * 100)); // [100, 200, 300] } // Broadcast all arrays together std::vector> arrays = {arr1, arr2, arr3}; auto broadcasted = broadcast_arrays(arrays); // All should have shape (2, 3) for (const auto& arr : broadcasted) { if (!((arr.getShape() == std::vector{2, 3}))) { std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector{2, 3}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // Check values if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) { .. _example-intervalarray-size-20: .. 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-intervalarray-slice-21: .. dropdown:: slice (np_test_1_all.cpp:13009) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12999 :emphasize-lines: 11 // Test substr valid start but excessive length auto result3 = str1.substr(2, 10); // start=2, len=10 on string of length 5 if (!(result3.to_string() == "llo")) { std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n"; // Test slice method with out-of-bounds auto result4 = str1.slice(10, 15); // slice[10:15] on string of length 5 if (!(result4.to_string() == "")) { std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n"; // Test slice with negative indices auto result5 = str1.slice(-2, -1); // slice[-2:-1] should be "l" if (!(result5.to_string() == "l")) { .. _example-intervalarray-type-22: .. dropdown:: type (np_test_1_all.cpp:2240) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2230 :emphasize-lines: 11 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 1}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(multi_array.getElementAt({0, 2}).is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 2}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 4: Arrays with all same type (homogeneous) NDArray homo_array = createObjectZeros({5}); for (size_t i = 0; i < 5; ++i) { homo_array.setElementAt({i}, object_(static_cast(i * 10))); } // Verify all are same type for (size_t i = 0; i < 5; ++i) { object_ obj = homo_array.getElementAt({i}); if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())";