From 7efbe72104eebd41a941db29f2c4d071fdccc9a5 Mon Sep 17 00:00:00 2001 From: Martin Reinecke Date: Wed, 27 Jun 2018 15:27:45 +0200 Subject: [PATCH] introduce and use squared_norm() --- nifty5/field.py | 10 ++++++++++ nifty5/library/unit_log_gauss.py | 2 +- nifty5/multi/multi_field.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/nifty5/field.py b/nifty5/field.py index e8251d2b..8915c7d5 100644 --- a/nifty5/field.py +++ b/nifty5/field.py @@ -487,6 +487,16 @@ class Field(object): """ return np.sqrt(abs(self.vdot(x=self))) + def squared_norm(self): + """ Computes the square of the L2-norm of the field values. + + Returns + ------- + float + The square of the L2-norm of the field values. + """ + return abs(self.vdot(x=self)) + def conjugate(self): """ Returns the complex conjugate of the field. diff --git a/nifty5/library/unit_log_gauss.py b/nifty5/library/unit_log_gauss.py index 66a05c3d..4ed1a12f 100644 --- a/nifty5/library/unit_log_gauss.py +++ b/nifty5/library/unit_log_gauss.py @@ -45,7 +45,7 @@ class UnitLogGauss(Energy): @property @memo def value(self): - return .5 * self._s.value.vdot(self._s.value).real + return .5 * self._s.value.squared_norm() @property @memo diff --git a/nifty5/multi/multi_field.py b/nifty5/multi/multi_field.py index 024cddb6..97b9aa87 100644 --- a/nifty5/multi/multi_field.py +++ b/nifty5/multi/multi_field.py @@ -151,6 +151,16 @@ class MultiField(object): """ return np.sqrt(np.abs(self.vdot(x=self))) + def squared_norm(self): + """ Computes the square of the L2-norm of the field values. + + Returns + ------- + float + The square of the L2-norm of the field values. + """ + return abs(self.vdot(x=self)) + def __neg__(self): return MultiField({key: -val for key, val in self.items()}) -- GitLab