Skip to content

Commit dad2629

Browse files
authored
Merge pull request numpy#26679 from luxedo/DOC/21351/np.linalg
DOC: add ``np.linalg`` examples
2 parents 9730ba2 + b01740a commit dad2629

File tree

1 file changed

+274
-0
lines changed

1 file changed

+274
-0
lines changed

numpy/linalg/_linalg.py

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,40 @@ def outer(x1, x2, /):
876876
--------
877877
outer
878878
879+
Examples
880+
--------
881+
Make a (*very* coarse) grid for computing a Mandelbrot set:
882+
883+
>>> rl = np.linalg.outer(np.ones((5,)), np.linspace(-2, 2, 5))
884+
>>> rl
885+
array([[-2., -1., 0., 1., 2.],
886+
[-2., -1., 0., 1., 2.],
887+
[-2., -1., 0., 1., 2.],
888+
[-2., -1., 0., 1., 2.],
889+
[-2., -1., 0., 1., 2.]])
890+
>>> im = np.linalg.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))
891+
>>> im
892+
array([[0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j],
893+
[0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j],
894+
[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
895+
[0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j],
896+
[0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j]])
897+
>>> grid = rl + im
898+
>>> grid
899+
array([[-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j],
900+
[-2.+1.j, -1.+1.j, 0.+1.j, 1.+1.j, 2.+1.j],
901+
[-2.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 2.+0.j],
902+
[-2.-1.j, -1.-1.j, 0.-1.j, 1.-1.j, 2.-1.j],
903+
[-2.-2.j, -1.-2.j, 0.-2.j, 1.-2.j, 2.-2.j]])
904+
905+
An example using a "vector" of letters:
906+
907+
>>> x = np.array(['a', 'b', 'c'], dtype=object)
908+
>>> np.linalg.outer(x, [1, 2, 3])
909+
array([['a', 'aa', 'aaa'],
910+
['b', 'bb', 'bbb'],
911+
['c', 'cc', 'ccc']], dtype=object)
912+
879913
"""
880914
x1 = asanyarray(x1)
881915
x2 = asanyarray(x2)
@@ -1852,6 +1886,23 @@ def svdvals(x, /):
18521886
--------
18531887
scipy.linalg.svdvals : Compute singular values of a matrix.
18541888
1889+
Examples
1890+
--------
1891+
1892+
>>> np.linalg.svdvals([[1, 2, 3, 4, 5],
1893+
... [1, 4, 9, 16, 25],
1894+
... [1, 8, 27, 64, 125]])
1895+
array([146.68862757, 5.57510612, 0.60393245])
1896+
1897+
Determine the rank of a matrix using singular values:
1898+
1899+
>>> s = np.linalg.svdvals([[1, 2, 3],
1900+
... [2, 4, 6],
1901+
... [-1, 1, -1]]); s
1902+
array([8.38434191e+00, 1.64402274e+00, 2.31534378e-16])
1903+
>>> np.count_nonzero(s > 1e-10) # Matrix of rank 2
1904+
2
1905+
18551906
"""
18561907
return svd(x, compute_uv=False, hermitian=False)
18571908

@@ -3073,6 +3124,58 @@ def diagonal(x, /, *, offset=0):
30733124
--------
30743125
numpy.diagonal
30753126
3127+
Examples
3128+
--------
3129+
>>> a = np.arange(4).reshape(2, 2); a
3130+
array([[0, 1],
3131+
[2, 3]])
3132+
>>> np.linalg.diagonal(a)
3133+
array([0, 3])
3134+
3135+
A 3-D example:
3136+
3137+
>>> a = np.arange(8).reshape(2, 2, 2); a
3138+
array([[[0, 1],
3139+
[2, 3]],
3140+
[[4, 5],
3141+
[6, 7]]])
3142+
>>> np.linalg.diagonal(a)
3143+
array([[0, 3],
3144+
[4, 7]])
3145+
3146+
Diagonals adjacent to the main diagonal can be obtained by using the
3147+
`offset` argument:
3148+
3149+
>>> a = np.arange(9).reshape(3, 3)
3150+
>>> a
3151+
array([[0, 1, 2],
3152+
[3, 4, 5],
3153+
[6, 7, 8]])
3154+
>>> np.linalg.diagonal(a, offset=1) # First superdiagonal
3155+
array([1, 5])
3156+
>>> np.linalg.diagonal(a, offset=2) # Second superdiagonal
3157+
array([2])
3158+
>>> np.linalg.diagonal(a, offset=-1) # First subdiagonal
3159+
array([3, 7])
3160+
>>> np.linalg.diagonal(a, offset=-2) # Second subdiagonal
3161+
array([6])
3162+
3163+
The anti-diagonal can be obtained by reversing the order of elements
3164+
using either `numpy.flipud` or `numpy.fliplr`.
3165+
3166+
>>> a = np.arange(9).reshape(3, 3)
3167+
>>> a
3168+
array([[0, 1, 2],
3169+
[3, 4, 5],
3170+
[6, 7, 8]])
3171+
>>> np.linalg.diagonal(np.fliplr(a)) # Horizontal flip
3172+
array([2, 4, 6])
3173+
>>> np.linalg.diagonal(np.flipud(a)) # Vertical flip
3174+
array([6, 4, 2])
3175+
3176+
Note that the order in which the diagonal is retrieved varies depending
3177+
on the flip function.
3178+
30763179
"""
30773180
return _core_diagonal(x, offset, axis1=-2, axis2=-1)
30783181

@@ -3126,6 +3229,38 @@ def trace(x, /, *, offset=0, dtype=None):
31263229
--------
31273230
numpy.trace
31283231
3232+
Examples
3233+
--------
3234+
>>> np.linalg.trace(np.eye(3))
3235+
3.0
3236+
>>> a = np.arange(8).reshape((2, 2, 2))
3237+
>>> np.linalg.trace(a)
3238+
array([3, 11])
3239+
3240+
Trace is computed with the last two axes as the 2-d sub-arrays.
3241+
This behavior differs from :py:func:`numpy.trace` which uses the first two
3242+
axes by default.
3243+
3244+
>>> a = np.arange(24).reshape((3, 2, 2, 2))
3245+
>>> np.linalg.trace(a).shape
3246+
(3, 2)
3247+
3248+
Traces adjacent to the main diagonal can be obtained by using the
3249+
`offset` argument:
3250+
3251+
>>> a = np.arange(9).reshape((3, 3)); a
3252+
array([[0, 1, 2],
3253+
[3, 4, 5],
3254+
[6, 7, 8]])
3255+
>>> np.linalg.trace(a, offset=1) # First superdiagonal
3256+
6
3257+
>>> np.linalg.trace(a, offset=2) # Second superdiagonal
3258+
2
3259+
>>> np.linalg.trace(a, offset=-1) # First subdiagonal
3260+
10
3261+
>>> np.linalg.trace(a, offset=-2) # Second subdiagonal
3262+
6
3263+
31293264
"""
31303265
return _core_trace(x, offset, axis1=-2, axis2=-1, dtype=dtype)
31313266

@@ -3170,6 +3305,31 @@ def cross(x1, x2, /, *, axis=-1):
31703305
--------
31713306
numpy.cross
31723307
3308+
Examples
3309+
--------
3310+
Vector cross-product.
3311+
3312+
>>> x = np.array([1, 2, 3])
3313+
>>> y = np.array([4, 5, 6])
3314+
>>> np.linalg.cross(x, y)
3315+
array([-3, 6, -3])
3316+
3317+
Multiple vector cross-products. Note that the direction of the cross
3318+
product vector is defined by the *right-hand rule*.
3319+
3320+
>>> x = np.array([[1,2,3], [4,5,6]])
3321+
>>> y = np.array([[4,5,6], [1,2,3]])
3322+
>>> np.linalg.cross(x, y)
3323+
array([[-3, 6, -3],
3324+
[ 3, -6, 3]])
3325+
3326+
>>> x = np.array([[1, 2], [3, 4], [5, 6]])
3327+
>>> y = np.array([[4, 5], [6, 1], [2, 3]])
3328+
>>> np.linalg.cross(x, y, axis=0)
3329+
array([[-24, 6],
3330+
[ 18, 24],
3331+
[-6, -18]])
3332+
31733333
"""
31743334
x1 = asanyarray(x1)
31753335
x2 = asanyarray(x2)
@@ -3223,6 +3383,53 @@ def matmul(x1, x2, /):
32233383
--------
32243384
numpy.matmul
32253385
3386+
Examples
3387+
--------
3388+
For 2-D arrays it is the matrix product:
3389+
3390+
>>> a = np.array([[1, 0],
3391+
... [0, 1]])
3392+
>>> b = np.array([[4, 1],
3393+
... [2, 2]])
3394+
>>> np.linalg.matmul(a, b)
3395+
array([[4, 1],
3396+
[2, 2]])
3397+
3398+
For 2-D mixed with 1-D, the result is the usual.
3399+
3400+
>>> a = np.array([[1, 0],
3401+
... [0, 1]])
3402+
>>> b = np.array([1, 2])
3403+
>>> np.linalg.matmul(a, b)
3404+
array([1, 2])
3405+
>>> np.linalg.matmul(b, a)
3406+
array([1, 2])
3407+
3408+
3409+
Broadcasting is conventional for stacks of arrays
3410+
3411+
>>> a = np.arange(2 * 2 * 4).reshape((2, 2, 4))
3412+
>>> b = np.arange(2 * 2 * 4).reshape((2, 4, 2))
3413+
>>> np.linalg.matmul(a,b).shape
3414+
(2, 2, 2)
3415+
>>> np.linalg.matmul(a, b)[0, 1, 1]
3416+
98
3417+
>>> sum(a[0, 1, :] * b[0 , :, 1])
3418+
98
3419+
3420+
Vector, vector returns the scalar inner product, but neither argument
3421+
is complex-conjugated:
3422+
3423+
>>> np.linalg.matmul([2j, 3j], [2j, 3j])
3424+
(-13+0j)
3425+
3426+
Scalar multiplication raises an error.
3427+
3428+
>>> np.linalg.matmul([1,2], 3)
3429+
Traceback (most recent call last):
3430+
...
3431+
ValueError: matmul: Input operand 1 does not have enough dimensions ...
3432+
32263433
"""
32273434
return _core_matmul(x1, x2)
32283435

@@ -3282,6 +3489,36 @@ def matrix_norm(x, /, *, keepdims=False, ord="fro"):
32823489
--------
32833490
numpy.linalg.norm : Generic norm function
32843491
3492+
Examples
3493+
--------
3494+
>>> from numpy import linalg as LA
3495+
>>> a = np.arange(9) - 4
3496+
>>> a
3497+
array([-4, -3, -2, ..., 2, 3, 4])
3498+
>>> b = a.reshape((3, 3))
3499+
>>> b
3500+
array([[-4, -3, -2],
3501+
[-1, 0, 1],
3502+
[ 2, 3, 4]])
3503+
3504+
>>> LA.matrix_norm(b)
3505+
7.745966692414834
3506+
>>> LA.matrix_norm(b, ord='fro')
3507+
7.745966692414834
3508+
>>> LA.matrix_norm(b, ord=np.inf)
3509+
9.0
3510+
>>> LA.matrix_norm(b, ord=-np.inf)
3511+
2.0
3512+
3513+
>>> LA.matrix_norm(b, ord=1)
3514+
7.0
3515+
>>> LA.matrix_norm(b, ord=-1)
3516+
6.0
3517+
>>> LA.matrix_norm(b, ord=2)
3518+
7.3484692283495345
3519+
>>> LA.matrix_norm(b, ord=-2)
3520+
1.8570331885190563e-016 # may vary
3521+
32853522
"""
32863523
x = asanyarray(x)
32873524
return norm(x, axis=(-2, -1), keepdims=keepdims, ord=ord)
@@ -3321,6 +3558,34 @@ def vector_norm(x, /, *, axis=None, keepdims=False, ord=2):
33213558
--------
33223559
numpy.linalg.norm : Generic norm function
33233560
3561+
Examples
3562+
--------
3563+
>>> from numpy import linalg as LA
3564+
>>> a = np.arange(9) + 1
3565+
>>> a
3566+
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
3567+
>>> b = a.reshape((3, 3))
3568+
>>> b
3569+
array([[1, 2, 3],
3570+
[4, 5, 6],
3571+
[7, 8, 9]])
3572+
3573+
>>> LA.vector_norm(b)
3574+
16.881943016134134
3575+
>>> LA.vector_norm(b, ord=np.inf)
3576+
9.0
3577+
>>> LA.vector_norm(b, ord=-np.inf)
3578+
1.0
3579+
3580+
>>> LA.vector_norm(b, ord=1)
3581+
45.0
3582+
>>> LA.vector_norm(b, ord=-1)
3583+
0.3534857623790153
3584+
>>> LA.vector_norm(b, ord=2)
3585+
16.881943016134134
3586+
>>> LA.vector_norm(b, ord=-2)
3587+
0.8058837395885292
3588+
33243589
"""
33253590
x = asanyarray(x)
33263591
shape = list(x.shape)
@@ -3400,5 +3665,14 @@ def vecdot(x1, x2, /, *, axis=-1):
34003665
--------
34013666
numpy.vecdot
34023667
3668+
Examples
3669+
--------
3670+
Get the projected size along a given normal for an array of vectors.
3671+
3672+
>>> v = np.array([[0., 5., 0.], [0., 0., 10.], [0., 6., 8.]])
3673+
>>> n = np.array([0., 0.6, 0.8])
3674+
>>> np.linalg.vecdot(v, n)
3675+
array([ 3., 8., 10.])
3676+
34033677
"""
34043678
return _core_vecdot(x1, x2, axis=axis)

0 commit comments

Comments
 (0)