@@ -28,9 +28,9 @@ Base.promote_rule(::Type{Quaternion{T}}, ::Type{S}) where {T <: Real, S <: Real}
28
28
Base. promote_rule (:: Type{Quaternion{T}} , :: Type{Quaternion{S}} ) where {T <: Real , S <: Real } = Quaternion{promote_type (T, S)}
29
29
30
30
"""
31
- quat(r , [i, j, k ])
31
+ quat(w , [x, y, z ])
32
32
33
- Convert real numbers or arrays to quaternion. `i, j, k ` defaults to zero.
33
+ Convert real numbers or arrays to quaternion. `x, y, z ` defaults to zero.
34
34
35
35
# Examples
36
36
```jldoctest
@@ -49,15 +49,45 @@ julia> quat([1, 2, 3])
49
49
"""
50
50
quat
51
51
52
- quat (p, v1, v2, v3) = Quaternion (p, v1, v2, v3)
53
- quat (x) = Quaternion (x)
52
+ quat (q:: Quaternion ) = q
53
+ quat (s:: Real ) = Quaternion (s)
54
+ quat (s:: Real , v1:: Real , v2:: Real , v3:: Real ) = Quaternion (s, v1, v2, v3)
55
+
56
+ # # Array operations on quaternions ##
57
+ quat (A:: AbstractArray{<:Quaternion} ) = A
54
58
function quat (A:: AbstractArray{T} ) where T
55
59
if ! isconcretetype (T)
56
60
error (" `quat` not defined on abstractly-typed arrays; please convert to a more specific type" )
57
61
end
58
62
convert (AbstractArray{typeof (quat (zero (T)))}, A)
59
63
end
60
64
65
+ """
66
+ quat(T::Type)
67
+
68
+ Return an appropriate type that can represent a value of type `T` as a quaternion.
69
+ Equivalent to `typeof(quat(zero(T)))`.
70
+
71
+ # Examples
72
+ ```jldoctest
73
+ julia> quat(Quaternion{Int})
74
+ Quaternion{Int64}
75
+
76
+ julia> quat(Int)
77
+ Quaternion{Int64}
78
+ ```
79
+ """
80
+ quat (:: Type{T} ) where {T<: Real } = Quaternion{T}
81
+ quat (:: Type{Quaternion{T}} ) where {T<: Real } = Quaternion{T}
82
+
83
+ quat (:: Missing ) = missing
84
+ # Same definitioin as in Base: https://github.com/JuliaLang/julia/blob/v1.9.3/base/missing.jl#L111-L115
85
+ quat (:: Type{Missing} ) = Missing
86
+ function quat (:: Type{Union{T, Missing}} ) where T
87
+ T === Any && throw (MethodError (quat, (Any,))) # To prevent StackOverflowError
88
+ Union{quat (T), Missing}
89
+ end
90
+
61
91
"""
62
92
real(T::Type{<:Quaternion})
63
93
0 commit comments