Skip to content

Commit eb7e06c

Browse files
authored
Add normalize function (#28)
* Add `normalize` * Bump version
1 parent 89407fc commit eb7e06c

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Quaternionic"
22
uuid = "0756cd96-85bf-4b6f-a009-b5012ea7a443"
33
authors = ["Michael Boyle <michael.oliver.boyle@gmail.com>"]
4-
version = "0.1.0"
4+
version = "0.2.0"
55

66
[deps]
77
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"

src/Quaternionic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export AbstractQuaternion
88
export Quaternion, QuaternionF64, QuaternionF32, QuaternionF16, imx, imy, imz, 𝐢, 𝐣, 𝐤
99
export Rotor, RotorF64, RotorF32, RotorF16
1010
export QuatVec, QuatVecF64, QuatVecF32, QuatVecF16
11-
export (), (×), (×̂)
11+
export (), (×), (×̂), normalize
1212
export abs2vec, absvec
1313
export from_float_array, to_float_array,
1414
from_euler_angles, to_euler_angles,

src/algebra.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,19 @@ vector is returned.
117117
return axb / av
118118
end
119119
end
120+
121+
"""
122+
normalize(q)
123+
124+
Return a copy of this quaternion, normalized.
125+
126+
Note that this returns the same type as the input quaternion. If you want to
127+
convert to a `Rotor`, just call `Rotor(q)`, which includes a normalization
128+
step.
129+
"""
130+
@inline function normalize(q::AbstractQuaternion)
131+
return q / abs(q)
132+
end
133+
@inline function normalize(q::Rotor)
134+
return Rotor(q) # already normalizes
135+
end

test/basis.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@
6767
@test basis[i1] - basis[i2] == Q{T}(s...)
6868
end
6969
end
70+
71+
# Normalization
72+
for q in basis
73+
n = normalize(q)
74+
@test typeof(n) === Q{float(T)}
75+
if Q === QuatVec && q == u
76+
continue
77+
end
78+
@test q == n
79+
@test q == normalize(2n)
80+
end
81+
end
82+
83+
let Q = Rotor
84+
# Define basis elements
85+
u = Q{T}(1)
86+
i = Q{T}(𝐢)
87+
j = Q{T}(𝐣)
88+
k = Q{T}(𝐤)
89+
basis = [u, i, j, k]
90+
91+
# Normalization
92+
for q in basis
93+
n = normalize(q)
94+
@test typeof(n) === Q{float(T)}
95+
@test q == n
96+
@test q == normalize(2n)
97+
twoq = Q{T}(2*(q.components)) # Won't normalize
98+
@test q == normalize(twoq)
99+
end
70100
end
71101

72102
let Q = QuatVec

0 commit comments

Comments
 (0)