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.array(np.ones((rows, cols)).astype(np.int32), 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 
48 def test_cast(from_dtype, to_dtype):
49  np.can_cast(from_dtype, to_dtype)
50 
51  from_mat = np.zeros((rows, cols), dtype=from_dtype)
52  to_mat = from_mat.astype(dtype=to_dtype) # noqa
53 
54 
55 test(user_type.CustomDouble)
56 
57 test_cast(user_type.CustomDouble, np.double)
58 test_cast(np.double, user_type.CustomDouble)
59 
60 test_cast(user_type.CustomDouble, np.int64)
61 test_cast(np.int64, user_type.CustomDouble)
62 
63 test_cast(user_type.CustomDouble, np.int32)
64 test_cast(np.int32, user_type.CustomDouble)
65 
66 test(user_type.CustomFloat)
67 
68 v = user_type.CustomDouble(1)
69 a = np.array(v)
70 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:48


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