Skip to content

Commit

Permalink
Adjust Gram matrix tests: now the lower triangle is really undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
projekter committed Nov 6, 2024
1 parent 8d5ddf1 commit 201fd19
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions sparse_dot_mkl/tests/test_gram_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ def setUp(self):
self.mat1_d = self.MATRIX_1.toarray()

gram_ut = np.dot(self.MATRIX_1.toarray().T, self.MATRIX_1.toarray())
gram_ut[np.tril_indices(gram_ut.shape[0], k=-1)] = 0.0
self.tril = np.tril_indices(gram_ut.shape[0], k=-1)
gram_ut[self.tril] = 0.0
self.gram_ut = gram_ut

gram_ut_t = np.dot(self.MATRIX_1.toarray(), self.MATRIX_1.toarray().T)
gram_ut_t[np.tril_indices(gram_ut_t.shape[0], k=-1)] = 0.0
self.tril_t = np.tril_indices(gram_ut_t.shape[0], k=-1)
gram_ut_t[self.tril_t] = 0.0
self.gram_ut_t = gram_ut_t


Expand All @@ -52,6 +54,7 @@ def test_gram_matrix_sp_single(self):

def test_gram_matrix_d_single(self):
mat2 = gram_matrix_mkl(self.mat1.astype(self.single_dtype), dense=True)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)

mat2 = gram_matrix_mkl(
Expand All @@ -63,7 +66,7 @@ def test_gram_matrix_d_single(self):
),
out_scalar=1.0,
)
mat2[np.tril_indices(mat2.shape[0], k=-1)] = 0.0
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)

with self.assertRaises(ValueError):
Expand All @@ -78,6 +81,7 @@ def test_gram_matrix_d(self):
print(self.mat1)

mat2 = gram_matrix_mkl(self.mat1, dense=True)
mat2[self.tril] = 0.0 # lower triangle is undefined
print(mat2 - self.gram_ut)
print(mat2[np.tril_indices(mat2.shape[0], k=1)])

Expand All @@ -92,7 +96,7 @@ def test_gram_matrix_d(self):
),
out_scalar=1.0,
)
mat2[np.tril_indices(mat2.shape[0], k=-1)] = 0.0
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut)

def test_gram_matrix_sp_t(self):
Expand All @@ -101,6 +105,7 @@ def test_gram_matrix_sp_t(self):

def test_gram_matrix_d_t(self):
mat2 = gram_matrix_mkl(self.mat1, dense=True, transpose=True)
mat2[self.tril_t] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut_t)

def test_gram_matrix_csc_sp(self):
Expand All @@ -110,13 +115,15 @@ def test_gram_matrix_csc_sp(self):
def test_gram_matrix_csc_d(self):
mat = self.mat1.tocsc()
mat2 = gram_matrix_mkl(mat, dense=True, cast=True)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat.toarray(), self.mat1.toarray())
np_almost_equal(mat2, self.gram_ut)


class TestGramMatrixDense(TestGramMatrix):
def test_gram_matrix_dd_double(self):
mat2 = gram_matrix_mkl(self.mat1.toarray(), dense=True)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut)

mat2 = gram_matrix_mkl(
Expand All @@ -128,12 +135,14 @@ def test_gram_matrix_dd_double(self):
),
out_scalar=1.0,
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut)

def test_gram_matrix_dd_single(self):
mat2 = gram_matrix_mkl(
self.mat1.astype(self.single_dtype).toarray(), dense=True
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)

mat2 = gram_matrix_mkl(
Expand All @@ -145,13 +154,15 @@ def test_gram_matrix_dd_single(self):
),
out_scalar=1.0,
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)

def test_gram_matrix_dd_double_F(self):
mat2 = gram_matrix_mkl(
np.asarray(self.mat1.toarray(), order="F"),
dense=True
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut)

mat2 = gram_matrix_mkl(
Expand All @@ -164,6 +175,7 @@ def test_gram_matrix_dd_double_F(self):
),
out_scalar=1.0,
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut)

def test_gram_matrix_dd_single_F(self):
Expand All @@ -174,6 +186,7 @@ def test_gram_matrix_dd_single_F(self):
),
dense=True,
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)

mat2 = gram_matrix_mkl(
Expand All @@ -189,6 +202,7 @@ def test_gram_matrix_dd_single_F(self):
),
out_scalar=1.0,
)
mat2[self.tril] = 0.0 # lower triangle is undefined
np_almost_equal(mat2, self.gram_ut, decimal=5)


Expand Down

0 comments on commit 201fd19

Please sign in to comment.