Skip to content

Commit 77e8267

Browse files
author
m.lohmann
committed
Fields can now be added to the "highest" one
1 parent 9003572 commit 77e8267

File tree

4 files changed

+125
-119
lines changed

4 files changed

+125
-119
lines changed

odl/operator/operator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,6 @@ class OperatorSum(Operator):
10711071
10721072
The sum is only well-defined for `Operator` instances where
10731073
`Operator.range` is a `LinearSpace`.
1074-
10751074
"""
10761075

10771076
def __init__(self, left, right, tmp_ran=None, tmp_dom=None):
@@ -1106,8 +1105,14 @@ def __init__(self, left, right, tmp_ran=None, tmp_dom=None):
11061105
rn(3).element([ 2., 4., 6.])
11071106
"""
11081107
if left.range != right.range:
1109-
raise OpTypeError('operator ranges {!r} and {!r} do not match'
1110-
''.format(left.range, right.range))
1108+
if isinstance(left.range, Field) and \
1109+
isinstance(right.range, Field):
1110+
range = left.range + right.range
1111+
else:
1112+
raise OpTypeError('operator ranges {!r} and {!r} do not match'
1113+
''.format(left.range, right.range))
1114+
else:
1115+
range = left.range
11111116
if not isinstance(left.range, (LinearSpace, Field)):
11121117
raise OpTypeError('`left.range` {!r} not a `LinearSpace` or '
11131118
'`Field` instance'.format(left.range))
@@ -1124,7 +1129,7 @@ def __init__(self, left, right, tmp_ran=None, tmp_dom=None):
11241129
''.format(tmp_dom, left.domain))
11251130

11261131
super(OperatorSum, self).__init__(
1127-
left.domain, left.range,
1132+
left.domain, range,
11281133
linear=left.is_linear and right.is_linear)
11291134
self.__left = left
11301135
self.__right = right

odl/set/sets.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,25 @@ def field(self):
310310
"""
311311
return self
312312

313+
def contains_set(self, other):
314+
raise NotImplementedError('field {!r} does not have method '
315+
'`contains_set`'.format(self))
316+
317+
def __add__(self, other):
318+
if self.contains_set(other):
319+
return self
320+
elif other.contains_set(self):
321+
return other
322+
else:
323+
raise ValueError('Fields {!r} and {!r} do not include '
324+
'each other'.format(self, other))
325+
326+
def __radd__(self, other):
327+
if other == 0:
328+
return self
329+
else:
330+
return self.__add__(other)
331+
313332

314333
class ComplexNumbers(Field):
315334

@@ -428,10 +447,7 @@ def __hash__(self):
428447

429448
def element(self, inp=None):
430449
"""Return a real number from ``inp`` or from scratch."""
431-
if inp is not None:
432-
return float(inp)
433-
else:
434-
return 0.0
450+
return float(getattr(inp, 'real', 0.0))
435451

436452
@property
437453
def examples(self):

0 commit comments

Comments
 (0)