Commit c9e87ba7 authored by Martin Reinecke's avatar Martin Reinecke

more

parent 0f2f262e
......@@ -630,7 +630,6 @@ for op in ["__add__", "__radd__",
tval = getattr(self._val, op)(other)
return Field(self._domain, tval)
raise TypeError("should not arrive here")
return NotImplemented
return func2
setattr(Field, op, func(op))
......
......@@ -229,6 +229,7 @@ class MultiField(object):
res[key] = f[key]
return MultiField.from_dict(res)
for op in ["__add__", "__radd__",
"__sub__", "__rsub__",
"__mul__", "__rmul__",
......
......@@ -6,8 +6,6 @@ import numpy as np
from .compat import *
from .utilities import NiftyMetaBase
#from ..domain_tuple import DomainTuple
#from ..multi.multi_domain import MultiDomain
from .field import Field
from .multi.multi_field import MultiField
......@@ -62,8 +60,8 @@ class Linearization(object):
return Linearization(self._val*other._val,
self._jac*d2 + d1*other._jac)
if isinstance(other, (int, float, complex)):
#if other == 0:
# return ...
# if other == 0:
# return ...
return Linearization(self._val*other, self._jac*other)
if isinstance(other, (Field, MultiField)):
d2 = makeOp(other)
......@@ -82,11 +80,13 @@ class Linearization(object):
def make_var(field):
from .operators.scaling_operator import ScalingOperator
return Linearization(field, ScalingOperator(1., field.domain))
@staticmethod
def make_const(field):
from .operators.null_operator import NullOperator
return Linearization(field, NullOperator({}, field.domain))
class Operator(NiftyMetaBase()):
"""Transforms values living on one domain into values living on another
domain, and can also provide the Jacobian.
......
......@@ -5,6 +5,7 @@ from .linear_operator import LinearOperator
from ..multi.multi_domain import MultiDomain
from ..multi.multi_field import MultiField
class FieldAdapter(LinearOperator):
def __init__(self, op, name_dom, name_tgt):
if name_dom is None:
......
......@@ -23,17 +23,19 @@ import numpy as np
from ..compat import *
from ..utilities import my_sum
from .linear_operator import LinearOperator
from ..sugar import domain_union
from ..multi.multi_domain import MultiDomain
from ..multi.multi_field import MultiField
class RelaxedSumOperator(LinearOperator):
"""Class representing sums of operators with compatible MultiDomains."""
"""Class representing sums of operators with compatible domains."""
def __init__(self, ops):
super(RelaxedSumOperator, self).__init__()
self._ops = ops
self._domain = MultiDomain.union([op.domain for op in ops])
self._target = MultiDomain.union([op.target for op in ops])
self._domain = domain_union([op.domain for op in ops])
self._target = domain_union([op.target for op in ops])
self._capability = self.TIMES | self.ADJOINT_TIMES
for op in ops:
self._capability &= op.capability
......@@ -58,9 +60,14 @@ class RelaxedSumOperator(LinearOperator):
self._check_mode(mode)
res = None
for op in self._ops:
tmp = x.extract(op._dom(mode), mode)
if isinstance(x.domain, MultiDomain):
x = x.extract(op._dom(mode))
x = op.apply(x, mode)
if res is None:
res = tmp
else:
res = MultiField.combine([res, tmp])
if isinstance(x.domain, MultiDomain):
res = MultiField.combine([res, tmp])
else:
res = res + tmp
return res
......@@ -38,7 +38,7 @@ __all__ = ['PS_field', 'power_analyze', 'create_power_operator',
'create_harmonic_smoothing_operator', 'from_random',
'full', 'from_global_data', 'from_local_data',
'makeDomain', 'sqrt', 'exp', 'log', 'tanh', 'conjugate',
'get_signal_variance', 'makeOp']
'get_signal_variance', 'makeOp', 'domain_union']
def PS_field(pspace, func):
......@@ -242,6 +242,14 @@ def makeOp(input):
input.domain, tuple(makeOp(val) for val in input.values()))
raise NotImplementedError
def domain_union(domains):
if isinstance(domains[0], DomainTuple):
if any(dom is not domains[0] for dom in domains[1:]):
raise ValueError("domain mismatch")
return domains[0]
return MultiDomain.union(domains)
# Arithmetic functions working on Fields
......
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