NDIter#

class numpy::NDIter#

numpy C++ class.

Example#

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

// Use NDIter
NDIter obj;
// ... operations ...

Constructors#

Signature

Location

Example

NDIter(const NDArray<T>&array, boolc_order = true)

NP_NDITER.H:40

NDIter(const NDArray<T>&array, const std::vector<int>&order)

NP_NDITER.H:62

Operators#

Signature

Return Type

Location

Example

value_type operator\*()

value_type

NP_NDITER.H:86

NDIter & operator++()

NDIter &

NP_NDITER.H:97

NDIter operator++(int)

NDIter

NP_NDITER.H:121

bool operator==(const NDIter &other)

bool

NP_NDITER.H:128

bool operator!=(const NDIter &other)

bool

NP_NDITER.H:139

Type Checking#

Signature

Return Type

Location

Example

bool is_finished()

bool

NP_NDITER.H:144

View

Other Methods#

Signature

Return Type

Location

Example

NDIter end(const NDArray<T>&array)

NDIter

NP_NDITER.H:177

View

size_t flat_index()

size_t

NP_NDITER.H:154

View

const std::vector<size_t>& indices()

const std::vector<size_t>&

NP_NDITER.H:149

View

void reset()

void

NP_NDITER.H:171

View

Code Examples#

The following examples are extracted from the test suite.

is_finished (np_test_2_all.cpp:3824)
3814      for (size_t i = 0; i < 2; ++i) {
3815        for (size_t j = 0; j < 3; ++j) {
3816          arr.setElementAt({ i, j }, static_cast<double>(i * 3 + j));
3817        }
3818      }
3819
3820      auto iter = nditer(arr);
3821      size_t count = 0;
3822      double expected_values[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
3823
3824      for (; !iter.is_finished(); ++iter, ++count) {
3825        if (!(isApproxEqualMCO(*iter, expected_values[count]))) {
3826            std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(*iter, expected_values[count]))";
3827            std::cout << std::string("[FAIL] ") + description << std::endl;
3828            throw std::runtime_error(description);
3829        }
3830      }
3831      if (!(count == 6)) {
3832          std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(count == 6)";
3833          std::cout << std::string("[FAIL] ") + description << std::endl;
3834          throw std::runtime_error(description);
end (np_test_1_all.cpp:6171)
6161    // Test sorted data generation
6162    std::vector<int> data = gen.generate(50, DataPattern::SORTED);
6163
6164    if (!(data.size() == 50)) {
6165        std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)";
6166        std::cout << std::string("[FAIL] ") + description << std::endl;
6167        throw std::runtime_error(description);
6168    }
6169
6170    // Verify data is sorted
6171    if (!(std::is_sorted(data.begin(), data.end()))) {
6172        std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))";
6173        std::cout << std::string("[FAIL] ") + description << std::endl;
6174        throw std::runtime_error(description);
6175    }
6176
6177    // std::cout << "Sorted data generation test passed" << std::endl;
6178
6179    std::cout << " -> tests passed" << std::endl;
6180}
flat_index (np_test_5_all.cpp:11181)
11171      for (size_t i = 0; i < 12; ++i) {
11172        arr.flat(i) = static_cast<int>(i);
11173      }
11174
11175      // Test C-order iteration
11176      auto it = numpy::nditer(arr, true);
11177      int count = 0;
11178      while (!it.is_finished()) {
11179        int value = *it;
11180        auto indices = it.indices();
11181        size_t flat_idx = it.flat_index();
11182
11183        // Verify correctness
11184        if (value != count || flat_idx != count) {
11185          std::cout << "  [FAIL] : in np_test_nditer_basic() : incorrect iteration order";
11186          throw std::runtime_error("np_test_nditer_basic failed: incorrect iteration order");
11187        }
11188
11189        ++it;
11190        count++;
11191      }
indices (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43    const double TOLERANCE = 1e-10;
44
45    bool allTrue(const NDArray<bool>& arr) {
46        std::vector<size_t> indices(arr.getShape().size(), 0);
47        do {
48            if (!arr.getElementAt(indices)) {
49                return false;
50            }
51        } while (incrementIndices(indices, arr.getShape()));
52        return true;
53    }
54
55    bool allFalse(const NDArray<bool>& arr) {
56        std::vector<size_t> indices(arr.getShape().size(), 0);
reset (np_test_2_all.cpp:22610)
22600      // Count iterations
22601      size_t outer_count = 0;
22602      size_t total_inner_count = 0;
22603      for (; !outer.is_finished(); ++outer) {
22604        size_t inner_count = 0;
22605        for (; !inner.is_finished(); ++inner) {
22606          inner_count++;
22607          total_inner_count++;
22608        }
22609        outer_count++;
22610        inner.reset();
22611      }
22612
22613      if (outer_count != 3 || total_inner_count != 12) {
22614        std::cout << "  [FAIL] : Iteration counts incorrect";
22615        throw std::runtime_error("np_test_nested_iters_basic failed");
22616      }
22617
22618      std::cout << " -> tests passed" << std::endl;
22619    }