GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: Improve np.linalg.det performance by eendebakpt · Pull Request #28649 · numpy/numpy

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.

Sign up for GitHub

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
mhvk merged 7 commits into numpy:main from eendebakpt:linalg_refactor
Apr 9, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

ENH: Improve np.linalg.det performance #28649

Changes from 1 commit
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
a4ff9e0
ENH: Improve np.linalg.det performance
eendebakpt Apr 4, 2025
05abe63
Update numpy/linalg/_linalg.py
eendebakpt Apr 4, 2025
c020ed7
revert change to complex detection
eendebakpt Apr 9, 2025
a760647
use suggestion
eendebakpt Apr 9, 2025
3f094cd
whitespace
eendebakpt Apr 9, 2025
ab6cc07
add more small array benchmarks
eendebakpt Apr 9, 2025
2fc384f
trigger build
eendebakpt Apr 9, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
ENH: Improve np.linalg.det performance
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt
eendebakpt committed Apr 4, 2025
commit a4ff9e0b32702d81129de16bc4b21f4b8f359dea

There are no files selected for viewing

23 changes: 5 additions & 18 deletions 23 numpy/linalg/_linalg.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def _commonType(*arrays):
for a in arrays:
type_ = a.dtype.type
if issubclass(type_, inexact):
if isComplexType(type_):
is_complex = True
is_complex = is_complex or isComplexType(type_):
Comment thread
eendebakpt marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


rt = _realType(type_, default=None)
if rt is double:
result_type = double
Expand Down Expand Up @@ -197,6 +197,9 @@ def _assert_stacked_2d(*arrays):

def _assert_stacked_square(*arrays):
for a in arrays:
if a.ndim < 2:
Comment thread Outdated

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I like the combination. If one really wants to get out the most, it could be

try:
    m, n = a.shape[-2:]
except ValueError:
    riase LinAlgError(f"{a.ndim}-dimensional...") from None
if m != n:
   ...

Using that these days try/except has no cost if no exception is raised.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, pushed this after submitting the review - above is the most relevant comment! (and not very relevant at that!)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The _assert_stacked_square is about 10% faster using your suggestion, I updated the PR with this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
raise LinAlgError('%d-dimensional array given. Array must be '
'at least two-dimensional' % a.ndim)
m, n = a.shape[-2:]
if m != n:
raise LinAlgError('Last 2 dimensions of the array must be square')
Expand Down Expand Up @@ -392,7 +395,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 +601,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 +682,6 @@ def matrix_power(a, n):

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

try:
Expand Down Expand Up @@ -830,7 +830,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 +1200,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 +1308,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 +1317,6 @@ def eigvalsh(a, UPLO='L'):
w = gufunc(a, signature=signature)
return w.astype(_realType(result_t), copy=False)

def _convertarray(a):
Comment thread

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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


# Eigenvectors

Expand Down Expand Up @@ -1461,7 +1453,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 +1603,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 +1968,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 +2307,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 +2365,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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.