Matrix-vector product with matrix-variate Normal only works for `ndim > 1`
Closed this issue · 0 comments
JonathanWenger commented
Describe the bug
When computing a matrix-vector product with a matrix-variate random variable, if the vector has shape x.shape = (n,)
, then the matrix-vector product raises a TypeError
.
To Reproduce
import numpy as np
from probnum import randvars, linops
n = 5
A = randvars.Normal(mean=linops.Identity(n), cov=linops.SymmetricKronecker(linops.Identity(n)))
x = np.ones(n)
A @ x # raises TypeError
A @ x[:, None] # works as expected
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/randvars/_random_variable.py", line 723, in __matmul__
return matmul(self, other)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/randvars/_arithmetic.py", line 31, in matmul
return _apply(_matmul_fns, rv1, rv2)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/randvars/_arithmetic.py", line 88, in _apply
res = op_registry[key](rv1, rv2)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/randvars/_arithmetic.py", line 311, in _matmul_normal_constant
cov_update = _linear_operators.Kronecker(
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/linops/_kronecker.py", line 108, in __init__
self.B = _utils.aslinop(B)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/linops/_utils.py", line 46, in aslinop
return _linear_operator.Matrix(A=A)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/linops/_linear_operator.py", line 931, in __init__
super().__init__(
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/linops/_linear_operator.py", line 123, in __init__
self.__shape = probnum.utils.as_shape(shape, ndim=2)
File "/home/j/Documents/research/probabilistic_numerics/code/probnum/src/probnum/utils/argutils.py", line 66, in as_shape
raise TypeError(f"The given shape {shape} must have {ndim} dimensions.")
TypeError: The given shape (5,) must have 2 dimensions.
Expected behavior
A Gaussian random variable of shape (n,)
should result instead of a TypeError
.