test_matrix.py
Go to the documentation of this file.
1 import platform
2 
3 import matrix as eigenpy
4 import numpy as np
5 
6 verbose = True
7 
8 if verbose:
9  print("===> From empty MatrixXd to Py")
10 M = eigenpy.emptyMatrix()
11 assert M.shape == (0, 0)
12 
13 if verbose:
14  print("===> From empty VectorXd to Py")
15 v = eigenpy.emptyVector()
16 assert v.shape == (0,)
17 
18 if verbose:
19  print("===> From Py to Matrix1")
20 eigenpy.matrix1x1(np.array([1]))
21 
22 if verbose:
23  print("===> From MatrixXd to Py")
24 M = eigenpy.naturals(3, 3, verbose)
25 Mcheck = np.reshape(np.array(range(9), np.double), [3, 3])
26 assert np.array_equal(Mcheck, M)
27 
28 if verbose:
29  print("===> From Matrix3d to Py")
30 M33 = eigenpy.naturals33(verbose)
31 assert np.array_equal(Mcheck, M33)
32 
33 if verbose:
34  print("===> From VectorXd to Py")
35 v = eigenpy.naturalsX(3, verbose)
36 vcheck = np.array(range(3), np.double).T
37 assert np.array_equal(vcheck, v)
38 
39 if verbose:
40  print("===> From Py to Eigen::MatrixXd")
41 if verbose:
42  print("===> From Py to Eigen::MatrixXd")
43 if verbose:
44  print("===> From Py to Eigen::MatrixXd")
45 Mref = np.reshape(np.array(range(64), np.double), [8, 8])
46 
47 # Test base function
48 Mref_from_base = eigenpy.base(Mref)
49 assert np.array_equal(Mref, Mref_from_base)
50 
51 # Test plain function
52 Mref_from_plain = eigenpy.plain(Mref)
53 assert np.array_equal(Mref, Mref_from_plain)
54 
55 if verbose:
56  print("===> Matrix 8x8")
57 M = Mref
58 assert np.array_equal(M, eigenpy.reflex(M, verbose))
59 
60 if verbose:
61  print("===> Block 0:3x0:3")
62 M = Mref[0:3, 0:3]
63 assert np.array_equal(M, eigenpy.reflex(M, verbose))
64 
65 if verbose:
66  print("===> Block 1:3x1:3")
67 M = Mref[1:3, 1:3]
68 assert np.array_equal(M, eigenpy.reflex(M, verbose))
69 
70 if verbose:
71  print("===> Block 1:5:2x1:5:2")
72 M = Mref[1:5:2, 1:5:2]
73 assert np.array_equal(M, eigenpy.reflex(M, verbose))
74 
75 if verbose:
76  print("===> Block 1:8:3x1:5")
77 M = Mref[1:8:3, 1:5]
78 assert np.array_equal(M, eigenpy.reflex(M, verbose))
79 
80 if verbose:
81  print("===> Block transpose 1:8:3x1:6:2")
82 M = Mref[1:8:3, 0:6:2].T
83 assert np.array_equal(M, eigenpy.reflex(M, verbose))
84 
85 if verbose:
86  print("===> Block Vector 1x0:6:2")
87 M = Mref[1:2, 0:6:2]
88 assert np.array_equal(M.squeeze(), eigenpy.reflex(M, verbose))
89 
90 if verbose:
91  print("===> Block Vector 1x0:6:2 tanspose")
92 M = Mref[1:2, 0:6:2].T
93 assert np.array_equal(M.squeeze(), eigenpy.reflex(M, verbose))
94 
95 if verbose:
96  print("===> Block Vector 0:6:2x1")
97 M = Mref[0:6:2, 1:2]
98 assert np.array_equal(M.squeeze(), eigenpy.reflex(M, verbose))
99 
100 if verbose:
101  print("===> Block Vector 0:6:2x1 tanspose")
102 M = Mref[0:6:2, 1:2].T
103 assert np.array_equal(M.squeeze(), eigenpy.reflex(M, verbose))
104 
105 if verbose:
106  print("===> From Py to Eigen::VectorXd")
107 if verbose:
108  print("===> From Py to Eigen::VectorXd")
109 if verbose:
110  print("===> From Py to Eigen::VectorXd")
111 
112 if verbose:
113  print("===> Block Vector 0:6:2x1 1 dim")
114 M = Mref[0:6:2, 1].T
115 # TODO
116 # assert( np.array_equal(M.T,eigenpy.reflexV(M,verbose)) );
117 
118 if verbose:
119  print("===> Block Vector 0:6:2x1")
120 M = Mref[0:6:2, 1:2]
121 assert np.array_equal(M.squeeze(), eigenpy.reflexV(M, verbose))
122 
123 if verbose:
124  print("===> Block Vector 0:6:2x1 transpose")
125 M = Mref[0:6:2, 1:2].T
126 # TODO
127 # assert( np.array_equal(M.T,eigenpy.reflexV(M,verbose)) );
128 
129 if verbose:
130  print("===> From Py to Eigen::Matrix3d")
131 if verbose:
132  print("===> From Py to Eigen::Matrix3d")
133 if verbose:
134  print("===> From Py to Eigen::Matrix3d")
135 
136 if verbose:
137  print("===> Block Vector 0:3x0:6:2 ")
138 M = Mref[0:3, 0:6:2]
139 assert np.array_equal(M, eigenpy.reflex33(M, verbose))
140 
141 if verbose:
142  print("===> Block Vector 0:3x0:6:2 T")
143 M = Mref[0:3, 0:6].T
144 # TODO
145 # try:
146 # assert( np.array_equal(M,eigenpy.reflex33(M,verbose)) );
147 # except eigenpy.Exception as e:
148 # if verbose: print("As expected, got the following /ROW/ error:", e.message)
149 
150 if verbose:
151  print("===> From Py to Eigen::Vector3d")
152 if verbose:
153  print("===> From Py to Eigen::Vector3d")
154 if verbose:
155  print("===> From Py to Eigen::Vector3d")
156 
157 # TODO
158 # M = Mref[0:3,1:2]
159 # assert( np.array_equal(M,eigenpy.reflex3(M,verbose)) );
160 
161 value = 2.0
162 mat1x1 = eigenpy.matrix1x1(value)
163 assert mat1x1.size == 1
164 assert mat1x1[0, 0] == value
165 
166 vec1x1 = eigenpy.vector1x1(value)
167 assert vec1x1.size == 1
168 assert vec1x1[0] == value
169 
170 # test registration of matrix6
171 mat6 = eigenpy.matrix6(0.0)
172 assert mat6.size == 36
173 
174 # test RowMajor
175 
176 mat = np.arange(0, 10).reshape(2, 5)
177 assert (eigenpy.asRowMajorFromColMajorMatrix(mat) == mat).all()
178 assert (eigenpy.asRowMajorFromRowMajorMatrix(mat) == mat).all()
179 
180 vec = np.arange(0, 10)
181 assert (eigenpy.asRowMajorFromColMajorMatrix(vec) == vec).all()
182 assert (eigenpy.asRowMajorFromColMajorVector(vec) == vec).all()
183 assert (eigenpy.asRowMajorFromRowMajorMatrix(vec) == vec).all()
184 assert (eigenpy.asRowMajorFromRowMajorVector(vec) == vec).all()
185 
186 # Test numpy -> Eigen -> numpy for all same type
187 
188 
189 def test_conversion(function, dtype):
190  input_array = np.array([1, 0], dtype=dtype)
191  assert input_array.dtype == dtype
192  output_array = function(input_array)
193  assert output_array.dtype == dtype
194  assert (output_array == input_array).all()
195 
196 
197 bool_t = np.dtype(np.bool_)
198 int8_t = np.dtype(np.int8)
199 uint8_t = np.dtype(np.uint8)
200 int16_t = np.dtype(np.int16)
201 uint16_t = np.dtype(np.uint16)
202 int32_t = np.dtype(np.int32)
203 uint32_t = np.dtype(np.uint32)
204 int64_t = np.dtype(np.int64)
205 uint64_t = np.dtype(np.uint64)
206 # On Windows long is a 32 bits integer but is a different type than int32.
207 long_t = np.dtype(np.int32 if platform.system() == "Windows" else np.int64)
208 ulong_t = np.dtype(np.uint32 if platform.system() == "Windows" else np.uint64)
209 longlong_t = np.dtype(np.longlong)
210 ulonglong_t = np.dtype(np.ulonglong)
211 
212 float32_t = np.dtype(np.float32)
213 float64_t = np.dtype(np.float64)
214 
215 complex64_t = np.dtype(np.complex64)
216 complex128_t = np.dtype(np.complex128)
217 complex256_t = np.dtype(np.clongdouble)
218 
219 
220 test_conversion(eigenpy.copyBoolToBool, bool_t)
221 
222 test_conversion(eigenpy.copyInt8ToInt8, int8_t)
223 test_conversion(eigenpy.copyCharToChar, int8_t)
224 test_conversion(eigenpy.copyUCharToUChar, uint8_t)
225 
226 test_conversion(eigenpy.copyInt16ToInt16, int16_t)
227 test_conversion(eigenpy.copyUInt16ToUInt16, uint16_t)
228 
229 test_conversion(eigenpy.copyInt32ToInt32, int32_t)
230 test_conversion(eigenpy.copyUInt32ToUInt32, uint32_t)
231 
232 test_conversion(eigenpy.copyInt64ToInt64, int64_t)
233 test_conversion(eigenpy.copyUInt64ToUInt64, uint64_t)
234 
235 test_conversion(eigenpy.copyLongToLong, long_t)
236 test_conversion(eigenpy.copyULongToULong, ulong_t)
237 
238 # On Windows long long is an int64_t alias.
239 # The numpy dtype match between longlong and int64.
240 
241 # On Linux long long is a 64 bits integer but is a different type than int64_t.
242 # The numpy dtype doesn't match and it's not an issue since C++ type are different.
243 
244 # On Mac long long is an int64_t and long alias.
245 # This is an issue because longlong numpy dtype is different than long numpy dtype
246 # but long and long long are the same type in C++.
247 # The test should pass thanks to the promotion code.
248 test_conversion(eigenpy.copyLongLongToLongLong, longlong_t)
249 test_conversion(eigenpy.copyULongLongToULongLong, ulonglong_t)
250 
251 test_conversion(eigenpy.copyFloatToFloat, float32_t)
252 test_conversion(eigenpy.copyDoubleToDouble, float64_t)
253 # On Windows and Mac longdouble is 64 bits
254 test_conversion(eigenpy.copyLongDoubleToLongDouble, np.dtype(np.longdouble))
255 
256 test_conversion(eigenpy.copyCFloatToCFloat, complex64_t)
257 test_conversion(eigenpy.copyCDoubleToCDouble, complex128_t)
258 # On Windows and Mac clongdouble is 128 bits
259 test_conversion(eigenpy.copyCLongDoubleToCLongDouble, complex256_t)
260 
261 
262 # Test numpy -> Eigen -> numpy promotion
263 
264 
265 def test_conversion_promotion(function, input_dtype, output_dtype):
266  input_array = np.array([1, 0], dtype=input_dtype)
267  assert input_array.dtype == input_dtype
268  output_array = function(input_array)
269  assert output_array.dtype == output_dtype
270  assert (output_array == input_array).all()
271 
272 
273 # Test bool to other type
274 test_conversion_promotion(eigenpy.copyInt8ToInt8, bool_t, int8_t)
275 test_conversion_promotion(eigenpy.copyUCharToUChar, bool_t, uint8_t)
276 test_conversion_promotion(eigenpy.copyInt16ToInt16, bool_t, int16_t)
277 test_conversion_promotion(eigenpy.copyUInt16ToUInt16, bool_t, uint16_t)
278 test_conversion_promotion(eigenpy.copyInt32ToInt32, bool_t, int32_t)
279 test_conversion_promotion(eigenpy.copyUInt32ToUInt32, bool_t, uint32_t)
280 test_conversion_promotion(eigenpy.copyInt64ToInt64, bool_t, int64_t)
281 test_conversion_promotion(eigenpy.copyUInt64ToUInt64, bool_t, uint64_t)
282 test_conversion_promotion(eigenpy.copyLongToLong, bool_t, long_t)
283 test_conversion_promotion(eigenpy.copyULongToULong, bool_t, ulong_t)
284 test_conversion_promotion(eigenpy.copyLongLongToLongLong, bool_t, int64_t)
285 test_conversion_promotion(eigenpy.copyULongLongToULongLong, bool_t, uint64_t)
286 
287 # Test int8 to other type
288 test_conversion_promotion(eigenpy.copyInt16ToInt16, int8_t, int16_t)
289 test_conversion_promotion(eigenpy.copyInt16ToInt16, uint8_t, int16_t)
290 test_conversion_promotion(eigenpy.copyUInt16ToUInt16, uint8_t, uint16_t)
291 test_conversion_promotion(eigenpy.copyInt32ToInt32, int8_t, int32_t)
292 test_conversion_promotion(eigenpy.copyInt32ToInt32, uint8_t, int32_t)
293 test_conversion_promotion(eigenpy.copyUInt32ToUInt32, uint8_t, uint32_t)
294 test_conversion_promotion(eigenpy.copyInt64ToInt64, int8_t, int64_t)
295 test_conversion_promotion(eigenpy.copyInt64ToInt64, uint8_t, int64_t)
296 test_conversion_promotion(eigenpy.copyUInt64ToUInt64, uint8_t, uint64_t)
297 test_conversion_promotion(eigenpy.copyLongToLong, int8_t, long_t)
298 test_conversion_promotion(eigenpy.copyLongToLong, uint8_t, long_t)
299 test_conversion_promotion(eigenpy.copyULongToULong, uint8_t, ulong_t)
300 test_conversion_promotion(eigenpy.copyLongLongToLongLong, int8_t, int64_t)
301 test_conversion_promotion(eigenpy.copyLongLongToLongLong, uint8_t, int64_t)
302 test_conversion_promotion(eigenpy.copyULongLongToULongLong, uint8_t, uint64_t)
303 
304 # Test int16 to other type
305 test_conversion_promotion(eigenpy.copyInt32ToInt32, int16_t, int32_t)
306 test_conversion_promotion(eigenpy.copyInt32ToInt32, uint16_t, int32_t)
307 test_conversion_promotion(eigenpy.copyUInt32ToUInt32, uint16_t, uint32_t)
308 test_conversion_promotion(eigenpy.copyInt64ToInt64, int16_t, int64_t)
309 test_conversion_promotion(eigenpy.copyInt64ToInt64, uint16_t, int64_t)
310 test_conversion_promotion(eigenpy.copyUInt64ToUInt64, uint16_t, uint64_t)
311 test_conversion_promotion(eigenpy.copyLongToLong, int16_t, long_t)
312 test_conversion_promotion(eigenpy.copyLongToLong, uint16_t, long_t)
313 test_conversion_promotion(eigenpy.copyULongToULong, uint16_t, ulong_t)
314 test_conversion_promotion(eigenpy.copyLongLongToLongLong, int16_t, int64_t)
315 test_conversion_promotion(eigenpy.copyLongLongToLongLong, uint16_t, int64_t)
316 test_conversion_promotion(eigenpy.copyULongLongToULongLong, uint16_t, uint64_t)
317 
318 # Test int32 to other type
319 test_conversion_promotion(eigenpy.copyInt64ToInt64, int32_t, int64_t)
320 test_conversion_promotion(eigenpy.copyInt64ToInt64, uint32_t, int64_t)
321 test_conversion_promotion(eigenpy.copyUInt64ToUInt64, uint32_t, uint64_t)
322 test_conversion_promotion(eigenpy.copyLongToLong, int32_t, long_t)
323 test_conversion_promotion(eigenpy.copyLongToLong, uint32_t, long_t)
324 test_conversion_promotion(eigenpy.copyULongToULong, uint32_t, ulong_t)
325 test_conversion_promotion(eigenpy.copyLongLongToLongLong, int32_t, int64_t)
326 test_conversion_promotion(eigenpy.copyLongLongToLongLong, uint32_t, int64_t)
327 test_conversion_promotion(eigenpy.copyULongLongToULongLong, uint32_t, uint64_t)
328 
329 # Test float to double
330 test_conversion_promotion(eigenpy.copyDoubleToDouble, float32_t, float64_t)
331 
332 # Test complex to other type
333 test_conversion_promotion(eigenpy.copyCDoubleToCDouble, complex64_t, complex128_t)
334 
336  eigenpy.copyCLongDoubleToCLongDouble,
337  complex64_t,
338  complex256_t,
339 )
340 
341 # Only Linux store complex double into 258 bits.
342 # Then, 128 bits complex can be promoted.
343 if platform.system() == "Linux":
345  eigenpy.copyCLongDoubleToCLongDouble,
346  complex128_t,
347  complex256_t,
348  )
print
void print(const Eigen::SparseMatrix< Scalar, Options > &mat)
Definition: sparse_matrix.cpp:50
test_matrix.test_conversion_promotion
def test_conversion_promotion(function, input_dtype, output_dtype)
Definition: test_matrix.py:265
test_matrix.test_conversion
def test_conversion(function, dtype)
Definition: test_matrix.py:189


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45