Skip to content
Draft
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
12 changes: 11 additions & 1 deletion cuqi/distribution/_gmrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from cuqi.distribution import Distribution
from cuqi.utilities import force_ndarray

import collections

class GMRF(Distribution):
""" Gaussian Markov random field (GMRF).

Expand Down Expand Up @@ -94,6 +96,9 @@ class GMRF(Distribution):
"""

def __init__(self, mean=None, prec=None, bc_type="zero", order=1, **kwargs):
if not isinstance(mean, collections.abc.Iterable) and 'geometry' not in kwargs.keys():
raise ValueError(f"Cannot infer dimension if scalar mean and no supported geometry is provided.")

# Init from abstract distribution class
super().__init__(**kwargs)

Expand Down Expand Up @@ -147,7 +152,12 @@ def mean(self):

@mean.setter
def mean(self, value):
self._mean = force_ndarray(value, flatten=True)
# Force the mean to be an array of proper length.
if isinstance(value, collections.abc.Iterable) or callable(value):
self._mean = force_ndarray(value, flatten=True)
else:
# Direct call to the private '_geometry' as the public 'geometry' tries to infer the dimension using the variable we currently setting.
self._mean = value*np.ones(self._geometry.par_dim)

@property
def prec(self):
Expand Down
Loading