Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
f1032df8
Commit
f1032df8
authored
May 05, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
only do numpy contiguous C/Fortran array conversion when explicitly requested
parent
876eeab4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
8 deletions
+28
-8
docs/advanced.rst
+19
-5
include/pybind11/numpy.h
+9
-3
No files found.
docs/advanced.rst
View file @
f1032df8
...
...
@@ -902,16 +902,30 @@ type of Python object satisfying the buffer protocol).
In many situations, we want to define a function which only accepts a NumPy
array of a certain data type. This is possible via the ``py::array_t<T>``
template. For instance, the following function requires the argument to be a
dense array of doubles in C-style ordering
.
NumPy array containing double precision values
.
.. code-block:: cpp
void f(py::array_t<double> array);
When it is invoked with a different type (e.g. an integer), the binding code
will attempt to cast the input into a NumPy array of the requested type. Note
that this feature requires the :file:``pybind11/numpy.h`` header to be
included.
When it is invoked with a different type (e.g. an integer or a list of
integers), the binding code will attempt to cast the input into a NumPy array
of the requested type. Note that this feature requires the
:file:``pybind11/numpy.h`` header to be included.
Data in NumPy arrays is not guaranteed to packed in a dense manner;
furthermore, entries can be separated by arbitrary column and row strides.
Sometimes, it can be useful to require a function to only accept dense arrays
using either the C (row-major) or Fortran (column-major) ordering. This can be
accomplished via a second template argument with values ``py::array::c_style``
or ``py::array::f_style``.
.. code-block:: cpp
void f(py::array_t<double, py::array::c_style> array);
As before, the implementation will attempt to convert non-conforming arguments
into an array satisfying the specified requirements.
Vectorizing functions
=====================
...
...
include/pybind11/numpy.h
View file @
f1032df8
...
...
@@ -77,6 +77,11 @@ public:
PYBIND11_OBJECT_DEFAULT
(
array
,
buffer
,
lookup_api
().
PyArray_Check_
)
enum
{
c_style
=
API
::
NPY_C_CONTIGUOUS_
,
f_style
=
API
::
NPY_F_CONTIGUOUS_
};
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
Type
>::
value
);
...
...
@@ -120,7 +125,7 @@ protected:
}
};
template
<
typename
T
>
class
array_t
:
public
array
{
template
<
typename
T
,
int
ExtraFlags
=
0
>
class
array_t
:
public
array
{
public
:
PYBIND11_OBJECT_CVT
(
array_t
,
array
,
is_non_null
,
m_ptr
=
ensure
(
m_ptr
));
array_t
()
:
array
()
{
}
...
...
@@ -131,8 +136,9 @@ public:
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
T
>::
value
);
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_C_CONTIGUOUS_
|
API
::
NPY_ENSURE_ARRAY_
|
API
::
NPY_ARRAY_FORCECAST_
,
nullptr
);
ptr
,
descr
,
0
,
0
,
API
::
NPY_ENSURE_ARRAY_
|
API
::
NPY_ARRAY_FORCECAST_
|
ExtraFlags
,
nullptr
);
Py_DECREF
(
ptr
);
return
result
;
}
...
...
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