Matrix or vector norm. This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ``ord`` parameter. Parameters ---------- x : array_like Input array. If `a
(x, ord=None, axis=None, keepdims=False)
| 2597 | |
| 2598 | @array_function_dispatch(_norm_dispatcher) |
| 2599 | def norm(x, ord=None, axis=None, keepdims=False): |
| 2600 | """ |
| 2601 | Matrix or vector norm. |
| 2602 | |
| 2603 | This function is able to return one of eight different matrix norms, |
| 2604 | or one of an infinite number of vector norms (described below), depending |
| 2605 | on the value of the ``ord`` parameter. |
| 2606 | |
| 2607 | Parameters |
| 2608 | ---------- |
| 2609 | x : array_like |
| 2610 | Input array. If `axis` is None, `x` must be 1-D or 2-D, unless `ord` |
| 2611 | is None. If both `axis` and `ord` are None, the 2-norm of |
| 2612 | ``x.ravel`` will be returned. |
| 2613 | ord : {int, float, inf, -inf, 'fro', 'nuc'}, optional |
| 2614 | Order of the norm (see table under ``Notes`` for what values are |
| 2615 | supported for matrices and vectors respectively). inf means numpy's |
| 2616 | `inf` object. The default is None. |
| 2617 | axis : {None, int, 2-tuple of ints}, optional. |
| 2618 | If `axis` is an integer, it specifies the axis of `x` along which to |
| 2619 | compute the vector norms. If `axis` is a 2-tuple, it specifies the |
| 2620 | axes that hold 2-D matrices, and the matrix norms of these matrices |
| 2621 | are computed. If `axis` is None then either a vector norm (when `x` |
| 2622 | is 1-D) or a matrix norm (when `x` is 2-D) is returned. The default |
| 2623 | is None. |
| 2624 | |
| 2625 | keepdims : bool, optional |
| 2626 | If this is set to True, the axes which are normed over are left in the |
| 2627 | result as dimensions with size one. With this option the result will |
| 2628 | broadcast correctly against the original `x`. |
| 2629 | |
| 2630 | Returns |
| 2631 | ------- |
| 2632 | n : float or ndarray |
| 2633 | Norm of the matrix or vector(s). |
| 2634 | |
| 2635 | See Also |
| 2636 | -------- |
| 2637 | scipy.linalg.norm : Similar function in SciPy. |
| 2638 | |
| 2639 | Notes |
| 2640 | ----- |
| 2641 | For values of ``ord < 1``, the result is, strictly speaking, not a |
| 2642 | mathematical 'norm', but it may still be useful for various numerical |
| 2643 | purposes. |
| 2644 | |
| 2645 | The following norms can be calculated: |
| 2646 | |
| 2647 | ===== ============================ ========================== |
| 2648 | ord norm for matrices norm for vectors |
| 2649 | ===== ============================ ========================== |
| 2650 | None Frobenius norm 2-norm |
| 2651 | 'fro' Frobenius norm -- |
| 2652 | 'nuc' nuclear norm -- |
| 2653 | inf max(sum(abs(x), axis=1)) max(abs(x)) |
| 2654 | -inf min(sum(abs(x), axis=1)) min(abs(x)) |
| 2655 | 0 -- sum(x != 0) |
| 2656 | 1 max(sum(abs(x), axis=0)) as below |
searching dependent graphs…