Skip to content

Commit

Permalink
Update around inv (#133)
Browse files Browse the repository at this point in the history
* add docstring to `inv(::Quaternion)`

* add methods for `//`

* add tests for `//`

* bump version to v0.7.5
  • Loading branch information
hyrodium authored Nov 20, 2023
1 parent 89232ef commit 2055de5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 4 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ round(::Quaternion)
conj
```

```@docs
inv
```

```@docs
sign
```
Expand Down
22 changes: 22 additions & 0 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

3 comments on commit 2055de5

@hyrodium
Copy link
Collaborator Author

@hyrodium hyrodium commented on 2055de5 Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyrodium
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/95637

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.5 -m "<description of version>" 2055de550e5335b9c7d3d27563dc0c7a9118359a
git push origin v0.7.5

Please sign in to comment.