View Source Integrator.Utils (Integrator v0.1.3)

Various utility functions used in Integrator

Summary

Functions

Returns the columns of a tensor as a list of vector tensors

Gets the epsilon for a particular Nx type

Performs a 3rd order Hermite interpolation. Adapted from function hermite_cubic_interpolation in runge_kutta_interpolate.m

Performs a 4th order Hermite interpolation. Used by an ODE solver to interpolate the solution at the time t_out. As proposed by Shampine in Lawrence, Shampine, "Some Practical Runge-Kutta Formulas", 1986.

Implements the Kahan summation algorithm, also known as compensated summation. Based on this code in Octave. This is really a private function, but is made public so the docs are visible.

Returns the sign of the tensor as -1 or 1 (or 0 for zero tensors)

Given a list of elements, create a new list with only the unique elements from the list

Converts a Nx vector into a list of 1-D tensors

Functions

Link to this function

columns_as_list(matrix, start_index, end_index \\ nil)

View Source
@spec columns_as_list(Nx.t(), integer(), integer() | nil) :: [Nx.t()]

Returns the columns of a tensor as a list of vector tensors

E.g., a tensor of the form:

~M[
  1  2  3  4
  5  6  7  8
  9 10 11 12
]s8

will return

[

~V[ 1  5   9 ]s8,
~V[ 2  6  10 ]s8,
~V[ 3  7  11 ]s8,
~V[ 4  8  12 ]s8

]

@spec epsilon(Nx.Type.t()) :: float()
@spec epsilon(Nx.Type.t()) :: float()

Gets the epsilon for a particular Nx type

Link to this function

hermite_cubic_interpolation(t, x, der, t_out)

View Source
@spec hermite_cubic_interpolation(Nx.t(), Nx.t(), Nx.t(), Nx.t()) :: Nx.t()

Performs a 3rd order Hermite interpolation. Adapted from function hermite_cubic_interpolation in runge_kutta_interpolate.m

See Wikipedia

Link to this function

hermite_quartic_interpolation(t, x, der, t_out)

View Source
@spec hermite_quartic_interpolation(Nx.t(), Nx.t(), Nx.t(), Nx.t()) :: Nx.t()

Performs a 4th order Hermite interpolation. Used by an ODE solver to interpolate the solution at the time t_out. As proposed by Shampine in Lawrence, Shampine, "Some Practical Runge-Kutta Formulas", 1986.

See hermite_quartic_interpolation function in Octave.

Link to this function

kahan_sum(sum, comp, term)

View Source
@spec kahan_sum(Nx.t(), Nx.t(), Nx.t()) :: {Nx.t(), Nx.t()}

Implements the Kahan summation algorithm, also known as compensated summation. Based on this code in Octave. This is really a private function, but is made public so the docs are visible.

The algorithm significantly reduces the numerical error in the total obtained by adding a sequence of finite precision floating point numbers compared to the straightforward approach. For more details see this Wikipedia entry. This function is called by AdaptiveStepsize.integrate to better catch equality comparisons.

The first input argument is the variable that will contain the summation. This variable is also returned as the first output argument in order to reuse it in subsequent calls to kahan_sum/3 function.

The second input argument contains the compensation term and is returned as the second output argument so that it can be reused in future calls of the same summation.

The third input argument term is the variable to be added to sum.

@spec sign(float()) :: float()

Returns the sign of the tensor as -1 or 1 (or 0 for zero tensors)

@spec unique(list()) :: list()

Given a list of elements, create a new list with only the unique elements from the list

@spec vector_as_list(Nx.t()) :: [Nx.t()]

Converts a Nx vector into a list of 1-D tensors

Is there an existing Nx way to do this? If so, swap the usage of this function and then delete this

Note that

vector |> Nx.as_list() |> Enum.map(& Nx.tensor(&1, type: Nx.type(vector)))

seems to introduce potential precision issues