Skip to content

Commit

Permalink
JuliaFormatter.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaelbosch committed Oct 23, 2023
1 parent 0dac4cd commit 26a0f6f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
8 changes: 6 additions & 2 deletions src/caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ function OrdinaryDiffEq.alg_cache(
E0, E1, E2 = Proj(0), Proj(1), Proj(2)
@assert f isa SciMLBase.AbstractODEFunction
SolProj = if is_secondorder_ode
E0 isa IsometricKroneckerProduct ? IsometricKroneckerProduct(d, [Proj(1).B; Proj(0).B]) : [Proj(1); Proj(0)]
E0 isa IsometricKroneckerProduct ?
IsometricKroneckerProduct(d, [Proj(1).B; Proj(0).B]) : [Proj(1); Proj(0)]
else
Proj(0)
end
Expand All @@ -136,7 +137,10 @@ function OrdinaryDiffEq.alg_cache(
initial_variance = ones(uElType, q + 1)
μ0 = zeros(uElType, D)
Σ0 = PSDMatrix(
to_factorized_matrix(FAC, IsometricKroneckerProduct(d, diagm(sqrt.(initial_variance)))),
to_factorized_matrix(
FAC,
IsometricKroneckerProduct(d, diagm(sqrt.(initial_variance))),
),
)
x0 = Gaussian(μ0, Σ0)

Expand Down
3 changes: 2 additions & 1 deletion src/covariance_structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ end
factorized_zeros(::DenseCovariance, elType, sizes...; d, q) = zeros(elType, sizes...)

to_factorized_matrix(::DenseCovariance, M::AbstractMatrix) = Matrix(M)
to_factorized_matrix(::IsometricKroneckerCovariance, M::AbstractMatrix) = IsometricKroneckerProduct(M) # probably errors
to_factorized_matrix(::IsometricKroneckerCovariance, M::AbstractMatrix) =
IsometricKroneckerProduct(M) # probably errors
to_factorized_matrix(::IsometricKroneckerCovariance, M::IsometricKroneckerProduct) = M
6 changes: 5 additions & 1 deletion src/filtering/predict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ predict_mean(μ::AbstractVector, A::AbstractMatrix) = A * μ
predict_cov::AbstractMatrix, A::AbstractMatrix, Q::AbstractMatrix) = A * Σ * A' + Q
predict_cov::PSDMatrix, A::AbstractMatrix, Q::PSDMatrix) =
PSDMatrix(qr([Σ.R * A'; Q.R]).R)
predict_cov::PSDMatrix{T,<:IsometricKroneckerProduct}, A::IsometricKroneckerProduct, Q::PSDMatrix{T,<:IsometricKroneckerProduct}) where {T} = begin
predict_cov(
Σ::PSDMatrix{T,<:IsometricKroneckerProduct},
A::IsometricKroneckerProduct,
Q::PSDMatrix{T,<:IsometricKroneckerProduct},
) where {T} = begin
P_pred_breve = predict_cov(PSDMatrix.R.B), A.B, PSDMatrix(Q.R.B))
return PSDMatrix(IsometricKroneckerProduct.R.ldim, P_pred_breve.R))
end
Expand Down
53 changes: 21 additions & 32 deletions src/kronecker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ K = I_d \otimes B
- `left_factor_dim::Int64`: Dimension `d` of the left identity kronecker factor.
- `right_factor::AbstractMatrix`: Right Kronecker factor.
"""
struct IsometricKroneckerProduct{T<:Number,TB<:AbstractMatrix} <: Kronecker.AbstractKroneckerProduct{T}
struct IsometricKroneckerProduct{T<:Number,TB<:AbstractMatrix} <:
Kronecker.AbstractKroneckerProduct{T}
ldim::Int64
B::TB
function IsometricKroneckerProduct(left_factor_dim::Int64, right_factor::AbstractMatrix{T}) where {T}
function IsometricKroneckerProduct(
left_factor_dim::Int64,
right_factor::AbstractMatrix{T},
) where {T}
return new{T,typeof(right_factor)}(left_factor_dim, right_factor)
end
end
Expand Down Expand Up @@ -137,7 +141,7 @@ reshape_no_alloc(a, dims::Tuple) =
reshape_no_alloc(a, dims...) = reshape_no_alloc(a, Tuple(dims))
reshape_no_alloc(a::Missing, dims::Tuple) = missing

function mul_vectrick!(x::AbstractVecOrMat, A::IsometricKroneckerProduct, v::AbstractVecOrMat)
function mul_vectrick!(x::AbstractVecOrMat, A::IKP, v::AbstractVecOrMat)
N = A.B
c, d = size(N)

Expand All @@ -148,7 +152,7 @@ function mul_vectrick!(x::AbstractVecOrMat, A::IsometricKroneckerProduct, v::Abs
end
function mul_vectrick!(
x::AbstractVecOrMat,
A::IsometricKroneckerProduct,
A::IKP,
v::AbstractVecOrMat,
alpha::Number,
beta::Number,
Expand All @@ -162,54 +166,39 @@ function mul_vectrick!(
return x
end

_matmul!(C::AbstractVecOrMat, A::IsometricKroneckerProduct, B::AbstractVecOrMat) =
_matmul!(C::AbstractVecOrMat, A::IKP, B::AbstractVecOrMat) = mul_vectrick!(C, A, B)
mul!(C::AbstractMatrix, A::IKP, B::AbstractMatrix) = mul_vectrick!(C, A, B)
mul!(C::AbstractMatrix, A::IKP, B::Adjoint{T,<:AbstractMatrix{T}}) where {T} =
mul_vectrick!(C, A, B)
mul!(C::AbstractMatrix, A::IsometricKroneckerProduct, B::AbstractMatrix) = mul_vectrick!(C, A, B)
mul!(
C::AbstractMatrix,
A::IsometricKroneckerProduct,
B::Adjoint{T,<:AbstractMatrix{T}},
) where {T} = mul_vectrick!(C, A, B)
mul!(C::AbstractVector, A::IsometricKroneckerProduct, B::AbstractVector) = mul_vectrick!(C, A, B)
mul!(C::AbstractVector, A::IKP, B::AbstractVector) = mul_vectrick!(C, A, B)

_matmul!(
C::AbstractVecOrMat{T},
A::IsometricKroneckerProduct{T},
A::IKP{T},
B::AbstractVecOrMat{T},
) where {T<:LinearAlgebra.BlasFloat} = mul_vectrick!(C, A, B)
_matmul!(C::AbstractVecOrMat, A::AbstractVecOrMat, B::IsometricKroneckerProduct) =
_matmul!(C', B', A')
_matmul!(C::AbstractVecOrMat, A::AbstractVecOrMat, B::IKP) = _matmul!(C', B', A')
_matmul!(
C::AbstractVecOrMat{T},
A::AbstractVecOrMat{T},
B::IsometricKroneckerProduct{T},
B::IKP{T},
) where {T<:LinearAlgebra.BlasFloat} = _matmul!(C', B', A')

_matmul!(
C::AbstractVecOrMat,
A::IsometricKroneckerProduct,
B::AbstractVecOrMat,
alpha::Number,
beta::Number,
) = mul_vectrick!(C, A, B, alpha, beta)
_matmul!(C::AbstractVecOrMat, A::IKP, B::AbstractVecOrMat, alpha::Number, beta::Number) =
mul_vectrick!(C, A, B, alpha, beta)
_matmul!(
C::AbstractVecOrMat{T},
A::IsometricKroneckerProduct{T},
A::IKP{T},
B::AbstractVecOrMat{T},
alpha::Number,
beta::Number,
) where {T<:LinearAlgebra.BlasFloat} = mul_vectrick!(C, A, B, alpha, beta)
_matmul!(
C::AbstractVecOrMat,
A::AbstractVecOrMat,
B::IsometricKroneckerProduct,
alpha::Number,
beta::Number,
) = mul_vectrick!(C', B', A', alpha, beta)
_matmul!(C::AbstractVecOrMat, A::AbstractVecOrMat, B::IKP, alpha::Number, beta::Number) =
mul_vectrick!(C', B', A', alpha, beta)
_matmul!(
C::AbstractVecOrMat{T},
A::AbstractVecOrMat{T},
B::IsometricKroneckerProduct{T},
B::IKP{T},
alpha::Number,
beta::Number,
) where {T<:LinearAlgebra.BlasFloat} = mul_vectrick!(C', B', A', alpha, beta)
7 changes: 6 additions & 1 deletion src/preconditioning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ end
return PI
end

@fastmath @inbounds function make_preconditioner_inv!(PI::IsometricKroneckerProduct, h, d, q)
@fastmath @inbounds function make_preconditioner_inv!(
PI::IsometricKroneckerProduct,
h,
d,
q,
)
val = h^(q + 1 / 2) / factorial(q)
for j in 0:q
PI.B.diag[j+1] = val
Expand Down

0 comments on commit 26a0f6f

Please sign in to comment.