format on save

This commit is contained in:
Martin Bubel 2023-10-06 18:50:55 +02:00
parent 58f61d5a5c
commit 8af7c8286c

View file

@ -1,18 +1,19 @@
import numpy as np
import scipy as sp
from ..util.linalg import jitchol,trace_dot, ijk_jlk_to_il, ijk_ljk_to_ilk
from ..util.linalg import jitchol, trace_dot, ijk_jlk_to_il, ijk_ljk_to_ilk
class LinalgTests(np.testing.TestCase):
def setUp(self):
#Create PD matrix
A = np.random.randn(20,100)
class LinalgTests:
def setup(self):
# Create PD matrix
A = np.random.randn(20, 100)
self.A = A.dot(A.T)
#compute Eigdecomp
# compute Eigdecomp
vals, vectors = np.linalg.eig(self.A)
#Set smallest eigenval to be negative with 5 rounds worth of jitter
# Set smallest eigenval to be negative with 5 rounds worth of jitter
vals[vals.argmin()] = 0
default_jitter = 1e-6*np.mean(vals)
vals[vals.argmin()] = -default_jitter*(10**3.5)
default_jitter = 1e-6 * np.mean(vals)
vals[vals.argmin()] = -default_jitter * (10**3.5)
self.A_corrupt = (vectors * vals).dot(vectors.T)
def test_jitchol_success(self):
@ -23,7 +24,9 @@ class LinalgTests(np.testing.TestCase):
L = jitchol(self.A_corrupt, maxtries=5)
A_new = L.dot(L.T)
diff = A_new - self.A_corrupt
np.testing.assert_allclose(diff, np.eye(A_new.shape[0])*np.diag(diff).mean(), atol=1e-13)
np.testing.assert_allclose(
diff, np.eye(A_new.shape[0]) * np.diag(diff).mean(), atol=1e-13
)
def test_jitchol_failure(self):
try:
@ -38,23 +41,23 @@ class LinalgTests(np.testing.TestCase):
def test_trace_dot(self):
N = 5
A = np.random.rand(N,N)
B = np.random.rand(N,N)
A = np.random.rand(N, N)
B = np.random.rand(N, N)
trace = np.trace(A.dot(B))
test_trace = trace_dot(A,B)
np.testing.assert_allclose(trace,test_trace,atol=1e-13)
test_trace = trace_dot(A, B)
np.testing.assert_allclose(trace, test_trace, atol=1e-13)
def test_einsum_ij_jlk_to_ilk(self):
A = np.random.randn(15, 150, 5)
B = np.random.randn(150, 50, 5)
pure = np.einsum('ijk,jlk->il', A, B)
quick = ijk_jlk_to_il(A,B)
pure = np.einsum("ijk,jlk->il", A, B)
quick = ijk_jlk_to_il(A, B)
np.testing.assert_allclose(pure, quick)
def test_einsum_ijk_ljk_to_ilk(self):
A = np.random.randn(150, 20, 5)
B = np.random.randn(150, 20, 5)
#B = A.copy()
pure = np.einsum('ijk,ljk->ilk', A, B)
quick = ijk_ljk_to_ilk(A,B)
# B = A.copy()
pure = np.einsum("ijk,ljk->ilk", A, B)
quick = ijk_ljk_to_ilk(A, B)
np.testing.assert_allclose(pure, quick)