From 3517e77dac0ffefb5d5d21260c49dfafc8948fd3 Mon Sep 17 00:00:00 2001
From: Reimar Leike <reimar@leike.name>
Date: Mon, 23 Jul 2018 15:42:08 +0200
Subject: [PATCH] added .sum() and .size method to MultiFields

---
 nifty5/multi/multi_field.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/nifty5/multi/multi_field.py b/nifty5/multi/multi_field.py
index 4f3852968..33150a033 100644
--- a/nifty5/multi/multi_field.py
+++ b/nifty5/multi/multi_field.py
@@ -147,6 +147,36 @@ class MultiField(object):
         """
         return np.sqrt(np.abs(self.vdot(x=self)))
 
+    def sum(self):
+        """ Computes the sum all field values.
+
+        Returns
+        -------
+        norm : float
+            The sum of the field values.
+        """
+        result = 0.0
+        for v in self._val:
+            if isinstance(v, Field):
+                #not using += here in case of complex numbers
+                result = result + v.sum()
+        return result
+
+    @property
+    def size(self):
+        """ Computes the overall degrees of freedom.
+
+        Returns
+        -------
+        size : int
+            The sum of the size of the individual fields
+        """
+        result = 0
+        for k in self.keys():
+            result += self._domain[k].size
+        return result
+
+
     def squared_norm(self):
         """ Computes the square of the L2-norm of the field values.
 
-- 
GitLab