From 3f60a578f7f09c4c54dd56f3dbbb3501b647e453 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 20 Jan 2021 17:17:33 -0700 Subject: [PATCH] init commit --- .gitignore | 35 +++++++++++++++++++++++++++++++++++ Project.toml | 7 +++++++ src/AstroHelper.jl | 7 +++++++ src/Quaternions.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/Project.toml | 2 ++ test/runtests.jl | 12 ++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 Project.toml create mode 100644 src/AstroHelper.jl create mode 100644 src/Quaternions.jl create mode 100644 test/Project.toml create mode 100644 test/runtests.jl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d61e000 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/vscode,julia +# Edit at https://www.toptal.com/developers/gitignore?templates=vscode,julia + +### Julia ### +# Files generated by invoking Julia with --code-coverage +*.jl.cov +*.jl.*.cov + +# Files generated by invoking Julia with --track-allocation +*.jl.mem + +# System-specific files and directories generated by the BinaryProvider and BinDeps packages +# They contain absolute paths specific to the host computer, and so should not be committed +deps/deps.jl +deps/build.log +deps/downloads/ +deps/usr/ +deps/src/ + +# Build artifacts for creating documentation generated by the Documenter package +docs/build/ +docs/site/ + +# File generated by Pkg, the package manager, based on a corresponding Project.toml +# It records a fixed state of all packages used by the project. As such, it should not be +# committed for packages, but should be committed for applications that require a static +# environment. +Manifest.toml + +### vscode ### +.vscode/* +*.code-workspace + +# End of https://www.toptal.com/developers/gitignore/api/vscode,julia \ No newline at end of file diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..5021c23 --- /dev/null +++ b/Project.toml @@ -0,0 +1,7 @@ +name = "AstroHelper" +uuid = "e9cfef36-66ad-4d03-9ce9-ac75942e61e4" +authors = ["Anson "] +version = "0.1.0" + +[deps] +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/AstroHelper.jl b/src/AstroHelper.jl new file mode 100644 index 0000000..70c154f --- /dev/null +++ b/src/AstroHelper.jl @@ -0,0 +1,7 @@ +module AstroHelper +using LinearAlgebra + + +include("Quaternions.jl") + +end # module diff --git a/src/Quaternions.jl b/src/Quaternions.jl new file mode 100644 index 0000000..5fb7efd --- /dev/null +++ b/src/Quaternions.jl @@ -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]) \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..0c36332 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,2 @@ +[deps] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..b86de1d --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,12 @@ +using AstroHelper +using Test + +import AstroHelper: Quaternion + +@testset "Quaternion Initialization" begin + @test Quaternion().r == 1.0 + @test Quaternion([0,0,0,1]) == Quaternion(0, 0, 0, 1) + @test Quaternion([0 0 0], 0) == Quaternion(0, 0, 0, 1) + @test Quaternion([1 -2 2], (-2 * pi / 3)) ≈ Quaternion([-sqrt(3) / 6, sqrt(3) / 3, -sqrt(3) / 3, 1 / 2]) + @test_throws ErrorException Quaternion(1, 2, 3, 4) +end \ No newline at end of file