scaling_operator.py 2.93 KB
 Martin Reinecke committed Dec 29, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 ``````# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # `````` Martin Reinecke committed Jan 17, 2018 14 ``````# Copyright(C) 2013-2018 Max-Planck-Society `````` Martin Reinecke committed Dec 29, 2017 15 16 17 18 19 20 21 22 23 24 25 26 ``````# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik # and financially supported by the Studienstiftung des deutschen Volkes. from __future__ import division import numpy as np from ..field import Field from ..domain_tuple import DomainTuple from .endomorphic_operator import EndomorphicOperator class ScalingOperator(EndomorphicOperator): `````` Martin Reinecke committed Feb 16, 2018 27 `````` """Operator which multiplies a Field with a scalar. `````` Martin Reinecke committed Dec 29, 2017 28 `````` `````` Martin Reinecke committed Jan 18, 2018 29 `````` The NIFTy ScalingOperator class is a subclass derived from the `````` Martin Reinecke committed Dec 29, 2017 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 `````` EndomorphicOperator. It multiplies an input field with a given factor. Parameters ---------- factor : scalar The multiplication factor domain : tuple of DomainObjects The domain on which the Operator's input Field lives. """ def __init__(self, factor, domain): super(ScalingOperator, self).__init__() if not np.isscalar(factor): raise TypeError("Scalar required") self._factor = factor self._domain = DomainTuple.make(domain) def apply(self, x, mode): self._check_input(x, mode) `````` Martin Reinecke committed Jan 05, 2018 51 52 53 54 55 `````` if self._factor == 1.: return x.copy() if self._factor == 0.: return Field.zeros_like(x, dtype=x.dtype) `````` Martin Reinecke committed Dec 29, 2017 56 57 58 59 60 61 62 63 64 `````` if mode == self.TIMES: return x*self._factor elif mode == self.ADJOINT_TIMES: return x*np.conj(self._factor) elif mode == self.INVERSE_TIMES: return x*(1./self._factor) else: return x*(1./np.conj(self._factor)) `````` Martin Reinecke committed Jan 05, 2018 65 66 `````` @property def inverse(self): `````` Martin Reinecke committed Jan 30, 2018 67 `````` if self._factor != 0.: `````` Martin Reinecke committed Jan 19, 2018 68 69 70 `````` return ScalingOperator(1./self._factor, self._domain) from .inverse_operator import InverseOperator return InverseOperator(self) `````` Martin Reinecke committed Jan 05, 2018 71 72 73 `````` @property def adjoint(self): `````` Martin Reinecke committed Feb 04, 2018 74 `````` return ScalingOperator(np.conj(self._factor), self._domain) `````` Martin Reinecke committed Jan 05, 2018 75 `````` `````` Martin Reinecke committed Dec 29, 2017 76 77 78 79 80 81 `````` @property def domain(self): return self._domain @property def capability(self): `````` Martin Reinecke committed Jan 30, 2018 82 `````` if self._factor == 0.: `````` Martin Reinecke committed Jan 19, 2018 83 `````` return self.TIMES | self.ADJOINT_TIMES `````` Martin Reinecke committed Feb 06, 2018 84 `````` return self._all_ops `````` Martin Reinecke committed Feb 02, 2018 85 `````` `````` Martin Reinecke committed Feb 05, 2018 86 `````` def draw_sample(self): `````` Martin Reinecke committed Feb 02, 2018 87 88 89 90 `````` return Field.from_random(random_type="normal", domain=self._domain, std=np.sqrt(self._factor), dtype=np.result_type(self._factor))``````