SparseDtype =========== .. cpp:class:: numpy::SparseDtype numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use SparseDtype SparseDtype obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - df_sparse_dtype.h:120 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_default_fill_value() const`` - bool - df_sparse_dtype.h:182 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static T default_fill_value()`` - static T - df_sparse_dtype.h:78 - :ref:`View ` * - ``T fill_value() const`` - T - df_sparse_dtype.h:136 - :ref:`View ` * - ``SparseDtype() : fill_value\_(default_fill_value())`` - SparseDtype() : - df_sparse_dtype.h:64 - * - ``explicit SparseDtype(T fill_value) : fill_value\_(fill_value)`` - explicit SparseDtype(T fill_value) : - df_sparse_dtype.h:70 - * - ``std::string kind() const override`` - std::string - df_sparse_dtype.h:128 - * - ``std::string name() const override`` - std::string - df_sparse_dtype.h:92 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - df_sparse_dtype.h:113 - * - ``std::string repr() const override`` - std::string - df_sparse_dtype.h:175 - * - ``std::string subtype_name() const`` - std::string - df_sparse_dtype.h:144 - * - ``const std::type_info& type() const override`` - const std::type_info& - df_sparse_dtype.h:151 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-sparsedtype-itemsize-0: .. dropdown:: itemsize (np_test_2_all.cpp:3540) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3530 :emphasize-lines: 11 } // 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); } // 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); .. _example-sparsedtype-default_fill_value-1: .. dropdown:: default_fill_value (np_test_1_all.cpp:27637) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 27627 :emphasize-lines: 11 // std::cout << " ma::isMA(regular_array): " << (numpy::ma::isMA(regular_array) ? "true" : "false") << " (expected: false)" << std::endl; std::cout << " -> tests passed" << std::endl; } void test_fill_value_functions() { std::cout << "========= Fill value query functions ==="; int int_val = 42; double double_val = 3.14; int default_int = numpy::ma::default_fill_value(int_val); double default_double = numpy::ma::default_fill_value(double_val); // std::cout << " ma::default_fill_value(int): " << default_int << " (expected: 999999)" << std::endl; // std::cout << " ma::default_fill_value(double): " << default_double << " (expected: 1e+20)" << std::endl; int max_int = numpy::ma::maximum_fill_value(int_val); double max_double = numpy::ma::maximum_fill_value(double_val); // std::cout << " ma::maximum_fill_value(int): " << max_int << " (expected: INT_MAX)" << std::endl; // std::cout << " ma::maximum_fill_value(double): " << (std::isinf(max_double) ? "inf" : "finite") << " (expected: inf)" << std::endl; .. _example-sparsedtype-fill_value-2: .. dropdown:: fill_value (np_test_4_all.cpp:20577) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20567 :emphasize-lines: 11 } // std::cout << "OK: masked_all_like(NDArray) test passed" << std::endl; } // Test with custom fill value { NDArray data({2, 2}); auto masked = ma::masked_all_like(data, 999); if (!(masked.fill_value() == 999)) { std::string description = std::string("np_test_phase6_ma_masked_all_like():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(masked.fill_value() == 999)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // All elements should have fill_value for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 2; ++j) { if (!(masked.data().getElementAt({i, j}) == 999)) { std::string description = std::string("np_test_phase6_ma_masked_all_like():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(masked.data().getElementAt({i, j}) == 999)"; .. _example-sparsedtype-name-3: .. 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-sparsedtype-type-4: .. 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())";