Utils
Index
In the examples in the documentation below, the methods are not prefixed by the module name even if they are private.
julia> using CTModels
julia> x = 1
julia> private_fun(x) # throw an error
must be replaced by
julia> using CTModels
julia> x = 1
julia> CTModels.private_fun(x)
However, if the method is reexported by another package, then, there is no need of prefixing.
julia> module OptimalControl
import CTModels: private_fun
export private_fun
end
julia> using OptimalControl
julia> x = 1
julia> private_fun(x)
Documentation
CTModels.ctinterpolate
— Methodctinterpolate(x, f) -> Any
Return a linear interpolation function for the data f
defined at points x
.
This function creates a one-dimensional linear interpolant using the Interpolations.jl
package, with linear extrapolation beyond the bounds of x
.
Arguments
x
: A vector of points at which the valuesf
are defined.f
: A vector of values to interpolate.
Returns
A callable interpolation object that can be evaluated at new points.
Example
julia> x = 0:0.5:2
julia> f = [0.0, 1.0, 0.0, -1.0, 0.0]
julia> interp = ctinterpolate(x, f)
julia> interp(1.2)
CTModels.matrix2vec
— Functionmatrix2vec(A::Matrix{<:Real}) -> Vector{<:Vector{<:Real}}
matrix2vec(
A::Matrix{<:Real},
dim::Int64
) -> Vector{<:Vector{<:Real}}
Transform a matrix into a vector of vectors along the specified dimension.
Each row or column of the matrix A
is extracted and stored as an individual vector, depending on dim
.
Arguments
A
: A matrix of elements of type<:ctNumber
.dim
: The dimension along which to split the matrix (1
for rows,2
for columns). Defaults to1
.
Returns
A Vector
of Vector
s extracted from the rows or columns of A
.
Note
This is useful when data needs to be represented as a sequence of state or control vectors in optimal control problems.
Example
julia> A = [1 2 3; 4 5 6]
julia> matrix2vec(A, 1) # splits into rows: [[1, 2, 3], [4, 5, 6]]
julia> matrix2vec(A, 2) # splits into columns: [[1, 4], [2, 5], [3, 6]]
CTModels.to_out_of_place
— Methodto_out_of_place(
f!,
n;
T
) -> Union{Nothing, CTModels.var"#f#13"{CTModels.var"#f#12#14"{DataType, _A, _B}} where {_A, _B}}
Convert an in-place function f!
to an out-of-place function f
.
The resulting function f
returns a vector of type T
and length n
by first allocating memory and then calling f!
to fill it.
Arguments
f!
: An in-place function of the formf!(result, args...)
.n
: The length of the output vector.T
: The element type of the output vector (default isFloat64
).
Returns
An out-of-place function f(args...; kwargs...)
that returns the result as a vector or scalar, depending on n
.
Example
julia> f!(r, x) = (r[1] = sin(x); r[2] = cos(x))
julia> f = to_out_of_place(f!, 2)
julia> f(π/4) # returns approximately [0.707, 0.707]
CTModels.@ensure
— Macro@ensure condition exception
Throws the provided exception
if condition
is false.
Usage
julia> @ensure x > 0 CTBase.IncorrectArgument("x must be positive")
Arguments
condition
: A Boolean expression to test.exception
: An instance of an exception to throw ifcondition
is false.
Throws
- The provided
exception
if the condition is not satisfied.