Post · 4 min read· pp. 861–863

aerosandbox.numpy.integrate_discrete

Attributes

Functions

integrate_discrete_intervals(f[, x, multiply_by_dx, ...])Given a set of sampled points (x_i, f_i) from a function, computes the integral of that function over each set of
integrate_discrete_squared_curvature(f[, x, method])Given a set of sampled points (x_i, f_i) from a function f(x), computes the following quantity:

Module Contents

aerosandbox.numpy.integrate_discrete.integrate_discrete_intervals(f, x=None, multiply_by_dx=True, method='trapezoidal', method_endpoints='lower_order') : Given a set of sampled points (x_i, f_i) from a function, computes the integral of that function over each set of adjacent points (“intervals”). Does this via a reconstruction approach, with several methods available.

code
In general, N points will yield N-1 integrals (one for each “interval” between points).

Parameters:
:   - **f** (*Union**[**numpy.ndarray**,* *casadi.MX**]*) – A 1D array of function values.
    - **x** (*Union**[**numpy.ndarray**,* *casadi.MX**]*) – A 1D array of x-values where the function was evaluated. If not specified, defaults to the indices of f.
      Should be the same length as f and should be monotonically increasing (i.e. x[i] < x[i+1], with no duplicated points).
    - **multiply\_by\_dx** (*bool*) – Whether to multiply the integral by the width of the segment. Defaults to True.
      - If True, summing the integrals will yield the integral of the function over the entire domain (x[0] to x[-1])
      - If False, you can think of the output as the “average function value” over each interval.
    - **method** (*str*) –

      The integration method to use. Options are:
      - “forward\_euler”
      - “backward\_euler”
      - “trapezoidal” (default)
      - “forward\_simpson”
      - “backward\_simpson”
      - “cubic”
      Note that some methods, like “cubic”, approximate each segment interval by looking beyond just the integral itself (i.e., f(a) and f(b)),

      > and so are not possible near the endpoints of the array.
    - **method\_endpoints** (*str*) – The integration method to use at the endpoints, for those higher-order methods that require handling. Options are:
      - “lower\_order” (default)
      - “ignore” (i.e. return the integral of the interior points only - note that this may result in a different number of integrals than segments!)
      - “periodic”

aerosandbox.numpy.integrate_discrete.integrate_discrete_squared_curvature(f, x=None, method='hybrid_simpson_cubic') : Given a set of sampled points (x_i, f_i) from a function f(x), computes the following quantity:

code
> int\_{x[0]}^{x[-1]} (f’’(x))^2 dx

This is useful for regularization of smooth curves (i.e., encouraging smooth functions as optimization results).

Performs this through one of several reconstruction-based methods, specified by method:

> - “cubic”: On each interval, reconstructs a piecewise cubic polynomial. This cubic is the unique polynomial
>
> that passes through the two points at the endpoints of the interval, plus the next point beyond each endpoint
> of the interval (i.e., 4 points in total). Numerically, this cubic is obtained using Bernstein polynomial
> reconstruction, so it is numerically stable. This cubic is then analytically differentiated twice, squared,
> and integrated over the interval. At the ends of the overall array, where this “look beyond” strategy is not
> possible, a one-sided cubic is used instead (i.e., looks beyond the interval at one end only and uses two
> extra points from this side).
>
> - “simpson”: On each interval, makes two unique quadratic reconstructions:
>
>   > - One reconstruction that uses the two points at the endpoints of the interval, plus the next point beyond
>   >
>   > the right endpoint of the interval (i.e., 3 points in total).
>   >
>   > - One reconstruction that uses the two points at the endpoints of the interval, plus the next point beyond
>   >
>   > the left endpoint of the interval (i.e., 3 points in total).
>   >
>   > These two quadratics are then analytically differentiated twice, squared, and integrated over the
>   > interval. This requires much less calculation, since the quadratics have uniform curvature over the
>   > interval, causing a lot of things to simplify. The result is then computed by combining the results of this
>   > process for the two quadratic reconstructions.
>   >
>   > This is similar to a Simpson’s rule integration, balanced between the two sides of the interval. In
>   > frequency-domain testing, this method appears to be more accurate than the “cubic” strategy at every
>   > frequency, with less computational effort. Thus, it should be preferred to the “cubic” strategy.
> - “hybrid\_simpson\_cubic”: First, starts out by estimating the first derivative of the function at each point
>
> in the array (including endpoints) using a quadratic reconstruction. (See numpy.gradient() for more
> information or source code on this; this code uses numpy.gradient() directly for this step.) Then,
> reconstructs a cubic polynomial on each interval, with the following boundary conditions:
>
> > - The cubic passes through the two points at the endpoints of the interval.
> > - The cubic has the same first derivative as the precomputed derivatives at the endpoints of the interval.
> >
> > This cubic is then analytically differentiated twice, squared, and integrated over the interval.
> >
> > In frequency-domain testing, this method is also more accurate than the “cubic” strategy at every
> > frequency. Compared to the “simpson” strategy, it is more accurate at high frequencies and less accurate
> > at low frequencies. Because the goal of this function is to be used as a regularization term,
> > which should be more sensitive to high-frequency oscillations, this method is preferred to the “simpson”
> > strategy. This method is also preferred as its estimate tends to err high rather than low, which serves
> > well as a regularization strategy. (It is still convergent to the true value in the high-sample-rate limit.)

Parameters:
:   - **f** (*Union**[**numpy.ndarray**,* *casadi.MX**]*) –
    - **x** (*Union**[**numpy.ndarray**,* *casadi.MX**]*) –
    - **method** (*str*) –

aerosandbox.numpy.integrate_discrete.a = 0