Ufunc ===== .. cpp:class:: numpy::Ufunc numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Ufunc Ufunc obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``Ufunc(Funcfunc, size_tnin, size_tnout, std::optionalidentity = std::nullopt, const std::string &doc = "")`` - NP_UFUNC.H:63 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``auto -> NDArray operator()(const NDArray&arr)`` - auto -> NDArray - NP_UFUNC.H:102 - * - ``auto -> NDArray operator()(const NDArray&arr1, const NDArray&arr2)`` - auto -> NDArray - NP_UFUNC.H:137 - * - ``auto -> NDArray operator()(const NDArray&arr1, const NDArray&arr2, const NDArray&arr3)`` - auto -> NDArray - NP_UFUNC.H:183 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const std::string & getDoc()`` - const std::string & - NP_UFUNC.H:304 - * - ``const Func & getFunction()`` - const Func & - NP_UFUNC.H:299 - * - ``std::any getIdentity()`` - std::any - NP_UFUNC.H:81 - :ref:`View ` * - ``TypeId getTypeId(const std::type_info &ti)`` - TypeId - NP_UFUNC.H:429 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``std::any callFunctionWithAny(const std::any &arg)`` - std::any - NP_UFUNC.H:361 - * - ``std::any callFunctionWithAny(const std::any &arg1, const std::any &arg2)`` - std::any - NP_UFUNC.H:449 - * - ``std::enable_if_t,std::any> callFunctionWithAnyBinary(const std::any &arg1, const std::any &arg2)`` - std::enable_if_t,std::any> - NP_UFUNC.H:336 - * - ``std::enable_if_t,std::any> callFunctionWithAnyBinary(const std::any &arg1, const std::any &arg2)`` - std::enable_if_t,std::any> - NP_UFUNC.H:354 - * - ``std::any callFunctionWithAnyBinaryPromoted(const std::any &arg1, const std::any &arg2, TypeIdt1, TypeIdt2)`` - std::any - NP_UFUNC.H:523 - * - ``std::any callFunctionWithAnyBinaryPromotedComplex(const std::any &arg1, const std::any &arg2, TypeIdt1, TypeIdt2)`` - std::any - NP_UFUNC.H:542 - * - ``std::any callFunctionWithAnyBinaryWithObject(const std::any &arg1, const std::any &arg2, TypeIdt1, TypeIdt2)`` - std::any - NP_UFUNC.H:561 - * - ``std::enable_if_t,std::any> callFunctionWithAnyUnary(const std::any &arg)`` - std::enable_if_t,std::any> - NP_UFUNC.H:310 - * - ``std::enable_if_t,std::any> callFunctionWithAnyUnary(const std::any &arg)`` - std::enable_if_t,std::any> - NP_UFUNC.H:327 - * - ``object\_ convertToObject(const std::any &arg, TypeIdtid)`` - object\_ - NP_UFUNC.H:619 - * - ``TargetT convertToType(const std::any &arg, TypeIdtid)`` - TargetT - NP_UFUNC.H:579 - * - ``bool hasIdentity()`` - bool - NP_UFUNC.H:80 - :ref:`View ` * - ``size_t nin()`` - size_t - NP_UFUNC.H:78 - * - ``size_t nout()`` - size_t - NP_UFUNC.H:79 - * - ``std::string signature()`` - std::string - NP_UFUNC.H:84 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-ufunc-getidentity-0: .. dropdown:: getIdentity (np_test_3_all.cpp:1055) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1045 :emphasize-lines: 11 try { auto mult_func = [](double a, double b) { return a * b; }; auto ufunc_mult = numpy::frompyfunc(mult_func, 2, 1, std::any{ 1.0 }); if (!ufunc_mult.hasIdentity()) { std::cout << "[FAIL] Identity value failed"; return 1; } double identity = std::any_cast(ufunc_mult.getIdentity()); if (std::abs(identity - 1.0) > 1e-10) { std::cout << "[FAIL] Identity value failed"; return 1; } // std::cout << "[OK] Identity value works correctly" << std::endl; std::cout << " -> tests passed" << std::endl; return 0; } catch (const std::exception& e) { .. _example-ufunc-hasidentity-1: .. dropdown:: hasIdentity (np_test_3_all.cpp:1050) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1040 :emphasize-lines: 11 } // Test 8: Identity value int np_test_frompyfunc_identity_value() { std::cout << "========= np_test_frompyfunc_identity_value ======================="; try { auto mult_func = [](double a, double b) { return a * b; }; auto ufunc_mult = numpy::frompyfunc(mult_func, 2, 1, std::any{ 1.0 }); if (!ufunc_mult.hasIdentity()) { std::cout << "[FAIL] Identity value failed"; return 1; } double identity = std::any_cast(ufunc_mult.getIdentity()); if (std::abs(identity - 1.0) > 1e-10) { std::cout << "[FAIL] Identity value failed"; return 1; }