mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
feat: guards added for functionality that breaks with numpy>=1.25 and
autograd==1.5.
This commit is contained in:
parent
f14042132f
commit
13ace62262
3 changed files with 31 additions and 20 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
from packaging import version
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import autograd.numpy as anp # Thinly-wrapped numpy
|
import autograd.numpy as anp # Thinly-wrapped numpy
|
||||||
from .obs import derived_observable, CObs, Obs, import_jackknife
|
from .obs import derived_observable, CObs, Obs, import_jackknife
|
||||||
|
@ -260,6 +261,8 @@ def _mat_mat_op(op, obs, **kwargs):
|
||||||
|
|
||||||
def eigh(obs, **kwargs):
|
def eigh(obs, **kwargs):
|
||||||
"""Computes the eigenvalues and eigenvectors of a given hermitian matrix of Obs according to np.linalg.eigh."""
|
"""Computes the eigenvalues and eigenvectors of a given hermitian matrix of Obs according to np.linalg.eigh."""
|
||||||
|
if version.parse(np.__version__) >= version.parse("1.25.0"):
|
||||||
|
raise NotImplementedError("eigh error propagation is not working with numpy>=1.25 and autograd==1.5.")
|
||||||
w = derived_observable(lambda x, **kwargs: anp.linalg.eigh(x)[0], obs)
|
w = derived_observable(lambda x, **kwargs: anp.linalg.eigh(x)[0], obs)
|
||||||
v = derived_observable(lambda x, **kwargs: anp.linalg.eigh(x)[1], obs)
|
v = derived_observable(lambda x, **kwargs: anp.linalg.eigh(x)[1], obs)
|
||||||
return w, v
|
return w, v
|
||||||
|
@ -278,6 +281,8 @@ def pinv(obs, **kwargs):
|
||||||
|
|
||||||
def svd(obs, **kwargs):
|
def svd(obs, **kwargs):
|
||||||
"""Computes the singular value decomposition of a matrix of Obs."""
|
"""Computes the singular value decomposition of a matrix of Obs."""
|
||||||
|
if version.parse(np.__version__) >= version.parse("1.25.0"):
|
||||||
|
raise NotImplementedError("svd error propagation is not working with numpy>=1.25 and autograd==1.5.")
|
||||||
u = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[0], obs)
|
u = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[0], obs)
|
||||||
s = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[1], obs)
|
s = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[1], obs)
|
||||||
vh = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[2], obs)
|
vh = derived_observable(lambda x, **kwargs: anp.linalg.svd(x, full_matrices=False)[2], obs)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from packaging import version
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import autograd.numpy as anp
|
import autograd.numpy as anp
|
||||||
import math
|
import math
|
||||||
|
@ -291,23 +292,26 @@ def test_matrix_functions():
|
||||||
diff = entry - sym[i, j]
|
diff = entry - sym[i, j]
|
||||||
assert diff.is_zero()
|
assert diff.is_zero()
|
||||||
|
|
||||||
# Check eigh
|
# These linalg functions don't work with numpy>=1.25 and autograd==1.5.
|
||||||
e, v = pe.linalg.eigh(sym)
|
# Remove this guard once this is fixed in autograd.
|
||||||
for i in range(dim):
|
if version.parse(np.__version__) < version.parse("1.25.0"):
|
||||||
tmp = sym @ v[:, i] - v[:, i] * e[i]
|
# Check eigh
|
||||||
for j in range(dim):
|
e, v = pe.linalg.eigh(sym)
|
||||||
assert tmp[j].is_zero()
|
for i in range(dim):
|
||||||
|
tmp = sym @ v[:, i] - v[:, i] * e[i]
|
||||||
|
for j in range(dim):
|
||||||
|
assert tmp[j].is_zero()
|
||||||
|
|
||||||
# Check eig function
|
# Check eig function
|
||||||
e2 = pe.linalg.eig(sym)
|
e2 = pe.linalg.eig(sym)
|
||||||
assert np.all(np.sort(e) == np.sort(e2))
|
assert np.all(np.sort(e) == np.sort(e2))
|
||||||
|
|
||||||
# Check svd
|
# Check svd
|
||||||
u, v, vh = pe.linalg.svd(sym)
|
u, v, vh = pe.linalg.svd(sym)
|
||||||
diff = sym - u @ np.diag(v) @ vh
|
diff = sym - u @ np.diag(v) @ vh
|
||||||
|
|
||||||
for (i, j), entry in np.ndenumerate(diff):
|
for (i, j), entry in np.ndenumerate(diff):
|
||||||
assert entry.is_zero()
|
assert entry.is_zero()
|
||||||
|
|
||||||
# Check determinant
|
# Check determinant
|
||||||
assert pe.linalg.det(np.diag(np.diag(matrix))) == np.prod(np.diag(matrix))
|
assert pe.linalg.det(np.diag(np.diag(matrix))) == np.prod(np.diag(matrix))
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from packaging import version
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pyerrors as pe
|
import pyerrors as pe
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -5,10 +6,11 @@ import pytest
|
||||||
np.random.seed(0)
|
np.random.seed(0)
|
||||||
|
|
||||||
|
|
||||||
def test_mpm():
|
if version.parse(np.__version__) < version.parse("1.25.0"):
|
||||||
corr_content = []
|
def test_mpm():
|
||||||
for t in range(8):
|
corr_content = []
|
||||||
f = 0.8 * np.exp(-0.4 * t)
|
for t in range(8):
|
||||||
corr_content.append(pe.pseudo_Obs(np.random.normal(f, 1e-2 * f), 1e-2 * f, 't'))
|
f = 0.8 * np.exp(-0.4 * t)
|
||||||
|
corr_content.append(pe.pseudo_Obs(np.random.normal(f, 1e-2 * f), 1e-2 * f, 't'))
|
||||||
|
|
||||||
res = pe.mpm.matrix_pencil_method(corr_content)
|
res = pe.mpm.matrix_pencil_method(corr_content)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue