Please see the code below.
import numpy as np
a = np.array([-1, -3, -1, -9, 2])
b = np.array([[-1, -3, -1, -9, 2]])
print("Infinity norm of a:", np.linalg.norm(a, ord=np.inf))
print("Infinity norm of b:", np.linalg.norm(b, ord=np.inf))
The output (Google Colab) is:
Infinity norm of a: 9.0
Infinity norm of b: 16.0
Numpy version is 1.25.2
Google Colab Python version is 3.10.12
Please see the description above. I expected the same results. But L-inf norm of 1-D and 2-D arryas are apparently different.
EDIT: Thanks for referring me to the numpy documentation and the table therein. However, is this what Gilbert Strang would expect to get? A row vector and a column vector and their equivalent 1-dim array should have the same L-inf norm. Is this not confusing?
7
The table in the docs directly gives a rationale for this difference!
https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html
ord | norm for matrices | norm for vectors |
---|---|---|
inf |
max(sum(abs(x), axis=1)) |
max(abs(x)) |
As @Julien notes in the comments, you can expressly set the 2D axis to achieve a comparable result
>>> np.linalg.norm(b, ord=np.inf)
16.0
>>> np.linalg.norm(b, ord=np.inf, axis=1)
array([9.])
or, perhaps most clearly, manipulating the internal inf
norm function
>>> from numpy import * # clobber builtin sum() thanks @Julien again!
>>> sum(abs(b), axis=0) # max: 9
array([1, 3, 1, 9, 2])
>>> sum(abs(b), axis=1) # max: 16
array([16])