Integrate along the given axis using the composite trapezoidal rule. Integrate `y` (`x`) along given axis. Parameters ---------- y : array_like Input array to integrate. x : array_like, optional The sample points corresponding to the `y` values. If `x` is N
(y, x=None, dx=1.0, axis=-1)
| 3981 | |
| 3982 | @array_function_dispatch(_trapz_dispatcher) |
| 3983 | def trapz(y, x=None, dx=1.0, axis=-1): |
| 3984 | """ |
| 3985 | Integrate along the given axis using the composite trapezoidal rule. |
| 3986 | |
| 3987 | Integrate `y` (`x`) along given axis. |
| 3988 | |
| 3989 | Parameters |
| 3990 | ---------- |
| 3991 | y : array_like |
| 3992 | Input array to integrate. |
| 3993 | x : array_like, optional |
| 3994 | The sample points corresponding to the `y` values. If `x` is None, |
| 3995 | the sample points are assumed to be evenly spaced `dx` apart. The |
| 3996 | default is None. |
| 3997 | dx : scalar, optional |
| 3998 | The spacing between sample points when `x` is None. The default is 1. |
| 3999 | axis : int, optional |
| 4000 | The axis along which to integrate. |
| 4001 | |
| 4002 | Returns |
| 4003 | ------- |
| 4004 | trapz : float |
| 4005 | Definite integral as approximated by trapezoidal rule. |
| 4006 | |
| 4007 | See Also |
| 4008 | -------- |
| 4009 | sum, cumsum |
| 4010 | |
| 4011 | Notes |
| 4012 | ----- |
| 4013 | Image [2]_ illustrates trapezoidal rule -- y-axis locations of points |
| 4014 | will be taken from `y` array, by default x-axis distances between |
| 4015 | points will be 1.0, alternatively they can be provided with `x` array |
| 4016 | or with `dx` scalar. Return value will be equal to combined area under |
| 4017 | the red lines. |
| 4018 | |
| 4019 | |
| 4020 | References |
| 4021 | ---------- |
| 4022 | .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule |
| 4023 | |
| 4024 | .. [2] Illustration image: |
| 4025 | https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png |
| 4026 | |
| 4027 | Examples |
| 4028 | -------- |
| 4029 | >>> np.trapz([1,2,3]) |
| 4030 | 4.0 |
| 4031 | >>> np.trapz([1,2,3], x=[4,6,8]) |
| 4032 | 8.0 |
| 4033 | >>> np.trapz([1,2,3], dx=2) |
| 4034 | 8.0 |
| 4035 | >>> a = np.arange(6).reshape(2, 3) |
| 4036 | >>> a |
| 4037 | array([[0, 1, 2], |
| 4038 | [3, 4, 5]]) |
| 4039 | >>> np.trapz(a, axis=0) |
| 4040 | array([ 1.5, 2.5, 3.5]) |
nothing calls this directly
no test coverage detected