trapz#

ivy.trapz(y, /, *, x=None, dx=1.0, axis=-1, out=None)[source]#

Integrate along the given axis using the composite trapezoidal rule.

If x is provided, the integration happens in sequence along its elements - they are not sorted..

Parameters:
  • y (Array) – The array that should be integrated.

  • x (Optional[Array], default: None) – The sample points corresponding to the input array values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (float, default: 1.0) – The spacing between sample points when x is None. The default is 1.

  • axis (int, default: -1) – The axis along which to integrate.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – Definite integral of n-dimensional array as approximated along a single axis by the trapezoidal rule. If the input array is a 1-dimensional array, then the result is a float. If n is greater than 1, then the result is an n-1 dimensional array.

Examples

>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3])
4.0
>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3], x=[4, 6, 8])
8.0
>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3], dx=2)
8.0
Array.trapz(self, /, *, x=None, dx=1.0, axis=-1, out=None)[source]#

ivy.Array instance method variant of ivy.trapz. This method simply wraps the function, and so the docstring for ivy.trapz also applies to this method with minimal changes.

Parameters:
  • self (Array) – The array that should be integrated.

  • x (Optional[Array], default: None) – The sample points corresponding to the input array values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (float, default: 1.0) – The spacing between sample points when x is None. The default is 1.

  • axis (int, default: -1) – The axis along which to integrate.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – Definite integral of n-dimensional array as approximated along a single axis by the trapezoidal rule. If the input array is a 1-dimensional array, then the result is a float. If n is greater than 1, then the result is an n-1 dimensional array.

Examples

>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz(y)
4.0
>>> y = ivy.array([1, 2, 3])
>>> x = ivy.array([4, 6, 8])
>>> ivy.trapz(y, x=x)
8.0
>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz(y, dx=2)
8.0
Container.trapz(self, /, *, x=None, dx=1.0, axis=-1, out=None)[source]#

ivy.Container instance method variant of ivy.trapz. This method simply wraps the function, and so the docstring for ivy.trapz also applies to this method with minimal changes.

Parameters:
  • self (Container) – The container whose arrays should be integrated.

  • x (Optional[Union[Array, NativeArray, Container]], default: None) – The sample points corresponding to the input array values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (Union[float, Container], default: 1.0) – The spacing between sample points when x is None. The default is 1.

  • axis (Union[int, Container], default: -1) – The axis along which to integrate.

  • out (Optional[Container], default: None) – optional output container, for writing the result to.

Return type:

Container

Returns:

ret – container including definite integrals of n-dimensional arrays as approximated along a single axis by the trapezoidal rule.

Examples

With one ivy.Container input: >>> y = ivy.Container(a=ivy.array((1, 2, 3)), b=ivy.array((1, 5, 10))) >>> y.trapz() {

a: 4.0 b: 10.5

}