If a struct’s field is a StaticArray, then how do I (generically) specify its type? As seen below, not specifying the type has an effect on performance, but my attempt is obviously no good either, as it’s far to specific.
using BenchmarkTools, StaticArrays
mutable struct TypedModel
A::SArray{Tuple{6,6},Float64,2,36}
Atranspose::SArray{Tuple{6,6},Float64,2,36}
u::SArray{Tuple{6},Float64,1,6}
R::SArray{Tuple{6,6},Float64,2,36}
end
mutable struct Model
A
Atranspose
u
R
end
function multiplythings!(μ, Σ, A, Atranspose, u, R, n)
for i = 1:n
μ[i] = A * μ[i] + u
Σ[i] = A * Σ[i] * Atranspose + R
end
end
function multiplythings!(μ, Σ, model, n)
for i = 1:n
μ[i] = model.A * μ[i] + model.u
Σ[i] = model.A * Σ[i] * model.Atranspose + model.R
end
end
# Model
xdim = 6
n = 1000
A = @SMatrix rand(xdim, xdim)
Atranspose = A'
u = @SVector rand(xdim)
R = @SMatrix rand(xdim, xdim)
# State estimate parameters
μ = [@SVector rand(xdim) for _ = 1:n]
Σ = [@SMatrix rand(xdim, xdim) for _ = 1:n]
# Create models
model = Model(A, Atranspose, u, R)
typedmodel = TypedModel(A, Atranspose, u, R)
@btime multiplythings!($μ, $Σ, $A, $Atranspose, $u, $R, $n)
@btime multiplythings!($μ, $Σ, $typedmodel, $n)
@btime multiplythings!($μ, $Σ, $model, $n)