Liking cljdoc? Tell your friends :D
Clojure only.

fastmath.calculus.samples

Numerical integration of sampled (tabulated) data.

The integrand is not a function but a sequence of samples ys, taken either on a uniform grid with a known spacing or at arbitrary abscissae. The integral over the whole sampled range (or its running value at every sample) is computed with the composite trapezoid rule or the composite Simpson rule. The behaviour follows scipy.integrate.trapezoid, simpson, cumulative_trapezoid and cumulative_simpson.

The namespace contains a single function, integrate-samples.

Numerical integration of sampled (tabulated) data.

The integrand is not a function but a sequence of samples `ys`, taken either on a uniform grid with a known spacing or at arbitrary abscissae. The integral over the whole sampled range (or its running value at every sample) is computed with the composite trapezoid rule or the composite Simpson rule. The behaviour follows `scipy.integrate.trapezoid`, `simpson`, `cumulative_trapezoid` and `cumulative_simpson`.

The namespace contains a single function, [[integrate-samples]].
raw docstring

integrate-samplesclj

(integrate-samples ys)
(integrate-samples ys x-or-dx)
(integrate-samples ys x-or-dx options)

Integrates sampled data with the composite trapezoid or Simpson rule.

The samples ys are taken either at equally spaced points with a known spacing, or at arbitrary (not necessarily uniform) abscissae. The result is the integral over the whole sampled range, or, in the cumulative mode, the running integral at every sample. The results match scipy.integrate.trapezoid, simpson, cumulative_trapezoid and cumulative_simpson (the last two with initial=0).

Parameters:

  • ys (sequence or double[] of numbers): the samples.
  • x-or-dx (optional): either a sequence or double[] of abscissae, of the same length as ys, or a number dx being the spacing between consecutive samples. Defaults to the spacing 1.0 (also when nil). A negative or zero spacing is allowed and is used as is, as a decreasing x.
  • options (optional map):
    • :method (:trapezoid or :simpson, default :trapezoid): the quadrature rule.
    • :cumulative? (boolean, default false): when true the running integral is returned instead of the total.

Returns a double when :cumulative? is false. Otherwise returns a vector of doubles of the same length as ys, where the first element is 0.0 and the i-th element is the integral from the first to the i-th sample.

The trapezoid rule is exact for linear data. The Simpson rule fits a parabola through each three consecutive samples; the total is exact for polynomials up to degree 3 on a uniform grid with an odd number of samples, and up to degree 2 otherwise. For an even number of samples the last interval is treated with the three-point correction of Cartwright, so any number of samples is handled. The cumulative Simpson integral is computed over every interval from the parabola through the nearest three samples. Simpson with fewer than 3 samples is the trapezoid rule.

Corner cases:

  • no samples: returns 0.0, or [] in the cumulative mode.
  • one sample: returns 0.0, or [0.0] in the cumulative mode.
  • zero spacings in x are not rejected: the trapezoid rule gives zero for such an interval, the total Simpson rule treats the undefined terms as zero, the cumulative Simpson rule gives NaN or infinite values.
  • throws ex-info with :ys-count and :x-count in the ex-data when x-or-dx is a sequence of a different length than ys.
  • throws ex-info with :option, :value and :expected in the ex-data for an unknown :method, and for ys or x-or-dx which are neither numbers nor sequences as required.

See also fastmath.calculus/integrate (integration of functions).

Integrates sampled data with the composite trapezoid or Simpson rule.

The samples `ys` are taken either at equally spaced points with a known spacing, or at arbitrary (not necessarily uniform) abscissae. The result is the integral over the whole sampled range, or, in the cumulative mode, the running integral at every sample. The results match `scipy.integrate.trapezoid`, `simpson`, `cumulative_trapezoid` and `cumulative_simpson` (the last two with `initial=0`).

Parameters:

- `ys` (sequence or `double[]` of numbers): the samples.
- `x-or-dx` (optional): either a sequence or `double[]` of abscissae, of the same length as `ys`, or a number `dx` being the spacing between consecutive samples. Defaults to the spacing `1.0` (also when `nil`). A negative or zero spacing is allowed and is used as is, as a decreasing `x`.
- `options` (optional map):
  - `:method` (`:trapezoid` or `:simpson`, default `:trapezoid`): the quadrature rule.
  - `:cumulative?` (boolean, default `false`): when true the running integral is returned instead of the total.

Returns a `double` when `:cumulative?` is false. Otherwise returns a vector of doubles of the same length as `ys`, where the first element is `0.0` and the i-th element is the integral from the first to the i-th sample.

The trapezoid rule is exact for linear data. The Simpson rule fits a parabola through each three consecutive samples; the total is exact for polynomials up to degree 3 on a uniform grid with an odd number of samples, and up to degree 2 otherwise. For an even number of samples the last interval is treated with the three-point correction of Cartwright, so any number of samples is handled. The cumulative Simpson integral is computed over every interval from the parabola through the nearest three samples. Simpson with fewer than 3 samples is the trapezoid rule.

Corner cases:

- no samples: returns `0.0`, or `[]` in the cumulative mode.
- one sample: returns `0.0`, or `[0.0]` in the cumulative mode.
- zero spacings in `x` are not rejected: the trapezoid rule gives zero for such an interval, the total Simpson rule treats the undefined terms as zero, the cumulative Simpson rule gives NaN or infinite values.
- throws `ex-info` with `:ys-count` and `:x-count` in the ex-data when `x-or-dx` is a sequence of a different length than `ys`.
- throws `ex-info` with `:option`, `:value` and `:expected` in the ex-data for an unknown `:method`, and for `ys` or `x-or-dx` which are neither numbers nor sequences as required.

See also [[fastmath.calculus/integrate]] (integration of functions).
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close