Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tabmat/ext/dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def dense_sandwich(np.ndarray X, floating[:] d, int[:] rows, int[:] cols, int th
elif X.flags["F_CONTIGUOUS"]:
_denseF_sandwich(rowsp, colsp, Xp, dp, outp, in_n, out_m, m, n, thresh1d, kratio, innerblock)
else:
raise Exception()
raise Exception("The matrix X is not contiguous.")
return out


Expand Down
25 changes: 25 additions & 0 deletions tests/test_fast_sandwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import pytest
import scipy as sp
import scipy.sparse
from scipy.sparse import csc_matrix

from tabmat.ext.dense import dense_sandwich
from tabmat.ext.sparse import sparse_sandwich

from tabmat import DenseMatrix, SparseMatrix, SplitMatrix


@pytest.mark.parametrize("dtype", [np.float64, np.float32])
def test_fast_sandwich_sparse(dtype):
Expand Down Expand Up @@ -62,6 +65,28 @@ def test_fast_sandwich_dense():
check(A, d, cols)


def test_dense_sandwich_on_non_contiguous():
"""Non-regression test for #208"""
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100, 20))

# Xd wraps a not-contiguous array.
Xd = DenseMatrix(X[:, :10])
Xs = SparseMatrix(csc_matrix(X[:, 10:]))
Xm = SplitMatrix([Xd, Xs])

# Making the sandwich product fail.
with pytest.raises(Exception, match="The matrix X is not contiguous"):
Xm.sandwich(np.ones(X.shape[0]))

# Xd wraps a copy, which makes the data contiguous.
Xd = DenseMatrix(X[:, :10].copy())
Xm = SplitMatrix([Xd, Xs])

# The sandwich product works without problem here.
Xm.sandwich(np.ones(X.shape[0]))


def check(A, d, cols):
Asub = A[:, cols]
true = (Asub.T.multiply(d)).dot(Asub).toarray()
Expand Down