1
0
mirror of https://gitlab.com/MisterBiggs/astro-helper.git synced 2025-08-02 11:21:23 +00:00

init commit

This commit is contained in:
2021-01-20 17:17:33 -07:00
commit 3f60a578f7
6 changed files with 108 additions and 0 deletions

7
src/AstroHelper.jl Normal file
View File

@@ -0,0 +1,7 @@
module AstroHelper
using LinearAlgebra
include("Quaternions.jl")
end # module

45
src/Quaternions.jl Normal file
View File

@@ -0,0 +1,45 @@
struct Quaternion
i::Float64
j::Float64
k::Float64
r::Float64
Quaternion(i, j, k, r) = norm([i, j, k, r]) 1 ? new(i, j, k, r) : error("Magnitude not equal to 1.")
Quaternion(q) = Quaternion(q[1], q[2], q[3], q[4])
Quaternion() = new(0, 0, 0, 1)
# Quaternion(ē, ϕ) = Quaternion(vcat(if sum(ē) == 0 ? [0 0 0] : ē / norm(ē) * sin(ϕ / 2),cos(ϕ / 2)))
function Quaternion(, ϕ)
if sum() == 0
return Quaternion([0 0 0 cos(ϕ / 2)])
end
dir = normalize() * sin(ϕ / 2)
return Quaternion(dir[1], dir[2], dir[3], cos(ϕ / 2))
end
end
function QuaternionMultiplication(l::Quaternion, r::Quaternion)
R = [r.r r.k r.j r.i;
-r.k r.r r.i r.j;
r.j -r.i r.r r.k;
-r.i -r.j -r.k r.r]
L = [l.i; l.j; l.k; l.r]
return Quaternion(R * L)
end
Base.:*(l::Quaternion, r::Quaternion) = QuaternionMultiplication(l::Quaternion, r::Quaternion)
Base.iterate(q::Quaternion) = (q.i, 1)
Base.iterate(q::Quaternion, state=1) = state > 4 ? nothing : ([q.i q.j q.k q.r][state], state + 1)
Base.length(q::Quaternion) = 4
Base.collect(q::Quaternion) = [q.i q.j q.k q.r]
Base.getindex(q::Quaternion, i) = collect(q)[i]
Base.isapprox(a::Quaternion,b::Quaternion) = isapprox(collect(a), collect(b))
LinearAlgebra.norm(q::Quaternion) = norm(collect(q))
LinearAlgebra.normalize(q::Quaternion) = collect(q) / norm(q)
# q = Quaternion([1,-2,2], -2 * pi / 3) * Quaternion([0,0,0,1])
# println(q[3])