DataArray ========= .. cpp:class:: numpy::DataArray numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use DataArray DataArray obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``DataArray(const numpy::NDArray& data, const std::vector& dims =`` - df_xarray.h:165 - * - ``DataArray(const std::vector& data, const std::string& dim_name = "dim_0", const std::optional& coord = std::nullopt, const std::optional& name = std::nullopt)`` - df_xarray.h:199 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool empty() const`` - bool - df_xarray.h:281 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T at(const std::vector& indices) const`` - T - df_xarray.h:319 - :ref:`View ` * - ``const std::map& attrs() const`` - const std::map& - df_xarray.h:238 - * - ``std::map& attrs()`` - std::map& - df_xarray.h:239 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::string to_string() const`` - std::string - df_xarray.h:337 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const Coordinate& coord(const std::string& name) const`` - const Coordinate& - df_xarray.h:297 - * - ``const std::map& coords() const`` - const std::map& - df_xarray.h:232 - * - ``std::map& coords()`` - std::map& - df_xarray.h:233 - * - ``const std::vector& dims() const`` - const std::vector& - df_xarray.h:227 - * - ``bool has_coord(const std::string& name) const`` - bool - df_xarray.h:290 - * - ``std::optional name() const`` - std::optional - df_xarray.h:244 - :ref:`View ` * - ``size_t ndim() const`` - size_t - df_xarray.h:254 - :ref:`View ` * - ``void set_coord(const std::string& name, const Coordinate& coord)`` - void - df_xarray.h:308 - * - ``void set_name(const std::string& name)`` - void - df_xarray.h:249 - * - ``std::vector shape() const`` - std::vector - df_xarray.h:259 - :ref:`View ` * - ``size_t size() const`` - size_t - df_xarray.h:264 - :ref:`View ` * - ``std::map sizes() const`` - std::map - df_xarray.h:269 - :ref:`View ` * - ``const numpy::NDArray& values() const`` - const numpy::NDArray& - df_xarray.h:221 - :ref:`View ` * - ``numpy::NDArray& values()`` - numpy::NDArray& - df_xarray.h:222 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-dataarray-empty-0: .. dropdown:: empty (np_test_1_all.cpp:6316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6306 :emphasize-lines: 11 } void test_data_generator_emptyBenchmarkSorting() { std::cout << "========= test_data_generator_empty ======================="; DataGenerator gen(42); // Test empty data generation std::vector data = gen.generate(0, DataPattern::RANDOM); if (!(data.empty())) { std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Empty data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-dataarray-at-1: .. dropdown:: at (np_test_1_all.cpp:144) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 134 :emphasize-lines: 11 array.setElementAt({0, 0}, 1); array.setElementAt({0, 1}, 2); array.setElementAt({0, 2}, 3); array.setElementAt({1, 1}, 5); array.setElementAt({2, 2}, 9); // std::cout << "Array after setting elements:" << std::endl; //array.printArray(); // std::cout << "Element at (1,1): " << array.getElementAt({1, 1}) << std::endl; // std::cout << "Element at (2,2): " << array.getElementAt({2, 2}); std::cout << " -> tests passed" << std::endl; } void testArithmetic() { std::cout << "========= testArithmeticOperations ======================="; auto array1 = createFloat32Array({2, 2}, 5.0f); auto array2 = createFloat32Array({2, 2}, 3.0f); .. _example-dataarray-to_string-2: .. 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-dataarray-name-3: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-dataarray-ndim-4: .. dropdown:: ndim (np_test_2_all.cpp:3526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3516 :emphasize-lines: 11 void testArrayInfo() { std::cout << "Testing array information functions...\n"; // Test basic info functions auto arr = createFloat64Array({ 2, 3, 4 }); for (size_t i = 0; i < arr.getSize(); ++i) { arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast(i)); } // Test ndim if (!(ndim(arr) == 3)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test size if (!(size(arr) == 24)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-dataarray-shape-5: .. dropdown:: shape (np_test_1_all.cpp:3751) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3741 :emphasize-lines: 11 } for (size_t j = 0; j < 3; ++j) { arr3.setElementAt({0, j}, static_cast((j + 1) * 100)); // [100, 200, 300] } // Broadcast all arrays together std::vector> arrays = {arr1, arr2, arr3}; auto broadcasted = broadcast_arrays(arrays); // All should have shape (2, 3) for (const auto& arr : broadcasted) { if (!((arr.getShape() == std::vector{2, 3}))) { std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector{2, 3}))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // Check values if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) { .. _example-dataarray-size-6: .. 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-dataarray-sizes-7: .. dropdown:: sizes (np_test_3_all.cpp:11536) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11526 :emphasize-lines: 11 std::string description = std::string("np_test_io_npz_compressed():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(loaded_uncompressed.getElementAt({ 500 }) - 500.0) < 1e-10)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(std::abs(loaded_compressed.getElementAt({ 500 }) - 500.0) < 1e-10)) { std::string description = std::string("np_test_io_npz_compressed():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(loaded_compressed.getElementAt({ 500 }) - 500.0) < 1e-10)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Check file sizes (compressed should be smaller for large arrays) auto size_uncompressed = std::filesystem::file_size(filename_uncompressed); auto size_compressed = std::filesystem::file_size(filename_compressed); // std::cout << "Uncompressed size: " << size_uncompressed << " bytes" << std::endl; // std::cout << "Compressed size: " << size_compressed << " bytes" << std::endl; // Note: Compression ratio depends on data patterns // For sequential data like arange, we may not see much compression // std::cout << "[OK] Compressed save/load (with zlib)" << std::endl; #else .. _example-dataarray-values-8: .. dropdown:: values (np_test_1_all.cpp:15133) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15123 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } else { std::cout << "[FAIL] Where function failed" << std::endl; errors++; } // Test 8: Clip function std::cout << "========= test_clip_function =======================" ; numpy::NDArray values({ 5 }); values.setElementAt({ 0 }, -2.0); values.setElementAt({ 1 }, -1.0); values.setElementAt({ 2 }, 0.5); values.setElementAt({ 3 }, 2.0); values.setElementAt({ 4 }, 5.0); double min_val = -1.0; double max_val = 3.0; auto clip_result = numpy::clip(values, min_val, max_val); .. _example-dataarray-values-9: .. dropdown:: values (np_test_1_all.cpp:15133) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 15123 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } else { std::cout << "[FAIL] Where function failed" << std::endl; errors++; } // Test 8: Clip function std::cout << "========= test_clip_function =======================" ; numpy::NDArray values({ 5 }); values.setElementAt({ 0 }, -2.0); values.setElementAt({ 1 }, -1.0); values.setElementAt({ 2 }, 0.5); values.setElementAt({ 3 }, 2.0); values.setElementAt({ 4 }, 5.0); double min_val = -1.0; double max_val = 3.0; auto clip_result = numpy::clip(values, min_val, max_val);