MCPcopy Index your code
hub / github.com/numpy/numpy / vector_norm

Function vector_norm

numpy/linalg/_linalg.py:3502–3595  ·  view source on GitHub ↗

Computes the vector norm of a vector (or batch of vectors) ``x``. This function is Array API compatible. Parameters ---------- x : array_like Input array. axis : {None, int, 2-tuple of ints}, optional If an integer, ``axis`` specifies the axis (dimension) a

(x, /, *, axis=None, keepdims=False, ord=2)

Source from the content-addressed store, hash-verified

3500
3501@array_function_dispatch(_vector_norm_dispatcher)
3502def vector_norm(x, /, *, axis=None, keepdims=False, ord=2):
3503 """
3504 Computes the vector norm of a vector (or batch of vectors) ``x``.
3505
3506 This function is Array API compatible.
3507
3508 Parameters
3509 ----------
3510 x : array_like
3511 Input array.
3512 axis : {None, int, 2-tuple of ints}, optional
3513 If an integer, ``axis`` specifies the axis (dimension) along which
3514 to compute vector norms. If an n-tuple, ``axis`` specifies the axes
3515 (dimensions) along which to compute batched vector norms. If ``None``,
3516 the vector norm must be computed over all array values (i.e.,
3517 equivalent to computing the vector norm of a flattened array).
3518 Default: ``None``.
3519 keepdims : bool, optional
3520 If this is set to True, the axes which are normed over are left in
3521 the result as dimensions with size one. Default: False.
3522 ord : {int, float, inf, -inf}, optional
3523 The order of the norm. For details see the table under ``Notes``
3524 in `numpy.linalg.norm`.
3525
3526 See Also
3527 --------
3528 numpy.linalg.norm : Generic norm function
3529
3530 Examples
3531 --------
3532 >>> from numpy import linalg as LA
3533 >>> a = np.arange(9) + 1
3534 >>> a
3535 array([1, 2, 3, 4, 5, 6, 7, 8, 9])
3536 >>> b = a.reshape((3, 3))
3537 >>> b
3538 array([[1, 2, 3],
3539 [4, 5, 6],
3540 [7, 8, 9]])
3541
3542 >>> LA.vector_norm(b)
3543 16.881943016134134
3544 >>> LA.vector_norm(b, ord=np.inf)
3545 9.0
3546 >>> LA.vector_norm(b, ord=-np.inf)
3547 1.0
3548
3549 >>> LA.vector_norm(b, ord=0)
3550 9.0
3551 >>> LA.vector_norm(b, ord=1)
3552 45.0
3553 >>> LA.vector_norm(b, ord=-1)
3554 0.3534857623790153
3555 >>> LA.vector_norm(b, ord=2)
3556 16.881943016134134
3557 >>> LA.vector_norm(b, ord=-2)
3558 0.8058837395885292
3559

Callers

nothing calls this directly

Calls 6

prodFunction · 0.90
asanyarrayFunction · 0.85
normalize_axis_tupleFunction · 0.85
normFunction · 0.85
reshapeMethod · 0.80
ravelMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…