From c0ea19217ca7d58832788767bb15d39533807fb3 Mon Sep 17 00:00:00 2001
From: Jorn Hoofwijk <jornhoofwijk@gmail.com>
Date: Mon, 17 Sep 2018 21:19:14 +0200
Subject: [PATCH] change code to be more explicit in my humble opinion

---
 adaptive/learner/learner1D.py | 38 +++++++++++++++++------------------
 1 file changed, 18 insertions(+), 20 deletions(-)

diff --git a/adaptive/learner/learner1D.py b/adaptive/learner/learner1D.py
index f940c2e7..7efff7fe 100644
--- a/adaptive/learner/learner1D.py
+++ b/adaptive/learner/learner1D.py
@@ -147,38 +147,36 @@ class Learner1D(BaseLearner):
                 self.losses_combined[x_left, x_right] = loss
 
     def update_losses(self, x, real=True):
-        a, b = self.find_neighbors(x, self.neighbors_combined)
         x_left, x_right = self.find_neighbors(x, self.neighbors)
-        losses_combined = self.losses_combined
+        a, b = self.find_neighbors(x, self.neighbors_combined)
 
+        self.losses_combined.pop((a, b), None) # since x is going in-between
+        
         if real:
             self.update_interpolated_loss_in_interval(x_left, x)
             self.update_interpolated_loss_in_interval(x, x_right)
             self.losses.pop((x_left, x_right), None)
             self.losses_combined.pop((x_left, x_right), None)
-            a, b = self.find_neighbors(x, self.neighbors_combined)
-            self.losses_combined.pop((a, b), None)
-
-            if x_left is None and a is not None:
-                losses_combined[a, x] = float('inf')
-            if x_right is None and b is not None:
-                losses_combined[x, b] = float('inf')
         else:
             if x_left is not None and x_right is not None:
                 dx = x_right - x_left
                 loss = self.losses[x_left, x_right]
-                losses_combined[a, x] = (x - a) * loss / dx
-                losses_combined[x, b] = (b - x) * loss / dx
+                self.losses_combined[a, x] = (x - a) * loss / dx
+                self.losses_combined[x, b] = (b - x) * loss / dx
+
+        # (no real point left of x) or (no real point right of a)
+        left_loss_is_unknown = (x_left is None) or
+                               (not real and x_right is None)
+        if (a is not None) and left_loss_is_unknown:
+            self.losses_combined[a, x] = float('inf')
+
+        # (no real point right of x) or (no real point left of b)
+        right_loss_is_unknown = (x_right is None) or
+                                (not real and x_left is None)
+        if (b is not None) and right_loss_is_unknown:
+            self.losses_combined[x, b] = float('inf')
+
         
-        real_left = real and x_left is None
-        real_right = real and x_right is None
-        not_real = (not real) and (x_left is None or x_right is None)
-        if (a is not None) and (real_left or not_real):
-            losses_combined[a, x] = float('inf')
-        if (b is not None) and (real_right or not_real):
-            losses_combined[x, b] = float('inf')
-
-        losses_combined.pop((a, b), None)
 
     def find_neighbors(self, x, neighbors):
         if x in neighbors:
-- 
GitLab