@@ -44,44 +44,88 @@ def vvsort(val, vec, size, xp):
44
44
vec [:, imax ] = temp
45
45
46
46
47
- @pytest .mark .parametrize (
48
- "array" ,
49
- [
50
- [[[1 , - 2 ], [2 , 5 ]]],
51
- [[[1.0 , - 2.0 ], [2.0 , 5.0 ]]],
52
- [[[1.0 , - 2.0 ], [2.0 , 5.0 ]], [[1.0 , - 2.0 ], [2.0 , 5.0 ]]],
53
- ],
54
- ids = [
55
- "[[[1, -2], [2, 5]]]" ,
56
- "[[[1., -2.], [2., 5.]]]" ,
57
- "[[[1., -2.], [2., 5.]], [[1., -2.], [2., 5.]]]" ,
58
- ],
59
- )
60
- def test_cholesky (array ):
61
- a = numpy .array (array )
62
- ia = inp .array (a )
63
- result = inp .linalg .cholesky (ia )
64
- expected = numpy .linalg .cholesky (a )
65
- assert_array_equal (expected , result )
47
+ class TestCholesky :
48
+ @pytest .mark .parametrize (
49
+ "array" ,
50
+ [
51
+ [[1 , 2 ], [2 , 5 ]],
52
+ [[[5 , 2 ], [2 , 6 ]], [[7 , 3 ], [3 , 8 ]], [[3 , 1 ], [1 , 4 ]]],
53
+ [
54
+ [[[5 , 2 ], [2 , 5 ]], [[6 , 3 ], [3 , 6 ]]],
55
+ [[[7 , 2 ], [2 , 7 ]], [[8 , 3 ], [3 , 8 ]]],
56
+ ],
57
+ ],
58
+ ids = [
59
+ "2D_array" ,
60
+ "3D_array" ,
61
+ "4D_array" ,
62
+ ],
63
+ )
64
+ @pytest .mark .parametrize ("dtype" , get_all_dtypes (no_bool = True ))
65
+ def test_cholesky_3d_4d (self , array , dtype ):
66
+ a = numpy .array (array , dtype = dtype )
67
+ ia = inp .array (a )
68
+ result = inp .linalg .cholesky (ia )
69
+ expected = numpy .linalg .cholesky (a )
70
+ assert_dtype_allclose (result , expected )
66
71
72
+ def test_cholesky_strides (self ):
73
+ a_np = numpy .array (
74
+ [
75
+ [5 , 2 , 0 , 0 , 1 ],
76
+ [2 , 6 , 0 , 0 , 2 ],
77
+ [0 , 0 , 7 , 0 , 0 ],
78
+ [0 , 0 , 0 , 4 , 0 ],
79
+ [1 , 2 , 0 , 0 , 5 ],
80
+ ]
81
+ )
67
82
68
- @pytest .mark .parametrize (
69
- "shape" ,
70
- [
71
- (0 , 0 ),
72
- (3 , 0 , 0 ),
73
- ],
74
- ids = [
75
- "(0, 0)" ,
76
- "(3, 0, 0)" ,
77
- ],
78
- )
79
- def test_cholesky_0D (shape ):
80
- a = numpy .empty (shape )
81
- ia = inp .array (a )
82
- result = inp .linalg .cholesky (ia )
83
- expected = numpy .linalg .cholesky (a )
84
- assert_array_equal (expected , result )
83
+ a_dp = inp .array (a_np )
84
+
85
+ # positive strides
86
+ expected = numpy .linalg .cholesky (a_np [::2 , ::2 ])
87
+ result = inp .linalg .cholesky (a_dp [::2 , ::2 ])
88
+ assert_allclose (expected , result , rtol = 1e-3 , atol = 1e-4 )
89
+
90
+ # negative strides
91
+ expected = numpy .linalg .cholesky (a_np [::- 2 , ::- 2 ])
92
+ result = inp .linalg .cholesky (a_dp [::- 2 , ::- 2 ])
93
+ assert_allclose (expected , result , rtol = 1e-3 , atol = 1e-4 )
94
+
95
+ @pytest .mark .parametrize (
96
+ "shape" ,
97
+ [
98
+ (0 , 0 ),
99
+ (3 , 0 , 0 ),
100
+ (0 , 2 , 2 ),
101
+ ],
102
+ ids = [
103
+ "(0, 0)" ,
104
+ "(3, 0, 0)" ,
105
+ "(0, 2, 2)" ,
106
+ ],
107
+ )
108
+ def test_cholesky_empty (self , shape ):
109
+ a = numpy .empty (shape )
110
+ ia = inp .array (a )
111
+ result = inp .linalg .cholesky (ia )
112
+ expected = numpy .linalg .cholesky (a )
113
+ assert_array_equal (expected , result )
114
+
115
+ def test_cholesky_errors (self ):
116
+ a_dp = inp .array ([[1 , 2 ], [2 , 5 ]], dtype = "float32" )
117
+
118
+ # unsupported type
119
+ a_np = inp .asnumpy (a_dp )
120
+ assert_raises (TypeError , inp .linalg .cholesky , a_np )
121
+
122
+ # a.ndim < 2
123
+ a_dp_ndim_1 = a_dp .flatten ()
124
+ assert_raises (inp .linalg .LinAlgError , inp .linalg .cholesky , a_dp_ndim_1 )
125
+
126
+ # a is not square
127
+ a_dp = inp .ones ((2 , 3 ))
128
+ assert_raises (inp .linalg .LinAlgError , inp .linalg .cholesky , a_dp )
85
129
86
130
87
131
@pytest .mark .parametrize (
0 commit comments