Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed May 5, 2023
1 parent 865d97f commit 3b89650
Show file tree
Hide file tree
Showing 25 changed files with 887 additions and 721 deletions.
39 changes: 19 additions & 20 deletions src/discretize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ Returns a discretization of the input system `system` with discretization method
function discretize(system::AbstractContinuousSystem, ΔT::Real,
algorithm::AbstractDiscretizationAlgorithm=ExactDiscretization(),
constructor=_default_complementary_constructor(system))

(!isaffine(system)) && throw(ArgumentError("system needs to be affine"))

sets(x) = x [:X,:U,:W]
matrices(x) = x [:A,:B,:b,:c,:D]
sets(x) = x [:X, :U, :W]
matrices(x) = x [:A, :B, :b, :c, :D]

# get all fields from system
fields = collect(fieldnames(typeof(system)))
Expand Down Expand Up @@ -168,14 +167,14 @@ function _discretize(::ExactDiscretization, ΔT::Real,
B::AbstractMatrix,
c::AbstractVector,
D::AbstractMatrix)
if rank(A) == size(A, 1)
A_d = exp(A*ΔT)
Matr = inv(A)*(A_d - I)
B_d = Matr*B
c_d = Matr*c
D_d = Matr*D
if rank(A) == size(A, 1)
A_d = exp(A * ΔT)
Matr = inv(A) * (A_d - I)
B_d = Matr * B
c_d = Matr * c
D_d = Matr * D
else
error("exact discretization for singular state matrix, i.e. A is non-invertible,"*
error("exact discretization for singular state matrix, i.e. A is non-invertible," *
" not implemented yet, please use algorithm `EulerDiscretization`")
end
return [A_d, B_d, c_d, D_d]
Expand All @@ -186,10 +185,10 @@ function _discretize(::EulerDiscretization, ΔT::Real,
B::AbstractMatrix,
c::AbstractVector,
D::AbstractMatrix)
A_d = I + ΔT*A
B_d = ΔT*B
c_d = ΔT*c
D_d = ΔT*D
A_d = I + ΔT * A
B_d = ΔT * B
c_d = ΔT * c
D_d = ΔT * D
return [A_d, B_d, c_d, D_d]
end

Expand All @@ -215,7 +214,7 @@ See [`discretize`](@ref) for more details.
"""
function _discretize(algorithm::AbstractDiscretizationAlgorithm, ΔT::Real,
A::AbstractMatrix)
n = size(A,1)
n = size(A, 1)
mzero = spzeros(n, n)
vzero = spzeros(n)
A_d, _, _, _ = _discretize(algorithm, ΔT, A, mzero, vzero, mzero)
Expand Down Expand Up @@ -249,7 +248,7 @@ See [`discretize`](@ref) for more details.
"""
function _discretize(algorithm::AbstractDiscretizationAlgorithm, ΔT::Real,
A::AbstractMatrix, B::AbstractMatrix)
n = size(A,1)
n = size(A, 1)
mzero = spzeros(n, n)
vzero = spzeros(n)
A_d, B_d, _, _ = _discretize(algorithm, ΔT, A, B, vzero, mzero)
Expand Down Expand Up @@ -279,8 +278,8 @@ Returns a vector containing the discretized input arguments `A` and `c`.
See [`discretize`](@ref) for more details.
"""
function _discretize(algorithm::AbstractDiscretizationAlgorithm, ΔT::Real,
A::AbstractMatrix,c::AbstractVector)
n = size(A,1)
A::AbstractMatrix, c::AbstractVector)
n = size(A, 1)
mzero = spzeros(n, n)
A_d, _, c_d, _ = _discretize(algorithm, ΔT, A, mzero, c, mzero)
return [A_d, c_d]
Expand Down Expand Up @@ -311,7 +310,7 @@ See [`discretize`](@ref) for more details.
"""
function _discretize(algorithm::AbstractDiscretizationAlgorithm, ΔT::Real,
A::AbstractMatrix, B::AbstractMatrix, c::AbstractVector)
n = size(A,1)
n = size(A, 1)
mzero = spzeros(n, n)
A_d, B_d, c_d, _ = _discretize(algorithm, ΔT, A, B, c, mzero)
return [A_d, B_d, c_d]
Expand Down Expand Up @@ -342,7 +341,7 @@ See [`discretize`](@ref) for more details.
"""
function _discretize(algorithm::AbstractDiscretizationAlgorithm, ΔT::Real,
A::AbstractMatrix, B::AbstractMatrix, D::AbstractMatrix)
n = size(A,1)
n = size(A, 1)
vzero = spzeros(n)
A_d, B_d, _, D_d = _discretize(algorithm, ΔT, A, B, vzero, D)
return [A_d, B_d, D_d]
Expand Down
31 changes: 17 additions & 14 deletions src/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ IdentityMultiple{Rational{Int64}} of value 2//1 and order 2
struct IdentityMultiple{T} <: AbstractMatrix{T}
M::UniformScaling{T}
n::Int
IdentityMultiple(M::UniformScaling{T}, n::Int) where {T} = begin
(n < 1) && throw(ArgumentError("the dimension of `IdentityMultiple` cannot be negative or zero"))
return new{T}(M, n)
function IdentityMultiple(M::UniformScaling{T}, n::Int) where {T}
begin
(n < 1) &&
throw(ArgumentError("the dimension of `IdentityMultiple` cannot be negative or zero"))
return new{T}(M, n)
end
end
end

Expand All @@ -76,19 +79,19 @@ Base.size(𝐼::IdentityMultiple) = (𝐼.n, 𝐼.n)

function Base.getindex(𝐼::IdentityMultiple, inds...)
any(idx -> idx > 𝐼.n, inds) && throw(BoundsError(𝐼, inds))
getindex(𝐼.M, inds...)
return getindex(𝐼.M, inds...)
end

function Base.getindex(𝐼::IdentityMultiple{T}, ind) where {T}
if 1 ind 𝐼.n^2
return rem(ind-1, 𝐼.n+1) == 0 ? 𝐼.M.λ : zero(T)
return rem(ind - 1, 𝐼.n + 1) == 0 ? 𝐼.M.λ : zero(T)
else
throw(BoundsError(𝐼, ind))
end
end

function Base.setindex!(𝐼::IdentityMultiple, X, inds...)
error("cannot store a value in an `IdentityMultiple` because this type is immutable")
return error("cannot store a value in an `IdentityMultiple` because this type is immutable")
end

Base.:(-)(𝐼::IdentityMultiple) = IdentityMultiple(-𝐼.M, 𝐼.n)
Expand Down Expand Up @@ -134,26 +137,26 @@ function Base.:(*)(𝐼1::IdentityMultiple, 𝐼2::IdentityMultiple)
return IdentityMultiple(𝐼1.M * 𝐼2.M, 𝐼1.n)
end

function Base.:(*)(𝐼::IdentityMultiple{T}, U::UniformScaling{S}) where {T<:Number, S<:Number}
function Base.:(*)(𝐼::IdentityMultiple{T}, U::UniformScaling{S}) where {T<:Number,S<:Number}
return IdentityMultiple(𝐼.M.λ * U, 𝐼.n)
end

function Base.:(*)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Number, S<:Number}
function Base.:(*)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Number,S<:Number}
return IdentityMultiple(𝐼.M.λ * U, 𝐼.n)
end

function Base.:(/)(𝐼::IdentityMultiple{T}, U::UniformScaling{S}) where {T<:Number, S<:Number}
function Base.:(/)(𝐼::IdentityMultiple{T}, U::UniformScaling{S}) where {T<:Number,S<:Number}
@assert !iszero(U.λ)
return IdentityMultiple(𝐼.M * inv(U.λ), 𝐼.n)
end

function Base.:(/)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Number, S<:Number}
function Base.:(/)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Number,S<:Number}
@assert !iszero(𝐼.M.λ)
return IdentityMultiple(U * inv(𝐼.M.λ), 𝐼.n)
end

function Base.show(io::IO, ::MIME"text/plain", 𝐼::IdentityMultiple{T}) where T
print(io, "$(typeof(𝐼)) of value $(𝐼.M.λ) and order $(𝐼.n)")
function Base.show(io::IO, ::MIME"text/plain", 𝐼::IdentityMultiple{T}) where {T}
return print(io, "$(typeof(𝐼)) of value $(𝐼.M.λ) and order $(𝐼.n)")
end

"""
Expand All @@ -171,11 +174,11 @@ Convenience constructor of an [`IdentityMultiple`](@ref).
An `IdentityMultiple` of the given size and scaling factor.
"""
function Id(n::Int, λ::Number=1.0)
return IdentityMultiple*I, n)
return IdentityMultiple * I, n)
end

# callable identity matrix given the scaling factor and the size
IdentityMultiple::Number, n::Int) = IdentityMultiple*I, n)
IdentityMultiple::Number, n::Int) = IdentityMultiple * I, n)

function LinearAlgebra.Hermitian(𝐼::IdentityMultiple)
return Hermitian(Diagonal(fill(𝐼.M.λ, 𝐼.n)))
Expand Down
6 changes: 3 additions & 3 deletions src/inputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct ConstantInput{UT} <: AbstractInput
end

Base.eltype(::Type{ConstantInput{UT}}) where {UT} = UT
Base.iterate(input::ConstantInput, state::Union{Int, Nothing}=nothing) = (input.U, nothing)
Base.iterate(input::ConstantInput, state::Union{Int,Nothing}=nothing) = (input.U, nothing)
Base.IteratorSize(::Type{<:ConstantInput}) = Base.IsInfinite()
Base.IteratorEltype(::Type{<:ConstantInput}) = Base.HasEltype()

Expand Down Expand Up @@ -177,11 +177,11 @@ julia> map(x->2*x, v)
VaryingInput{Rational{Int64}, Vector{Rational{Int64}}}(Rational{Int64}[-1//1, 1//1])
```
"""
struct VaryingInput{UT, VUT<:AbstractVector{UT}} <: AbstractInput
struct VaryingInput{UT,VUT<:AbstractVector{UT}} <: AbstractInput
U::VUT # input sequence
end

Base.eltype(::Type{VaryingInput{UT, VUT}}) where {UT, VUT} = UT
Base.eltype(::Type{VaryingInput{UT,VUT}}) where {UT,VUT} = UT

function Base.iterate(input::VaryingInput, state::Int=1)
if state > length(input.U)
Expand Down
31 changes: 18 additions & 13 deletions src/instantiate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ The `_instantiate` method generalizes the `successor` of an `AbstractDiscreteSys
and the `vector_field` of an `AbstractContinuousSystem` into a single method.
"""
function _instantiate(system::AbstractSystem, x::AbstractVector;
check_constraints::Bool=true)
check_constraints::Bool=true)
!_is_conformable_state(system, x) && _argument_error(:x)
if isconstrained(system) && check_constraints
!_in_stateset(system, x) && _argument_error(:x,:X)
!_in_stateset(system, x) && _argument_error(:x, :X)
end

if islinear(system)
Expand Down Expand Up @@ -96,15 +96,19 @@ The `_instantiate` method generalizes the `successor` of an `AbstractDiscreteSys
and the `vector_field` of an `AbstractContinuousSystem` into a single method.
"""
function _instantiate(system::AbstractSystem, x::AbstractVector, u::AbstractVector;
check_constraints::Bool=true)
check_constraints::Bool=true)
# Figure out if `system` is a controlled or noisy system and set the function
# `matrix` to either `input_matix` or `noise_matrix`
if iscontrolled(system) && !isnoisy(system)
input_var = :u; input_set = :U; matrix = input_matrix
input_var = :u
input_set = :U
matrix = input_matrix
_is_conformable = _is_conformable_input
_in_set = _in_inputset
elseif isnoisy(system) && !iscontrolled(system)
input_var = :w; input_set = :W; matrix = noise_matrix
input_var = :w
input_set = :W
matrix = noise_matrix
_is_conformable = _is_conformable_noise
_in_set = _in_noiseset
else
Expand All @@ -115,7 +119,7 @@ function _instantiate(system::AbstractSystem, x::AbstractVector, u::AbstractVect
!_is_conformable(system, u) && _argument_error(input_var)

if isconstrained(system) && check_constraints
!_in_stateset(system, x) && _argument_error(:x,:X)
!_in_stateset(system, x) && _argument_error(:x, :X)
!_in_set(system, u) && _argument_error(input_var, input_set)
end

Expand Down Expand Up @@ -158,29 +162,30 @@ The result of applying the system to state `x`, input `u` and noise `w`.
The `_instantiate` method generalizes the `successor` of an `AbstractDiscreteSystem`
and the `vector_field` of an `AbstractContinuousSystem` into a single method.
"""
function _instantiate(system::AbstractSystem, x::AbstractVector, u::AbstractVector, w::AbstractVector;
check_constraints::Bool=true)
function _instantiate(system::AbstractSystem, x::AbstractVector, u::AbstractVector,
w::AbstractVector;
check_constraints::Bool=true)
!_is_conformable_state(system, x) && _argument_error(:x)
!_is_conformable_input(system, u) && _argument_error(:u)
!_is_conformable_noise(system, w) && _argument_error(:w)

if isconstrained(system) && check_constraints
!_in_stateset(system, x) && _argument_error(:x,:X)
!_in_inputset(system, u) && _argument_error(:u,:U)
!_in_noiseset(system, w) && _argument_error(:w,:W)
!_in_stateset(system, x) && _argument_error(:x, :X)
!_in_inputset(system, u) && _argument_error(:u, :U)
!_in_noiseset(system, w) && _argument_error(:w, :W)
end

if islinear(system)
return state_matrix(system) * x + input_matrix(system) * u + noise_matrix(system) * w

elseif isaffine(system)
return state_matrix(system) * x + affine_term(system) + input_matrix(system) * u + noise_matrix(system) * w
return state_matrix(system) * x + affine_term(system) + input_matrix(system) * u +
noise_matrix(system) * w

elseif ispolynomial(system) || isblackbox(system)
return mapping(system)(x, u, w)

else
throw(ArgumentError("_instantiate not defined for type `$(typename(system))`"))

end
end
2 changes: 1 addition & 1 deletion src/ivp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ julia> inputdim(p)
0
```
"""
struct InitialValueProblem{S <: AbstractSystem, XT} <: AbstractSystem
struct InitialValueProblem{S<:AbstractSystem,XT} <: AbstractSystem
s::S
x0::XT
end
Expand Down
Loading

0 comments on commit 3b89650

Please sign in to comment.