Skip to content

Commit 632fbe5

Browse files
Avoid nesting Block objects for quasisep kernels (#267)
* Avoid nesting Block objects for quasisep kernels Fixing a bug which caused the addition of three or more quasiseparable kernels to fail in certain cases due to nested Block objects * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Adding block multiplication on the right Avoid crashing from scalar multiplication of kernels with block stationary covariance matrices * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f84c8e3 commit 632fbe5

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/tinygp/kernels/quasisep.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,13 @@ def coord_to_sortable(self, X: JAXArray) -> JAXArray:
243243
return self.kernel1.coord_to_sortable(X)
244244

245245
def _block_or_dense(self, m1: JAXArray, m2: JAXArray) -> JAXArray:
246-
if self.use_block:
247-
return Block(m1, m2)
248-
return jsp_block_diag(m1, m2)
246+
if not self.use_block:
247+
return jsp_block_diag(m1, m2)
248+
249+
# Ensure we don't nest Block objects to fix Issue #265
250+
blocks1 = m1.blocks if isinstance(m1, Block) else (m1,)
251+
blocks2 = m2.blocks if isinstance(m2, Block) else (m2,)
252+
return Block(*blocks1, *blocks2)
249253

250254
def design_matrix(self) -> JAXArray:
251255
return self._block_or_dense(

src/tinygp/solvers/quasisep/block.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def to_dense(self) -> JAXArray:
5555
def __mul__(self, other: Any) -> "Block":
5656
return Block(*(b * other for b in self.blocks))
5757

58+
def __rmul__(self, other: Any) -> "Block":
59+
return self.__mul__(other)
60+
5861
@jax.jit
5962
def __add__(self, other: Any) -> Any:
6063
if isinstance(other, Block):

0 commit comments

Comments
 (0)