Commit 7f18a1cc authored by Theo Steininger's avatar Theo Steininger

Removed trace_log and determinat methods from diagonal operator.

parent 3dc1ba04
......@@ -70,7 +70,7 @@ class ConjugateGradient(Loggable, object):
References
----------
Thomas V. Mikosch et al., "Numerical Optimization", Second Edition,
Jorge Nocedal & Stephen Wright, "Numerical Optimization", Second Edition,
2006, Springer-Verlag New York
"""
......
......@@ -132,127 +132,6 @@ class DiagonalOperator(EndomorphicOperator):
return self._times_helper(x, spaces,
operation=lambda z: z.adjoint().__rdiv__)
def diagonal(self, bare=False, copy=True):
""" Returns the diagonal of the Operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
copy : boolean
Whether the returned Field should be copied or not.
Returns
-------
out : Field
The diagonal of the Operator.
"""
if bare:
diagonal = self._diagonal.weight(power=-1)
elif copy:
diagonal = self._diagonal.copy()
else:
diagonal = self._diagonal
return diagonal
def inverse_diagonal(self, bare=False):
""" Returns the inverse-diagonal of the operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
Returns
-------
out : Field
The inverse of the diagonal of the Operator.
"""
return 1./self.diagonal(bare=bare, copy=False)
def trace(self, bare=False):
""" Returns the trace the operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
Returns
-------
out : scalar
The trace of the Operator.
"""
return self.diagonal(bare=bare, copy=False).sum()
def inverse_trace(self, bare=False):
""" Returns the inverse-trace of the operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
Returns
-------
out : scalar
The inverse of the trace of the Operator.
"""
return self.inverse_diagonal(bare=bare).sum()
def trace_log(self):
""" Returns the trave-log of the operator.
Returns
-------
out : scalar
the trace of the logarithm of the Operator.
"""
log_diagonal = nifty_log(self.diagonal(copy=False))
return log_diagonal.sum()
def determinant(self):
""" Returns the determinant of the operator.
Returns
-------
out : scalar
out : scalar
the determinant of the Operator
"""
return self.diagonal(copy=False).val.prod()
def inverse_determinant(self):
""" Returns the inverse-determinant of the operator.
Returns
-------
out : scalar
the inverse-determinant of the Operator
"""
return 1/self.determinant()
def log_determinant(self):
""" Returns the log-eterminant of the operator.
Returns
-------
out : scalar
the log-determinant of the Operator
"""
return np.log(self.determinant())
# ---Mandatory properties and methods---
@property
......
......@@ -85,7 +85,7 @@ class ResponseOperator(LinearOperator):
kernel_smoothing = len(self._domain)*[None]
kernel_exposure = len(self._domain)*[None]
if len(sigma)!= len(exposure):
if len(sigma) != len(exposure):
raise ValueError("Length of smoothing kernel and length of"
"exposure do not match")
......
......@@ -76,61 +76,3 @@ class DiagonalOperator_Tests(unittest.TestCase):
D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
tt = D.adjoint_inverse_times(rand1)
assert_equal(tt.domain[0], space)
@expand(product(spaces, [True, False]))
def test_diagonal(self, space, copy):
Please register or sign in to reply
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
diag_op = D.diagonal()
assert_allclose(diag.val.get_full_data(), diag_op.val.get_full_data())
@expand(product(spaces, [True, False]))
def test_inverse(self, space, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
diag_op = D.inverse_diagonal()
assert_allclose(1./diag.val.get_full_data(), diag_op.val.get_full_data())
@expand(product(spaces, [True, False]))
def test_trace(self, space, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
trace_op = D.trace()
assert_allclose(trace_op, np.sum(diag.val.get_full_data()))
@expand(product(spaces, [True, False]))
def test_inverse_trace(self, space, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
trace_op = D.inverse_trace()
assert_allclose(trace_op, np.sum(1./diag.val.get_full_data()))
@expand(product(spaces, [True, False]))
#MR FIXME: what if any diagonal element <=0?
def test_trace_log(self, space, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
trace_log = D.trace_log()
assert_allclose(trace_log, np.sum(np.log(diag.val.get_full_data())))
@expand(product(spaces, [True, False]))
def test_determinant(self, space, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, copy=copy)
det = D.determinant()
assert_allclose(det, np.prod(diag.val.get_full_data()))
@expand(product(spaces, [True, False], [True, False]))
def test_inverse_determinant(self, space, bare, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
inv_det = D.inverse_determinant()
assert_allclose(inv_det, 1./D.determinant())
@expand(product(spaces, [True, False], [True, False]))
#MR FIXME: what if determinant <=0?
def test_log_determinant(self, space, bare, copy):
diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(space, diagonal=diag, bare=bare, copy=copy)
log_det = D.log_determinant()
assert_allclose(log_det, np.log(D.determinant()))
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