Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
61587164
Commit
61587164
authored
Jan 18, 2016
by
Wenzel Jakob
Browse files
example on manually vectorizing numpy code (closes #27)
parent
260bc58f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
1 deletion
+55
-1
docs/advanced.rst
docs/advanced.rst
+54
-0
docs/changelog.rst
docs/changelog.rst
+1
-1
No files found.
docs/advanced.rst
View file @
61587164
...
@@ -473,6 +473,11 @@ follows:
...
@@ -473,6 +473,11 @@ follows:
Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
0) policies from Boost.Python.
0) policies from Boost.Python.
.. seealso::
The file :file:`example/example13.cpp` contains a complete example that
demonstrates using :class:`keep_alive` in more detail.
Implicit type conversions
Implicit type conversions
=========================
=========================
...
@@ -850,6 +855,55 @@ This can be done with a stateful Lambda closure:
...
@@ -850,6 +855,55 @@ This can be done with a stateful Lambda closure:
}
}
);
);
In cases where the computation is too complicated to be reduced to
``vectorize``, it will be necessary to create and access the buffer contents
manually. The following snippet contains a complete example that shows how this
works (the code is somewhat contrived, since it could have been done more
simply using ``vectorize``).
.. code-block:: cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
auto buf1 = input1.request(), buf2 = input2.request();
if (buf1.ndim != 1 || buf2.ndim != 1)
throw std::runtime_error("Number of dimensions must be one");
if (buf1.shape[0] != buf2.shape[0])
throw std::runtime_error("Input shapes must match");
auto result = py::array(py::buffer_info(
nullptr, /* Pointer to data (nullptr -> ask NumPy to allocate!) */
sizeof(double), /* Size of one item */
py::format_descriptor<double>::value(), /* Buffer format */
buf1.ndim, /* How many dimensions? */
{ buf1.shape[0] }, /* Number of elements for each dimension */
{ sizeof(double) } /* Strides for each dimension */
));
auto buf3 = result.request();
double *ptr1 = (double *) buf1.ptr,
*ptr2 = (double *) buf2.ptr,
*ptr3 = (double *) buf3.ptr;
for (size_t idx = 0; idx < buf1.shape[0]; idx++)
ptr3[idx] = ptr1[idx] + ptr2[idx];
return result;
}
PYBIND11_PLUGIN(test) {
py::module m("test");
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
return m.ptr();
}
.. seealso::
.. seealso::
The file :file:`example/example10.cpp` contains a complete example that
The file :file:`example/example10.cpp` contains a complete example that
...
...
docs/changelog.rst
View file @
61587164
...
@@ -27,7 +27,7 @@ Changelog
...
@@ -27,7 +27,7 @@ Changelog
* Added conversions for additional exception types
* Added conversions for additional exception types
* Documentation improvements (using multiple extension modules, smart pointers,
* Documentation improvements (using multiple extension modules, smart pointers,
other minor clarifications)
other minor clarifications)
* unified infrastructure for parsing variadic arguments in class_ and cpp_function
* unified infrastructure for parsing variadic arguments in
``
class_
``
and cpp_function
* Fixed license text (was: ZLIB, should have been: 3-clause BSD)
* Fixed license text (was: ZLIB, should have been: 3-clause BSD)
* Python 3.2 compatibility
* Python 3.2 compatibility
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment