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

Function cumulative_sum

numpy/_core/fromnumeric.py:2788–2869  ·  view source on GitHub ↗

Return the cumulative sum of the elements along a given axis. This function is an Array API compatible alternative to `numpy.cumsum`. Parameters ---------- x : array_like Input array. axis : int, optional Axis along which the cumulative sum is computed. The

(x, /, *, axis=None, dtype=None, out=None,
                   include_initial=False)

Source from the content-addressed store, hash-verified

2786
2787@array_function_dispatch(_cumulative_sum_dispatcher)
2788def cumulative_sum(x, /, *, axis=None, dtype=None, out=None,
2789 include_initial=False):
2790 """
2791 Return the cumulative sum of the elements along a given axis.
2792
2793 This function is an Array API compatible alternative to `numpy.cumsum`.
2794
2795 Parameters
2796 ----------
2797 x : array_like
2798 Input array.
2799 axis : int, optional
2800 Axis along which the cumulative sum is computed. The default
2801 (None) is only allowed for one-dimensional arrays. For arrays
2802 with more than one dimension ``axis`` is required.
2803 dtype : dtype, optional
2804 Type of the returned array and of the accumulator in which the
2805 elements are summed. If ``dtype`` is not specified, it defaults
2806 to the dtype of ``x``, unless ``x`` has an integer dtype with
2807 a precision less than that of the default platform integer.
2808 In that case, the default platform integer is used.
2809 out : ndarray, optional
2810 Alternative output array in which to place the result. It must
2811 have the same shape and buffer length as the expected output
2812 but the type will be cast if necessary. See :ref:`ufuncs-output-type`
2813 for more details.
2814 include_initial : bool, optional
2815 Boolean indicating whether to include the initial value (zeros) as
2816 the first value in the output. With ``include_initial=True``
2817 the shape of the output is different than the shape of the input.
2818 Default: ``False``.
2819
2820 Returns
2821 -------
2822 cumulative_sum_along_axis : ndarray
2823 A new array holding the result is returned unless ``out`` is
2824 specified, in which case a reference to ``out`` is returned. The
2825 result has the same shape as ``x`` if ``include_initial=False``.
2826
2827 See Also
2828 --------
2829 sum : Sum array elements.
2830 trapezoid : Integration of array values using composite trapezoidal rule.
2831 diff : Calculate the n-th discrete difference along given axis.
2832
2833 Notes
2834 -----
2835 Arithmetic is modular when using integer types, and no error is
2836 raised on overflow.
2837
2838 ``cumulative_sum(a)[-1]`` may not be equal to ``sum(a)`` for
2839 floating-point values since ``sum`` may use a pairwise summation routine,
2840 reducing the roundoff-error. See `sum` for more information.
2841
2842 Examples
2843 --------
2844 >>> a = np.array([1, 2, 3, 4, 5, 6])
2845 >>> a

Callers

nothing calls this directly

Calls 1

_cumulative_funcFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…