1
0
mirror of https://gitlab.com/MisterBiggs/astro-helper.git synced 2025-06-15 14:46:40 +00:00

init commit

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

35
.gitignore vendored Normal file
View File

@ -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

7
Project.toml Normal file
View File

@ -0,0 +1,7 @@
name = "AstroHelper"
uuid = "e9cfef36-66ad-4d03-9ce9-ac75942e61e4"
authors = ["Anson <anson@ansonbiggs.com>"]
version = "0.1.0"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

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])

2
test/Project.toml Normal file
View File

@ -0,0 +1,2 @@
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

12
test/runtests.jl Normal file
View File

@ -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