test_eigen_ref.py
Go to the documentation of this file.
1 import numpy as np
2 from eigen_ref import (
3  asConstRef,
4  asRef,
5  copyRowVectorFromConstRef,
6  copyVectorFromConstRef,
7  editBlock,
8  fill,
9  getBlock,
10  getRefToStatic,
11  has_ref_member,
12  modify_block,
13  printMatrix,
14 )
15 
16 
17 def test_fill_print(mat):
18  print("print matrix:")
19  printMatrix(mat)
20  print("calling fill():")
21  fill(mat, 1.0)
22  assert np.array_equal(mat, np.full(mat.shape, 1.0))
23 
24  print("fill a slice")
25  mat[:, :] = 0.0
26  fill(mat[:3, :2], 1.0)
27  printMatrix(mat[:3, :2])
28  assert np.array_equal(mat[:3, :2], np.ones((3, 2)))
29 
30 
32  # create ref to static:
33  print()
34  print("[asRef(int, int)]")
35  A_ref = getRefToStatic(mat.shape[0], mat.shape[1])
36  A_ref.fill(1.0)
37  A_ref[0, 1] = -1.0
38  print("make second reference:")
39  A_ref2 = getRefToStatic(mat.shape[0], mat.shape[1])
40  print(A_ref2)
41 
42  assert np.array_equal(A_ref, A_ref2)
43 
44  A_ref2.fill(0)
45  assert np.array_equal(A_ref, A_ref2)
46 
47 
49  data = np.array([[0, 0.2, 0.3, 0.4], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0]])
50 
51  data_strided = data[:, 0]
52 
53  data_strided_copy = copyVectorFromConstRef(data_strided)
54  assert np.all(data_strided == data_strided_copy)
55 
56  data_strided_copy = copyRowVectorFromConstRef(data_strided)
57  assert np.all(data_strided == data_strided_copy)
58 
59 
60 def test_create_ref(mat):
61  print("[asRef(mat)]")
62  ref = asRef(mat)
63  assert np.array_equal(ref, mat), f"ref=\n{ref}\nmat=\n{mat}"
64  assert not (ref.flags.owndata)
65  assert ref.flags.writeable
66 
67 
69  print("[asConstRef]")
70  const_ref = asConstRef(mat)
71  assert np.array_equal(const_ref, mat), f"ref=\n{const_ref}\nmat=\n{mat}"
72  assert not (const_ref.flags.writeable)
73  assert not (const_ref.flags.owndata)
74 
75 
76 def test_edit_block(rows, cols):
77  print("set mat data to arange()")
78  mat.fill(0.0)
79  mat[:, :] = np.arange(rows * cols).reshape(rows, cols)
80  mat0 = mat.copy()
81  for i, rowsize, colsize in ([0, 3, 2], [1, 1, 2], [0, 3, 1]):
82  print(f"taking block [{i}:{rowsize + i}, {0}:{colsize}]")
83  B = getBlock(mat, i, 0, rowsize, colsize)
84  B = B.reshape(rowsize, colsize)
85  lhs = mat[i : rowsize + i, :colsize]
86  assert np.array_equal(lhs, B), f"got lhs\n{lhs}\nrhs B=\n{B}"
87 
88  B[:] = 1.0
89  rhs = np.ones((rowsize, colsize))
90  assert np.array_equal(mat[i : rowsize + i, :colsize], rhs)
91 
92  mat[:, :] = mat0
93 
94  mat.fill(0.0)
95  mat_copy = mat.copy()
96  print("[editBlock]")
97  editBlock(mat, 0, 0, 3, 2)
98  mat_copy[:3, :2] = np.arange(6).reshape(3, 2)
99 
100  assert np.array_equal(mat, mat_copy)
101 
102  class ModifyBlockImpl(modify_block):
103  def call(self, mat):
104  n, m = mat.shape
105  mat[:, :] = np.arange(n * m).reshape(n, m)
106 
107  modify = ModifyBlockImpl()
108  modify.modify(2, 3)
109  Jref = np.zeros((10, 10))
110  Jref[:2, :3] = np.arange(6).reshape(2, 3)
111 
112  assert np.array_equal(Jref, modify.J)
113 
114  hasref = has_ref_member()
115  A = np.ones((3, 3)) / 2
116  hasref.Jref[:, :] = A
117  J_true = np.zeros((4, 4))
118  J_true[:3, 1:] = A
119 
120  assert np.array_equal(hasref.J, J_true)
121 
122 
123 def do_test(mat):
124  test_fill_print(mat)
127  test_create_ref(mat)
128  test_edit_block(rows, cols)
130  print("=" * 10)
131 
132 
133 if __name__ == "__main__":
134  rows = 8
135  cols = 10
136 
137  mat = np.ones((rows, cols), order="F")
138  mat[0, 0] = 0
139  mat[1:5, 1:5] = 6
140  do_test(mat)
141 
142  # mat2 = np.ones((rows, cols))
143  # mat2[:2, :5] = 0.
144  # mat2[2:4, 1:4] = 2
145  # mat2[:, -1] = 3
146  # do_test(mat2)
test_eigen_ref.test_edit_block
def test_edit_block(rows, cols)
Definition: test_eigen_ref.py:76
fill
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)
Definition: eigen_ref.cpp:62
editBlock
Eigen::Ref< MatType > editBlock(Eigen::Ref< MatType > mat, Eigen::DenseIndex i, Eigen::DenseIndex j, Eigen::DenseIndex n, Eigen::DenseIndex m)
Definition: eigen_ref.cpp:47
print
void print(const Eigen::SparseMatrix< Scalar, Options > &mat)
Definition: sparse_matrix.cpp:50
test_eigen_ref.test_create_ref_to_static
def test_create_ref_to_static(mat)
Definition: test_eigen_ref.py:31
printMatrix
void printMatrix(const Eigen::Ref< const MatType > mat)
Definition: eigen_ref.cpp:15
getRefToStatic
Eigen::Ref< MatType > getRefToStatic(const int rows, const int cols)
Get ref to a static matrix of size ( rows, cols )
Definition: eigen_ref.cpp:68
getBlock
Eigen::Ref< MatType > getBlock(Eigen::Ref< MatType > mat, Eigen::DenseIndex i, Eigen::DenseIndex j, Eigen::DenseIndex n, Eigen::DenseIndex m)
Definition: eigen_ref.cpp:40
asConstRef
const Eigen::Ref< const MatType > asConstRef(Eigen::Ref< const MatType > mat)
Definition: eigen_ref.cpp:81
asRef
Eigen::Ref< MatType > asRef(Eigen::Ref< MatType > mat)
Definition: eigen_ref.cpp:75
test_eigen_ref.test_fill_print
def test_fill_print(mat)
Definition: test_eigen_ref.py:17
test_eigen_ref.do_test
def do_test(mat)
Definition: test_eigen_ref.py:123
copyVectorFromConstRef
VecType copyVectorFromConstRef(const Eigen::Ref< const VecType > vec)
Definition: eigen_ref.cpp:34
test_eigen_ref.test_create_const_ref
def test_create_const_ref(mat)
Definition: test_eigen_ref.py:68
test_eigen_ref.test_create_ref
def test_create_ref(mat)
Definition: test_eigen_ref.py:60
test_eigen_ref.test_read_block
def test_read_block()
Definition: test_eigen_ref.py:48


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 14 2024 02:15:58