Skip to content

Commit d0ddf12

Browse files
hyrodiumsethaxen
andauthored
Add support for exp(::Quaternion{Int}) (#49)
* add support for exp(::Quaternion{Int}) * replace abs(s) < eps() with iszero(s) * Update src/Quaternion.jl Co-authored-by: Seth Axen <[email protected]> * add tests for exp Co-authored-by: Seth Axen <[email protected]>
1 parent 82e791b commit d0ddf12

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Quaternion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function exp(q::Quaternion)
118118
if th > 0
119119
scale *= sin(th) / th
120120
end
121-
Quaternion(se * cos(th), scale * q.v1, scale * q.v2, scale * q.v3, abs(s) < eps(typeof(s)))
121+
Quaternion(se * cos(th), scale * q.v1, scale * q.v2, scale * q.v3, iszero(s))
122122
end
123123

124124
function log(q::Quaternion)

test/test_Quaternion.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,37 @@ for _ in 1:100
153153
end
154154
end
155155

156+
@testset "exp" begin
157+
@test exp(Quaternion(0, 0, 0, 0)) == Quaternion(1, 0, 0, 0, true)
158+
@test exp(Quaternion(2, 0, 0, 0)) == Quaternion(exp(2), 0, 0, 0, false)
159+
@test exp(Quaternion(0, 2, 0, 0)) == Quaternion(cos(2), sin(2), 0, 0, true)
160+
@test exp(Quaternion(0, 0, 2, 0)) == Quaternion(cos(2), 0, sin(2), 0, true)
161+
@test exp(Quaternion(0, 0, 0, 2)) == Quaternion(cos(2), 0, 0, sin(2), true)
162+
163+
@test norm(exp(Quaternion(0, 0, 0, 0))) 1
164+
@test norm(exp(Quaternion(2, 0, 0, 0))) 1
165+
@test norm(exp(Quaternion(0, 2, 0, 0))) 1
166+
@test norm(exp(Quaternion(0, 0, 2, 0))) 1
167+
@test norm(exp(Quaternion(0, 0, 0, 2))) 1
168+
169+
@test exp(Quaternion(0., 0., 0., 0.)) == Quaternion(1, 0, 0, 0, true)
170+
@test exp(Quaternion(2., 0., 0., 0.)) == Quaternion(exp(2), 0, 0, 0, false)
171+
@test exp(Quaternion(0., 2., 0., 0.)) == Quaternion(cos(2), sin(2), 0, 0, true)
172+
@test exp(Quaternion(0., 0., 2., 0.)) == Quaternion(cos(2), 0, sin(2), 0, true)
173+
@test exp(Quaternion(0., 0., 0., 2.)) == Quaternion(cos(2), 0, 0, sin(2), true)
174+
175+
@test norm(exp(Quaternion(0., 0., 0., 0.))) 1
176+
@test norm(exp(Quaternion(2., 0., 0., 0.))) 1
177+
@test norm(exp(Quaternion(0., 2., 0., 0.))) 1
178+
@test norm(exp(Quaternion(0., 0., 2., 0.))) 1
179+
@test norm(exp(Quaternion(0., 0., 0., 2.))) 1
180+
181+
@test exp(Quaternion(0,0,0,0)) isa Quaternion{Float64}
182+
@test exp(Quaternion(0.,0,0,0)) isa Quaternion{Float64}
183+
@test exp(Quaternion(0//1,0,0,0)) isa Quaternion{Float64}
184+
@test exp(Quaternion(BigFloat(0),0,0,0)) isa Quaternion{BigFloat}
185+
end
186+
156187
@testset "random quaternions" begin
157188
@testset "quatrand" begin
158189
rng = Random.MersenneTwister(42)

0 commit comments

Comments
 (0)