Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more methods to quat #132

Merged
merged 7 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Base.promote_rule(::Type{Quaternion{T}}, ::Type{S}) where {T <: Real, S <: Real}
Base.promote_rule(::Type{Quaternion{T}}, ::Type{Quaternion{S}}) where {T <: Real, S <: Real} = Quaternion{promote_type(T, S)}

"""
quat(r, [i, j, k])
quat(w, [x, y, z])

Convert real numbers or arrays to quaternion. `i, j, k` defaults to zero.
Convert real numbers or arrays to quaternion. `x, y, z` defaults to zero.

# Examples
```jldoctest
Expand All @@ -49,15 +49,45 @@ julia> quat([1, 2, 3])
"""
quat

quat(p, v1, v2, v3) = Quaternion(p, v1, v2, v3)
quat(x) = Quaternion(x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically this removal would be breaking. e.g. this would block users from constructing quaternions with SymbolicUtils.Num or Unitful.Quantity. Not that I'm opposed to it, but we need to be certain we're fine with that first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is not a breaking change.

No problem with Symbolics.Num because it's a subtype of Real.

julia> using Symbolics, Quaternions

julia> a = Symbolics.Num(3)
3

julia> quat(a,a,a,a)
Quaternion{Num}(3, 3, 3, 3)

julia> quat(a)
Quaternion{Num}(3, 0, 0, 0)

julia> Symbolics.Num <: Real
true

Unitful.Quantitiy is already not compatible with Quaternions.jl.

Before this PR

julia> using Unitful, Quaternions

julia> d = 3u"°"
3°

julia> quat(d)
ERROR: MethodError: no method matching Quaternion(::Quantity{Int64, NoDims, Unitful.FreeUnits{(°,), NoDims, nothing}})

Closest candidates are:
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
   @ Base char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number
   @ Base twiceprecision.jl:266
  ...

After this PR

julia> using Unitful, Quaternions

julia> d = 3u"°"
3°

julia> quat(d)
ERROR: MethodError: no method matching quat(::Quantity{Int64, NoDims, Unitful.FreeUnits{(°,), NoDims, nothing}})

Closest candidates are:
  quat(::Quaternion)
   @ Quaternions ~/.julia/dev/Quaternions/src/Quaternion.jl:52
  quat(::Real)
   @ Quaternions ~/.julia/dev/Quaternions/src/Quaternion.jl:53
  quat(::Real, ::Real, ::Real, ::Real)
   @ Quaternions ~/.julia/dev/Quaternions/src/Quaternion.jl:54
  ...

quat(q::Quaternion) = q
quat(s::Real) = Quaternion(s)
quat(s::Real, v1::Real, v2::Real, v3::Real) = Quaternion(s, v1, v2, v3)

## Array operations on quaternions ##
quat(A::AbstractArray{<:Quaternion}) = A
function quat(A::AbstractArray{T}) where T
if !isconcretetype(T)
error("`quat` not defined on abstractly-typed arrays; please convert to a more specific type")
end
convert(AbstractArray{typeof(quat(zero(T)))}, A)
end

"""
quat(T::Type)

Return an appropriate type that can represent a value of type `T` as a quaternion.
Equivalent to `typeof(quat(zero(T)))`.

# Examples
```jldoctest
julia> quat(Quaternion{Int})
Quaternion{Int64}

julia> quat(Int)
Quaternion{Int64}
```
"""
quat(::Type{T}) where {T<:Real} = Quaternion{T}
quat(::Type{Quaternion{T}}) where {T<:Real} = Quaternion{T}

quat(::Missing) = missing
# Same definitioin as in Base: https://github.com/JuliaLang/julia/blob/v1.9.3/base/missing.jl#L111-L115
quat(::Type{Missing}) = Missing
function quat(::Type{Union{T, Missing}}) where T
T === Any && throw(MethodError(quat, (Any,))) # To prevent StackOverflowError
Union{quat(T), Missing}
end
hyrodium marked this conversation as resolved.
Show resolved Hide resolved

"""
real(T::Type{<:Quaternion})

Expand Down
12 changes: 12 additions & 0 deletions test/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ end
@test quat(Quaternion(1, 2, 3, 4)) === Quaternion(1, 2, 3, 4)
@test quat([2, 3, 4]) == Quaternion{Int}[2, 3, 4]
@test_throws ErrorException quat(Real[1,2,3])
@test quat(Quaternion[1,2]) == Quaternion[1,2]

@test quat(Int) === Quaternion{Int}
@test quat(Float32) === Quaternion{Float32}
@test quat(Quaternion{Int}) === Quaternion{Int}
@test quat(Quaternion{Float32}) === Quaternion{Float32}

# Note that `quat(1,missing,0,0)` throws an error.
# This is the same behavior as `complex(1,missing)`.
@test quat(missing) === missing
@test quat(Missing) === Missing
@test quat(Union{Missing, Int}) === Union{Missing, Quaternion{Int}}
end

@testset "random generation" begin
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ using Quaternions
using Aqua
using RealDot

Aqua.test_all(Quaternions)
# Avoid tests on unbound_args.
# https://github.com/JuliaGeometry/Quaternions.jl/pull/132#discussion_r1383817950
Aqua.test_all(Quaternions, unbound_args=false)

include("helpers.jl")
include("Quaternion.jl")
Loading