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_list_inverse():
"Test of the inversion of a square matrix"
mat_list = [[1, 1], [1, 0]]
# numpy.linalg.inv(mat_list) does calculate the inverse even
# though mat_list is a list of lists (and not a matrix). Can
# ulinalg do the same? Here is a test:
mat_list_inv = unumpy.ulinalg.inv(mat_list)
# More type testing:
mat_matrix = numpy.asmatrix(mat_list)
assert isinstance(unumpy.ulinalg.inv(mat_matrix),
type(numpy.linalg.inv(mat_matrix)))
# unumpy.ulinalg should behave in the same way as numpy.linalg,
# with respect to types:
mat_list_inv_numpy = numpy.linalg.inv(mat_list)
assert type(mat_list_inv) == type(mat_list_inv_numpy)
# The resulting matrix does not have to be a matrix that can
# handle uncertainties, because the input matrix does not have
# uncertainties:
assert not isinstance(mat_list_inv, unumpy.matrix)
# Individual element check:
assert isinstance(mat_list_inv[1, 1], float)
assert mat_list_inv[1, 1] == -1
# handle uncertainties, because the input matrix does not have
# uncertainties:
assert not isinstance(mat_list_inv, unumpy.matrix)
# Individual element check:
assert isinstance(mat_list_inv[1, 1], float)
assert mat_list_inv[1, 1] == -1
x = ufloat((1, 0.1))
y = ufloat((2, 0.1))
mat = unumpy.matrix([[x, x], [y, 0]])
# 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 matrices_close(unumpy.ulinalg.inv(mat), mat.I)
Input matrix.
The states are [roll rate,
steer rate,
roll angle,
steer angle]
The inputs are [roll torque,
steer torque]
'''
a11 = -v * C1
a12 = -(g * K0 + v**2 * K2)
a21 = np.eye(2)
a22 = np.zeros((2, 2))
A = np.vstack((np.dot(unumpy.ulinalg.inv(M), np.hstack((a11, a12))),
np.hstack((a21, a22))))
B = np.vstack((unumpy.ulinalg.inv(M), np.zeros((2, 2))))
return A, B
The states are [roll rate,
steer rate,
roll angle,
steer angle]
The inputs are [roll torque,
steer torque]
'''
a11 = -v * C1
a12 = -(g * K0 + v**2 * K2)
a21 = np.eye(2)
a22 = np.zeros((2, 2))
A = np.vstack((np.dot(unumpy.ulinalg.inv(M), np.hstack((a11, a12))),
np.hstack((a21, a22))))
B = np.vstack((unumpy.ulinalg.inv(M), np.zeros((2, 2))))
return A, B