Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
eigenpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stack Of Tasks
eigenpy
Commits
d6766a20
Commit
d6766a20
authored
2 years ago
by
Wilson Jallet
Browse files
Options
Downloads
Patches
Plain Diff
unittest/eigen_ref: tidy up test and cpp file
unittest/eigen_ref.cpp: asConstRef should take input as Ref<const T>
parent
af87fa1a
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
unittest/eigen_ref.cpp
+15
-13
15 additions, 13 deletions
unittest/eigen_ref.cpp
unittest/python/test_eigen_ref.py
+50
-15
50 additions, 15 deletions
unittest/python/test_eigen_ref.py
unittest/vector.cpp
+2
-0
2 additions, 0 deletions
unittest/vector.cpp
with
67 additions
and
28 deletions
unittest/eigen_ref.cpp
+
15
−
13
View file @
d6766a20
...
@@ -15,7 +15,7 @@ using namespace eigenpy;
...
@@ -15,7 +15,7 @@ using namespace eigenpy;
template
<
typename
MatType
>
template
<
typename
MatType
>
void
printMatrix
(
const
Eigen
::
Ref
<
const
MatType
>
mat
)
{
void
printMatrix
(
const
Eigen
::
Ref
<
const
MatType
>
mat
)
{
if
(
MatType
::
IsVectorAtCompileTime
)
std
::
cout
<<
"isVector"
<<
std
::
endl
;
if
(
MatType
::
IsVectorAtCompileTime
)
std
::
cout
<<
"isVector"
<<
std
::
endl
;
std
::
cout
<<
"size: cols "
<<
mat
.
cols
()
<<
" rows "
<<
mat
.
rows
()
std
::
cout
<<
"
input
size: cols "
<<
mat
.
cols
()
<<
" rows "
<<
mat
.
rows
()
<<
std
::
endl
;
<<
std
::
endl
;
std
::
cout
<<
mat
<<
std
::
endl
;
std
::
cout
<<
mat
<<
std
::
endl
;
}
}
...
@@ -61,17 +61,19 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
...
@@ -61,17 +61,19 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
template
<
typename
MatType
>
template
<
typename
MatType
>
Eigen
::
Ref
<
MatType
>
asRef
(
const
int
rows
,
const
int
cols
)
{
Eigen
::
Ref
<
MatType
>
asRef
(
const
int
rows
,
const
int
cols
)
{
static
MatType
mat
(
rows
,
cols
);
static
MatType
mat
(
rows
,
cols
);
std
::
cout
<<
"
mat:
\n
"
<<
mat
<<
std
::
endl
;
std
::
cout
<<
"
create ref to matrix of size ("
<<
rows
<<
","
<<
cols
<<
")
\n
"
;
return
mat
;
return
mat
;
}
}
template
<
typename
MatType
>
template
<
typename
MatType
>
Eigen
::
Ref
<
MatType
>
asRef
(
Eigen
::
Ref
<
MatType
>
mat
)
{
Eigen
::
Ref
<
MatType
>
asRef
(
Eigen
::
Ref
<
MatType
>
mat
)
{
std
::
cout
<<
"create Ref to input mutable Ref
\n
"
;
return
Eigen
::
Ref
<
MatType
>
(
mat
);
return
Eigen
::
Ref
<
MatType
>
(
mat
);
}
}
template
<
typename
MatType
>
template
<
typename
MatType
>
const
Eigen
::
Ref
<
const
MatType
>
asConstRef
(
Eigen
::
Ref
<
MatType
>
mat
)
{
const
Eigen
::
Ref
<
const
MatType
>
asConstRef
(
Eigen
::
Ref
<
const
MatType
>
mat
)
{
std
::
cout
<<
"create const Ref to input
\n
"
;
return
Eigen
::
Ref
<
const
MatType
>
(
mat
);
return
Eigen
::
Ref
<
const
MatType
>
(
mat
);
}
}
...
@@ -82,8 +84,8 @@ struct modify_block {
...
@@ -82,8 +84,8 @@ struct modify_block {
virtual
void
call
(
Eigen
::
Ref
<
MatrixXd
>
mat
)
=
0
;
virtual
void
call
(
Eigen
::
Ref
<
MatrixXd
>
mat
)
=
0
;
};
};
struct
modify_wrap
:
modify_block
,
bp
::
wrapper
<
modify_block
>
{
struct
modify_
block_
wrap
:
modify_block
,
bp
::
wrapper
<
modify_block
>
{
modify_wrap
()
:
modify_block
()
{}
modify_
block_
wrap
()
:
modify_block
()
{}
void
call
(
Eigen
::
Ref
<
MatrixXd
>
mat
)
{
this
->
get_override
(
"call"
)(
mat
);
}
void
call
(
Eigen
::
Ref
<
MatrixXd
>
mat
)
{
this
->
get_override
(
"call"
)(
mat
);
}
};
};
...
@@ -112,20 +114,20 @@ BOOST_PYTHON_MODULE(eigen_ref) {
...
@@ -112,20 +114,20 @@ BOOST_PYTHON_MODULE(eigen_ref) {
bp
::
def
(
"fillVec"
,
fill
<
VectorXd
>
);
bp
::
def
(
"fillVec"
,
fill
<
VectorXd
>
);
bp
::
def
(
"fill"
,
fill
<
MatrixXd
>
);
bp
::
def
(
"fill"
,
fill
<
MatrixXd
>
);
bp
::
def
(
"asRef"
,
bp
::
def
<
Eigen
::
Ref
<
MatrixXd
>
(
*
)(
const
int
,
const
int
)
>
(
"asRef"
,
(
Eigen
::
Ref
<
MatrixXd
>
(
*
)(
const
int
,
const
int
))
asRef
<
MatrixXd
>
);
asRef
<
MatrixXd
>
);
bp
::
def
(
"asRef"
,
bp
::
def
<
Eigen
::
Ref
<
MatrixXd
>
(
*
)(
Eigen
::
Ref
<
MatrixXd
>
)
>
(
"asRef"
,
(
Eigen
::
Ref
<
MatrixXd
>
(
*
)(
Eigen
::
Ref
<
MatrixXd
>
))
asRef
<
MatrixXd
>
);
asRef
<
MatrixXd
>
);
bp
::
def
(
"asConstRef"
,
(
const
Eigen
::
Ref
<
const
MatrixXd
>
(
*
)(
bp
::
def
(
"asConstRef"
,
asConstRef
<
MatrixXd
>
);
Eigen
::
Ref
<
MatrixXd
>
))
asConstRef
<
MatrixXd
>
);
bp
::
def
(
"getBlock"
,
&
getBlock
<
MatrixXd
>
);
bp
::
def
(
"getBlock"
,
&
getBlock
<
MatrixXd
>
);
bp
::
def
(
"editBlock"
,
&
editBlock
<
MatrixXd
>
);
bp
::
def
(
"editBlock"
,
&
editBlock
<
MatrixXd
>
);
bp
::
class_
<
modify_wrap
,
boost
::
noncopyable
>
(
"modify_block"
,
bp
::
init
<>
())
bp
::
class_
<
modify_block_wrap
,
boost
::
noncopyable
>
(
"modify_block"
,
bp
::
init
<>
())
.
def_readonly
(
"J"
,
&
modify_block
::
J
)
.
def_readonly
(
"J"
,
&
modify_block
::
J
)
.
def
(
"modify"
,
&
modify_block
::
modify
)
.
def
(
"modify"
,
&
modify_block
::
modify
)
.
def
(
"call"
,
bp
::
pure_virtual
(
&
modify_wrap
::
call
));
.
def
(
"call"
,
bp
::
pure_virtual
(
&
modify_
block_
wrap
::
call
));
bp
::
class_
<
has_ref_member
,
boost
::
noncopyable
>
(
"has_ref_member"
,
bp
::
init
<>
())
bp
::
class_
<
has_ref_member
,
boost
::
noncopyable
>
(
"has_ref_member"
,
bp
::
init
<>
())
.
def_readonly
(
"J"
,
&
has_ref_member
::
J
)
.
def_readonly
(
"J"
,
&
has_ref_member
::
J
)
...
...
This diff is collapsed.
Click to expand it.
unittest/python/test_eigen_ref.py
+
50
−
15
View file @
d6766a20
...
@@ -11,48 +11,68 @@ from eigen_ref import (
...
@@ -11,48 +11,68 @@ from eigen_ref import (
)
)
def
test
(
mat
):
def
test
_fill_print
(
mat
):
print
(
"
print matrix:
"
)
printMatrix
(
mat
)
printMatrix
(
mat
)
print
(
"
calling fill():
"
)
fill
(
mat
,
1.0
)
fill
(
mat
,
1.0
)
print
(
"
print again:
"
)
printMatrix
(
mat
)
printMatrix
(
mat
)
assert
np
.
array_equal
(
mat
,
np
.
full
(
mat
.
shape
,
1.0
))
assert
np
.
array_equal
(
mat
,
np
.
full
(
mat
.
shape
,
1.0
))
def
test_create_ref_to_static
(
mat
):
# create ref to static:
print
()
print
(
"
[asRef(int, int)]
"
)
A_ref
=
asRef
(
mat
.
shape
[
0
],
mat
.
shape
[
1
])
A_ref
=
asRef
(
mat
.
shape
[
0
],
mat
.
shape
[
1
])
A_ref
.
fill
(
1.0
)
A_ref
.
fill
(
1.0
)
A_ref
[
0
,
1
]
=
-
1.0
print
(
"
make second reference:
"
)
A_ref2
=
asRef
(
mat
.
shape
[
0
],
mat
.
shape
[
1
])
A_ref2
=
asRef
(
mat
.
shape
[
0
],
mat
.
shape
[
1
])
print
(
A_ref2
)
assert
np
.
array_equal
(
A_ref
,
A_ref2
)
assert
np
.
array_equal
(
A_ref
,
A_ref2
)
A_ref2
.
fill
(
0
)
A_ref2
.
fill
(
0
)
assert
np
.
array_equal
(
A_ref
,
A_ref2
)
assert
np
.
array_equal
(
A_ref
,
A_ref2
)
def
test_create_ref
(
mat
):
# create ref to input:
print
(
"
[asRef(mat)]
"
)
ref
=
asRef
(
mat
)
ref
=
asRef
(
mat
)
assert
np
.
all
(
ref
==
mat
)
assert
np
.
array_equal
(
ref
,
mat
)
assert
not
(
ref
.
flags
.
owndata
)
assert
ref
.
flags
.
writeable
print
(
"
[asConstRef]
"
)
const_ref
=
asConstRef
(
mat
)
const_ref
=
asConstRef
(
mat
)
assert
np
.
all
(
const_ref
==
mat
)
print
(
const_ref
.
flags
)
assert
np
.
array_equal
(
const_ref
,
mat
)
assert
not
(
const_ref
.
flags
.
writeable
)
assert
not
(
const_ref
.
flags
.
owndata
)
mat
.
fill
(
0.0
)
print
(
"
fill a slice
"
)
mat
[:,
:]
=
0.0
fill
(
mat
[:
3
,
:
2
],
1.0
)
fill
(
mat
[:
3
,
:
2
],
1.0
)
assert
np
.
array_equal
(
mat
[:
3
,
:
2
],
np
.
ones
((
3
,
2
)))
assert
np
.
all
(
mat
[:
3
,
:
2
]
==
np
.
ones
((
3
,
2
)))
mat
[:,
:]
=
0.0
mat
.
fill
(
0.0
)
fill
(
mat
[:
2
,
:
3
],
1.0
)
fill
(
mat
[:
2
,
:
3
],
1.0
)
assert
np
.
array_equal
(
mat
[:
2
,
:
3
],
np
.
ones
((
2
,
3
)))
assert
np
.
all
(
mat
[:
2
,
:
3
]
==
np
.
ones
((
2
,
3
)))
print
(
"
set mat data to arange()
"
)
mat
.
fill
(
0.0
)
mat
.
fill
(
0.0
)
mat
[:,
:]
=
np
.
arange
(
rows
*
cols
).
reshape
(
rows
,
cols
)
mat
[:,
:]
=
np
.
arange
(
rows
*
cols
).
reshape
(
rows
,
cols
)
printMatrix
(
mat
)
mat0
=
mat
.
copy
()
mat0
=
mat
.
copy
()
mat_as_C_order
=
np
.
array
(
mat
,
order
=
"
F
"
)
mat_as_C_order
=
np
.
array
(
mat
,
order
=
"
F
"
)
for
i
,
rowsize
,
colsize
in
([
0
,
3
,
2
],
[
1
,
1
,
2
],
[
0
,
3
,
1
]):
for
i
,
rowsize
,
colsize
in
([
0
,
3
,
2
],
[
1
,
1
,
2
],
[
0
,
3
,
1
]):
print
(
"
taking block [{}:{}, {}:{}]
"
.
format
(
i
,
rowsize
+
i
,
0
,
colsize
))
print
(
"
taking block [{}:{}, {}:{}]
"
.
format
(
i
,
rowsize
+
i
,
0
,
colsize
))
B
=
getBlock
(
mat_as_C_order
,
i
,
0
,
rowsize
,
colsize
)
B
=
getBlock
(
mat_as_C_order
,
i
,
0
,
rowsize
,
colsize
)
lhs
=
mat_as_C_order
[
i
:
rowsize
+
i
,
:
colsize
]
lhs
=
mat_as_C_order
[
i
:
rowsize
+
i
,
:
colsize
]
print
(
"
should be:
\n
{}
\n
got:
\n
{}
"
.
format
(
lhs
,
B
))
assert
np
.
array_equal
(
lhs
,
B
.
reshape
(
rowsize
,
colsize
))
assert
np
.
array_equal
(
lhs
,
B
.
reshape
(
rowsize
,
colsize
))
B
[:]
=
1.0
B
[:]
=
1.0
...
@@ -62,6 +82,7 @@ def test(mat):
...
@@ -62,6 +82,7 @@ def test(mat):
mat_as_C_order
[:,
:]
=
mat0
mat_as_C_order
[:,
:]
=
mat0
mat_copy
=
mat_as_C_order
.
copy
()
mat_copy
=
mat_as_C_order
.
copy
()
print
(
"
[editBlock]
"
)
editBlock
(
mat_as_C_order
,
0
,
0
,
3
,
2
)
editBlock
(
mat_as_C_order
,
0
,
0
,
3
,
2
)
mat_copy
[:
3
,
:
2
]
=
np
.
arange
(
6
).
reshape
(
3
,
2
)
mat_copy
[:
3
,
:
2
]
=
np
.
arange
(
6
).
reshape
(
3
,
2
)
...
@@ -91,9 +112,23 @@ def test(mat):
...
@@ -91,9 +112,23 @@ def test(mat):
assert
np
.
array_equal
(
hasref
.
J
,
J_true
)
assert
np
.
array_equal
(
hasref
.
J
,
J_true
)
rows
=
10
def
_do_test
(
mat
):
cols
=
30
test_fill_print
(
mat
)
test_create_ref_to_static
(
mat
)
test_create_ref
(
mat
)
if
__name__
==
"
__main__
"
:
rows
=
8
cols
=
10
mat
=
np
.
ones
((
rows
,
cols
),
order
=
"
F
"
)
mat
=
np
.
ones
((
rows
,
cols
),
order
=
"
F
"
)
mat
[
0
,
0
]
=
0
mat
[
1
:
5
,
1
:
5
]
=
6
_do_test
(
mat
)
test
(
mat
)
mat
=
np
.
ones
((
rows
,
cols
))
mat
[
2
:
4
,
1
:
4
]
=
2
_do_test
(
mat
)
mat_f
=
np
.
asfortranarray
(
mat
)
_do_test
(
mat_f
)
This diff is collapsed.
Click to expand it.
unittest/vector.cpp
+
2
−
0
View file @
d6766a20
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
#include
<type_traits>
#include
<type_traits>
#include
"eigenpy/eigenpy.hpp"
#include
"eigenpy/eigenpy.hpp"
// include main first
#include
"eigenpy/eigen-from-python.hpp"
#include
"eigenpy/std-vector.hpp"
#include
"eigenpy/std-vector.hpp"
template
<
typename
MatType
>
template
<
typename
MatType
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment