Skip to content

Commit

Permalink
uarrays_close
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Apr 3, 2024
1 parent 925f74a commit e882aad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions tests/test_ulinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.exit() # There is no reason to test the interface to NumPy

from uncertainties import unumpy, ufloat
from uncertainties.testing import arrays_close
from uncertainties.testing import uuarrays_close

def test_list_inverse():
"Test of the inversion of a square matrix"
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_list_inverse():
# Internal consistency: ulinalg.inv() must coincide with the
# unumpy.matrix inverse, for square matrices (.I is the
# pseudo-inverse, for non-square matrices, but inv() is not).
assert arrays_close(unumpy.ulinalg.inv(mat), mat.I)
assert uarrays_close(unumpy.ulinalg.inv(mat), mat.I)


def test_list_pseudo_inverse():
Expand All @@ -60,8 +60,8 @@ def test_list_pseudo_inverse():

# Internal consistency: the inverse and the pseudo-inverse yield
# the same result on square matrices:
assert arrays_close(mat.I, unumpy.ulinalg.pinv(mat), 1e-4)
assert arrays_close(unumpy.ulinalg.inv(mat),
assert uarrays_close(mat.I, unumpy.ulinalg.pinv(mat), 1e-4)
assert uarrays_close(unumpy.ulinalg.inv(mat),
# Support for the optional pinv argument is
# tested:
unumpy.ulinalg.pinv(mat, 1e-15), 1e-4)
Expand All @@ -73,5 +73,5 @@ def test_list_pseudo_inverse():
mat2 = unumpy.matrix([[x, y], [1, 3+x], [y, 2*x]]) # "Tall" matrix

# Internal consistency:
assert arrays_close(mat1.I, unumpy.ulinalg.pinv(mat1, 1e-10))
assert arrays_close(mat2.I, unumpy.ulinalg.pinv(mat2, 1e-8))
assert uarrays_close(mat1.I, unumpy.ulinalg.pinv(mat1, 1e-10))
assert uarrays_close(mat2.I, unumpy.ulinalg.pinv(mat2, 1e-8))
14 changes: 7 additions & 7 deletions tests/test_unumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import uncertainties.core as uncert_core
from uncertainties import ufloat, unumpy
from uncertainties.unumpy import core
from uncertainties.testing import numbers_close, arrays_close
from uncertainties.testing import numbers_close, uarrays_close

def test_numpy():

Expand Down Expand Up @@ -149,7 +149,7 @@ def test_inverse():
assert numbers_close(m_double_inverse[0, 0].std_dev,
m[0, 0].std_dev)

assert arrays_close(m_double_inverse, m)
assert uarrays_close(m_double_inverse, m)

# Partial test:
assert derivatives_close(m_double_inverse[0, 0], m[0, 0])
Expand All @@ -166,7 +166,7 @@ def test_inverse():

# Correlations between m and m_inverse should create a perfect
# inversion:
assert arrays_close(m * m_inverse, numpy.eye(m.shape[0]))
assert uarrays_close(m * m_inverse, numpy.eye(m.shape[0]))

def test_wrap_array_func():
'''
Expand Down Expand Up @@ -201,7 +201,7 @@ def f(mat, *args, **kwargs):
m_f_wrapped = f_wrapped(m, 2, factor=10)
m_f_unc = f_unc(m, 2, factor=10)

assert arrays_close(m_f_wrapped, m_f_unc)
assert uarrays_close(m_f_wrapped, m_f_unc)


def test_pseudo_inverse():
Expand All @@ -221,22 +221,22 @@ def test_pseudo_inverse():
rcond = 1e-8 # Test of the second argument to pinv()
m_pinv_num = pinv_num(m, rcond)
m_pinv_package = core.pinv(m, rcond)
assert arrays_close(m_pinv_num, m_pinv_package)
assert uarrays_close(m_pinv_num, m_pinv_package)

##########
# Example with a non-full rank rectangular matrix:
vector = [ufloat(10, 1), -3.1, 11]
m = unumpy.matrix([vector, vector])
m_pinv_num = pinv_num(m, rcond)
m_pinv_package = core.pinv(m, rcond)
assert arrays_close(m_pinv_num, m_pinv_package)
assert uarrays_close(m_pinv_num, m_pinv_package)

##########
# Example with a non-full-rank square matrix:
m = unumpy.matrix([[ufloat(10, 1), 0], [3, 0]])
m_pinv_num = pinv_num(m, rcond)
m_pinv_package = core.pinv(m, rcond)
assert arrays_close(m_pinv_num, m_pinv_package)
assert uarrays_close(m_pinv_num, m_pinv_package)

def test_broadcast_funcs():
"""
Expand Down
2 changes: 1 addition & 1 deletion uncertainties/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def compare_derivatives(func, numerical_derivatives,
pass
else:

def arrays_close(m1, m2, precision=1e-4):
def uarrays_close(m1, m2, precision=1e-4):
"""
Returns True iff m1 and m2 are almost equal, where elements
can be either floats or AffineScalarFunc objects.
Expand Down

0 comments on commit e882aad

Please sign in to comment.