From 1a2813cf5b3e6e5e19baddec6fe1386a205d1463 Mon Sep 17 00:00:00 2001
From: Pablo Piskunow <pablo.perez.piskunow@gmail.com>
Date: Mon, 26 Aug 2019 14:47:00 +0200
Subject: [PATCH] use less memory to build the moments matrix for all energies

---
 kwant/kpm.py | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/kwant/kpm.py b/kwant/kpm.py
index db19f4ea..a897c8f9 100644
--- a/kwant/kpm.py
+++ b/kwant/kpm.py
@@ -731,23 +731,25 @@ class Correlator:
         g_kernel[0] /= 2
         mu_kernel = np.outer(g_kernel, g_kernel) * self.moments_matrix
 
-        e = (self.energies - self._b) / self._a
+        e_scaled = (self.energies - self._b) / self._a
 
-        # arrays for faster calculation
-        sqrt_e = np.sqrt(1 - e ** 2)
-        arccos_e = np.arccos(e)
+        m_array = np.arange(n_moments)
+        def _integral_factor(e):
+            # arrays for faster calculation
+            sqrt_e = np.sqrt(1 - e ** 2)
+            arccos_e = np.arccos(e)
 
-        exp_n = np.exp(1j * np.outer(arccos_e, np.arange(n_moments)))
-        t_n = np.real(exp_n)
+            exp_n = np.exp(1j * arccos_e * m_array)
+            t_n = np.real(exp_n)
 
-        e_plus = (np.outer(e, np.ones(n_moments)) -
-                  1j * np.outer(sqrt_e, np.arange(n_moments)))
-        e_plus = e_plus * exp_n
+            e_plus = (e - 1j * sqrt_e * m_array)
+            e_plus = e_plus * exp_n
 
-        big_gamma = e_plus[:, None, :] * t_n[:, :, None]
-        big_gamma += big_gamma.conj().swapaxes(1, 2)
-
-        self._integral_factor = np.tensordot(mu_kernel, big_gamma.T)
+            big_gamma = e_plus[None, :] * t_n[:, None]
+            big_gamma += big_gamma.conj().T
+            return np.tensordot(mu_kernel, big_gamma.T)
+        self._integral_factor = np.array([_integral_factor(e)
+                                          for e in e_scaled]).T
 
 
 def conductivity(hamiltonian, alpha='x', beta='x', positions=None, **kwargs):
-- 
GitLab