Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_matrix():
"Matrices of numbers with uncertainties"
# Matrix inversion:
# Matrix with a mix of Variable objects and regular
# Python numbers:
m = unumpy.matrix([[ufloat((10, 1)), -3.1],
[0, ufloat((3, 0))]])
m_nominal_values = unumpy.nominal_values(m)
# Test of the nominal_value attribute:
assert numpy.all(m_nominal_values == m.nominal_values)
assert type(m[0, 0]) == uncertainties.Variable
# Test of scalar multiplication, both sides:
3*m
m*3
def test_component_extraction():
"Extracting the nominal values and standard deviations from an array"
arr = unumpy.uarray(([1, 2], [0.1, 0.2]))
assert numpy.all(unumpy.nominal_values(arr) == [1, 2])
assert numpy.all(unumpy.std_devs(arr) == [0.1, 0.2])
# unumpy matrices, in addition, should have nominal_values that
# are simply numpy matrices (not unumpy ones, because they have no
# uncertainties):
mat = unumpy.matrix(arr)
assert numpy.all(unumpy.nominal_values(mat) == [1, 2])
assert numpy.all(unumpy.std_devs(mat) == [0.1, 0.2])
assert type(unumpy.nominal_values(mat)) == numpy.matrix
# Checks of the numerical values: the diagonal elements of the
# inverse should be the inverses of the diagonal elements of
# m (because we started with a triangular matrix):
assert _numbers_close(1/m_nominal_values[0, 0],
m_inv_uncert[0, 0].nominal_value), "Wrong value"
assert _numbers_close(1/m_nominal_values[1, 1],
m_inv_uncert[1, 1].nominal_value), "Wrong value"
####################
# Checks of the covariances between elements:
x = ufloat((10, 1))
m = unumpy.matrix([[x, x],
[0, 3+2*x]])
m_inverse = m.I
# Check of the properties of the inverse:
m_double_inverse = m_inverse.I
# The initial matrix should be recovered, including its
# derivatives, which define covariances:
assert _numbers_close(m_double_inverse[0, 0].nominal_value,
m[0, 0].nominal_value)
assert _numbers_close(m_double_inverse[0, 0].std_dev(),
m[0, 0].std_dev())
assert matrices_close(m_double_inverse, m)
# Partial test:
def test_pseudo_inverse():
"Tests of the pseudo-inverse"
# Numerical version of the pseudo-inverse:
pinv_num = core.wrap_array_func(numpy.linalg.pinv)
##########
# Full rank rectangular matrix:
m = unumpy.matrix([[ufloat((10, 1)), -3.1],
[0, ufloat((3, 0))],
[1, -3.1]])
# Numerical and package (analytical) pseudo-inverses: they must be
# the same:
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 matrices_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)
x = ufloat((1, 0.1))
y = ufloat((2, 0.1))
mat = unumpy.matrix([[x, x], [y, 0]])
# Internal consistency: the inverse and the pseudo-inverse yield
# the same result on square matrices:
assert matrices_close(mat.I, unumpy.ulinalg.pinv(mat), 1e-4)
assert matrices_close(unumpy.ulinalg.inv(mat),
# Support for the optional pinv argument is
# tested:
unumpy.ulinalg.pinv(mat, 1e-15), 1e-4)
# Non-square matrices:
x = ufloat((1, 0.1))
y = ufloat((2, 0.1))
mat1 = unumpy.matrix([[x, y]]) # "Long" matrix
mat2 = unumpy.matrix([[x, y], [1, 3+x], [y, 2*x]]) # "Tall" matrix
# Internal consistency:
assert matrices_close(mat1.I, unumpy.ulinalg.pinv(mat1, 1e-10))
assert matrices_close(mat2.I, unumpy.ulinalg.pinv(mat2, 1e-8))
def test_list_pseudo_inverse():
"Test of the pseudo-inverse"
x = ufloat((1, 0.1))
y = ufloat((2, 0.1))
mat = unumpy.matrix([[x, x], [y, 0]])
# Internal consistency: the inverse and the pseudo-inverse yield
# the same result on square matrices:
assert matrices_close(mat.I, unumpy.ulinalg.pinv(mat), 1e-4)
assert matrices_close(unumpy.ulinalg.inv(mat),
# Support for the optional pinv argument is
# tested:
unumpy.ulinalg.pinv(mat, 1e-15), 1e-4)
# Non-square matrices:
x = ufloat((1, 0.1))
y = ufloat((2, 0.1))
mat1 = unumpy.matrix([[x, y]]) # "Long" matrix
mat2 = unumpy.matrix([[x, y], [1, 3+x], [y, 2*x]]) # "Tall" matrix
# Internal consistency:
'''
num = range(len(slopes))
allComb = cartesian((num, num))
comb = []
# remove doubles
for row in allComb:
if row[0] != row[1]:
comb.append(row)
comb = np.array(comb)
# initialize the matrix to store the line intersections
lineX = np.zeros((len(comb), 2), dtype='object')
# for each line intersection...
for j, row in enumerate(comb):
sl = np.array([slopes[row[0]], slopes[row[1]]])
a = unumpy.matrix(np.vstack((-sl, np.ones((2)))).T)
b = np.array([intercepts[row[0]], intercepts[row[1]]])
lineX[j] = np.dot(a.I, b)
com = np.mean(lineX, axis=0)
return com[0], com[1]
'''
num = range(len(slopes))
allComb = cartesian((num, num))
comb = []
# remove doubles
for row in allComb:
if row[0] != row[1]:
comb.append(row)
comb = np.array(comb)
# initialize the matrix to store the line intersections
lineX = np.zeros((len(comb), 2), dtype='object')
# for each line intersection...
for j, row in enumerate(comb):
sl = np.array([slopes[row[0]], slopes[row[1]]])
a = unumpy.matrix(np.vstack((-sl, np.ones((2)))).T)
b = np.array([intercepts[row[0]], intercepts[row[1]]])
lineX[j] = np.dot(a.I, b)
com = np.mean(lineX, axis=0)
return com[0], com[1]