Skip to content

Commit

Permalink
Add support for exp(::Quaternion{Int}) (#49)
Browse files Browse the repository at this point in the history
* add support for exp(::Quaternion{Int})

* replace abs(s) < eps() with iszero(s)

* Update src/Quaternion.jl

Co-authored-by: Seth Axen <seth.axen@gmail.com>

* add tests for exp

Co-authored-by: Seth Axen <seth.axen@gmail.com>
  • Loading branch information
hyrodium and sethaxen authored Feb 20, 2022
1 parent 82e791b commit d0ddf12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function exp(q::Quaternion)
if th > 0
scale *= sin(th) / th
end
Quaternion(se * cos(th), scale * q.v1, scale * q.v2, scale * q.v3, abs(s) < eps(typeof(s)))
Quaternion(se * cos(th), scale * q.v1, scale * q.v2, scale * q.v3, iszero(s))
end

function log(q::Quaternion)
Expand Down
31 changes: 31 additions & 0 deletions test/test_Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,37 @@ for _ in 1:100
end
end

@testset "exp" begin
@test exp(Quaternion(0, 0, 0, 0)) == Quaternion(1, 0, 0, 0, true)
@test exp(Quaternion(2, 0, 0, 0)) == Quaternion(exp(2), 0, 0, 0, false)
@test exp(Quaternion(0, 2, 0, 0)) == Quaternion(cos(2), sin(2), 0, 0, true)
@test exp(Quaternion(0, 0, 2, 0)) == Quaternion(cos(2), 0, sin(2), 0, true)
@test exp(Quaternion(0, 0, 0, 2)) == Quaternion(cos(2), 0, 0, sin(2), true)

@test norm(exp(Quaternion(0, 0, 0, 0))) 1
@test norm(exp(Quaternion(2, 0, 0, 0))) 1
@test norm(exp(Quaternion(0, 2, 0, 0))) 1
@test norm(exp(Quaternion(0, 0, 2, 0))) 1
@test norm(exp(Quaternion(0, 0, 0, 2))) 1

@test exp(Quaternion(0., 0., 0., 0.)) == Quaternion(1, 0, 0, 0, true)
@test exp(Quaternion(2., 0., 0., 0.)) == Quaternion(exp(2), 0, 0, 0, false)
@test exp(Quaternion(0., 2., 0., 0.)) == Quaternion(cos(2), sin(2), 0, 0, true)
@test exp(Quaternion(0., 0., 2., 0.)) == Quaternion(cos(2), 0, sin(2), 0, true)
@test exp(Quaternion(0., 0., 0., 2.)) == Quaternion(cos(2), 0, 0, sin(2), true)

@test norm(exp(Quaternion(0., 0., 0., 0.))) 1
@test norm(exp(Quaternion(2., 0., 0., 0.))) 1
@test norm(exp(Quaternion(0., 2., 0., 0.))) 1
@test norm(exp(Quaternion(0., 0., 2., 0.))) 1
@test norm(exp(Quaternion(0., 0., 0., 2.))) 1

@test exp(Quaternion(0,0,0,0)) isa Quaternion{Float64}
@test exp(Quaternion(0.,0,0,0)) isa Quaternion{Float64}
@test exp(Quaternion(0//1,0,0,0)) isa Quaternion{Float64}
@test exp(Quaternion(BigFloat(0),0,0,0)) isa Quaternion{BigFloat}
end

@testset "random quaternions" begin
@testset "quatrand" begin
rng = Random.MersenneTwister(42)
Expand Down

0 comments on commit d0ddf12

Please sign in to comment.