diff --git a/nifty5/__init__.py b/nifty5/__init__.py index 51cc578bc8bb66e0dddc9c36fbdfe01474ff5df3..7a2acc0bafebf47adc99531d0be137c4c57ea32a 100644 --- a/nifty5/__init__.py +++ b/nifty5/__init__.py @@ -54,6 +54,7 @@ from .operators.selection_operator import SelectionOperator from .operators.slope_operator import SlopeOperator from .operators.smoothness_operator import SmoothnessOperator from .operators.symmetrizing_operator import SymmetrizingOperator +from .operators.vdot_operator import VdotOperator from .probing.utils import probe_with_posterior_samples, probe_diagonal, \ StatCalculator diff --git a/nifty5/operator.py b/nifty5/operator.py index e357e59ecdfbd6b78af3e99c7aed0458fa3c3a39..d47d6df1c449929cfb9d7ed08725b2ef88f9f956 100644 --- a/nifty5/operator.py +++ b/nifty5/operator.py @@ -58,7 +58,7 @@ class Linearization(object): d1 = makeOp(self._val) d2 = makeOp(other._val) return Linearization(self._val*other._val, - self._jac*d2 + d1*other._jac) + d2*self._jac + d1*other._jac) if isinstance(other, (int, float, complex)): # if other == 0: # return ... diff --git a/nifty5/operators/vdot_operator.py b/nifty5/operators/vdot_operator.py new file mode 100644 index 0000000000000000000000000000000000000000..df54dc31b7f9fc5fe624244366b1be9747bc0875 --- /dev/null +++ b/nifty5/operators/vdot_operator.py @@ -0,0 +1,52 @@ +# 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 <http://www.gnu.org/licenses/>. +# +# Copyright(C) 2013-2018 Max-Planck-Society +# +# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik +# and financially supported by the Studienstiftung des deutschen Volkes. + +from __future__ import absolute_import, division, print_function + +import numpy as np + +from ..compat import * +from ..domain_tuple import DomainTuple +from ..domains.unstructured_domain import UnstructuredDomain +from .linear_operator import LinearOperator +from ..sugar import full + + +class VdotOperator(LinearOperator): + def __init__(self, field): + super(VdotOperator, self).__init__() + self._field = field + self._target = DomainTuple.make(UnstructuredDomain(1)) + + @property + def domain(self): + return self._field.domain + + @property + def target(self): + return self._target + + @property + def capability(self): + return self.TIMES | self.ADJOINT_TIMES + + def apply(self, x, mode): + self._check_input(x, mode) + if mode == self.TIMES: + return full(self._target, self._field.vdot(x)) + return self._field*x.to_global_data()[()] diff --git a/test/test_operators/test_adjoint.py b/test/test_operators/test_adjoint.py index b34195f7aa5ef52d3bfc5d2111fc8684c67dac5d..6f625c7057c57f19abcacb0e231bed1ef73211fb 100644 --- a/test/test_operators/test_adjoint.py +++ b/test/test_operators/test_adjoint.py @@ -49,6 +49,13 @@ class Consistency_Tests(unittest.TestCase): op = a+b ift.extra.consistency_check(op, dtype, dtype) + @expand(product(_h_spaces + _p_spaces + _pow_spaces, + [np.float64, np.complex128])) + def testVdotOperator(self, sp, dtype): + op = ift.VdotOperator(ift.Field.from_random("normal", sp, + dtype=dtype)) + ift.extra.consistency_check(op, dtype, dtype) + @expand(product([(ift.RGSpace(10, harmonic=True), 4, 0), (ift.RGSpace((24, 31), distances=(0.4, 2.34), harmonic=True), 3, 0),