1
- """
2
- Focused test suite for the dot function in numpy_quaddtype
3
-
4
- This module tests the QuadBLAS dot function for:
5
- - Vector-vector dot products
6
- - Matrix-vector multiplication
7
- - Matrix-matrix multiplication
8
- - Small and large matrix operations
9
- - Basic correctness validation
10
-
11
- Uses only the Sleef backend for simplicity.
12
- """
13
-
14
1
import pytest
15
2
import numpy as np
16
- from numpy_quaddtype import QuadPrecision , QuadPrecDType , dot
3
+ from numpy_quaddtype import QuadPrecision , QuadPrecDType
17
4
18
5
19
6
# ================================================================================
@@ -81,14 +68,14 @@ def create_quad_array(values, shape=None):
81
68
# ================================================================================
82
69
83
70
class TestVectorVectorDot :
84
- """Test vector-vector dot products"""
71
+ """Test vector-vector np.matmul products"""
85
72
86
73
def test_simple_dot_product (self ):
87
- """Test basic vector dot product"""
74
+ """Test basic vector np.matmul product"""
88
75
x = create_quad_array ([1 , 2 , 3 ])
89
76
y = create_quad_array ([4 , 5 , 6 ])
90
77
91
- result = dot (x , y )
78
+ result = np . matmul (x , y )
92
79
expected = 1 * 4 + 2 * 5 + 3 * 6 # = 32
93
80
94
81
assert isinstance (result , QuadPrecision )
@@ -99,14 +86,14 @@ def test_orthogonal_vectors(self):
99
86
x = create_quad_array ([1 , 0 , 0 ])
100
87
y = create_quad_array ([0 , 1 , 0 ])
101
88
102
- result = dot (x , y )
89
+ result = np . matmul (x , y )
103
90
assert_quad_equal (result , 0.0 )
104
91
105
92
def test_same_vector (self ):
106
- """Test dot product of vector with itself"""
93
+ """Test np.matmul product of vector with itself"""
107
94
x = create_quad_array ([2 , 3 , 4 ])
108
95
109
- result = dot (x , x )
96
+ result = np . matmul (x , x )
110
97
expected = 2 * 2 + 3 * 3 + 4 * 4 # = 29
111
98
112
99
assert_quad_equal (result , expected )
@@ -121,7 +108,7 @@ def test_various_vector_sizes(self, size):
121
108
x = create_quad_array (x_vals )
122
109
y = create_quad_array (y_vals )
123
110
124
- result = dot (x , y )
111
+ result = np . matmul (x , y )
125
112
expected = sum (x_vals [i ] * y_vals [i ] for i in range (size ))
126
113
127
114
assert_quad_equal (result , expected )
@@ -131,7 +118,7 @@ def test_negative_and_fractional_values(self):
131
118
x = create_quad_array ([1.5 , - 2.5 , 3.25 ])
132
119
y = create_quad_array ([- 1.25 , 2.75 , - 3.5 ])
133
120
134
- result = dot (x , y )
121
+ result = np . matmul (x , y )
135
122
expected = 1.5 * (- 1.25 ) + (- 2.5 )* 2.75 + 3.25 * (- 3.5 )
136
123
137
124
assert_quad_equal (result , expected )
@@ -151,7 +138,7 @@ def test_simple_matrix_vector(self):
151
138
# 3x1 vector
152
139
x = create_quad_array ([1 , 1 , 1 ])
153
140
154
- result = dot (A , x )
141
+ result = np . matmul (A , x )
155
142
expected = [1 + 2 + 3 , 4 + 5 + 6 ] # [6, 15]
156
143
157
144
assert result .shape == (2 ,)
@@ -164,7 +151,7 @@ def test_identity_matrix_vector(self):
164
151
I = create_quad_array ([1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ], shape = (3 , 3 ))
165
152
x = create_quad_array ([2 , 3 , 4 ])
166
153
167
- result = dot (I , x )
154
+ result = np . matmul (I , x )
168
155
169
156
assert result .shape == (3 ,)
170
157
for i in range (3 ):
@@ -181,7 +168,7 @@ def test_various_matrix_vector_sizes(self, m, n):
181
168
x_vals = [i + 1 for i in range (n )]
182
169
x = create_quad_array (x_vals )
183
170
184
- result = dot (A , x )
171
+ result = np . matmul (A , x )
185
172
186
173
assert result .shape == (m ,)
187
174
@@ -205,7 +192,7 @@ def test_simple_matrix_matrix(self):
205
192
A = create_quad_array ([1 , 2 , 3 , 4 ], shape = (2 , 2 ))
206
193
B = create_quad_array ([5 , 6 , 7 , 8 ], shape = (2 , 2 ))
207
194
208
- result = dot (A , B )
195
+ result = np . matmul (A , B )
209
196
210
197
# Expected: [[1*5+2*7, 1*6+2*8], [3*5+4*7, 3*6+4*8]] = [[19, 22], [43, 50]]
211
198
expected = [[19 , 22 ], [43 , 50 ]]
@@ -221,11 +208,11 @@ def test_identity_matrix_multiplication(self):
221
208
I = create_quad_array ([1 , 0 , 0 , 1 ], shape = (2 , 2 ))
222
209
223
210
# A * I should equal A
224
- result1 = dot (A , I )
211
+ result1 = np . matmul (A , I )
225
212
assert_quad_array_equal (result1 , A )
226
213
227
214
# I * A should equal A
228
- result2 = dot (I , A )
215
+ result2 = np . matmul (I , A )
229
216
assert_quad_array_equal (result2 , A )
230
217
231
218
@pytest .mark .parametrize ("m,n,k" , [(2 ,2 ,2 ), (2 ,3 ,4 ), (3 ,2 ,5 ), (4 ,4 ,4 ), (5 ,6 ,7 )])
@@ -239,7 +226,7 @@ def test_various_matrix_sizes(self, m, n, k):
239
226
B_vals = [(i * n + j + 1 ) for i in range (k ) for j in range (n )]
240
227
B = create_quad_array (B_vals , shape = (k , n ))
241
228
242
- result = dot (A , B )
229
+ result = np . matmul (A , B )
243
230
244
231
assert result .shape == (m , n )
245
232
@@ -258,12 +245,12 @@ def test_associativity(self):
258
245
C = create_quad_array ([1 , 1 , 2 , 1 ], shape = (2 , 2 ))
259
246
260
247
# Compute (A*B)*C
261
- AB = dot (A , B )
262
- result1 = dot (AB , C )
248
+ AB = np . matmul (A , B )
249
+ result1 = np . matmul (AB , C )
263
250
264
251
# Compute A*(B*C)
265
- BC = dot (B , C )
266
- result2 = dot (A , BC )
252
+ BC = np . matmul (B , C )
253
+ result2 = np . matmul (A , BC )
267
254
268
255
assert_quad_array_equal (result1 , result2 , rtol = 1e-25 )
269
256
@@ -285,7 +272,7 @@ def test_large_square_matrices(self, size):
285
272
A = create_quad_array (A_vals , shape = (size , size ))
286
273
B = create_quad_array (B_vals , shape = (size , size ))
287
274
288
- result = dot (A , B )
275
+ result = np . matmul (A , B )
289
276
290
277
assert result .shape == (size , size )
291
278
@@ -303,7 +290,7 @@ def test_large_square_matrices(self, size):
303
290
assert_quad_equal (result [size // 2 , size // 2 ], expected_value , rtol = 1e-15 , atol = 1e-15 )
304
291
305
292
def test_large_vector_operations (self ):
306
- """Test large vector dot products"""
293
+ """Test large vector np.matmul products"""
307
294
size = 1000
308
295
309
296
# Create vectors with known sum
@@ -313,7 +300,7 @@ def test_large_vector_operations(self):
313
300
x = create_quad_array (x_vals )
314
301
y = create_quad_array (y_vals )
315
302
316
- result = dot (x , y )
303
+ result = np . matmul (x , y )
317
304
expected = size * 1.0 * 2.0 # = 2000.0
318
305
319
306
assert_quad_equal (result , expected )
@@ -329,7 +316,7 @@ def test_rectangular_large_matrices(self):
329
316
A = create_quad_array (A_vals , shape = (m , k ))
330
317
B = create_quad_array (B_vals , shape = (k , n ))
331
318
332
- result = dot (A , B )
319
+ result = np . matmul (A , B )
333
320
334
321
assert result .shape == (m , n )
335
322
@@ -354,23 +341,23 @@ def test_dimension_mismatch_vectors(self):
354
341
y = create_quad_array ([1 , 2 , 3 ])
355
342
356
343
with pytest .raises (ValueError , match = "same length" ):
357
- dot (x , y )
344
+ np . matmul (x , y )
358
345
359
346
def test_dimension_mismatch_matrix_vector (self ):
360
347
"""Test dimension mismatch in matrix-vector"""
361
348
A = create_quad_array ([1 , 2 , 3 , 4 ], shape = (2 , 2 ))
362
349
x = create_quad_array ([1 , 2 , 3 ]) # Wrong size
363
350
364
351
with pytest .raises (ValueError , match = "columns must match" ):
365
- dot (A , x )
352
+ np . matmul (A , x )
366
353
367
354
def test_dimension_mismatch_matrices (self ):
368
355
"""Test dimension mismatch in matrix-matrix"""
369
356
A = create_quad_array ([1 , 2 , 3 , 4 ], shape = (2 , 2 ))
370
357
B = create_quad_array ([1 , 2 , 3 , 4 , 5 , 6 ], shape = (3 , 2 )) # Wrong size
371
358
372
359
with pytest .raises (ValueError , match = "Matrix inner dimensions must match" ):
373
- dot (A , B )
360
+ np . matmul (A , B )
374
361
375
362
376
363
if __name__ == "__main__" :
0 commit comments