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
b47a9de0
Commit
b47a9de0
authored
May 19, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ability to prevent force casts in numpy arguments
parent
93a317ec
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
13 deletions
+28
-13
docs/advanced.rst
+5
-3
example/example10.cpp
+5
-0
example/example10.py
+5
-0
example/example10.ref
+3
-0
include/pybind11/numpy.h
+10
-10
No files found.
docs/advanced.rst
View file @
b47a9de0
...
@@ -1100,10 +1100,12 @@ or ``py::array::f_style``.
...
@@ -1100,10 +1100,12 @@ or ``py::array::f_style``.
.. code-block:: cpp
.. code-block:: cpp
void f(py::array_t<double, py::array::c_style> array);
void f(py::array_t<double, py::array::c_style
| py::array::forcecast
> array);
As before, the implementation will attempt to convert non-conforming arguments
The ``py::array::forcecast`` argument is the default value of the second
into an array satisfying the specified requirements.
template paramenter, and it ensures that non-conforming arguments are converted
into an array satisfying the specified requirements instead of trying the next
function overload.
Vectorizing functions
Vectorizing functions
=====================
=====================
...
...
example/example10.cpp
View file @
b47a9de0
...
@@ -33,4 +33,9 @@ void init_ex10(py::module &m) {
...
@@ -33,4 +33,9 @@ void init_ex10(py::module &m) {
// Vectorize a complex-valued function
// Vectorize a complex-valued function
m
.
def
(
"vectorized_func3"
,
py
::
vectorize
(
my_func3
));
m
.
def
(
"vectorized_func3"
,
py
::
vectorize
(
my_func3
));
/// Numpy function which only accepts specific data types
m
.
def
(
"selective_func"
,
[](
py
::
array_t
<
int
,
py
::
array
::
c_style
>
)
{
std
::
cout
<<
"Int branch taken. "
<<
std
::
endl
;
});
m
.
def
(
"selective_func"
,
[](
py
::
array_t
<
float
,
py
::
array
::
c_style
>
)
{
std
::
cout
<<
"Float branch taken. "
<<
std
::
endl
;
});
m
.
def
(
"selective_func"
,
[](
py
::
array_t
<
std
::
complex
<
float
>
,
py
::
array
::
c_style
>
)
{
std
::
cout
<<
"Complex float branch taken. "
<<
std
::
endl
;
});
}
}
example/example10.py
View file @
b47a9de0
...
@@ -27,3 +27,8 @@ for f in [vectorized_func, vectorized_func2]:
...
@@ -27,3 +27,8 @@ for f in [vectorized_func, vectorized_func2]:
print
(
f
(
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]]),
np
.
array
([[
2
],
[
3
]]),
2
))
print
(
f
(
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]]),
np
.
array
([[
2
],
[
3
]]),
2
))
print
(
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]])
*
np
.
array
([[
2
],
[
3
]])
*
2
)
print
(
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
]])
*
np
.
array
([[
2
],
[
3
]])
*
2
)
from
example
import
selective_func
selective_func
(
np
.
array
([
1
],
dtype
=
np
.
int32
))
selective_func
(
np
.
array
([
1.0
],
dtype
=
np
.
float32
))
selective_func
(
np
.
array
([
1.0
j
],
dtype
=
np
.
complex64
))
example/example10.ref
View file @
b47a9de0
...
@@ -73,3 +73,6 @@ my_func(x:int=6, y:float=3, z:float=2)
...
@@ -73,3 +73,6 @@ my_func(x:int=6, y:float=3, z:float=2)
[ 24. 30. 36.]]
[ 24. 30. 36.]]
[[ 4 8 12]
[[ 4 8 12]
[24 30 36]]
[24 30 36]]
Int branch taken.
Float branch taken.
Complex float branch taken.
include/pybind11/numpy.h
View file @
b47a9de0
...
@@ -78,7 +78,8 @@ public:
...
@@ -78,7 +78,8 @@ public:
enum
{
enum
{
c_style
=
API
::
NPY_C_CONTIGUOUS_
,
c_style
=
API
::
NPY_C_CONTIGUOUS_
,
f_style
=
API
::
NPY_F_CONTIGUOUS_
f_style
=
API
::
NPY_F_CONTIGUOUS_
,
forcecast
=
API
::
NPY_ARRAY_FORCECAST_
};
};
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
template
<
typename
Type
>
array
(
size_t
size
,
const
Type
*
ptr
)
{
...
@@ -124,7 +125,7 @@ protected:
...
@@ -124,7 +125,7 @@ protected:
}
}
};
};
template
<
typename
T
,
int
ExtraFlags
=
0
>
class
array_t
:
public
array
{
template
<
typename
T
,
int
ExtraFlags
=
array
::
forcecast
>
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
()
{
}
...
@@ -135,10 +136,9 @@ public:
...
@@ -135,10 +136,9 @@ public:
return
nullptr
;
return
nullptr
;
API
&
api
=
lookup_api
();
API
&
api
=
lookup_api
();
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
detail
::
npy_format_descriptor
<
T
>::
value
);
PyObject
*
descr
=
api
.
PyArray_DescrFromType_
(
detail
::
npy_format_descriptor
<
T
>::
value
);
PyObject
*
result
=
api
.
PyArray_FromAny_
(
PyObject
*
result
=
api
.
PyArray_FromAny_
(
ptr
,
descr
,
0
,
0
,
API
::
NPY_ENSURE_ARRAY_
|
ExtraFlags
,
nullptr
);
ptr
,
descr
,
0
,
0
,
if
(
!
result
)
API
::
NPY_ENSURE_ARRAY_
|
API
::
NPY_ARRAY_FORCECAST_
|
ExtraFlags
,
PyErr_Clear
();
nullptr
);
Py_DECREF
(
ptr
);
Py_DECREF
(
ptr
);
return
result
;
return
result
;
}
}
...
@@ -318,11 +318,11 @@ struct vectorize_helper {
...
@@ -318,11 +318,11 @@ struct vectorize_helper {
template
<
typename
T
>
template
<
typename
T
>
vectorize_helper
(
T
&&
f
)
:
f
(
std
::
forward
<
T
>
(
f
))
{
}
vectorize_helper
(
T
&&
f
)
:
f
(
std
::
forward
<
T
>
(
f
))
{
}
object
operator
()(
array_t
<
Args
>
...
args
)
{
object
operator
()(
array_t
<
Args
,
array
::
c_style
|
array
::
forcecast
>
...
args
)
{
return
run
(
args
...,
typename
make_index_sequence
<
sizeof
...(
Args
)
>::
type
());
return
run
(
args
...,
typename
make_index_sequence
<
sizeof
...(
Args
)
>::
type
());
}
}
template
<
size_t
...
Index
>
object
run
(
array_t
<
Args
>&
...
args
,
index_sequence
<
Index
...
>
index
)
{
template
<
size_t
...
Index
>
object
run
(
array_t
<
Args
,
array
::
c_style
|
array
::
forcecast
>&
...
args
,
index_sequence
<
Index
...
>
index
)
{
/* Request buffers from all parameters */
/* Request buffers from all parameters */
const
size_t
N
=
sizeof
...(
Args
);
const
size_t
N
=
sizeof
...(
Args
);
...
@@ -332,7 +332,7 @@ struct vectorize_helper {
...
@@ -332,7 +332,7 @@ struct vectorize_helper {
int
ndim
=
0
;
int
ndim
=
0
;
std
::
vector
<
size_t
>
shape
(
0
);
std
::
vector
<
size_t
>
shape
(
0
);
bool
trivial_broadcast
=
broadcast
(
buffers
,
ndim
,
shape
);
bool
trivial_broadcast
=
broadcast
(
buffers
,
ndim
,
shape
);
size_t
size
=
1
;
size_t
size
=
1
;
std
::
vector
<
size_t
>
strides
(
ndim
);
std
::
vector
<
size_t
>
strides
(
ndim
);
if
(
ndim
>
0
)
{
if
(
ndim
>
0
)
{
...
@@ -384,7 +384,7 @@ struct vectorize_helper {
...
@@ -384,7 +384,7 @@ struct vectorize_helper {
}
}
};
};
template
<
typename
T
>
struct
handle_type_name
<
array_t
<
T
>>
{
template
<
typename
T
,
int
Flags
>
struct
handle_type_name
<
array_t
<
T
,
Flags
>>
{
static
PYBIND11_DESCR
name
()
{
return
_
(
"numpy.ndarray[dtype="
)
+
type_caster
<
T
>::
name
()
+
_
(
"]"
);
}
static
PYBIND11_DESCR
name
()
{
return
_
(
"numpy.ndarray[dtype="
)
+
type_caster
<
T
>::
name
()
+
_
(
"]"
);
}
};
};
...
...
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