ScalarBroadcast =============== .. cpp:class:: numpy::ScalarBroadcast numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use ScalarBroadcast ScalarBroadcast obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``ScalarBroadcast(const T &scalar)`` - NP_VECTORIZE_BROADCAST.H:361 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const T & getElementAt(const std::vector&)`` - const T & - NP_VECTORIZE_BROADCAST.H:369 - :ref:`View ` * - ``size_t getSize()`` - size_t - NP_VECTORIZE_BROADCAST.H:372 - :ref:`View ` * - ``const T & getValue()`` - const T & - NP_VECTORIZE_BROADCAST.H:363 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-scalarbroadcast-getelementat-0: .. dropdown:: getElementAt (np_test_1_all.cpp:49) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 39 :emphasize-lines: 11 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); do { if (arr.getElementAt(indices)) { .. _example-scalarbroadcast-getsize-1: .. dropdown:: getSize (np_test_1_all.cpp:2172) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2162 :emphasize-lines: 11 } std::cout << " -> tests passed" << std::endl; } void testArrayEdgeCases() { std::cout << "========= testArrayEdgeCases ======================="; // Test 1: Empty arrays NDArray empty_array = createObjectZeros({0}); if (!(empty_array.getSize() == 0)) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(empty_array.getSize() == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(empty_array.getShape() == std::vector{0})) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(empty_array.getShape() == std::vector{0})"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-scalarbroadcast-getvalue-2: .. dropdown:: getValue (np_test_1_all.cpp:28799) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28789 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_datetime64_accessors() { std::cout << "========= datetime64 accessors: getValue/getUnit ======================="; auto dt = numpy::datetime64("2024-01-15"); // Test getValue and getUnit int64_t value = dt.getValue(); numpy::DateTimeUnit unit = dt.getUnit(); bool passed = (value != 0); if (!passed) { std::cout << " [FAIL] : in np_test_datetime64_accessors() : Accessor methods failed"; throw std::runtime_error("np_test_datetime64_accessors failed"); } std::cout << " -> tests passed" << std::endl;