From 2448274fa78048587d0a6e010c64d3c91443615a Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 4 May 2023 14:56:00 -0700 Subject: [PATCH] move `project()` into `utils_np` --- project.py | 41 ----------------------------------------- utils_np.py | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 42 deletions(-) delete mode 100644 project.py diff --git a/project.py b/project.py deleted file mode 100644 index 6dd9fdf..0000000 --- a/project.py +++ /dev/null @@ -1,41 +0,0 @@ -import numpy as np - - -#def project(a, b): # projects b onto the unit cube, using a as an offset origin -# a = np.asanyarray(a) -# b = np.asanyarray(b) -# d = b - a -# if all(np.abs(d) <= 1e-8): -# return a -# inner = -1 / np.where(np.abs(d) > 1e-8, d, np.where(d >= 0, 1e-8, -1e-8)) -# small = np.minimum(1 - a, a - 1) -# large = np.maximum(0 - a, a - 0) -# rescale = np.min(np.maximum(inner * small, inner * large)) -# assert np.isfinite(rescale), rescale -# if rescale < 1: -# return a + (rescale - 1e-6) * d -# else: -# return b - - -def project(p, a, eps=1e-8): - # https://www.desmos.com/calculator/gdcu0ivk0i - p = np.asanyarray(p) - a = np.asanyarray(a) - d = p - a - if all(np.abs(d) <= eps): - # we might still be inching out of bounds, so just to be sure: - a[a <= 0] = 0 - a[a >= 1] = 1 - return a - - inner = 1 / np.where(np.abs(d) > eps, d, np.where(d >= 0, eps, -eps)) - small = -np.abs(p - 1) # np.minimum(1 - p, p - 1) - large = np.abs(p) # np.maximum(0 - p, p - 0) - rescale = np.min(np.maximum(inner * small, inner * large)) - - if rescale <= 1: - b = p - max(0, rescale - 1e-8) * d - return b - else: - return a diff --git a/utils_np.py b/utils_np.py index a77f5d6..3c3706a 100644 --- a/utils_np.py +++ b/utils_np.py @@ -1,9 +1,31 @@ # i've separated numpy-dependent methods from the rest of the utils. -from project import project from utils import AcquireForWriting, merge_summaries, feps, m33, m34, m93 import numpy as np +def project(p, a, eps=1e-8): + # https://www.desmos.com/calculator/gdcu0ivk0i + p = np.asanyarray(p) + a = np.asanyarray(a) + d = p - a + if all(np.abs(d) <= eps): + # we might still be inching out of bounds, so just to be sure: + a[a <= 0] = 0 + a[a >= 1] = 1 + return a + + inner = 1 / np.where(np.abs(d) > eps, d, np.where(d >= 0, eps, -eps)) + small = -np.abs(p - 1) # np.minimum(1 - p, p - 1) + large = np.abs(p) # np.maximum(0 - p, p - 0) + rescale = np.min(np.maximum(inner * small, inner * large)) + + if rescale <= 1: + b = p - max(0, rescale - 1e-8) * d + return b + else: + return a + + def do_bounding(x, method="clip"): if method == "clip": x = np.clip(x, 0, 1)