UfuncWithMethods ================ .. cpp:class:: numpy::UfuncWithMethods numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use UfuncWithMethods UfuncWithMethods obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void at(NDArray&arr, const std::vector>&indices, const NDArray&values)`` - void - NP_UFUNC_METHODS.H:334 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::any reduce(const NDArray&arr, std::optionalaxis = std::nullopt, std::optionalinitial = std::nullopt)`` - std::any - NP_UFUNC_METHODS.H:300 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-ufuncwithmethods-at-0: .. 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-ufuncwithmethods-reduce-1: .. dropdown:: reduce (np_test_3_all.cpp:946) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 936 :emphasize-lines: 11 sum_arr.setElementAt({ 1 }, 2.0); sum_arr.setElementAt({ 2 }, 3.0); sum_arr.setElementAt({ 3 }, 4.0); // Convert to object array for reduce numpy::NDArray obj_sum_arr({ 4 }); for (size_t i = 0; i < 4; ++i) { obj_sum_arr.setElementAt({ i }, std::any{ sum_arr.getElementAt({i}) }); } auto reduce_result = ufunc_sum.reduce(obj_sum_arr); double final_sum = std::any_cast(reduce_result); if (std::abs(final_sum - 10.0) > 1e-10) { std::cout << "[FAIL] Reduce method failed"; return 1; } // std::cout << "[OK] Reduce method works correctly" << std::endl; std::cout << " -> tests passed" << std::endl; return 0;