mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-17 06:00:51 +00:00
Add doc of py::numpy_scalar
This commit is contained in:
parent
58771146a9
commit
9d846f6e27
@ -232,6 +232,46 @@ prevent many types of unsupported structures, it is still the user's
|
||||
responsibility to use only "plain" structures that can be safely manipulated as
|
||||
raw memory without violating invariants.
|
||||
|
||||
Scalar types
|
||||
============
|
||||
|
||||
In some cases we may want to accept or return NumPy scalar values such as
|
||||
``np.float32`` or ``np.float64``. We hope to be able to handle single-precision
|
||||
and double-precision on the C-side. However, both are bound to Python's
|
||||
double-precision builtin float by default, so they cannot be processed separately.
|
||||
We used the ``py::buffer`` trick to implement the previous approach, which
|
||||
will cause the readability of the code to drop significantly.
|
||||
|
||||
Luckily, there's a helper type for this occasion - ``py::numpy_scalar``:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
m.def("add", [](py::numpy_scalar<float> a, py::numpy_scalar<float> b) {
|
||||
return py::make_scalar(a + b);
|
||||
});
|
||||
m.def("add", [](py::numpy_scalar<double> a, py::numpy_scalar<double> b) {
|
||||
return py::make_scalar(a + b);
|
||||
});
|
||||
|
||||
This type is trivially convertible to and from the type it wraps; currently
|
||||
supported scalar types are NumPy arithmetic types: ``bool_``, ``int8``,
|
||||
``int16``, ``int32``, ``int64``, ``uint8``, ``uint16``, ``uint32``,
|
||||
``uint64``, ``float32``, ``float64``, ``complex64``, ``complex128``, all of
|
||||
them mapping to respective C++ counterparts.
|
||||
|
||||
.. note::
|
||||
|
||||
This is a strict type, it will only allow input arguments of the specified
|
||||
NumPy type and nothing else (e.g., ``py::numpy_scalar<int64_t>`` will not
|
||||
accept built-in ``int`` or any other type for that matter).
|
||||
|
||||
.. note::
|
||||
|
||||
Native C types are mapped to NumPy types in a platform specific way: for
|
||||
instance, ``char`` may be mapped to either ``np.int8`` or ``np.uint8``
|
||||
depending on the platform. If you want to ensure specific NumPy types,
|
||||
it is recommended to use fixed-width aliases from ``<cstdint>``.
|
||||
|
||||
Vectorizing functions
|
||||
=====================
|
||||
|
||||
|
@ -9,8 +9,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pybind11.h"
|
||||
#include "complex.h"
|
||||
#include "pybind11/pybind11.h"
|
||||
#include "pybind11/complex.h"
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
@ -159,9 +159,9 @@ struct npy_api {
|
||||
NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_),
|
||||
NPY_FLOAT64_ = platform_lookup<8, double, float, long double>(
|
||||
NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_),
|
||||
NPY_COMPLEX64_ = platform_lookup<8, double, float, long double>(
|
||||
NPY_COMPLEX64_ = platform_lookup<8, std::complex<double>, std::complex<float>, std::complex<long double>>(
|
||||
NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_),
|
||||
NPY_COMPLEX128_ = platform_lookup<8, double, float, long double>(
|
||||
NPY_COMPLEX128_ = platform_lookup<8, std::complex<double>, std::complex<float>, std::complex<long double>>(
|
||||
NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_),
|
||||
NPY_CHAR_ = std::is_signed<char>::value ? NPY_BYTE_ : NPY_UBYTE_,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user