NestedIterLevel =============== .. cpp:class:: numpy::NestedIterLevel numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use NestedIterLevel NestedIterLevel obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``NestedIterLevel(const std::vector\*>&arrays, const std::vector&axes, std::vector>\*shared_indices)`` - NP_NDITER.H:455 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T operator\*()`` - T - NP_NDITER.H:521 - * - ``NestedIterLevel & operator++()`` - NestedIterLevel & - NP_NDITER.H:526 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_finished()`` - bool - NP_NDITER.H:495 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const std::vector& indices(size_tarray_idx = 0)`` - const std::vector& - NP_NDITER.H:505 - :ref:`View ` * - ``size_t iteration()`` - size_t - NP_NDITER.H:500 - :ref:`View ` * - ``void reset()`` - void - NP_NDITER.H:566 - :ref:`View ` * - ``size_t size()`` - size_t - NP_NDITER.H:581 - :ref:`View ` * - ``T value(size_tarray_idx = 0)`` - T - NP_NDITER.H:513 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-nestediterlevel-is_finished-0: .. dropdown:: is_finished (np_test_2_all.cpp:3824) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3814 :emphasize-lines: 11 for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 3; ++j) { arr.setElementAt({ i, j }, static_cast(i * 3 + j)); } } auto iter = nditer(arr); size_t count = 0; double expected_values[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; for (; !iter.is_finished(); ++iter, ++count) { if (!(isApproxEqualMCO(*iter, expected_values[count]))) { std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(*iter, expected_values[count]))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } if (!(count == 6)) { std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(count == 6)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-nestediterlevel-indices-1: .. dropdown:: indices (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; 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); .. _example-nestediterlevel-iteration-2: .. dropdown:: iteration (np_test_2_all.cpp:22720) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22710 :emphasize-lines: 11 numpy::NDArray arr({ 3, 4 }); auto iters = numpy::nested_iters(arr, { {0}, {1} }); auto& outer = iters[0]; auto& inner = iters[1]; // Iterate partway ++outer; ++inner; ++inner; if (outer.iteration() != 1 || inner.iteration() != 2) { std::cout << " [FAIL] : Iteration state incorrect before reset"; throw std::runtime_error("np_test_nested_iters_reset failed"); } // Reset inner inner.reset(); if (inner.iteration() != 0 || inner.is_finished()) { std::cout << " [FAIL] : Inner reset failed"; throw std::runtime_error("np_test_nested_iters_reset failed"); } .. _example-nestediterlevel-reset-3: .. dropdown:: reset (np_test_2_all.cpp:22610) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22600 :emphasize-lines: 11 // Count iterations size_t outer_count = 0; size_t total_inner_count = 0; for (; !outer.is_finished(); ++outer) { size_t inner_count = 0; for (; !inner.is_finished(); ++inner) { inner_count++; total_inner_count++; } outer_count++; inner.reset(); } if (outer_count != 3 || total_inner_count != 12) { std::cout << " [FAIL] : Iteration counts incorrect"; throw std::runtime_error("np_test_nested_iters_basic failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-nestediterlevel-size-4: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; 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); .. _example-nestediterlevel-value-5: .. dropdown:: value (np_test_1_all.cpp:7786) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7776 :emphasize-lines: 11 } void testFloat16PrecisionFloat16() { std::cout << "========= testFloat16PrecisionFloat16 ======================="; // Test precision limits float16 small_val(0.0001f); float16 large_val(30000.0f); float16 very_small(1e-8f); // This might underflow to zero // std::cout << "Small value (0.0001): " << small_val << std::endl; // std::cout << "Large value (30000): " << large_val << std::endl; // std::cout << "Very small value (1e-8): " << very_small << std::endl; // Test conversion accuracy float original = 1.234f; float16 converted(original); float back_converted = static_cast(converted); // std::cout << "Original float: " << std::setprecision(10) << original << std::endl; // std::cout << "Float16 version: " << converted << std::endl;