Skip to content

Commit 7e0efc6

Browse files
hyrodiumsethaxen
andauthored
Add support for RealDot (#119)
* add support for RealDot * add tests for realdot * add compat table for RealDot * Add `@inferred` in test for `realdot` Co-authored-by: Seth Axen <seth@sethaxen.com> * use realdot in slerp * replace ≈ with == in realdot test * remove muladd in realdot definition * Add suggested comment Co-authored-by: Seth Axen <seth@sethaxen.com> * Use explicit RealDot.realdot Co-authored-by: Seth Axen <seth@sethaxen.com> * use RealDot.realdot in abs2 * add commutative law test for realdot * add commutative law test for real∘dot * bump version to v0.7.3 Co-authored-by: Seth Axen <seth@sethaxen.com>
1 parent 268cee0 commit 7e0efc6

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
name = "Quaternions"
22
uuid = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
3-
version = "0.7.2"
3+
version = "0.7.3"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
8+
RealDot = "c1ae055f-0cd5-4b69-90a6-9a35b1a98df9"
89

910
[compat]
1011
Aqua = "0.5"
12+
RealDot = "0.1"
1113
julia = "1"
1214

1315
[extras]

src/Quaternion.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Base.conj(q::Quaternion) = Quaternion(q.s, -q.v1, -q.v2, -q.v3)
139139
Base.abs(q::Quaternion) = sqrt(abs2(q))
140140
Base.float(q::Quaternion{T}) where T = convert(Quaternion{float(T)}, q)
141141
abs_imag(q::Quaternion) = sqrt(q.v2 * q.v2 + (q.v1 * q.v1 + q.v3 * q.v3)) # ordered to match abs2
142-
Base.abs2(q::Quaternion) = (q.s * q.s + q.v2 * q.v2) + (q.v1 * q.v1 + q.v3 * q.v3)
142+
Base.abs2(q::Quaternion) = RealDot.realdot(q,q)
143143
Base.inv(q::Quaternion) = conj(q) / abs2(q)
144144

145145
Base.isreal(q::Quaternion) = iszero(q.v1) & iszero(q.v2) & iszero(q.v3)
@@ -320,7 +320,7 @@ true
320320
iszero(qb0) && throw(DomainError(qb0, "The input quaternion must be non-zero."))
321321
qa = qa0 / abs(qa0)
322322
qb = qb0 / abs(qb0)
323-
coshalftheta = qa.s * qb.s + qa.v1 * qb.v1 + qa.v2 * qb.v2 + qa.v3 * qb.v3
323+
coshalftheta = RealDot.realdot(qa, qb)
324324

325325
if coshalftheta < 0
326326
qb = -qb
@@ -386,6 +386,9 @@ LinearAlgebra.lyap(a::Quaternion, c::Quaternion) = lyap(promote(a, c)...)
386386
LinearAlgebra.lyap(a::Real, c::Quaternion) = c / -2a
387387
LinearAlgebra.lyap(a::Quaternion, c::Real) = c / -2real(a)
388388

389+
## RealDot
390+
# ordering chosen so that real(p'q) == real(q'p) == realdot(p, q) == realdot(q, p), i.e. exact equality
391+
@inline RealDot.realdot(p::Quaternion, q::Quaternion) = (p.s * q.s + p.v2 * q.v2) + (p.v1 * q.v1 + p.v3 * q.v3)
389392
Base.widen(::Type{Quaternion{T}}) where {T} = Quaternion{widen(T)}
390393

391394
Base.flipsign(x::Quaternion, y::Real) = ifelse(signbit(y), -x, x)

src/Quaternions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Quaternions
22

33
using Random
44
using LinearAlgebra
5+
using RealDot
56

67
include("Quaternion.jl")
78

test/Quaternion.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,21 @@ end
580580
end
581581
end
582582

583+
@testset "RealDot with $T" for T in (Float32, Float64)
584+
for _ in 1:10
585+
q1 = randn(Quaternion{T})
586+
q2 = randn(Quaternion{T})
587+
# Check real∘dot is equal to realdot.
588+
@test real(dot(q1,q2)) == @inferred(realdot(q1,q2))
589+
# Check realdot is commutative.
590+
@test realdot(q1,q2) == realdot(q2,q1)
591+
# Check real∘dot is also commutative just in case.
592+
@test real(dot(q1,q2)) == real(dot(q2,q1))
593+
# Check the return type of realdot is correct.
594+
@test realdot(q1,q2) isa T
595+
end
596+
end
597+
583598
@testset "widen" begin
584599
@test widen(Quaternion{Int}) === Quaternion{Int128}
585600
@test widen(QuaternionF32) === QuaternionF64

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test
22
using Quaternions
33
using Aqua
4+
using RealDot
45

56
Aqua.test_all(Quaternions)
67

0 commit comments

Comments
 (0)