|
| 1 | +"""Example demonstrating the usage of the ``auto_weighting`` decorator.""" |
| 2 | + |
| 3 | +import odl |
| 4 | +from odl.space.space_utils import auto_weighting |
| 5 | + |
| 6 | + |
| 7 | +class ScalingOp(odl.Operator): |
| 8 | + |
| 9 | + """Operator that scales input by a constant.""" |
| 10 | + |
| 11 | + def __init__(self, dom, ran, c): |
| 12 | + super(ScalingOp, self).__init__(dom, ran, linear=True) |
| 13 | + self.c = c |
| 14 | + |
| 15 | + def _call(self, x): |
| 16 | + return self.c * x |
| 17 | + |
| 18 | + @property |
| 19 | + @auto_weighting |
| 20 | + def adjoint(self): |
| 21 | + return ScalingOp(self.range, self.domain, self.c) |
| 22 | + |
| 23 | + |
| 24 | +rn = odl.rn(2) # Constant weight 1 |
| 25 | +discr = odl.uniform_discr(0, 4, 2) # Constant weight 2 |
| 26 | +print('*** Spaces ***') |
| 27 | +print('Rn =', rn, '- weight =', rn.weighting.const) |
| 28 | +print('discr =', discr, '- weight =', discr.weighting.const) |
| 29 | +print('') |
| 30 | + |
| 31 | +op1 = ScalingOp(rn, rn, 2) # Same weightings, no scaling in adjoint |
| 32 | +op2 = ScalingOp(discr, discr, 2) # Same weightings, no scaling in adjoint |
| 33 | +op3 = ScalingOp(rn, discr, 2) # Different weightings, adjoint scales |
| 34 | + |
| 35 | +# Look at output of ajoint |
| 36 | +print('*** Ajoint evaluation ***') |
| 37 | +print('Rn -> Rn adjoint at one :', op1.adjoint(op1.range.one())) |
| 38 | +print('discr -> discr adjoint at one:', op2.adjoint(op2.range.one())) |
| 39 | +print('Rn -> discr adjoint at one :', op3.adjoint(op3.range.one())) |
| 40 | +print('') |
| 41 | + |
| 42 | +# Check adjointness |
| 43 | +print('*** Check adjointness ***') |
| 44 | +inner1_dom = op1.domain.one().inner(op1.adjoint(op1.range.one())) |
| 45 | +inner1_ran = op1(op1.domain.one()).inner(op1.range.one()) |
| 46 | +print('Rn -> Rn: <Sx, y> = {}, <x, S^*y> = {}' |
| 47 | + ''.format(inner1_ran, inner1_dom)) |
| 48 | + |
| 49 | +inner2_dom = op2.domain.one().inner(op2.adjoint(op2.range.one())) |
| 50 | +inner2_ran = op2(op2.domain.one()).inner(op2.range.one()) |
| 51 | +print('discr -> discr: <Sx, y> = {}, <x, S^*y> = {}' |
| 52 | + ''.format(inner2_ran, inner2_dom)) |
| 53 | + |
| 54 | +inner3_dom = op3.domain.one().inner(op3.adjoint(op3.range.one())) |
| 55 | +inner3_ran = op3(op3.domain.one()).inner(op3.range.one()) |
| 56 | +print('Rn -> discr: <Sx, y> = {}, <x, S^*y> = {}' |
| 57 | + ''.format(inner3_ran, inner3_dom)) |
0 commit comments