ExtensionIndex ============== .. cpp:class:: numpy::ExtensionIndex numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use ExtensionIndex ExtensionIndex obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit ExtensionIndex(const ArrayType& array, const std::optional& name = std::nullopt, bool copy = false)`` - df_extension_index.h:335 - * - ``explicit ExtensionIndex(ArrayType&& array, const std::optional& name = std::nullopt)`` - df_extension_index.h:346 - * - ``ExtensionIndex(const ExtensionIndex& other)`` - df_extension_index.h:356 - * - ``ExtensionIndex(ExtensionIndex&& other) noexcept = default`` - df_extension_index.h:370 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool empty() const override`` - bool - df_extension_index.h:408 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray get_indexer( const ExtensionIndex& target, const std::string& method = "") const`` - numpy::NDArray - df_extension_index.h:674 - * - ``numpy::NDArray get_indexer_for( const std::vector& values) const`` - numpy::NDArray - df_extension_index.h:707 - * - ``std::variant> get_loc(const value_type& key) const`` - std::variant> - df_extension_index.h:654 - * - ``int64_t get_loc_str(const std::string& key_str) const override`` - int64_t - df_extension_index.h:510 - * - ``std::string get_value_str(size_t index) const override`` - std::string - df_extension_index.h:543 - * - ``oss << get_value_str(i)`` - oss << - df_extension_index.h:574 - * - ``ExtensionIndex take(const std::vector& indices) const`` - ExtensionIndex - df_extension_index.h:788 - :ref:`View ` * - ``ExtensionIndex take_impl(const std::vector& indices) const`` - ExtensionIndex - df_extension_index.h:1452 - Sorting ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray argsort(bool ascending = true) const`` - numpy::NDArray - df_extension_index.h:1279 - :ref:`View ` * - ``ExtensionIndex sort_values(bool ascending = true) const`` - ExtensionIndex - df_extension_index.h:1317 - Linear Algebra -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void invalidate_caches() const`` - void - df_extension_index.h:195 - Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool equals(const ExtensionIndex& other) const`` - bool - df_extension_index.h:1372 - Set Operations -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::NDArray isin(const std::vector& values) const`` - numpy::NDArray - df_extension_index.h:807 - :ref:`View ` * - ``ExtensionIndex unique() const`` - ExtensionIndex - df_extension_index.h:901 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> to_list() const`` - std::vector> - df_extension_index.h:625 - * - ``std::string to_string() const override`` - std::string - df_extension_index.h:567 - :ref:`View ` * - ``std::vector to_string_vector() const override`` - std::vector - df_extension_index.h:531 - Joining / Splitting ------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``ExtensionIndex append(const ExtensionIndex& other) const`` - ExtensionIndex - df_extension_index.h:1107 - :ref:`View ` * - ``ExtensionIndex delete\_(size_t loc) const`` - ExtensionIndex - df_extension_index.h:847 - :ref:`View ` * - ``ExtensionIndex delete\_(const std::vector& locs) const`` - ExtensionIndex - df_extension_index.h:865 - :ref:`View ` Type Handling ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``ExtensionIndex copy() const`` - ExtensionIndex - df_extension_index.h:885 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_monotonic_decreasing() const`` - bool - df_extension_index.h:1265 - * - ``bool is_monotonic_increasing() const`` - bool - df_extension_index.h:1255 - * - ``bool is_unique() const override`` - bool - df_extension_index.h:466 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const ArrayType& array() const`` - const ArrayType& - df_extension_index.h:618 - :ref:`View ` * - ``void build_hash_table() const`` - void - df_extension_index.h:208 - * - ``size_t cache_memory_usage() const override`` - size_t - df_extension_index.h:1431 - * - ``void clear_cache() const override`` - void - df_extension_index.h:1409 - * - ``std::unique_ptr clone() const override`` - std::unique_ptr - df_extension_index.h:560 - :ref:`View ` * - ``numpy::NDArray codes(std::vector`` - numpy::NDArray - df_extension_index.h:1069 - * - ``void compute_monotonicity() const`` - void - df_extension_index.h:280 - * - ``bool contains(const value_type& key) const`` - bool - df_extension_index.h:643 - :ref:`View ` * - ``bool contains_str(const std::string& key_str) const override`` - bool - df_extension_index.h:498 - * - ``value_type convert_from_string(const std::string& str) const`` - value_type - df_extension_index.h:235 - * - ``std::string convert_to_string(const value_type& val) const`` - std::string - df_extension_index.h:259 - * - ``ExtensionIndex difference(const ExtensionIndex& other, bool sort = true) const`` - ExtensionIndex - df_extension_index.h:1208 - :ref:`View ` * - ``ExtensionIndex drop(const std::vector& labels) const`` - ExtensionIndex - df_extension_index.h:826 - * - ``ExtensionIndex drop_duplicates(const std::string& keep = "first") const`` - ExtensionIndex - df_extension_index.h:925 - * - ``ExtensionIndex dropna() const`` - ExtensionIndex - df_extension_index.h:777 - * - ``std::string dtype_name() const override`` - std::string - df_extension_index.h:422 - :ref:`View ` * - ``numpy::NDArray duplicated(const std::string& keep = "first") const`` - numpy::NDArray - df_extension_index.h:974 - * - ``void ensure_hash_table() const`` - void - df_extension_index.h:226 - * - ``std::pair, ExtensionIndex> factorize() const`` - std::pair, ExtensionIndex> - df_extension_index.h:1066 - * - ``ExtensionIndex fillna(const value_type& value) const`` - ExtensionIndex - df_extension_index.h:769 - * - ``bool has_cached_values() const override`` - bool - df_extension_index.h:1418 - * - ``bool has_duplicates() const override`` - bool - df_extension_index.h:484 - * - ``bool hasnans() const`` - bool - df_extension_index.h:760 - * - ``bool identical(const ExtensionIndex& other) const`` - bool - df_extension_index.h:1395 - * - ``std::string inferred_type() const override`` - std::string - df_extension_index.h:443 - * - ``ExtensionIndex intersection(const ExtensionIndex& other, bool sort = false) const`` - ExtensionIndex - df_extension_index.h:1115 - * - ``numpy::NDArray isna() const`` - numpy::NDArray - df_extension_index.h:732 - * - ``numpy::NDArray isnull() const`` - numpy::NDArray - df_extension_index.h:739 - * - ``std::optional name() const override`` - std::optional - df_extension_index.h:429 - :ref:`View ` * - ``size_t nbytes() const override`` - size_t - df_extension_index.h:415 - :ref:`View ` * - ``ArrayType new_array(values)`` - ArrayType - df_extension_index.h:1470 - * - ``numpy::NDArray notna() const`` - numpy::NDArray - df_extension_index.h:746 - * - ``numpy::NDArray notnull() const`` - numpy::NDArray - df_extension_index.h:753 - * - ``size_t nunique(bool dropna = true) const`` - size_t - df_extension_index.h:1033 - * - ``ExtensionIndex rename(const std::optional& new_name) const`` - ExtensionIndex - df_extension_index.h:892 - * - ``std::string repr() const override`` - std::string - df_extension_index.h:593 - * - ``ExtensionIndex result(\*this)`` - ExtensionIndex - df_extension_index.h:893 - :ref:`View ` * - ``void set_name(const std::optional& name) override`` - void - df_extension_index.h:436 - * - ``size_t size() const override`` - size_t - df_extension_index.h:401 - :ref:`View ` * - ``ExtensionIndex symmetric_difference(const ExtensionIndex& other, bool sort = false) const`` - ExtensionIndex - df_extension_index.h:1236 - * - ``ExtensionIndex union\_(const ExtensionIndex& other, bool sort = false) const`` - ExtensionIndex - df_extension_index.h:1148 - * - ``std::pair, std::vector> value_counts( bool dropna = true) const`` - std::pair, std::vector> - df_extension_index.h:1047 - * - ``ArrayType values() const`` - ArrayType - df_extension_index.h:611 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-extensionindex-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-extensionindex-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-extensionindex-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-extensionindex-isin-3: .. dropdown:: isin (np_test_1_all.cpp:19434) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19424 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(membership.getElementAt({ 5 }) == false)) { std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(membership.getElementAt({ 5 }) == false)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Membership testing (in1d) works correctly\n"; // Test isin (alias for in1d) auto isin_result = isin(test_array, test_values); // Should be identical to in1d result for (size_t i = 0; i < membership.getSize(); ++i) { if (!(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))) { std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } .. _example-extensionindex-unique-4: .. 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-extensionindex-to_string-5: .. 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-extensionindex-append-6: .. dropdown:: append (np_test_2_all.cpp:7028) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7018 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(deleted_dups.getElementAt({ 2 }) == 3)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(deleted_dups.getElementAt({ 2 }) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Delete with duplicate indices works correctly\n"; // Test append auto appended = append(array, 100); if (!(appended.getSize() == array.getSize() + 1)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getSize() == array.getSize() + 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(appended.getElementAt({ appended.getSize() - 1 }) == 100)) { std::string description = std::string("testModificationOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(appended.getElementAt({ appended.getSize() - 1 }) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-extensionindex-delete_-7: .. dropdown:: delete_ (np_test_4_all.cpp:18411) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18401 :emphasize-lines: 11 } // ============================================================================ // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS // ============================================================================ void np_test_phase1_delete() { std::cout << "========= delete_: element deletion ===="; auto arr = numpy::array({10, 20, 30, 40, 50}); auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30) // Verify size reduced if (result.getSize() != 4) { std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n"; throw std::runtime_error("delete_ failed: wrong size"); } // Verify correct element was removed if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 || result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) { .. _example-extensionindex-delete_-8: .. dropdown:: delete_ (np_test_4_all.cpp:18411) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18401 :emphasize-lines: 11 } // ============================================================================ // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS // ============================================================================ void np_test_phase1_delete() { std::cout << "========= delete_: element deletion ===="; auto arr = numpy::array({10, 20, 30, 40, 50}); auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30) // Verify size reduced if (result.getSize() != 4) { std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n"; throw std::runtime_error("delete_ failed: wrong size"); } // Verify correct element was removed if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 || result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) { .. _example-extensionindex-copy-9: .. 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-extensionindex-is_unique-10: .. dropdown:: is_unique (np_test_1_all.cpp:19676) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19666 :emphasize-lines: 11 unique_array.setElementAt({ 1 }, 2); unique_array.setElementAt({ 2 }, 3); unique_array.setElementAt({ 3 }, 4); auto duplicate_array = createInt32Array({ 4 }, 0); duplicate_array.setElementAt({ 0 }, 1); duplicate_array.setElementAt({ 1 }, 2); duplicate_array.setElementAt({ 2 }, 2); // Duplicate duplicate_array.setElementAt({ 3 }, 4); if (!(set_ops::is_unique(unique_array) == true)) { std::string description = std::string("testUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(set_ops::is_unique(unique_array) == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(set_ops::is_unique(duplicate_array) == false)) { std::string description = std::string("testUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(set_ops::is_unique(duplicate_array) == false)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] is_unique function works correctly\n"; .. _example-extensionindex-array-11: .. dropdown:: array (np_test_1_all.cpp:243) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 233 :emphasize-lines: 11 // std::cout << "Min: " << array.minArray() << std::endl; // std::cout << "Max: " << array.maxArray() << std::endl; std::cout << " -> tests passed" << std::endl; } void testReshapeAndFlatten() { std::cout << "========= testReshapeAndFlatten ======================="; auto range_array = NDArray::createRange(0, 12, 1); // std::cout << "Range array (0-11):" << std::endl; //range_array.printArray(); auto reshaped = range_array.reshapeArray({3, 4}); // std::cout << "Reshaped to 3x4:"; //reshaped.printArray(); auto flattened = reshaped.flattenArray(); // std::cout << "Flattened:" << std::endl; //flattened.printArray(); .. _example-extensionindex-clone-12: .. dropdown:: clone (np_test_1_all.cpp:24942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24932 :emphasize-lines: 11 } // Test reproducibility numpy::random::PCG64DXSM rng2(11111); if (rng2.next_uint64() != val) { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } // Test clone auto cloned = rng.clone(); if (cloned->name() != "PCG64DXSM") { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PHILOX TESTS .. _example-extensionindex-contains-13: .. 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-extensionindex-difference-14: .. dropdown:: difference (np_test_1_all.cpp:19333) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19323 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(difference.getElementAt({ 2 }) == 5)) { std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(difference.getElementAt({ 2 }) == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Set difference works correctly\n"; // Test symmetric difference (XOR) auto xor_result = setxor1d(arr1, arr2); if (!(xor_result.getSize() == 4)) { std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(xor_result.getSize() == 4)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(xor_result.getElementAt({ 0 }) == 1)) { std::string description = std::string("testSetDifferenceOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(xor_result.getElementAt({ 0 }) == 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-extensionindex-dtype_name-15: .. dropdown:: dtype_name (np_test_2_all.cpp:3567) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3557 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(flags_info.writeable == true)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.writeable == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test dtype functions if (!(dtype_name(arr) == "float64")) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_name(arr) == \"float64\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(dtype_itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-extensionindex-name-16: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-extensionindex-nbytes-17: .. 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-extensionindex-result-18: .. dropdown:: result (np_test_1_all.cpp:6090) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6080 :emphasize-lines: 11 throw std::runtime_error(description); } // std::cout << "Timer restart test passed. Second: " << second_elapsed << " ms" << std::endl; std::cout << " -> tests passed" << std::endl; } void test_benchmark_result_storageBenchmarkSorting() { std::cout << "========= test_benchmark_result_storage ======================="; BenchmarkResult result("TestAlgorithm", 1000, 15.5, true, 4096); if (!(result.algorithm_name == "TestAlgorithm")) { std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.algorithm_name == \"TestAlgorithm\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(result.array_size == 1000)) { std::string description = std::string("test_benchmark_result_storageBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.array_size == 1000)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-extensionindex-size-19: .. 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-extensionindex-values-20: .. dropdown:: values (np_test_1_all.cpp:15133) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15123 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } else { std::cout << "[FAIL] Where function failed" << std::endl; errors++; } // Test 8: Clip function std::cout << "========= test_clip_function =======================" ; numpy::NDArray values({ 5 }); values.setElementAt({ 0 }, -2.0); values.setElementAt({ 1 }, -1.0); values.setElementAt({ 2 }, 0.5); values.setElementAt({ 3 }, 2.0); values.setElementAt({ 4 }, 5.0); double min_val = -1.0; double max_val = 3.0; auto clip_result = numpy::clip(values, min_val, max_val);