Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH: Improve np.linalg.det performance #28649

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 7 commits into from
Apr 9, 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
12 changes: 12 additions & 0 deletions 12 benchmarks/benchmarks/bench_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def time_norm(self, typename):
class LinalgSmallArrays(Benchmark):
""" Test overhead of linalg methods for small arrays """
def setup(self):
self.array_3_3 = np.eye(3) + np.arange(9.).reshape((3, 3))
self.array_3 = np.arange(3.)
self.array_5 = np.arange(5.)
self.array_5_5 = np.reshape(np.arange(25.), (5, 5))

Expand All @@ -112,6 +114,16 @@ def time_norm_small_array(self):
def time_det_small_array(self):
np.linalg.det(self.array_5_5)

def time_det_3x3(self):
np.linalg.det(self.array_3_3)

def time_solve_3x3(self):
np.linalg.solve(self.array_3_3, self.array_3)

def time_eig_3x3(self):
np.linalg.eig(self.array_3_3)


class Lstsq(Benchmark):
def setup(self):
self.a = get_squares_()['float64']
Expand Down
22 changes: 5 additions & 17 deletions 22 numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ def _assert_stacked_2d(*arrays):

def _assert_stacked_square(*arrays):
for a in arrays:
m, n = a.shape[-2:]
try:
m, n = a.shape[-2:]
except ValueError:
raise LinAlgError('%d-dimensional array given. Array must be '
'at least two-dimensional' % a.ndim)
if m != n:
raise LinAlgError('Last 2 dimensions of the array must be square')

Expand Down Expand Up @@ -392,7 +396,6 @@ def solve(a, b):

"""
a, _ = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
b, wrap = _makearray(b)
t, result_t = _commonType(a, b)
Expand Down Expand Up @@ -599,7 +602,6 @@ def inv(a):

"""
a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)

Expand Down Expand Up @@ -681,7 +683,6 @@ def matrix_power(a, n):

"""
a = asanyarray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)

try:
Expand Down Expand Up @@ -830,7 +831,6 @@ def cholesky(a, /, *, upper=False):
"""
gufunc = _umath_linalg.cholesky_up if upper else _umath_linalg.cholesky_lo
a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)
signature = 'D->D' if isComplexType(t) else 'd->d'
Expand Down Expand Up @@ -1201,7 +1201,6 @@ def eigvals(a):

"""
a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
_assert_finite(a)
t, result_t = _commonType(a)
Expand Down Expand Up @@ -1310,7 +1309,6 @@ def eigvalsh(a, UPLO='L'):
gufunc = _umath_linalg.eigvalsh_up

a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)
signature = 'D->d' if isComplexType(t) else 'd->d'
Expand All @@ -1320,11 +1318,6 @@ def eigvalsh(a, UPLO='L'):
w = gufunc(a, signature=signature)
return w.astype(_realType(result_t), copy=False)

def _convertarray(a):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch that this is not actually used!

t, result_t = _commonType(a)
a = a.astype(t).T.copy()
return a, t, result_t


# Eigenvectors

Expand Down Expand Up @@ -1461,7 +1454,6 @@ def eig(a):

"""
a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
_assert_finite(a)
t, result_t = _commonType(a)
Expand Down Expand Up @@ -1612,7 +1604,6 @@ def eigh(a, UPLO='L'):
raise ValueError("UPLO argument must be 'L' or 'U'")

a, wrap = _makearray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)

Expand Down Expand Up @@ -1978,7 +1969,6 @@ def cond(x, p=None):
else:
# Call inv(x) ignoring errors. The result array will
# contain nans in the entries where inversion failed.
_assert_stacked_2d(x)
_assert_stacked_square(x)
t, result_t = _commonType(x)
signature = 'D->D' if isComplexType(t) else 'd->d'
Expand Down Expand Up @@ -2318,7 +2308,6 @@ def slogdet(a):

"""
a = asarray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)
real_t = _realType(result_t)
Expand Down Expand Up @@ -2377,7 +2366,6 @@ def det(a):

"""
a = asarray(a)
_assert_stacked_2d(a)
_assert_stacked_square(a)
t, result_t = _commonType(a)
signature = 'D->D' if isComplexType(t) else 'd->d'
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.