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


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 26 2025 02:17:29