From b719e4ae2038ba66a3a911ac9769b6aefe2593e5 Mon Sep 17 00:00:00 2001
From: Martin Reinecke <martin@mpa-garching.mpg.de>
Date: Thu, 2 Aug 2018 20:34:08 +0200
Subject: [PATCH] small optimizations

---
 demos/bernoulli_demo.py       | 2 +-
 demos/getting_started_2.py    | 7 +++----
 nifty5/minimization/energy.py | 4 ++--
 nifty5/multi/multi_field.py   | 9 ++++-----
 nifty5/sugar.py               | 3 ++-
 5 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/demos/bernoulli_demo.py b/demos/bernoulli_demo.py
index 01f6f4135..7efd68a31 100644
--- a/demos/bernoulli_demo.py
+++ b/demos/bernoulli_demo.py
@@ -33,7 +33,7 @@ if __name__ == '__main__':
     # Two-dimensional regular grid with inhomogeneous exposure
     position_space = ift.RGSpace([512, 512])
 
-    # # Sphere with with uniform exposure
+    #  Sphere with uniform exposure
     # position_space = ift.HPSpace(128)
     # exposure = ift.Field.full(position_space, 1.)
 
diff --git a/demos/getting_started_2.py b/demos/getting_started_2.py
index ffdaf5f64..d878ec0df 100644
--- a/demos/getting_started_2.py
+++ b/demos/getting_started_2.py
@@ -49,7 +49,7 @@ if __name__ == '__main__':
     position_space = ift.RGSpace([512, 512])
     exposure = get_2D_exposure()
 
-    # # Sphere with with uniform exposure
+    #  Sphere with uniform exposure
     # position_space = ift.HPSpace(128)
     # exposure = ift.Field.full(position_space, 1.)
 
@@ -70,7 +70,7 @@ if __name__ == '__main__':
     A = pd(a)
 
     # Set up a sky model
-    sky = HT.chain(ift.makeOp(A)).exp()
+    sky = ift.exp(HT.chain(ift.makeOp(A)))
 
     M = ift.DiagonalOperator(exposure)
     GR = ift.GeometryRemover(position_space)
@@ -99,7 +99,6 @@ if __name__ == '__main__':
     H, convergence = minimizer(H)
 
     # Plot results
-    result_sky = sky(H.position)
-    ift.plot(result_sky)
+    ift.plot(sky(H.position))
     ift.plot_finish()
     # FIXME PLOTTING
diff --git a/nifty5/minimization/energy.py b/nifty5/minimization/energy.py
index d2b53529f..8fd83882b 100644
--- a/nifty5/minimization/energy.py
+++ b/nifty5/minimization/energy.py
@@ -25,8 +25,8 @@ from ..utilities import NiftyMetaBase, memo
 class Energy(NiftyMetaBase()):
     """ Provides the functional used by minimization schemes.
 
-   The Energy object is an implementation of a scalar function including its
-   gradient and metric at some position.
+    The Energy object is an implementation of a scalar function including its
+    gradient and metric at some position.
 
     Parameters
     ----------
diff --git a/nifty5/multi/multi_field.py b/nifty5/multi/multi_field.py
index 652bd1700..525e79cec 100644
--- a/nifty5/multi/multi_field.py
+++ b/nifty5/multi/multi_field.py
@@ -221,17 +221,16 @@ class MultiField(object):
             return MultiField.from_dict({key: self[key] for key in subset})
 
     def unite(self, other):
+        if self._domain is other._domain:
+            return self + other
         return self.combine((self, other))
 
     @staticmethod
     def combine(fields):
         res = {}
         for f in fields:
-            for key in f.keys():
-                if key in res:
-                    res[key] = res[key]+f[key]
-                else:
-                    res[key] = f[key]
+            for key, val in f.items():
+                res[key] = res[key]+val if key in res else val
         return MultiField.from_dict(res)
 
 
diff --git a/nifty5/sugar.py b/nifty5/sugar.py
index 5f50fc635..dd290091d 100644
--- a/nifty5/sugar.py
+++ b/nifty5/sugar.py
@@ -261,7 +261,8 @@ for f in ["sqrt", "exp", "log", "tanh", "positive_tanh", "conjugate"]:
     def func(f):
         def func2(x):
             from .linearization import Linearization
-            if isinstance(x, (Field, MultiField, Linearization)):
+            from .operators.operator import Operator
+            if isinstance(x, (Field, MultiField, Linearization, Operator)):
                 return getattr(x, f)()
             else:
                 return getattr(np, f)(x)
-- 
GitLab