test_eigen_ref.py
Go to the documentation of this file.
1 import numpy as np
2 from eigen_ref import (
3  printMatrix,
4  getRefToStatic,
5  asRef,
6  asConstRef,
7  fill,
8  getBlock,
9  editBlock,
10  modify_block,
11  has_ref_member,
12  copyVectorFromConstRef,
13  copyRowVectorFromConstRef,
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), "ref=\n{}\nmat=\n{}".format(ref, 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), "ref=\n{}\nmat=\n{}".format(const_ref, 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("taking block [{}:{}, {}:{}]".format(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), "got lhs\n{}\nrhs B=\n{}".format(lhs, 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 __init__(self):
104  super(ModifyBlockImpl, self).__init__()
105 
106  def call(self, mat):
107  n, m = mat.shape
108  mat[:, :] = np.arange(n * m).reshape(n, m)
109 
110  modify = ModifyBlockImpl()
111  modify.modify(2, 3)
112  Jref = np.zeros((10, 10))
113  Jref[:2, :3] = np.arange(6).reshape(2, 3)
114 
115  assert np.array_equal(Jref, modify.J)
116 
117  hasref = has_ref_member()
118  A = np.ones((3, 3)) / 2
119  hasref.Jref[:, :] = A
120  J_true = np.zeros((4, 4))
121  J_true[:3, 1:] = A
122 
123  assert np.array_equal(hasref.J, J_true)
124 
125 
126 def do_test(mat):
127  test_fill_print(mat)
130  test_create_ref(mat)
131  test_edit_block(rows, cols)
133  print("=" * 10)
134 
135 
136 if __name__ == "__main__":
137  rows = 8
138  cols = 10
139 
140  mat = np.ones((rows, cols), order="F")
141  mat[0, 0] = 0
142  mat[1:5, 1:5] = 6
143  do_test(mat)
144 
145  # mat2 = np.ones((rows, cols))
146  # mat2[:2, :5] = 0.
147  # mat2[2:4, 1:4] = 2
148  # mat2[:, -1] = 3
149  # do_test(mat2)
def test_fill_print(mat)
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
def test_create_ref(mat)
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
const Eigen::Ref< const MatType > asConstRef(Eigen::Ref< const MatType > mat)
Definition: eigen_ref.cpp:81
void printMatrix(const Eigen::Ref< const MatType > mat)
Definition: eigen_ref.cpp:15
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
def test_create_const_ref(mat)
def test_edit_block(rows, cols)
Eigen::Ref< MatType > asRef(Eigen::Ref< MatType > mat)
Definition: eigen_ref.cpp:75
def test_create_ref_to_static(mat)
VecType copyVectorFromConstRef(const Eigen::Ref< const VecType > vec)
Definition: eigen_ref.cpp:34
void print(const Tensor &tensor)
Definition: tensor.cpp:35
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)
Definition: eigen_ref.cpp:62


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26