Commit ffeb61cc authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'fix_draw_sample' of gitlab.mpcdf.mpg.de:ift/NIFTy into fix_draw_sample

parents 94355504 d177d1c0
Pipeline #26490 failed with stage
in 4 minutes and 14 seconds
......@@ -60,12 +60,8 @@ class WienerFilterCurvature(EndomorphicOperator):
def apply(self, x, mode):
return self._op.apply(x, mode)
def draw_inverse_sample(self, dtype=np.float64):
n = self.N.draw_sample(dtype)
s = self.S.draw_sample(dtype)
def draw_sample(self, dtype=np.float64):
n = self.N.inverse_draw_sample(dtype)
s = self.S.inverse_draw_sample(dtype)
d = self.R(s) + n
j = self.R.adjoint_times(self.N.inverse_times(d))
m = self.inverse_times(j)
return s - m
return s - self.R.adjoint_times(n)
......@@ -147,10 +147,18 @@ class DiagonalOperator(EndomorphicOperator):
return res
def draw_sample(self, dtype=np.float64):
if np.issubdtype(self._ldiag.dtype, np.complexfloating):
raise ValueError("cannot draw sample from complex-valued operator")
if np.issubdtype(self._ldiag.dtype, np.complexfloating) or (self._ldiag <= 0.).any():
raise ValueError("operator not positive definite")
res = Field.from_random(random_type="normal", domain=self._domain,
dtype=dtype)
res.local_data[()] *= np.sqrt(self._ldiag)
return res
def inverse_draw_sample(self, dtype=np.float64):
if np.issubdtype(self._ldiag.dtype, np.complexfloating) or (self._ldiag <= 0.).any():
raise ValueError("operator not positive definite")
res = Field.from_random(random_type="normal", domain=self._domain,
dtype=dtype)
res.local_data[()] /= np.sqrt(self._ldiag)
return res
......@@ -17,6 +17,7 @@
# and financially supported by the Studienstiftung des deutschen Volkes.
from .linear_operator import LinearOperator
import numpy as np
class EndomorphicOperator(LinearOperator):
......@@ -35,7 +36,7 @@ class EndomorphicOperator(LinearOperator):
for endomorphic operators."""
return self.domain
def draw_sample(self):
def draw_sample(self, dtype=np.float64):
"""Generate a zero-mean sample
Generates a sample from a Gaussian distribution with zero mean and
......@@ -47,3 +48,19 @@ class EndomorphicOperator(LinearOperator):
A sample from the Gaussian of given covariance.
"""
raise NotImplementedError
def inverse_draw_sample(self, dtype=np.float64):
"""Generates a zero-mean sample
Generates a sample from a Gaussian distribution with zero mean and
covariance given by the inverse of the operator.
Returns
-------
A sample from the Gaussian of given covariance
"""
if self.capability & self.INVERSE_TIMES:
x = self.draw_sample(dtype=dtype)
return self.inverse_times(x)
else:
raise NotImplementedError
......@@ -100,8 +100,17 @@ class ScalingOperator(EndomorphicOperator):
def draw_sample(self, dtype=np.float64):
if self._factor.imag != 0. or self._factor.real <= 0.:
raise ValueError("Operator not positive definite")
raise ValueError("operator not positive definite")
return Field.from_random(random_type="normal",
domain=self._domain,
std=np.sqrt(self._factor),
dtype=dtype)
def inverse_draw_sample(self, dtype=np.float64):
if self._factor.imag != 0. or self._factor.real <= 0.:
raise ValueError("operator not positive definite")
return Field.from_random(random_type="normal",
domain=self._domain,
std=1./np.sqrt(self._factor),
dtype=dtype)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment