Skip to content

Commit b01740a

Browse files
committed
DOC: add linalg.vector_norm example
[skip actions][skip azp][skip cirrus] The example is an adaptation of `np.vector_norm` examples.
1 parent 974ed52 commit b01740a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

numpy/linalg/_linalg.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3235,7 +3235,7 @@ def trace(x, /, *, offset=0, dtype=None):
32353235
3.0
32363236
>>> a = np.arange(8).reshape((2, 2, 2))
32373237
>>> np.linalg.trace(a)
3238-
array([6, 8])
3238+
array([3, 11])
32393239
32403240
Trace is computed with the last two axes as the 2-d sub-arrays.
32413241
This behavior differs from :py:func:`numpy.trace` which uses the first two
@@ -3555,6 +3555,34 @@ def vector_norm(x, /, *, axis=None, keepdims=False, ord=2):
35553555
--------
35563556
numpy.linalg.norm : Generic norm function
35573557
3558+
Examples
3559+
--------
3560+
>>> from numpy import linalg as LA
3561+
>>> a = np.arange(9) + 1
3562+
>>> a
3563+
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
3564+
>>> b = a.reshape((3, 3))
3565+
>>> b
3566+
array([[1, 2, 3],
3567+
[4, 5, 6],
3568+
[7, 8, 9]])
3569+
3570+
>>> LA.vector_norm(b)
3571+
16.881943016134134
3572+
>>> LA.vector_norm(b, ord=np.inf)
3573+
9.0
3574+
>>> LA.vector_norm(b, ord=-np.inf)
3575+
1.0
3576+
3577+
>>> LA.vector_norm(b, ord=1)
3578+
45.0
3579+
>>> LA.vector_norm(b, ord=-1)
3580+
0.3534857623790153
3581+
>>> LA.vector_norm(b, ord=2)
3582+
16.881943016134134
3583+
>>> LA.vector_norm(b, ord=-2)
3584+
0.8058837395885292
3585+
35583586
"""
35593587
x = asanyarray(x)
35603588
shape = list(x.shape)

0 commit comments

Comments
 (0)