8000 Remove matrix by jagerber48 · Pull Request #334 · lmfit/uncertainties · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove matrix #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions tests/test_ulinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ def test_list_inverse():
# 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)
Expand Down
35 changes: 9 additions & 26 deletions uncertainties/unumpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,22 +453,12 @@
See the definition of func_with_deriv_to_uncert_func() for its
detailed semantics.
"""

inverse = numpy.linalg.inv(arr)
# The inverse of a numpy.matrix is a numpy.matrix. It is assumed
# that numpy.linalg.inv is such that other types yield
# numpy.ndarrays:
if issubclass(input_type, numpy.matrix):
inverse = inverse.view(numpy.matrix)
yield inverse

# It is mathematically convenient to work with matrices:
inverse_mat = numpy.asmatrix(inverse)

# Successive derivatives of the inverse:
for derivative in derivatives:
derivative_mat = numpy.asmatrix(derivative)
yield -inverse_mat * derivative_mat * inverse_mat
yield -inverse @ derivative @ inverse

Check warning on line 461 in uncertainties/unumpy/core.py

View check run for this annotation

Codecov / codecov/patch

uncertainties/unumpy/core.py#L461

Added line #L461 was not covered by tests


inv = func_with_deriv_to_uncert_func(inv_with_derivatives)
Expand Down Expand Up @@ -496,15 +486,9 @@
"""

inverse = numpy.linalg.pinv(arr, rcond)
# The pseudo-inverse of a numpy.matrix is a numpy.matrix. It is
# assumed that numpy.linalg.pinv is such that other types yield
# numpy.ndarrays:
if issubclass(input_type, numpy.matrix):
inverse = inverse.view(numpy.matrix)
yield inverse

# It is mathematically convenient to work with matrices:
inverse_mat = numpy.asmatrix(inverse)

# Formula (4.12) from The Differentiation of Pseudo-Inverses and
# Nonlinear Least Squares Problems Whose Variables
Expand All @@ -516,20 +500,19 @@
# http://mathoverflow.net/questions/25778/analytical-formula-for-numerical-derivative-of-the-matrix-pseudo-inverse

# Shortcuts. All the following factors should be numpy.matrix objects:
PA = arr * inverse_mat
AP = inverse_mat * arr
factor21 = inverse_mat * inverse_mat.H
PA = arr @ inverse
AP = inverse @ arr
factor21 = inverse @ inverse.conj().T

Check warning on line 505 in uncertainties/unumpy/core.py

View check run for this annotation

Codecov / codecov/patch

uncertainties/unumpy/core.py#L503-L505

Added lines #L503 - L505 were not covered by tests
factor22 = numpy.eye(arr.shape[0]) - PA
factor31 = numpy.eye(arr.shape[1]) - AP
factor32 = inverse_mat.H * inverse_mat
factor32 = inverse.conj().T @ inverse

Check warning on line 508 in uncertainties/unumpy/core.py

View check run for this annotation

Codecov / codecov/patch

uncertainties/unumpy/core.py#L508

Added line #L508 was not covered by tests

# Successive derivatives of the inverse:
for derivative in derivatives:
derivative_mat = numpy.asmatrix(derivative)
term1 = -inverse_mat * derivative_mat * inverse_mat
derivative_mat_H = derivative_mat.H
term2 = factor21 * derivative_mat_H * factor22
term3 = factor31 * derivative_mat_H * factor32
term1 = -inverse @ derivative @ inverse
derivative_H = derivative.conj().T
term2 = factor21 @ derivative_H @ factor22
term3 = factor31 @ derivative_H @ factor32

Check warning on line 515 in uncertainties/unumpy/core.py

View check run for this annotation

Codecov / codecov/patch

uncertainties/unumpy/core.py#L512-L515

Added lines #L512 - L515 were not covered by tests
yield term1 + term2 + term3


Expand Down
Loading
0