object_ ======= .. cpp:class:: numpy::object_ numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use object_ object_ obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``object\_(const T &value, typename std::enable_if::value,int>::type = 0)`` - NP_OBJECT_HOLDER.H:117 - :ref:`View ` * - ``object\_(const NDArray&arr)`` - NP_OBJECT_HOLDER.H:127 - :ref:`View ` * - ``object\_(const object\_ &other)`` - NP_OBJECT_HOLDER.H:136 - :ref:`View ` * - ``noexcept object\_(object\_ &&other)`` - NP_OBJECT_HOLDER.H:185 - :ref:`View ` Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``object\_ & operator=(const object\_ &other)`` - object\_ & - NP_OBJECT_HOLDER.H:159 - * - ``object\_ & noexcept operator=(object\_ &&other)`` - object\_ & noexcept - NP_OBJECT_HOLDER.H:214 - * - ``bool operator<(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:545 - * - ``bool operator<=(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:559 - * - ``bool operator>(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:563 - * - ``bool operator>=(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:567 - * - ``bool operator==(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:572 - * - ``bool operator!=(const object\_ &other)`` - bool - NP_OBJECT_HOLDER.H:579 - * - ``object\_ operator+(const object\_ &other)`` - object\_ - NP_OBJECT_HOLDER.H:590 - * - ``object\_ operator-(const object\_ &other)`` - object\_ - NP_OBJECT_HOLDER.H:620 - * - ``object\_ operator\*(const object\_ &other)`` - object\_ - NP_OBJECT_HOLDER.H:643 - * - ``object\_ operator/(const object\_ &other)`` - object\_ - NP_OBJECT_HOLDER.H:666 - * - ``object\_ operator-()`` - object\_ - NP_OBJECT_HOLDER.H:686 - * - ``object\_ & operator+=(const object\_ &other)`` - object\_ & - NP_OBJECT_HOLDER.H:697 - * - ``object\_ & operator-=(const object\_ &other)`` - object\_ & - NP_OBJECT_HOLDER.H:702 - * - ``object\_ & operator\*=(const object\_ &other)`` - object\_ & - NP_OBJECT_HOLDER.H:707 - * - ``object\_ & operator/=(const object\_ &other)`` - object\_ & - NP_OBJECT_HOLDER.H:712 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``double getAsDouble()`` - double - NP_OBJECT_HOLDER.H:719 - * - ``int64_t getAsInt64()`` - int64_t - NP_OBJECT_HOLDER.H:737 - * - ``T \* get_as()`` - T \* - NP_OBJECT_HOLDER.H:493 - :ref:`View ` * - ``const T \* get_as()`` - const T \* - NP_OBJECT_HOLDER.H:499 - :ref:`View ` Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``object\_ abs()`` - object\_ - NP_OBJECT_HOLDER.H:691 - :ref:`View ` I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string()`` - std::string - NP_OBJECT_HOLDER.H:539 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_array()`` - bool - NP_OBJECT_HOLDER.H:506 - :ref:`View ` * - ``bool is_floating()`` - bool - NP_OBJECT_HOLDER.H:586 - :ref:`View ` * - ``bool is_hashable()`` - bool - NP_OBJECT_HOLDER.H:523 - :ref:`View ` * - ``bool is_integral()`` - bool - NP_OBJECT_HOLDER.H:585 - :ref:`View ` * - ``bool is_null()`` - bool - NP_OBJECT_HOLDER.H:513 - :ref:`View ` * - ``bool is_numeric()`` - bool - NP_OBJECT_HOLDER.H:584 - :ref:`View ` * - ``bool is_sortable()`` - bool - NP_OBJECT_HOLDER.H:534 - :ref:`View ` * - ``bool is_string()`` - bool - NP_OBJECT_HOLDER.H:587 - :ref:`View ` * - ``bool is_type()`` - bool - NP_OBJECT_HOLDER.H:487 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t hash()`` - size_t - NP_OBJECT_HOLDER.H:528 - :ref:`View ` * - ``bool incrementIndices(std::vector&indices, const std::vector&shape)`` - bool - NP_OBJECT_HOLDER.H:475 - :ref:`View ` * - ``void setupArrayFunctions()`` - void - NP_OBJECT_HOLDER.H:411 - * - ``void setupFunctions()`` - void - NP_OBJECT_HOLDER.H:246 - * - ``std::string type_name()`` - std::string - NP_OBJECT_HOLDER.H:518 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-object_-object_-0: .. dropdown:: object_ (np_test_1_all.cpp:1469) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1459 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*assigned.get_as() == 42)) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*assigned.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test move constructor object_ moved(std::move(object_(100))); if (!(!moved.is_null())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!moved.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(moved.is_type())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(moved.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-object_-1: .. dropdown:: object_ (np_test_1_all.cpp:1469) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1459 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*assigned.get_as() == 42)) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*assigned.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test move constructor object_ moved(std::move(object_(100))); if (!(!moved.is_null())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!moved.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(moved.is_type())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(moved.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-object_-2: .. dropdown:: object_ (np_test_1_all.cpp:1469) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1459 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*assigned.get_as() == 42)) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*assigned.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test move constructor object_ moved(std::move(object_(100))); if (!(!moved.is_null())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!moved.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(moved.is_type())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(moved.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-object_-3: .. dropdown:: object_ (np_test_1_all.cpp:1469) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1459 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*assigned.get_as() == 42)) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*assigned.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test move constructor object_ moved(std::move(object_(100))); if (!(!moved.is_null())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!moved.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(moved.is_type())) { std::string description = std::string("testObjectCopyAndAssignment():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(moved.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-get_as-4: .. dropdown:: get_as (np_test_1_all.cpp:1363) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1353 :emphasize-lines: 11 if (!(!int_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!int_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(int_obj.is_type())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(int_obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*int_obj.get_as() == 42)) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*int_obj.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test string construction object_ str_obj(std::string("hello")); if (!(!str_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!str_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-object_-get_as-5: .. dropdown:: get_as (np_test_1_all.cpp:1363) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1353 :emphasize-lines: 11 if (!(!int_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!int_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(int_obj.is_type())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(int_obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*int_obj.get_as() == 42)) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*int_obj.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test string construction object_ str_obj(std::string("hello")); if (!(!str_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!str_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-object_-abs-6: .. dropdown:: abs (np_test_1_all.cpp:101) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 91 :emphasize-lines: 11 if (arr.getShape().size() == 1 && arr.getShape()[0] <= 10) { for (size_t i = 0; i < arr.getShape()[0]; ++i) { // std::cout << static_cast(arr.getElementAt({i})) << " "; } } // std::cout << std::endl; } // Helper function for core array extensions tests bool isApproxEqualExt(double a, double b, double tolerance = 1e-10) { return std::abs(a - b) < tolerance; } } void f_nothing() { // This function does nothing and tests nothing // It's a placeholder test function } // ------ merging np_test_advanced_indexing.cpp -- number of functions merged=16 -------------------------------- .. _example-object_-to_string-7: .. 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-object_-is_array-8: .. dropdown:: is_array (np_test_1_all.cpp:1395) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1385 :emphasize-lines: 11 } // Test NDArray construction NDArray arr({2, 2}, 3.14); object_ arr_obj(arr); if (!(!arr_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!arr_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(arr_obj.is_array())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(arr_obj.is_array())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(arr_obj.is_type>())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(arr_obj.is_type>())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-is_floating-9: .. dropdown:: is_floating (np_test_3_all.cpp:21656) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21646 :emphasize-lines: 11 CHECK(int_obj.is_numeric(), "np_test_object_arith_type_queries", "int should be numeric"); CHECK(dbl_obj.is_numeric(), "np_test_object_arith_type_queries", "dbl should be numeric"); CHECK(!str_obj.is_numeric(), "np_test_object_arith_type_queries", "str should not be numeric"); CHECK(!null_obj.is_numeric(), "np_test_object_arith_type_queries", "null should not be numeric"); CHECK(int_obj.is_integral(), "np_test_object_arith_type_queries", "int should be integral"); CHECK(!dbl_obj.is_integral(), "np_test_object_arith_type_queries", "dbl should not be integral"); CHECK(!str_obj.is_integral(), "np_test_object_arith_type_queries", "str should not be integral"); CHECK(!int_obj.is_floating(), "np_test_object_arith_type_queries", "int should not be floating"); CHECK(dbl_obj.is_floating(), "np_test_object_arith_type_queries", "dbl should be floating"); CHECK(!str_obj.is_floating(), "np_test_object_arith_type_queries", "str should not be floating"); CHECK(!int_obj.is_string(), "np_test_object_arith_type_queries", "int should not be string"); CHECK(!dbl_obj.is_string(), "np_test_object_arith_type_queries", "dbl should not be string"); CHECK(str_obj.is_string(), "np_test_object_arith_type_queries", "str should be string"); std::cout << " -> tests passed" << std::endl; } .. _example-object_-is_hashable-10: .. dropdown:: is_hashable (np_test_3_all.cpp:21836) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21826 :emphasize-lines: 11 } void np_test_object_mixed_hash_compare() { std::cout << "========= object_ hash and comparison ====" ; numpy::object_ a(10); numpy::object_ b(20); numpy::object_ c(10); // Hashable check CHECK(a.is_hashable(), "np_test_object_mixed_hash_compare", "a is_hashable failed"); CHECK(b.is_hashable(), "np_test_object_mixed_hash_compare", "b is_hashable failed"); // Hash values CHECK(a.hash() == c.hash(), "np_test_object_mixed_hash_compare", "hash equality failed"); CHECK(a.hash() != b.hash(), "np_test_object_mixed_hash_compare", "hash inequality failed"); // Sortable check CHECK(a.is_sortable(), "np_test_object_mixed_hash_compare", "a is_sortable failed"); CHECK(b.is_sortable(), "np_test_object_mixed_hash_compare", "b is_sortable failed"); .. _example-object_-is_integral-11: .. dropdown:: is_integral (np_test_3_all.cpp:21652) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21642 :emphasize-lines: 11 numpy::object_ int_obj(5); numpy::object_ dbl_obj(3.14); numpy::object_ str_obj(std::string("hello")); numpy::object_ null_obj; CHECK(int_obj.is_numeric(), "np_test_object_arith_type_queries", "int should be numeric"); CHECK(dbl_obj.is_numeric(), "np_test_object_arith_type_queries", "dbl should be numeric"); CHECK(!str_obj.is_numeric(), "np_test_object_arith_type_queries", "str should not be numeric"); CHECK(!null_obj.is_numeric(), "np_test_object_arith_type_queries", "null should not be numeric"); CHECK(int_obj.is_integral(), "np_test_object_arith_type_queries", "int should be integral"); CHECK(!dbl_obj.is_integral(), "np_test_object_arith_type_queries", "dbl should not be integral"); CHECK(!str_obj.is_integral(), "np_test_object_arith_type_queries", "str should not be integral"); CHECK(!int_obj.is_floating(), "np_test_object_arith_type_queries", "int should not be floating"); CHECK(dbl_obj.is_floating(), "np_test_object_arith_type_queries", "dbl should be floating"); CHECK(!str_obj.is_floating(), "np_test_object_arith_type_queries", "str should not be floating"); CHECK(!int_obj.is_string(), "np_test_object_arith_type_queries", "int should not be string"); CHECK(!dbl_obj.is_string(), "np_test_object_arith_type_queries", "dbl should not be string"); CHECK(str_obj.is_string(), "np_test_object_arith_type_queries", "str should be string"); .. _example-object_-is_null-12: .. dropdown:: is_null (np_test_1_all.cpp:1345) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1335 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // Object array test functions void testObjectConstruction() { std::cout << "========= testObjectConstruction ======================="; // Test default constructor object_ empty_obj; if (!(empty_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(empty_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test primitive type construction object_ int_obj(42); if (!(!int_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!int_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-object_-is_numeric-13: .. 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-object_-is_sortable-14: .. dropdown:: is_sortable (np_test_3_all.cpp:21844) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21834 :emphasize-lines: 11 // Hashable check CHECK(a.is_hashable(), "np_test_object_mixed_hash_compare", "a is_hashable failed"); CHECK(b.is_hashable(), "np_test_object_mixed_hash_compare", "b is_hashable failed"); // Hash values CHECK(a.hash() == c.hash(), "np_test_object_mixed_hash_compare", "hash equality failed"); CHECK(a.hash() != b.hash(), "np_test_object_mixed_hash_compare", "hash inequality failed"); // Sortable check CHECK(a.is_sortable(), "np_test_object_mixed_hash_compare", "a is_sortable failed"); CHECK(b.is_sortable(), "np_test_object_mixed_hash_compare", "b is_sortable failed"); // Comparison CHECK(a < b, "np_test_object_mixed_hash_compare", "a < b failed"); CHECK(b > a, "np_test_object_mixed_hash_compare", "b > a failed"); CHECK(a == c, "np_test_object_mixed_hash_compare", "a == c failed"); CHECK(a != b, "np_test_object_mixed_hash_compare", "a != b failed"); std::cout << " -> tests passed" << std::endl; } .. _example-object_-is_string-15: .. dropdown:: is_string (np_test_3_all.cpp:21660) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21650 :emphasize-lines: 11 CHECK(!null_obj.is_numeric(), "np_test_object_arith_type_queries", "null should not be numeric"); CHECK(int_obj.is_integral(), "np_test_object_arith_type_queries", "int should be integral"); CHECK(!dbl_obj.is_integral(), "np_test_object_arith_type_queries", "dbl should not be integral"); CHECK(!str_obj.is_integral(), "np_test_object_arith_type_queries", "str should not be integral"); CHECK(!int_obj.is_floating(), "np_test_object_arith_type_queries", "int should not be floating"); CHECK(dbl_obj.is_floating(), "np_test_object_arith_type_queries", "dbl should be floating"); CHECK(!str_obj.is_floating(), "np_test_object_arith_type_queries", "str should not be floating"); CHECK(!int_obj.is_string(), "np_test_object_arith_type_queries", "int should not be string"); CHECK(!dbl_obj.is_string(), "np_test_object_arith_type_queries", "dbl should not be string"); CHECK(str_obj.is_string(), "np_test_object_arith_type_queries", "str should be string"); std::cout << " -> tests passed" << std::endl; } void np_test_object_arith_promote_helper() { std::cout << "========= promoteType helper ====" ; numpy::object_ int_obj(5); .. _example-object_-is_type-16: .. dropdown:: is_type (np_test_1_all.cpp:1358) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1348 :emphasize-lines: 11 throw std::runtime_error(description); } // Test primitive type construction object_ int_obj(42); if (!(!int_obj.is_null())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!int_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(int_obj.is_type())) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(int_obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(*int_obj.get_as() == 42)) { std::string description = std::string("testObjectConstruction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(*int_obj.get_as() == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-object_-hash-17: .. dropdown:: hash (np_test_3_all.cpp:21840) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21830 :emphasize-lines: 11 numpy::object_ a(10); numpy::object_ b(20); numpy::object_ c(10); // Hashable check CHECK(a.is_hashable(), "np_test_object_mixed_hash_compare", "a is_hashable failed"); CHECK(b.is_hashable(), "np_test_object_mixed_hash_compare", "b is_hashable failed"); // Hash values CHECK(a.hash() == c.hash(), "np_test_object_mixed_hash_compare", "hash equality failed"); CHECK(a.hash() != b.hash(), "np_test_object_mixed_hash_compare", "hash inequality failed"); // Sortable check CHECK(a.is_sortable(), "np_test_object_mixed_hash_compare", "a is_sortable failed"); CHECK(b.is_sortable(), "np_test_object_mixed_hash_compare", "b is_sortable failed"); // Comparison CHECK(a < b, "np_test_object_mixed_hash_compare", "a < b failed"); CHECK(b > a, "np_test_object_mixed_hash_compare", "b > a failed"); CHECK(a == c, "np_test_object_mixed_hash_compare", "a == c failed"); .. _example-object_-incrementindices-18: .. dropdown:: incrementIndices (np_test_1_all.cpp:52) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 42 :emphasize-lines: 11 // 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); do { if (arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape()));