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
4609beb4
Commit
4609beb4
authored
Jul 06, 2016
by
Jason Rhinelander
Browse files
Merge remote-tracking branch 'upstream/master' into ternary-description
parents
8469f751
22201d08
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
6 deletions
+61
-6
example/eigen.cpp
example/eigen.cpp
+18
-0
example/eigen.py
example/eigen.py
+24
-1
example/eigen.ref
example/eigen.ref
+11
-0
example/run_test.py
example/run_test.py
+2
-2
include/pybind11/eigen.h
include/pybind11/eigen.h
+6
-3
No files found.
example/eigen.cpp
View file @
4609beb4
...
@@ -10,6 +10,19 @@
...
@@ -10,6 +10,19 @@
#include "example.h"
#include "example.h"
#include <pybind11/eigen.h>
#include <pybind11/eigen.h>
Eigen
::
VectorXf
double_col
(
const
Eigen
::
VectorXf
&
x
)
{
return
2.0
f
*
x
;
}
Eigen
::
RowVectorXf
double_row
(
const
Eigen
::
RowVectorXf
&
x
)
{
return
2.0
f
*
x
;
}
Eigen
::
MatrixXf
double_mat_cm
(
const
Eigen
::
MatrixXf
&
x
)
{
return
2.0
f
*
x
;
}
typedef
Eigen
::
Matrix
<
float
,
Eigen
::
Dynamic
,
Eigen
::
Dynamic
,
Eigen
::
RowMajor
>
MatrixXfRowMajor
;
MatrixXfRowMajor
double_mat_rm
(
const
MatrixXfRowMajor
&
x
)
{
return
2.0
f
*
x
;
}
void
init_eigen
(
py
::
module
&
m
)
{
void
init_eigen
(
py
::
module
&
m
)
{
typedef
Eigen
::
Matrix
<
float
,
5
,
6
,
Eigen
::
RowMajor
>
FixedMatrixR
;
typedef
Eigen
::
Matrix
<
float
,
5
,
6
,
Eigen
::
RowMajor
>
FixedMatrixR
;
typedef
Eigen
::
Matrix
<
float
,
5
,
6
>
FixedMatrixC
;
typedef
Eigen
::
Matrix
<
float
,
5
,
6
>
FixedMatrixC
;
...
@@ -23,6 +36,11 @@ void init_eigen(py::module &m) {
...
@@ -23,6 +36,11 @@ void init_eigen(py::module &m) {
mat
<<
0
,
3
,
0
,
0
,
0
,
11
,
22
,
0
,
0
,
0
,
17
,
11
,
7
,
5
,
0
,
1
,
0
,
11
,
0
,
mat
<<
0
,
3
,
0
,
0
,
0
,
11
,
22
,
0
,
0
,
0
,
17
,
11
,
7
,
5
,
0
,
1
,
0
,
11
,
0
,
0
,
0
,
0
,
0
,
11
,
0
,
0
,
14
,
0
,
8
,
11
;
0
,
0
,
0
,
0
,
11
,
0
,
0
,
14
,
0
,
8
,
11
;
m
.
def
(
"double_col"
,
&
double_col
);
m
.
def
(
"double_row"
,
&
double_row
);
m
.
def
(
"double_mat_cm"
,
&
double_mat_cm
);
m
.
def
(
"double_mat_rm"
,
&
double_mat_rm
);
m
.
def
(
"fixed_r"
,
[
mat
]()
->
FixedMatrixR
{
m
.
def
(
"fixed_r"
,
[
mat
]()
->
FixedMatrixR
{
return
FixedMatrixR
(
mat
);
return
FixedMatrixR
(
mat
);
});
});
...
...
example/eigen.py
View file @
4609beb4
...
@@ -9,6 +9,8 @@ from example import dense_r, dense_c
...
@@ -9,6 +9,8 @@ from example import dense_r, dense_c
from
example
import
dense_passthrough_r
,
dense_passthrough_c
from
example
import
dense_passthrough_r
,
dense_passthrough_c
from
example
import
sparse_r
,
sparse_c
from
example
import
sparse_r
,
sparse_c
from
example
import
sparse_passthrough_r
,
sparse_passthrough_c
from
example
import
sparse_passthrough_r
,
sparse_passthrough_c
from
example
import
double_row
,
double_col
from
example
import
double_mat_cm
,
double_mat_rm
import
numpy
as
np
import
numpy
as
np
ref
=
np
.
array
(
ref
=
np
.
array
(
...
@@ -20,7 +22,9 @@ ref = np.array(
...
@@ -20,7 +22,9 @@ ref = np.array(
def
check
(
mat
):
def
check
(
mat
):
return
'OK'
if
np
.
sum
(
mat
-
ref
)
==
0
else
'NOT OK'
return
'OK'
if
np
.
sum
(
abs
(
mat
-
ref
))
==
0
else
'NOT OK'
print
(
"should_give_NOT_OK = %s"
%
check
(
ref
[:,
::
-
1
]))
print
(
"fixed_r = %s"
%
check
(
fixed_r
()))
print
(
"fixed_r = %s"
%
check
(
fixed_r
()))
print
(
"fixed_c = %s"
%
check
(
fixed_c
()))
print
(
"fixed_c = %s"
%
check
(
fixed_c
()))
...
@@ -42,3 +46,22 @@ print("pt_r(sparse_r) = %s" % check(sparse_passthrough_r(sparse_r())))
...
@@ -42,3 +46,22 @@ print("pt_r(sparse_r) = %s" % check(sparse_passthrough_r(sparse_r())))
print
(
"pt_c(sparse_c) = %s"
%
check
(
sparse_passthrough_c
(
sparse_c
())))
print
(
"pt_c(sparse_c) = %s"
%
check
(
sparse_passthrough_c
(
sparse_c
())))
print
(
"pt_r(sparse_c) = %s"
%
check
(
sparse_passthrough_r
(
sparse_c
())))
print
(
"pt_r(sparse_c) = %s"
%
check
(
sparse_passthrough_r
(
sparse_c
())))
print
(
"pt_c(sparse_r) = %s"
%
check
(
sparse_passthrough_c
(
sparse_r
())))
print
(
"pt_c(sparse_r) = %s"
%
check
(
sparse_passthrough_c
(
sparse_r
())))
def
check_got_vs_ref
(
got_x
,
ref_x
):
return
'OK'
if
np
.
array_equal
(
got_x
,
ref_x
)
else
'NOT OK'
counting_mat
=
np
.
arange
(
9.0
,
dtype
=
np
.
float32
).
reshape
((
3
,
3
))
first_row
=
counting_mat
[
0
,
:]
first_col
=
counting_mat
[:,
0
]
print
(
"double_row(first_row) = %s"
%
check_got_vs_ref
(
double_row
(
first_row
),
2.0
*
first_row
))
print
(
"double_col(first_row) = %s"
%
check_got_vs_ref
(
double_col
(
first_row
),
2.0
*
first_row
))
print
(
"double_row(first_col) = %s"
%
check_got_vs_ref
(
double_row
(
first_col
),
2.0
*
first_col
))
print
(
"double_col(first_col) = %s"
%
check_got_vs_ref
(
double_col
(
first_col
),
2.0
*
first_col
))
counting_3d
=
np
.
arange
(
27.0
,
dtype
=
np
.
float32
).
reshape
((
3
,
3
,
3
))
slices
=
[
counting_3d
[
0
,
:,
:],
counting_3d
[:,
0
,
:],
counting_3d
[:,
:,
0
]]
for
slice_idx
,
ref_mat
in
enumerate
(
slices
):
print
(
"double_mat_cm(%d) = %s"
%
(
slice_idx
,
check_got_vs_ref
(
double_mat_cm
(
ref_mat
),
2.0
*
ref_mat
)))
print
(
"double_mat_rm(%d) = %s"
%
(
slice_idx
,
check_got_vs_ref
(
double_mat_rm
(
ref_mat
),
2.0
*
ref_mat
)))
example/eigen.ref
View file @
4609beb4
should_give_NOT_OK = NOT OK
fixed_r = OK
fixed_r = OK
fixed_c = OK
fixed_c = OK
pt_r(fixed_r) = OK
pt_r(fixed_r) = OK
...
@@ -16,3 +17,13 @@ pt_r(sparse_r) = OK
...
@@ -16,3 +17,13 @@ pt_r(sparse_r) = OK
pt_c(sparse_c) = OK
pt_c(sparse_c) = OK
pt_r(sparse_c) = OK
pt_r(sparse_c) = OK
pt_c(sparse_r) = OK
pt_c(sparse_r) = OK
double_row(first_row) = OK
double_col(first_row) = OK
double_row(first_col) = OK
double_col(first_col) = OK
double_mat_cm(0) = OK
double_mat_rm(0) = OK
double_mat_cm(1) = OK
double_mat_rm(1) = OK
double_mat_cm(2) = OK
double_mat_rm(2) = OK
example/run_test.py
View file @
4609beb4
...
@@ -68,6 +68,6 @@ else:
...
@@ -68,6 +68,6 @@ else:
print
(
'Test "%s" FAILED!'
%
name
)
print
(
'Test "%s" FAILED!'
%
name
)
print
(
'--- output'
)
print
(
'--- output'
)
print
(
'+++ reference'
)
print
(
'+++ reference'
)
print
(
''
.
join
(
difflib
.
ndiff
(
output
.
splitlines
(
keepends
=
True
),
print
(
''
.
join
(
difflib
.
ndiff
(
output
.
splitlines
(
True
),
reference
.
splitlines
(
keepends
=
True
))))
reference
.
splitlines
(
True
))))
exit
(
-
1
)
exit
(
-
1
)
include/pybind11/eigen.h
View file @
4609beb4
...
@@ -61,7 +61,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value>::t
...
@@ -61,7 +61,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value>::t
buffer_info
info
=
buffer
.
request
();
buffer_info
info
=
buffer
.
request
();
if
(
info
.
ndim
==
1
)
{
if
(
info
.
ndim
==
1
)
{
typedef
Eigen
::
Stride
<
Eigen
::
Dynamic
,
0
>
Strides
;
typedef
Eigen
::
Inner
Stride
<>
Strides
;
if
(
!
isVector
&&
if
(
!
isVector
&&
!
(
Type
::
RowsAtCompileTime
==
Eigen
::
Dynamic
&&
!
(
Type
::
RowsAtCompileTime
==
Eigen
::
Dynamic
&&
Type
::
ColsAtCompileTime
==
Eigen
::
Dynamic
))
Type
::
ColsAtCompileTime
==
Eigen
::
Dynamic
))
...
@@ -71,10 +71,13 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value>::t
...
@@ -71,10 +71,13 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value>::t
info
.
shape
[
0
]
!=
(
size_t
)
Type
::
SizeAtCompileTime
)
info
.
shape
[
0
]
!=
(
size_t
)
Type
::
SizeAtCompileTime
)
return
false
;
return
false
;
auto
strides
=
Strides
(
info
.
strides
[
0
]
/
sizeof
(
Scalar
),
0
);
auto
strides
=
Strides
(
info
.
strides
[
0
]
/
sizeof
(
Scalar
));
Strides
::
Index
n_elts
=
info
.
shape
[
0
];
Strides
::
Index
unity
=
1
;
value
=
Eigen
::
Map
<
Type
,
0
,
Strides
>
(
value
=
Eigen
::
Map
<
Type
,
0
,
Strides
>
(
(
Scalar
*
)
info
.
ptr
,
typename
Strides
::
Index
(
info
.
shape
[
0
]),
1
,
strides
);
(
Scalar
*
)
info
.
ptr
,
rowMajor
?
unity
:
n_elts
,
rowMajor
?
n_elts
:
unity
,
strides
);
}
else
if
(
info
.
ndim
==
2
)
{
}
else
if
(
info
.
ndim
==
2
)
{
typedef
Eigen
::
Stride
<
Eigen
::
Dynamic
,
Eigen
::
Dynamic
>
Strides
;
typedef
Eigen
::
Stride
<
Eigen
::
Dynamic
,
Eigen
::
Dynamic
>
Strides
;
...
...
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