Skip to content

Commit 2055de5

Browse files
authored
Update around inv (#133)
* add docstring to `inv(::Quaternion)` * add methods for `//` * add tests for `//` * bump version to v0.7.5
1 parent 89232ef commit 2055de5

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Quaternions"
22
uuid = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
3-
version = "0.7.4"
3+
version = "0.7.5"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

docs/src/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ round(::Quaternion)
2626
conj
2727
```
2828

29+
```@docs
30+
inv
31+
```
32+
2933
```@docs
3034
sign
3135
```

src/Quaternion.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ function abs_imag(q::Quaternion)
158158
end
159159
end
160160
Base.abs2(q::Quaternion) = RealDot.realdot(q,q)
161+
162+
"""
163+
inv(q::Quaternion)
164+
165+
Return the multiplicative inverse of `q::Quaternion`, such that `q*inv(q)` or `inv(q)*q`
166+
yields `one(q)` (the multiplicative identity) up to roundoff errors.
167+
168+
# Examples
169+
```jldoctest
170+
julia> inv(quat(1))
171+
QuaternionF64(1.0, -0.0, -0.0, -0.0)
172+
173+
julia> inv(quat(1, 2, 0, 0))
174+
QuaternionF64(0.2, -0.4, -0.0, -0.0)
175+
176+
julia> inv(quat(2//3))
177+
Quaternion{Rational{Int64}}(3//2, 0//1, 0//1, 0//1)
178+
```
179+
"""
161180
function Base.inv(q::Quaternion)
162181
if isinf(q)
163182
return quat(
@@ -231,6 +250,9 @@ function Base.:/(q::Quaternion{T}, w::Quaternion{T}) where T
231250
return (q * conj(p)) / RealDot.realdot(w, p)
232251
end
233252

253+
Base.://(x::Quaternion, y::Real) = quat(real(x)//y, imag_part(x).//y...)
254+
Base.://(x::Number, y::Quaternion) = x*conj(y)//abs2(y)
255+
234256
Base.:(==)(q::Quaternion, w::Quaternion) = (q.s == w.s) & (q.v1 == w.v1) & (q.v2 == w.v2) & (q.v3 == w.v3)
235257
function Base.isequal(q::Quaternion, w::Quaternion)
236258
isequal(q.s, w.s) & isequal(q.v1, w.v1) & isequal(q.v2, w.v2) & isequal(q.v3, w.v3)

test/Quaternion.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ end
351351
end
352352
end
353353

354+
@testset "//" begin
355+
q = quat(1,2,3,4)
356+
r = quat(1,2,3,4//1)
357+
@test q // 1 === r
358+
@test isone(q // q)
359+
@test quat(1,-2,0,0) // quat(2,1,0,0) === quat(0,-1//1,0,0)
360+
@test inv(r) === (1//1)/q
361+
end
362+
354363
@testset "^" begin
355364
@testset "^(::Quaternion, ::Real)" begin
356365
for _ in 1:100

0 commit comments

Comments
 (0)