From c36aead21213cbb5f4b036ddeebb41dfad55a3b2 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 7 May 2023 06:55:46 -0700 Subject: [PATCH] move a couple math functions into their own file --- thursday/utilities/__init__.py | 1 + thursday/utilities/closures.py | 2 +- thursday/utilities/math.py | 23 +++++++++++++++++++++++ thursday/utilities/utils.py | 23 ----------------------- thursday/utilities/utils_np.py | 3 ++- 5 files changed, 27 insertions(+), 25 deletions(-) create mode 100644 thursday/utilities/math.py diff --git a/thursday/utilities/__init__.py b/thursday/utilities/__init__.py index 6dbac33..45a4a2a 100644 --- a/thursday/utilities/__init__.py +++ b/thursday/utilities/__init__.py @@ -1,5 +1,6 @@ from .closures import * from .colors import * +from .math import * from .prog80 import prog from .scoring import * from .utils import * diff --git a/thursday/utilities/closures.py b/thursday/utilities/closures.py index b7918ff..4e226dd 100644 --- a/thursday/utilities/closures.py +++ b/thursday/utilities/closures.py @@ -1,4 +1,4 @@ -from .utils import scalar_softplus +from .math import scalar_softplus from .utils_np import do_bounding check = object() # secret "key" to pass to wrap_untrustworthy to extract feval_count diff --git a/thursday/utilities/math.py b/thursday/utilities/math.py new file mode 100644 index 0000000..6930cd2 --- /dev/null +++ b/thursday/utilities/math.py @@ -0,0 +1,23 @@ +import math + +feps = 2.0**-23.0 +tiniest = 2.0**-1022.0 + + +def scalar_softplus(x): + if x >= 33.276435657655455: + return float(x) + elif x <= -745.13330078125: + return 0.0 + else: + return math.log1p(math.exp(x)) + + +def phi(d): + # phi(1) = golden ratio + # phi(2) = plastic constant + # phi(3) = the positive real root of x**4-x-1 + x = 2.0 + for i in range(30 if d == 1 else max(10, 28 - d)): + x = pow(1 + x, 1 / (d + 1)) + return x diff --git a/thursday/utilities/utils.py b/thursday/utilities/utils.py index d6da0c2..8243667 100644 --- a/thursday/utilities/utils.py +++ b/thursday/utilities/utils.py @@ -1,27 +1,4 @@ from dataclasses import dataclass -import math - -feps = 2.0**-23.0 -tiniest = 2.0**-1022.0 - - -def scalar_softplus(x): - if x >= 33.276435657655455: - return float(x) - elif x <= -745.13330078125: - return 0.0 - else: - return math.log1p(math.exp(x)) - - -def phi(d): - # phi(1) = golden ratio - # phi(2) = plastic constant - # phi(3) = the positive real root of x**4-x-1 - x = 2.0 - for i in range(30 if d == 1 else max(10, 28 - d)): - x = pow(1 + x, 1 / (d + 1)) - return x @dataclass diff --git a/thursday/utilities/utils_np.py b/thursday/utilities/utils_np.py index 80eb8e2..1c412f2 100644 --- a/thursday/utilities/utils_np.py +++ b/thursday/utilities/utils_np.py @@ -1,6 +1,7 @@ # i've separated numpy-dependent methods from the rest of the utils. from .colors import m33, m34, m93 -from .utils import AcquireForWriting, merge_summaries, feps +from .math import feps +from .utils import AcquireForWriting, merge_summaries from time import time import numpy as np