From 66cf9aa575e295c76d97a54bbdcacc091dc96409 Mon Sep 17 00:00:00 2001 From: Martin Reinecke <martin@mpa-garching.mpg.de> Date: Tue, 29 May 2018 20:23:20 +0200 Subject: [PATCH] anticipate a few nice changes from symbolicDifferentiation --- demos/nonlinear_wiener_filter.py | 4 ++-- nifty4/__init__.py | 2 +- nifty4/minimization/line_energy.py | 2 +- nifty4/minimization/scipy_minimizer.py | 2 +- nifty4/operators/linear_operator.py | 8 ++++++++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/demos/nonlinear_wiener_filter.py b/demos/nonlinear_wiener_filter.py index d19e739ac..170d332d3 100644 --- a/demos/nonlinear_wiener_filter.py +++ b/demos/nonlinear_wiener_filter.py @@ -10,8 +10,8 @@ if __name__ == "__main__": p_spec = lambda k: (1. / (k*correlation_length + 1) ** 4) nonlinearity = Tanh() - #nonlinearity = Linear() - #nonlinearity = Exponential() + # nonlinearity = Linear() + # nonlinearity = Exponential() # Set up position space s_space = ift.RGSpace(1024) diff --git a/nifty4/__init__.py b/nifty4/__init__.py index 2f1de367b..842182d7a 100644 --- a/nifty4/__init__.py +++ b/nifty4/__init__.py @@ -28,5 +28,5 @@ from .multi import * __all__ = ["__version__", "dobj", "DomainTuple"] + \ domains.__all__ + operators.__all__ + minimization.__all__ + \ - ["DomainTuple", "Field", "sqrt", "exp", "log"] + \ + ["DomainTuple", "Field", "sqrt", "exp", "log", "tanh"] + \ multi.__all__ diff --git a/nifty4/minimization/line_energy.py b/nifty4/minimization/line_energy.py index 962271a9f..2ef3518fe 100644 --- a/nifty4/minimization/line_energy.py +++ b/nifty4/minimization/line_energy.py @@ -97,5 +97,5 @@ class LineEnergy(object): if abs(res.imag) / max(abs(res.real), 1.) > 1e-12: from ..logger import logger logger.warning("directional derivative has non-negligible " - "imaginary part:", res) + "imaginary part: {}".format(res)) return res.real diff --git a/nifty4/minimization/scipy_minimizer.py b/nifty4/minimization/scipy_minimizer.py index 7356c3e1c..c876cbc75 100644 --- a/nifty4/minimization/scipy_minimizer.py +++ b/nifty4/minimization/scipy_minimizer.py @@ -98,7 +98,7 @@ class ScipyMinimizer(Minimizer): r = opt.minimize(hlp.fun, x, method=self._method, jac=hlp.jac, hessp=hessp, options=self._options, bounds=bounds) if not r.success: - logger.error("Problem in Scipy minimization:", r.message) + logger.error("Problem in Scipy minimization: {}".format(r.message)) return hlp._energy, IterationController.ERROR return hlp._energy, IterationController.CONVERGED diff --git a/nifty4/operators/linear_operator.py b/nifty4/operators/linear_operator.py index 185c5049b..7799c98a8 100644 --- a/nifty4/operators/linear_operator.py +++ b/nifty4/operators/linear_operator.py @@ -130,16 +130,22 @@ class LinearOperator(NiftyMetaBase()): def __mul__(self, other): from .chain_operator import ChainOperator + if np.isscalar(other) and other == 1.: + return self other = self._toOperator(other, self.domain) return ChainOperator.make([self, other]) def __rmul__(self, other): from .chain_operator import ChainOperator + if np.isscalar(other) and other == 1.: + return self other = self._toOperator(other, self.target) return ChainOperator.make([other, self]) def __add__(self, other): from .sum_operator import SumOperator + if np.isscalar(other) and other == 0.: + return self other = self._toOperator(other, self.domain) return SumOperator.make([self, other], [False, False]) @@ -148,6 +154,8 @@ class LinearOperator(NiftyMetaBase()): def __sub__(self, other): from .sum_operator import SumOperator + if np.isscalar(other) and other == 0.: + return self other = self._toOperator(other, self.domain) return SumOperator.make([self, other], [False, True]) -- GitLab