CategoricalIndex ================ .. cpp:class:: numpy::CategoricalIndex numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use CategoricalIndex CategoricalIndex obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``explicit CategoricalIndex(const CategoricalArray& array, const std::optional& name = std::nullopt)`` - df_categorical_index.h:61 - * - ``explicit CategoricalIndex(CategoricalArray&& array, const std::optional& name = std::nullopt)`` - df_categorical_index.h:71 - * - ``explicit CategoricalIndex(const std::vector>& values, const std::optional& name = std::nullopt, bool ordered = false)`` - df_categorical_index.h:82 - * - ``CategoricalIndex(const std::vector>& values, const std::vector& categories, bool ordered = false, const std::optional& name = std::nullopt)`` - df_categorical_index.h:95 - * - ``CategoricalIndex(const CategoricalIndex& other)`` - df_categorical_index.h:105 - * - ``CategoricalIndex(CategoricalIndex&& other) noexcept`` - df_categorical_index.h:112 - * - ``\* Format: CategoricalIndex(['a', 'b', 'a'], categories=['a', 'b'], ordered=False, name='x')`` - df_categorical_index.h:508 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static CategoricalIndex from_codes( const std::vector& codes, const std::vector& categories, bool ordered = false, const std::optional& name = std::nullopt)`` - static CategoricalIndex - df_categorical_index.h:152 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``get_indexer_non_unique(const CategoricalIndex& target) const`` - - df_categorical_index.h:924 - * - ``CategoricalIndex get_level_values(int level) const`` - CategoricalIndex - df_categorical_index.h:863 - * - ``CategoricalIndex get_level_values(const std::string& level_name) const`` - CategoricalIndex - df_categorical_index.h:879 - * - ``size_t get_slice_bound(const std::string& label, const std::string& side = "left") const`` - size_t - df_categorical_index.h:982 - * - ``std::string get_value_str(size_t index) const override`` - std::string - df_categorical_index.h:566 - * - ``std::string item() const`` - std::string - df_categorical_index.h:1137 - :ref:`View ` * - ``CategoricalIndex putmask(const numpy::NDArray& mask, const std::string& value) const`` - CategoricalIndex - df_categorical_index.h:1365 - :ref:`View ` * - ``size_t searchsorted(const std::string& value, const std::string& side = "left") const`` - size_t - df_categorical_index.h:1497 - :ref:`View ` * - ``CategoricalIndex where(const numpy::NDArray& cond, const std::string& other) const`` - CategoricalIndex - df_categorical_index.h:1706 - :ref:`View ` Shape Manipulation ------------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector> ravel() const`` - std::vector> - df_categorical_index.h:1393 - :ref:`View ` * - ``CategoricalIndex transpose() const`` - CategoricalIndex - df_categorical_index.h:1686 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::optional max() const`` - std::optional - df_categorical_index.h:413 - :ref:`View ` * - ``std::optional min() const`` - std::optional - df_categorical_index.h:398 - :ref:`View ` 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_categorical_index.h:1738 - :ref:`View ` * - ``CategoricalIndex sort(bool ascending = true) const`` - CategoricalIndex - df_categorical_index.h:1818 - :ref:`View ` * - ``CategoricalIndex sort_values(bool ascending = true) const`` - CategoricalIndex - df_categorical_index.h:1799 - * - ``std::pair> sortlevel( int level = 0, bool ascending = true) const`` - std::pair> - df_categorical_index.h:1619 - Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``CategoricalIndex add_categories(const std::vector& new_cats) const`` - CategoricalIndex - df_categorical_index.h:307 - * - ``CategoricalIndex round(int decimals = 0) const`` - CategoricalIndex - df_categorical_index.h:1483 - :ref:`View ` Comparison ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool equals(const CategoricalIndex& other) const`` - bool - df_categorical_index.h:590 - Logical ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool all() const`` - bool - df_categorical_index.h:624 - :ref:`View ` * - ``bool any() const`` - bool - df_categorical_index.h:640 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``CategoricalIndex to_flat_index() const`` - CategoricalIndex - df_categorical_index.h:1648 - * - ``std::vector to_numpy() const`` - std::vector - df_categorical_index.h:1657 - :ref:`View ` * - ``std::string to_string() const override`` - std::string - df_categorical_index.h:512 - :ref:`View ` * - ``std::vector> tolist() const`` - std::vector> - df_categorical_index.h:1677 - :ref:`View ` Joining / Splitting ------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``CategoricalIndex repeat(size_t repeats) const`` - CategoricalIndex - df_categorical_index.h:1459 - :ref:`View ` Type Handling ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::vector astype(const std::string& dtype = "object") const`` - std::vector - df_categorical_index.h:760 - :ref:`View ` * - ``CategoricalIndex copy() const`` - CategoricalIndex - df_categorical_index.h:472 - :ref:`View ` * - ``std::vector> view() const`` - std::vector> - df_categorical_index.h:1695 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is\_(const CategoricalIndex& other) const`` - bool - df_categorical_index.h:1128 - * - ``bool is_boolean() const`` - bool - df_categorical_index.h:1050 - * - ``bool is_categorical() const`` - bool - df_categorical_index.h:1062 - * - ``bool is_floating() const`` - bool - df_categorical_index.h:1070 - * - ``bool is_integer() const`` - bool - df_categorical_index.h:1078 - * - ``bool is_interval() const`` - bool - df_categorical_index.h:1086 - * - ``bool is_numeric() const`` - bool - df_categorical_index.h:1094 - :ref:`View ` * - ``bool is_object() const`` - bool - df_categorical_index.h:1102 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::optional argmax() const`` - std::optional - df_categorical_index.h:439 - :ref:`View ` * - ``std::optional argmin() const`` - std::optional - df_categorical_index.h:426 - :ref:`View ` * - ``CategoricalIndex as_ordered() const`` - CategoricalIndex - df_categorical_index.h:368 - * - ``CategoricalIndex as_unordered() const`` - CategoricalIndex - df_categorical_index.h:381 - * - ``std::optional asof(const std::string& where) const`` - std::optional - df_categorical_index.h:662 - * - ``numpy::NDArray asof_locs(const std::vector& where) const`` - numpy::NDArray - df_categorical_index.h:724 - * - ``const std::vector& categories() const`` - const std::vector& - df_categorical_index.h:201 - * - ``std::unique_ptr clone() const override`` - std::unique_ptr - df_categorical_index.h:501 - :ref:`View ` * - ``const numpy::NDArray& codes() const`` - const numpy::NDArray& - df_categorical_index.h:213 - * - ``\* For CategoricalIndex, shifts codes (values become NA at edges).`` - \* For CategoricalIndex, shifts - df_categorical_index.h:1543 - * - ``numpy::NDArray diff(int64_t periods = 1) const`` - numpy::NDArray - df_categorical_index.h:803 - :ref:`View ` * - ``CategoricalIndex droplevel(int level = 0) const`` - CategoricalIndex - df_categorical_index.h:847 - * - ``std::vector format(const std::string& formatter = "") const`` - std::vector - df_categorical_index.h:899 - :ref:`View ` * - ``std::unordered_map> groupby( const std::vector& values) const`` - std::unordered_map> - df_categorical_index.h:1019 - * - ``bool has_category(const std::string& category) const`` - bool - df_categorical_index.h:242 - * - ``bool holds_integer() const`` - bool - df_categorical_index.h:1042 - * - ``bool identical(const CategoricalIndex& other) const`` - bool - df_categorical_index.h:609 - * - ``numpy::NDArray indexer(std::vector`` - numpy::NDArray - df_categorical_index.h:1414 - * - ``numpy::NDArray indexer_arr(std::vector`` - numpy::NDArray - df_categorical_index.h:962 - * - ``CategoricalIndex infer_objects(bool copy_data = true) const`` - CategoricalIndex - df_categorical_index.h:1118 - * - ``std::string inferred_type() const override`` - std::string - df_categorical_index.h:493 - * - ``join(const CategoricalIndex& other, const std::string& how = "left", bool sort = false) const`` - - df_categorical_index.h:1161 - :ref:`View ` * - ``numpy::NDArray left_arr(std::vector`` - numpy::NDArray - df_categorical_index.h:1308 - * - ``CategoricalIndex map(const std::unordered_map& mapping) const`` - CategoricalIndex - df_categorical_index.h:459 - * - ``size_t memory_usage(bool deep = false) const`` - size_t - df_categorical_index.h:1333 - * - ``numpy::NDArray missing_arr(std::vector`` - numpy::NDArray - df_categorical_index.h:967 - * - ``size_t num_categories() const`` - size_t - df_categorical_index.h:233 - * - ``\* Ordered categoricals support comparison operations (< <= > >=)`` - \* Ordered categoricals support comparison - df_categorical_index.h:220 - :ref:`View ` * - ``\* operations (< <= > >=) and min/max will raise errors.`` - \* - df_categorical_index.h:377 - :ref:`View ` * - ``bool ordered() const`` - bool - df_categorical_index.h:225 - :ref:`View ` * - ``CategoricalIndex joined_index(joined_values, merged_cats, ordered(), this->name())`` - CategoricalIndex joined_index(joined_values, merged_cats, - df_categorical_index.h:1299 - :ref:`View ` * - ``std::pair> reindex( const CategoricalIndex& target, const std::string& method = "") const`` - std::pair> - df_categorical_index.h:1410 - * - ``CategoricalIndex remove_categories(const std::vector& removals) const`` - CategoricalIndex - df_categorical_index.h:321 - * - ``CategoricalIndex remove_unused_categories() const`` - CategoricalIndex - df_categorical_index.h:333 - * - ``CategoricalIndex rename(const std::optional& new_name) const`` - CategoricalIndex - df_categorical_index.h:481 - * - ``CategoricalIndex rename_categories(const std::vector& new_names) const`` - CategoricalIndex - df_categorical_index.h:261 - * - ``CategoricalIndex rename_categories( const std::unordered_map& mapping) const`` - CategoricalIndex - df_categorical_index.h:275 - * - ``CategoricalIndex reorder_categories(const std::vector& new_order) const`` - CategoricalIndex - df_categorical_index.h:292 - * - ``std::string repr() const override`` - std::string - df_categorical_index.h:557 - * - ``numpy::NDArray right_arr(std::vector`` - numpy::NDArray - df_categorical_index.h:1309 - * - ``CategoricalIndex set_categories(const std::vector& new_cats, bool rename = false) const`` - CategoricalIndex - df_categorical_index.h:349 - * - ``CategoricalIndex set_names(const std::optional& new_name) const`` - CategoricalIndex - df_categorical_index.h:1536 - * - ``CategoricalIndex shift(int64_t periods) const`` - CategoricalIndex - df_categorical_index.h:1548 - :ref:`View ` * - ``std::pair slice_indexer( const std::optional& start, const std::optional& stop) const`` - std::pair - df_categorical_index.h:1583 - * - ``std::pair slice_locs( const std::optional& start = std::nullopt, const std::optional& stop = std::nullopt) const`` - std::pair - df_categorical_index.h:1606 - * - ``\* matched by value (values not in new_cats become NA). If rename=true,`` - \* matched by - df_categorical_index.h:342 - :ref:`View ` Internal Methods ---------------- *2 internal methods (prefixed with underscore)* Code Examples ------------- The following examples are extracted from the test suite. .. _example-categoricalindex-item-0: .. dropdown:: item (np_test_4_all.cpp:20048) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20038 :emphasize-lines: 11 /** * @file np_test_phase6.cpp * @brief Test suite for Phase 6A - HIGH PRIORITY NDArray methods and masked array utilities * * Tests: * - ndarray.nonzero() - Return indices of non-zero elements * - ndarray.tobytes() - Convert to bytes representation * - ndarray.tolist() - Convert to nested list string * - ndarray.fill() - Fill with scalar value (in-place) * - ndarray.item() - Get single element as scalar * - ndarray.itemset() - Set single element value * - ma.masked_all_like() - Create masked array with all elements masked * * Created: 2025-10-28 * Status: Phase 6A Implementation - HIGH PRIORITY functions */ #include "../numpy/np_ndarray.h" #include "../numpy/np_masked_array.h" #include .. _example-categoricalindex-putmask-1: .. dropdown:: putmask (np_test_5_all.cpp:4659) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4649 :emphasize-lines: 11 mask.setElementAt({ 1 }, false); mask.setElementAt({ 2 }, true); mask.setElementAt({ 3 }, false); mask.setElementAt({ 4 }, true); // Create value array numpy::NDArray values({ 1 }); values.setElementAt({ 0 }, 99); // Apply putmask numpy::putmask(arr, mask, values); // Expected: [99, 2, 99, 4, 99] if (arr.getElementAt({ 0 }) != 99 || arr.getElementAt({ 1 }) != 2 || arr.getElementAt({ 2 }) != 99 || arr.getElementAt({ 3 }) != 4 || arr.getElementAt({ 4 }) != 99) { std::cout << " [FAIL] : in np_test_putmask() : Incorrect result"; throw std::runtime_error("Test failed"); } // Test with multiple values .. _example-categoricalindex-searchsorted-2: .. dropdown:: searchsorted (np_test_2_all.cpp:8204) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8194 :emphasize-lines: 11 // Test missing NumPy functions void test_numpy_functions() { std::cout << "========= NumPy Functions Test =========================="; // Test searchsorted { std::vector sorted_arr = { 1, 3, 5, 7, 9 }; std::vector values = { 2, 6, 8 }; auto result = numpy::searchsorted(sorted_arr, values, "left"); std::vector expected = { 1, 3, 4 }; if (result != expected) { std::cout << " searchsorted (left): -> [FAIL]"; throw std::runtime_error("searchsorted (left): FAILED - incorrect result indices"); } // std::cout << "[OK] searchsorted (left): PASSED" << std::endl; auto result_right = numpy::searchsorted(sorted_arr, values, "right"); std::vector expected_right = { 1, 3, 4 }; .. _example-categoricalindex-where-3: .. dropdown:: where (np_test_3_all.cpp:15253) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15243 :emphasize-lines: 11 auto y_data = createInt32Array({ 4 }, 0); y_data.setElementAt({ 0 }, 100); y_data.setElementAt({ 1 }, 200); y_data.setElementAt({ 2 }, 300); y_data.setElementAt({ 3 }, 400); auto y_mask = createBoolArray({ 4 }, false); MaskedArray y(y_data, y_mask, -999); // Test where() selection auto result = where(condition, x, y); if (!(result.getElementAt({ 0 }) == 10)) { std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 0 }) == 10)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(result.getElementAt({ 1 }) == 200)) { std::string description = std::string("test_where_selection():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({ 1 }) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-categoricalindex-ravel-4: .. dropdown:: ravel (np_test_1_all.cpp:4082) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4072 :emphasize-lines: 11 std::cout << "Testing ravel function...\n"; // Test with 2-D array auto arr_2d = createFloat64Array({ 2, 3 }); for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 3; ++j) { arr_2d.setElementAt({ i, j }, static_cast(i * 3 + j)); } } auto raveled = ravel(arr_2d); if (!(raveled.getShape() == std::vector({ 6 }))) { std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(raveled.getShape() == std::vector({ 6 }))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 6; ++i) { if (!(isApproxEqualExt(raveled.getElementAt({ i }), static_cast(i)))) { std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualExt(raveled.getElementAt({ i }), static_cast(i)))"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-categoricalindex-transpose-5: .. 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-categoricalindex-max-6: .. dropdown:: max (np_test_1_all.cpp:7274) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7264 :emphasize-lines: 11 if (sizeof(uintp) == sizeof(void*)) { // std::cout << " -> uintp size matches pointer size"; } else { // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl; } // Test range limits // std::cout << "Range Information:" << std::endl; // std::cout << " intp min: " << std::numeric_limits::min() << std::endl; // std::cout << " intp max: " << std::numeric_limits::max() << std::endl; // std::cout << " uintp max: " << std::numeric_limits::max() << std::endl; // std::cout << " longdouble digits: " << std::numeric_limits::digits << std::endl; std::cout << " -> tests passed" << std::endl; } void testComplexArithmeticExtendedTypes() { std::cout << "========= testComplexArithmeticExtendedTypes ======================="; clongdouble c1(3.0L, 4.0L); // 3 + 4i .. _example-categoricalindex-min-7: .. dropdown:: min (np_test_1_all.cpp:2350) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2340 :emphasize-lines: 11 if (i % 3 == 0) { large_array.setElementAt({i}, object_(static_cast(i))); } else if (i % 3 == 1) { large_array.setElementAt({i}, object_(static_cast(i) * 0.5)); } else { large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i))); } } // Verify pattern for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100 object_ obj = large_array.getElementAt({i}); if (i % 3 == 0) { if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } else if (i % 3 == 1) { if (!(obj.is_type())) { std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; .. _example-categoricalindex-argsort-8: .. 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-categoricalindex-sort-9: .. dropdown:: sort (np_test_1_all.cpp:6258) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6248 :emphasize-lines: 11 std::vector data = gen.generate(100, DataPattern::FEW_UNIQUE); 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); } .. _example-categoricalindex-round-10: .. dropdown:: round (np_test_1_all.cpp:23769) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23759 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(power_scalar_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(power_scalar_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Power functions have correct signatures\n"; // Test rounding functions with new decimals parameter auto round_result = round(array); // Default decimals=0 auto round_decimals_result = round(array, 2); // With decimals if (!(round_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(round_decimals_result.getShape() == array.getShape())) { std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_decimals_result.getShape() == array.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-categoricalindex-all-11: .. dropdown:: all (np_test_4_all.cpp:23928) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23918 :emphasize-lines: 11 } } } auto result = numpy::einsum("ijk->ik", {A}); if (result.getShape()[0] != 2 || result.getShape()[1] != 2) { throw std::runtime_error("einsum partial sum shape wrong"); } // Sum over j: 1+2+3 = 6 for all (i,k) positions if (std::abs(result.getElementAt({0, 0}) - 6.0) > 1e-10 || std::abs(result.getElementAt({1, 1}) - 6.0) > 1e-10) { throw std::runtime_error("einsum partial sum values wrong"); } // std::cout << " OK: Partial sum: 'ijk->ik'\n"; } // Test 5: Size-1 dimension handling { .. _example-categoricalindex-any-12: .. dropdown:: any (np_test_2_all.cpp:16758) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 16748 :emphasize-lines: 11 // ANY() TESTS - SCALAR RESULT // ============================================================================ void np_test_logic_any_scalar_all_false() { std::cout << "========= any: all false elements ======================="; // Create array with all false/zero elements std::vector data = { 0.0, 0.0, 0.0 }; numpy::NDArray arr = numpy::createArrayFromVector({ 3 }, data); bool result = numpy::any(arr); if (result != false) { std::cout << " [FAIL] : in np_test_logic_any_scalar_all_false() : expected false for all-zero array"; throw std::runtime_error("np_test_logic_any_scalar_all_false failed: expected false"); } std::cout << " -> tests passed" << std::endl; } void np_test_logic_any_scalar_all_true() { .. _example-categoricalindex-to_numpy-13: .. dropdown:: to_numpy (np_test_5_all.cpp:21373) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21363 :emphasize-lines: 11 if (errors == 0) { std::cout << "np_test_timedelta_components -> tests passed" << std::endl; } return errors; } // ============================================================================= // Test 4: Total Conversion Properties // ============================================================================= int np_test_timedelta_total_conversions() { int errors = 0; numpy::Timedelta td(1, 12, 0); // 1 day 12 hours = 1.5 days = 36 hours // total_seconds { double secs = td.total_seconds(); double expected = (24 + 12) * 3600.0; if (std::abs(secs - expected) > 0.0001) { std::cout << "[FAIL] np_test_timedelta_total_conversions: total_seconds expected " << expected << ", got " << secs << std::endl; .. _example-categoricalindex-to_string-14: .. 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-categoricalindex-tolist-15: .. dropdown:: tolist (np_test_4_all.cpp:20046) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20036 :emphasize-lines: 11 } // namespace numpy_tests /** * @file np_test_phase6.cpp * @brief Test suite for Phase 6A - HIGH PRIORITY NDArray methods and masked array utilities * * Tests: * - ndarray.nonzero() - Return indices of non-zero elements * - ndarray.tobytes() - Convert to bytes representation * - ndarray.tolist() - Convert to nested list string * - ndarray.fill() - Fill with scalar value (in-place) * - ndarray.item() - Get single element as scalar * - ndarray.itemset() - Set single element value * - ma.masked_all_like() - Create masked array with all elements masked * * Created: 2025-10-28 * Status: Phase 6A Implementation - HIGH PRIORITY functions */ #include "../numpy/np_ndarray.h" .. _example-categoricalindex-repeat-16: .. dropdown:: repeat (np_test_1_all.cpp:18268) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18258 :emphasize-lines: 11 // Create test array auto array = createInt32Array({ 2, 3 }, 0); for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 3; ++j) { array.setElementAt({ i, j }, static_cast(i * 3 + j + 1)); } } // Test repeat without axis (flattened) auto repeated_flat = repeat(array, 2); if (!(repeated_flat.getShape()[0] == 12)) { std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getShape()[0] == 12)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(repeated_flat.getElementAt({ 0 }) == 1)) { std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getElementAt({ 0 }) == 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-categoricalindex-astype-17: .. dropdown:: astype (np_test_3_all.cpp:796) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 786 :emphasize-lines: 11 auto ufunc_square = numpy::frompyfunc(square, 1, 1); numpy::NDArray arr({ 3 }); arr.setElementAt({ 0 }, 2.0); arr.setElementAt({ 1 }, 3.0); arr.setElementAt({ 2 }, 4.0); auto result = ufunc_square(arr); // Convert back to double to check values auto typed_result = numpy::astype(result); if (std::abs(typed_result.getElementAt({ 0 }) - 4.0) > 1e-10 || std::abs(typed_result.getElementAt({ 1 }) - 9.0) > 1e-10 || std::abs(typed_result.getElementAt({ 2 }) - 16.0) > 1e-10) { std::cout << "[FAIL] Basic frompyfunc creation failed"; return 1; } // std::cout << "[OK] Basic frompyfunc creation works correctly" << std::endl; std::cout << " -> tests passed" << std::endl; return 0; .. _example-categoricalindex-copy-18: .. 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-categoricalindex-view-19: .. dropdown:: view (np_test_1_all.cpp:440) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 430 :emphasize-lines: 11 for (size_t i = 0; i < 3; ++i) { for (size_t j = 0; j < 3; ++j) { original.setElementAt({i, j}, static_cast(i * 3 + j)); } } // std::cout << "Original array:" << std::endl; //original.printArray(); // Create multiple views auto view1 = original.view(); auto view2 = original.transposeView(); auto view3 = original.sliceView({{1, 3}, {1, 3}}); // 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(); .. _example-categoricalindex-is_numeric-20: .. dropdown:: is_numeric (np_test_1_all.cpp:7511) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7501 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void testNumericConversionStringTypes() { std::cout << "========= testNumericStringOperationsStringTypes ======================="; str32 num_str1("123.45"); str32 num_str2("67.89"); str32 not_num("hello"); // std::cout << "String '" << num_str1 << "' is numeric: " << is_numeric(num_str1) << std::endl; // std::cout << "String '" << not_num << "' is numeric: " << is_numeric(not_num) << std::endl; if (is_numeric(num_str1)) { double val1 = to_numeric(num_str1); // std::cout << "Numeric value of '" << num_str1 << "': " << val1 << std::endl; } if (is_numeric(num_str2)) { double val2 = to_numeric(num_str2); // std::cout << "Numeric value of '" << num_str2 << "': " << val2 << std::endl; .. _example-categoricalindex-argmax-21: .. 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-categoricalindex-argmin-22: .. 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-categoricalindex-clone-23: .. 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-categoricalindex-diff-24: .. dropdown:: diff (np_test_2_all.cpp:4947) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4937 :emphasize-lines: 11 // Test matrix addition and subtraction numpy::Matrix a("1 2; 3 4"); numpy::Matrix b("5 6; 7 8"); auto sum = a + b; assert_test(std::abs(sum(0, 0) - 6.0) < 1e-10, "Matrix addition"); assert_test(std::abs(sum(1, 1) - 12.0) < 1e-10, "Matrix addition"); auto diff = b - a; assert_test(std::abs(diff(0, 0) - 4.0) < 1e-10, "Matrix subtraction"); assert_test(std::abs(diff(1, 1) - 4.0) < 1e-10, "Matrix subtraction"); // Test matrix multiplication (not element-wise!) auto product = a * b; assert_test(std::abs(product(0, 0) - 19.0) < 1e-10, "Matrix multiplication 0,0"); assert_test(std::abs(product(0, 1) - 22.0) < 1e-10, "Matrix multiplication 0,1"); assert_test(std::abs(product(1, 0) - 43.0) < 1e-10, "Matrix multiplication 1,0"); assert_test(std::abs(product(1, 1) - 50.0) < 1e-10, "Matrix multiplication 1,1"); // Test scalar multiplication .. _example-categoricalindex-format-25: .. dropdown:: format (main.cpp:22) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 12 :emphasize-lines: 11 int main() { // Automatically log all output to temp/test_output.log numpy::TestLogger logger("temp/test_output.log"); int res = 0; int res1 = 0; std::string resS = ""; // call all the tests res1 = numpy_tests::np_test_main(); resS += std::format(" np_test_main: {} errors\n", res1); res += res1; std::cout << "\n------------------------- main --------------------------------------------\n"; std::cout << std::endl << "All tests completed. Nb errors = " << res << std::endl; std::cout << "Details: \n" << resS; std::cout << "\n---------------------------------------------------------------------------\n"; return res; } .. _example-categoricalindex-join-26: .. dropdown:: join (np_test_5_all.cpp:1341) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1331 :emphasize-lines: 11 // ============================================================================ void np_test_string_join() { std::cout << "========= join operation ======================="; // Join takes a separator string and vector of CharArrays std::vector> arrays; arrays.push_back(numpy::char_::array<32>({ "a", "b", "c" })); arrays.push_back(numpy::char_::array<32>({ "x", "y", "z" })); auto result = numpy::char_::join("-", arrays); bool passed = true; if (!passed) { std::cout << " [FAIL] : in np_test_string_join() : join operation failed"; throw std::runtime_error("np_test_string_join failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-categoricalindex-operations-27: .. dropdown:: operations (np_test_1_all.cpp:26345) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26335 :emphasize-lines: 11 {"value", numpy::DType::FLOAT64} }; auto dtype = std::make_shared(fields); numpy::RecordArray arr(dtype, { 2 }); arr.setFieldValue({ 0 }, "id", numpy::int32(1)); arr.setFieldValue({ 0 }, "value", numpy::float64(10.5)); arr.setFieldValue({ 1 }, "id", numpy::int32(2)); arr.setFieldValue({ 1 }, "value", numpy::float64(20.5)); // Test basic comparison operations (indirectly tests createComparisonValue) auto rec0 = arr.getRecord({ 0 }); auto rec1 = arr.getRecord({ 1 }); // Verify records are different (basic comparison test) bool passed = true; // If we can get records without error, comparison values work if (!passed) { std::cout << " [FAIL] : in np_test_recarray_createComparisonValue() : Create comparison value failed"; throw std::runtime_error("np_test_recarray_createComparisonValue failed"); } .. _example-categoricalindex-operations-28: .. dropdown:: operations (np_test_1_all.cpp:26345) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26335 :emphasize-lines: 11 {"value", numpy::DType::FLOAT64} }; auto dtype = std::make_shared(fields); numpy::RecordArray arr(dtype, { 2 }); arr.setFieldValue({ 0 }, "id", numpy::int32(1)); arr.setFieldValue({ 0 }, "value", numpy::float64(10.5)); arr.setFieldValue({ 1 }, "id", numpy::int32(2)); arr.setFieldValue({ 1 }, "value", numpy::float64(20.5)); // Test basic comparison operations (indirectly tests createComparisonValue) auto rec0 = arr.getRecord({ 0 }); auto rec1 = arr.getRecord({ 1 }); // Verify records are different (basic comparison test) bool passed = true; // If we can get records without error, comparison values work if (!passed) { std::cout << " [FAIL] : in np_test_recarray_createComparisonValue() : Create comparison value failed"; throw std::runtime_error("np_test_recarray_createComparisonValue failed"); } .. _example-categoricalindex-ordered-29: .. dropdown:: ordered (np_test_2_all.cpp:8640) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8630 :emphasize-lines: 11 if (indices.size() != n) { std::cout << " IPP large lexsort: -> [FAIL]"; throw std::runtime_error("IPP large lexsort: FAILED - incorrect result size"); } // Verify sorting correctness for (size_t i = 1; i < indices.size(); ++i) { size_t prev_idx = indices[i - 1]; size_t curr_idx = indices[i]; // Check if correctly ordered (second key primary) if (keys[1][curr_idx] < keys[1][prev_idx] || (keys[1][curr_idx] == keys[1][prev_idx] && keys[0][curr_idx] < keys[0][prev_idx])) { std::cout << " IPP large lexsort order: -> [FAIL]"; throw std::runtime_error("IPP large lexsort: FAILED - incorrect ordering"); } } auto duration_us = std::chrono::duration_cast(end - start).count(); // std::cout << "[OK] IPP large lexsort (" << n << " elements): PASSED in " << duration_us << " μs" << std::endl; } .. _example-categoricalindex-ordered-30: .. dropdown:: ordered (np_test_2_all.cpp:8640) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8630 :emphasize-lines: 11 if (indices.size() != n) { std::cout << " IPP large lexsort: -> [FAIL]"; throw std::runtime_error("IPP large lexsort: FAILED - incorrect result size"); } // Verify sorting correctness for (size_t i = 1; i < indices.size(); ++i) { size_t prev_idx = indices[i - 1]; size_t curr_idx = indices[i]; // Check if correctly ordered (second key primary) if (keys[1][curr_idx] < keys[1][prev_idx] || (keys[1][curr_idx] == keys[1][prev_idx] && keys[0][curr_idx] < keys[0][prev_idx])) { std::cout << " IPP large lexsort order: -> [FAIL]"; throw std::runtime_error("IPP large lexsort: FAILED - incorrect ordering"); } } auto duration_us = std::chrono::duration_cast(end - start).count(); // std::cout << "[OK] IPP large lexsort (" << n << " elements): PASSED in " << duration_us << " μs" << std::endl; } .. _example-categoricalindex-shift-31: .. dropdown:: shift (np_test_1_all.cpp:2761) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2751 :emphasize-lines: 11 throw std::runtime_error(description); } if (!(left_array.getElementAt({3}) == 16384)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(left_array.getElementAt({3}) == 16384)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Left shift by array\n"; // Test excessive shift (should result in 0) auto excessive_left = left_shift(arr, 20); // More than 16 bits if (!(excessive_left.getElementAt({0}) == 0)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({0}) == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(excessive_left.getElementAt({1}) == 0)) { std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({1}) == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-categoricalindex-value-32: .. dropdown:: value (np_test_1_all.cpp:7786) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7776 :emphasize-lines: 11 } void testFloat16PrecisionFloat16() { std::cout << "========= testFloat16PrecisionFloat16 ======================="; // Test precision limits float16 small_val(0.0001f); float16 large_val(30000.0f); float16 very_small(1e-8f); // This might underflow to zero // std::cout << "Small value (0.0001): " << small_val << std::endl; // std::cout << "Large value (30000): " << large_val << std::endl; // std::cout << "Very small value (1e-8): " << very_small << std::endl; // Test conversion accuracy float original = 1.234f; float16 converted(original); float back_converted = static_cast(converted); // std::cout << "Original float: " << std::setprecision(10) << original << std::endl; // std::cout << "Float16 version: " << converted << std::endl;