ScalarBroadcast#

class numpy::ScalarBroadcast#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use ScalarBroadcast
ScalarBroadcast obj;
// ... operations ...

Constructors#

Signature

Location

Example

ScalarBroadcast(const T &scalar)

NP_VECTORIZE_BROADCAST.H:361

Indexing / Selection#

Signature

Return Type

Location

Example

const T & getElementAt(const std::vector<size_t>&)

const T &

NP_VECTORIZE_BROADCAST.H:369

View

size_t getSize()

size_t

NP_VECTORIZE_BROADCAST.H:372

View

const T & getValue()

const T &

NP_VECTORIZE_BROADCAST.H:363

View

Code Examples#

The following examples are extracted from the test suite.

getElementAt (np_test_1_all.cpp:49)
39using namespace numpy::benchmark;
40using namespace numpy::char_;
41
42// Helper functions for array comparison tests
43namespace {
44    const double TOLERANCE = 1e-10;
45
46    bool allTrue(const NDArray<bool>& arr) {
47        std::vector<size_t> indices(arr.getShape().size(), 0);
48        do {
49            if (!arr.getElementAt(indices)) {
50                return false;
51            }
52        } while (incrementIndices(indices, arr.getShape()));
53        return true;
54    }
55
56    bool allFalse(const NDArray<bool>& arr) {
57        std::vector<size_t> indices(arr.getShape().size(), 0);
58        do {
59            if (arr.getElementAt(indices)) {
getSize (np_test_1_all.cpp:2172)
2162    }
2163
2164    std::cout << " -> tests passed" << std::endl;
2165}
2166
2167void testArrayEdgeCases() {
2168    std::cout << "========= testArrayEdgeCases =======================";
2169
2170    // Test 1: Empty arrays
2171    NDArray<object_> empty_array = createObjectZeros({0});
2172    if (!(empty_array.getSize() == 0)) {
2173        std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(empty_array.getSize() == 0)";
2174        std::cout << std::string("[FAIL] ") + description << std::endl;
2175        throw std::runtime_error(description);
2176    }
2177    if (!(empty_array.getShape() == std::vector<size_t>{0})) {
2178        std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(empty_array.getShape() == std::vector<size_t>{0})";
2179        std::cout << std::string("[FAIL] ") + description << std::endl;
2180        throw std::runtime_error(description);
2181    }
getValue (np_test_1_all.cpp:28799)
28789      std::cout << " -> tests passed" << std::endl;
28790    }
28791
28792    void np_test_datetime64_accessors() {
28793      std::cout << "========= datetime64 accessors: getValue/getUnit =======================";
28794
28795      auto dt = numpy::datetime64("2024-01-15");
28796
28797      // Test getValue and getUnit
28798      int64_t value = dt.getValue();
28799      numpy::DateTimeUnit unit = dt.getUnit();
28800
28801      bool passed = (value != 0);
28802
28803      if (!passed) {
28804        std::cout << "  [FAIL] : in np_test_datetime64_accessors() : Accessor methods failed";
28805        throw std::runtime_error("np_test_datetime64_accessors failed");
28806      }
28807
28808      std::cout << " -> tests passed" << std::endl;