test_user_type.py
Go to the documentation of this file.
1 import user_type
2 import numpy as np
3 
4 # from packaging import version
5 
6 rows = 10
7 cols = 20
8 
9 
10 def test(dtype):
11  mat = np.array(np.ones((rows, cols)).astype(np.intc), dtype=dtype)
12  mat = np.random.rand(rows, cols).astype(dtype)
13  mat_copy = mat.copy()
14  assert (mat == mat_copy).all()
15  assert not (mat != mat_copy).all()
16 
17  # if version.parse(np.__version__) >= version.parse("1.21.0"):
18  # # check if it fixes for new version of NumPy
19  # mat.fill(mat.dtype.type(20.0))
20  # mat_copy = mat.copy()
21  # assert (mat == mat_copy).all()
22  # assert not (mat != mat_copy).all()
23 
24  mat_op = mat + mat
25  mat_op = mat.copy(order="F") + mat.copy(order="C")
26 
27  mat_op = mat - mat
28  mat_op = mat * mat
29  mat_op = mat.dot(mat.T)
30  mat_op = mat / mat
31 
32  mat_op = -mat # noqa
33 
34  assert (mat >= mat).all()
35  assert (mat <= mat).all()
36  assert not (mat > mat).all()
37  assert not (mat < mat).all()
38 
39  mat2 = mat.dot(mat.T)
40  mat2_ref = mat.astype(np.double).dot(mat.T.astype(np.double))
41  assert np.isclose(mat2.astype(np.double), mat2_ref).all()
42  if np.__version__ >= "1.17.0":
43  mat2 = np.matmul(mat, mat.T)
44  assert np.isclose(mat2.astype(np.double), mat2_ref).all()
45 
46 
47 def test_cast(from_dtype, to_dtype):
48  np.can_cast(from_dtype, to_dtype)
49 
50  from_mat = np.zeros((rows, cols), dtype=from_dtype)
51  to_mat = from_mat.astype(dtype=to_dtype) # noqa
52 
53 
54 test(user_type.CustomDouble)
55 
56 test_cast(user_type.CustomDouble, np.double)
57 test_cast(np.double, user_type.CustomDouble)
58 
59 test_cast(user_type.CustomDouble, np.int64)
60 test_cast(np.int64, user_type.CustomDouble)
61 
62 test_cast(user_type.CustomDouble, np.int32)
63 test_cast(np.int32, user_type.CustomDouble)
64 
65 test(user_type.CustomFloat)
66 
67 v = user_type.CustomDouble(1)
68 a = np.array(v)
69 assert type(v) == a.dtype.type
test_user_type.test
def test(dtype)
Definition: test_user_type.py:10
test_user_type.test_cast
def test_cast(from_dtype, to_dtype)
Definition: test_user_type.py:47


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Tue Jan 23 2024 03:15:01