Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
f1032df8
Commit
f1032df8
authored
May 05, 2016
by
Wenzel Jakob
Browse files
only do numpy contiguous C/Fortran array conversion when explicitly requested
parent
876eeab4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
8 deletions
+28
-8
docs/advanced.rst
docs/advanced.rst
+19
-5
include/pybind11/numpy.h
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).
...
@@ -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
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>``
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
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
.. code-block:: cpp
void f(py::array_t<double> array);
void f(py::array_t<double> array);
When it is invoked with a different type (e.g. an integer), the binding code
When it is invoked with a different type (e.g. an integer or a list of
will attempt to cast the input into a NumPy array of the requested type. Note
integers), the binding code will attempt to cast the input into a NumPy array
that this feature requires the :file:``pybind11/numpy.h`` header to be
of the requested type. Note that this feature requires the
included.
: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
Vectorizing functions
=====================
=====================
...
...
include/pybind11/numpy.h
View file @
f1032df8
...
@@ -77,6 +77,11 @@ public:
...
@@ -77,6 +77,11 @@ public:
PYBIND11_OBJECT_DEFAULT
(
array
,
buffer
,
lookup_api
().
PyArray_Check_
)
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
)
{
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
Type
>::
value
);
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
Type
>::
value
);
...
@@ -120,7 +125,7 @@ protected:
...
@@ -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:
public:
PYBIND11_OBJECT_CVT
(
array_t
,
array
,
is_non_null
,
m_ptr
=
ensure
(
m_ptr
));
PYBIND11_OBJECT_CVT
(
array_t
,
array
,
is_non_null
,
m_ptr
=
ensure
(
m_ptr
));
array_t
()
:
array
()
{
}
array_t
()
:
array
()
{
}
...
@@ -131,8 +136,9 @@ public:
...
@@ -131,8 +136,9 @@ public:
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
T
>::
value
);
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
npy_format_descriptor
<
T
>::
value
);
PyObject
*
result
=
api
.
PyArray_FromAny_
(
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_C_CONTIGUOUS_
|
API
::
NPY_ENSURE_ARRAY_
ptr
,
descr
,
0
,
0
,
|
API
::
NPY_ARRAY_FORCECAST_
,
nullptr
);
API
::
NPY_ENSURE_ARRAY_
|
API
::
NPY_ARRAY_FORCECAST_
|
ExtraFlags
,
nullptr
);
Py_DECREF
(
ptr
);
Py_DECREF
(
ptr
);
return
result
;
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