From 2055de550e5335b9c7d3d27563dc0c7a9118359a Mon Sep 17 00:00:00 2001 From: Yuto Horikawa Date: Mon, 20 Nov 2023 18:47:17 +0900 Subject: [PATCH] Update around `inv` (#133) * add docstring to `inv(::Quaternion)` * add methods for `//` * add tests for `//` * bump version to v0.7.5 --- Project.toml | 2 +- docs/src/api.md | 4 ++++ src/Quaternion.jl | 22 ++++++++++++++++++++++ test/Quaternion.jl | 9 +++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 13d907e..ada3f15 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Quaternions" uuid = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0" -version = "0.7.4" +version = "0.7.5" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/docs/src/api.md b/docs/src/api.md index 59935e5..c7c4eff 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -26,6 +26,10 @@ round(::Quaternion) conj ``` +```@docs +inv +``` + ```@docs sign ``` diff --git a/src/Quaternion.jl b/src/Quaternion.jl index 0098c0a..f2f5562 100644 --- a/src/Quaternion.jl +++ b/src/Quaternion.jl @@ -158,6 +158,25 @@ function abs_imag(q::Quaternion) end end Base.abs2(q::Quaternion) = RealDot.realdot(q,q) + +""" + inv(q::Quaternion) + +Return the multiplicative inverse of `q::Quaternion`, such that `q*inv(q)` or `inv(q)*q` +yields `one(q)` (the multiplicative identity) up to roundoff errors. + +# Examples +```jldoctest +julia> inv(quat(1)) +QuaternionF64(1.0, -0.0, -0.0, -0.0) + +julia> inv(quat(1, 2, 0, 0)) +QuaternionF64(0.2, -0.4, -0.0, -0.0) + +julia> inv(quat(2//3)) +Quaternion{Rational{Int64}}(3//2, 0//1, 0//1, 0//1) +``` +""" function Base.inv(q::Quaternion) if isinf(q) return quat( @@ -231,6 +250,9 @@ function Base.:/(q::Quaternion{T}, w::Quaternion{T}) where T return (q * conj(p)) / RealDot.realdot(w, p) end +Base.://(x::Quaternion, y::Real) = quat(real(x)//y, imag_part(x).//y...) +Base.://(x::Number, y::Quaternion) = x*conj(y)//abs2(y) + Base.:(==)(q::Quaternion, w::Quaternion) = (q.s == w.s) & (q.v1 == w.v1) & (q.v2 == w.v2) & (q.v3 == w.v3) function Base.isequal(q::Quaternion, w::Quaternion) isequal(q.s, w.s) & isequal(q.v1, w.v1) & isequal(q.v2, w.v2) & isequal(q.v3, w.v3) diff --git a/test/Quaternion.jl b/test/Quaternion.jl index 9f9b6bd..53b36bc 100644 --- a/test/Quaternion.jl +++ b/test/Quaternion.jl @@ -351,6 +351,15 @@ end end end + @testset "//" begin + q = quat(1,2,3,4) + r = quat(1,2,3,4//1) + @test q // 1 === r + @test isone(q // q) + @test quat(1,-2,0,0) // quat(2,1,0,0) === quat(0,-1//1,0,0) + @test inv(r) === (1//1)/q + end + @testset "^" begin @testset "^(::Quaternion, ::Real)" begin for _ in 1:100