summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorPieter Eendebak <pieter.eendebak@gmail.com>2022-05-03 14:52:12 +0200
committerGitHub <noreply@github.com>2022-05-03 14:52:12 +0200
commit5c740d0ca3d35c151c5120a551a69319ffac04d9 (patch)
tree5ec156f3e2e7deb9228fe4ef6e3dee530a1c12f2 /numpy
parentb222eb66c79b8eccba39f46f020ed8303614a87f (diff)
downloadnumpy-5c740d0ca3d35c151c5120a551a69319ffac04d9.tar.gz
PERF: Reduce overhead of np.linalg.norm for small arrays (#21394)
Reduce the overhead of np.linalg.norm by replacing dot(x,x) with x.dot(x). This is OK, since `x` is converted to a base-class array here.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/linalg/linalg.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index 7ce854197..40f259001 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -2520,9 +2520,11 @@ def norm(x, ord=None, axis=None, keepdims=False):
x = x.ravel(order='K')
if isComplexType(x.dtype.type):
- sqnorm = dot(x.real, x.real) + dot(x.imag, x.imag)
+ x_real = x.real
+ x_imag = x.imag
+ sqnorm = x_real.dot(x_real) + x_imag.dot(x_imag)
else:
- sqnorm = dot(x, x)
+ sqnorm = x.dot(x)
ret = sqrt(sqnorm)
if keepdims:
ret = ret.reshape(ndim*[1])