I’m trying to perform a 3D rotation on a diagonal matrix, which seems to work with the code below.
using LinearAlgebra
using StaticArrays
function euler(φ, θ, ψ)
R = Array{Float64}(undef,(3,3))
cosφ = cos(φ); cosψ = cos(ψ); cosθ = cos(θ)
sinφ = sin(φ); sinψ = sin(ψ); sinθ = sin(θ)
R[1,1] = cosφ*cosθ*cosψ - sinφ*sinψ
R[1,2] = sinφ*cosθ*cosψ + cosφ*sinψ
R[1,3] = -sinθ*cosψ
R[2,1] = -cosφ*cosθ*sinψ - sinφ*cosψ
R[2,2] = -sinφ*cosθ*sinψ + cosφ*cosψ
R[2,3] = sinθ*sinψ
R[3,1] = cosφ*sinθ
R[3,2] = sinφ*sinθ
R[3,3] = cosθ
return R
end
R = euler(pi,0.0,0.0)
v = [1.0; 2.0 ; 3.0]
m = transpose(R) * Diagonal(v) * R
It would seem a good case for static arrays, but I cannot seem to get the right syntax. Even basic things like
function euler(φ, θ, ψ)
R = SMatrix{3,3,Float64,9}
cosφ = cos(φ); cosψ = cos(ψ); cosθ = cos(θ)
sinφ = sin(φ); sinψ = sin(ψ); sinθ = sin(θ)
R[1,1] = cosφ*cosθ*cosψ - sinφ*sinψ
R[1,2] = sinφ*cosθ*cosψ + cosφ*sinψ
R[1,3] = -sinθ*cosψ
R[2,1] = -cosφ*cosθ*sinψ - sinφ*cosψ
R[2,2] = -sinφ*cosθ*sinψ + cosφ*cosψ
R[2,3] = sinθ*sinψ
R[3,1] = cosφ*sinθ
R[3,2] = sinφ*sinθ
R[3,3] = cosθ
return R
end
returns an error as R[1,1]
seemingly cannot be indexed. I’m also unable to create a diagonal matrix, or an identity matrix (eye(SMatrix{3,3})
suggested in the docs fails for me, with “eye undefined”).
Basically, I have no idea how to work with StaticArrays – I assumed it was going to be the exact same code and syntax as regular arrays, only they’d have a fixed immutable size, but clearly I got this wrong.
Any clarifications or suggestions welcome!