Skip to content

Commit b378d15

Browse files
hyrodiumsethaxen
andauthored
Update documentation (#97)
* add docstrings for real and Quaternion * add api.md * remove invisible character (U+00ad) * add docstrings for conj and imag_part * update api.md * Update docstring of `imag_part` Co-authored-by: Seth Axen <seth@sethaxen.com> * add docstring of `real(::Type{Quaternion{T}})` * add an example `real(::Array{<:Quaternion})` * Update the docstrings of `Quaternion` Co-authored-by: Seth Axen <seth@sethaxen.com> * update README.md * update README.md * more specific type in docstring Co-authored-by: Seth Axen <seth@sethaxen.com> * Add docstring for `Base.real(::AbstractArray{<:Quaternion})` Co-authored-by: Seth Axen <seth@sethaxen.com> * remove duplicated jldoctest for `Base.real` * add docstring for `quat` * update wording * rename APIs to API Co-authored-by: Seth Axen <seth@sethaxen.com> * Fix docstring with plural Co-authored-by: Seth Axen <seth@sethaxen.com> * Use Oxford comma in docstring Co-authored-by: Seth Axen <seth@sethaxen.com> * add more jidoctests for `quat` Co-authored-by: Seth Axen <seth@sethaxen.com> * update docstring of `imag_part` Co-authored-by: Seth Axen <seth@sethaxen.com> * update docstring of `imag_part` Co-authored-by: Seth Axen <seth@sethaxen.com> * update docstring of `imag_part` Co-authored-by: Seth Axen <seth@sethaxen.com> * update docstring of `real` Co-authored-by: Seth Axen <seth@sethaxen.com> * update docstring of `extend_analytic` Co-authored-by: Seth Axen <seth@sethaxen.com> * add depwarn to Quaternion(a::AbstractVector) * add `@test_deprecated` for `quat` * add docstring of `slerp` * add more `@docs` in api.md Co-authored-by: Seth Axen <seth@sethaxen.com>
1 parent f07a309 commit b378d15

File tree

5 files changed

+163
-56
lines changed

5 files changed

+163
-56
lines changed

README.md

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,7 @@ A Julia implementation of quaternions.
88
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
99

1010
[Quaternions](http://en.wikipedia.org/wiki/Quaternion) are best known for their suitability
11-
as representations of 3D rotational orientation. They can also be viewed as an extension of complex numbers.
11+
as representations of 3D rotational orientation.
12+
They can also be viewed as an extension of complex numbers.
1213

13-
Implemented functions are:
14-
15-
+-*/^
16-
real
17-
imag_part (tuple)
18-
conj
19-
abs
20-
abs2
21-
normalize
22-
normalizea (return normalized quaternion and absolute value as a pair)
23-
angleaxis (taken as an orientation, return the angle and axis (3 vector) as a tuple)
24-
angle
25-
axis
26-
sqrt
27-
exp
28-
exp2
29-
exp10
30-
expm1
31-
log2
32-
log10
33-
log1p
34-
sin
35-
cos
36-
tan
37-
asin
38-
acos
39-
atan
40-
sinh
41-
cosh
42-
tanh
43-
asinh
44-
acosh
45-
atanh
46-
csc
47-
sec
48-
cot
49-
acsc
50-
asec
51-
acot
52-
csch
53-
sech
54-
coth
55-
acsch
56-
asech
57-
acoth
58-
sinpi
59-
cospi
60-
sincos
61-
sincospi
62-
slerp
63-
rand
64-
randn
14+
In JuliaGeometry organization, there is also [Octonions.jl](https://github.com/JuliaGeometry/Octonions.jl) package.

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ makedocs(;
1414
),
1515
pages=[
1616
"Home" => "index.md",
17+
"APIs" => "api.md",
1718
"Examples" => ["examples/dual_quaternions.md"],
1819
],
1920
)

docs/src/api.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# API
2+
3+
```@docs
4+
Quaternion
5+
```
6+
7+
```@docs
8+
quat
9+
```
10+
11+
```@docs
12+
real(::Quaternion)
13+
real(::AbstractArray{<:Quaternion})
14+
real(::Type{Quaternion{T}}) where {T}
15+
```
16+
17+
```@docs
18+
imag_part
19+
```
20+
21+
```@docs
22+
conj
23+
```
24+
25+
```@docs
26+
slerp
27+
```
28+
29+
```@docs
30+
Quaternions.extend_analytic
31+
```

src/Quaternion.jl

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
Quaternion{T<:Real} <: Number
3+
4+
Quaternion number type with real and imaginary parts of type `T`.
5+
6+
`QuaternionF16`, `QuaternionF32`, and `QuaternionF64` are aliases for
7+
`Quaternion{Float16}`, `Quaternion{Float32}`, and `Quaternion{Float64}`, respectively.
8+
9+
See also: [`quat`](@ref), [`real`](@ref), [`imag_part`](@ref).
10+
"""
111
struct Quaternion{T<:Real} <: Number
212
s::T
313
v1::T
@@ -18,25 +28,116 @@ Quaternion(s::Real, v1::Real, v2::Real, v3::Real, n::Bool = false) =
1828
Quaternion(x::Real) = Quaternion(x, zero(x), zero(x), zero(x), abs(x) == one(x))
1929
Quaternion(z::Complex) = Quaternion(z.re, z.im, zero(z.re), zero(z.re), abs(z) == one(z.re))
2030
Quaternion(s::Real, a::AbstractVector) = Quaternion(s, a[1], a[2], a[3])
21-
Quaternion(a::AbstractVector) = Quaternion(0, a[1], a[2], a[3])
31+
function Quaternion(a::AbstractVector)
32+
Base.depwarn("`Quaternion(::AbstractVector)` is deprecated and will be removed in the next breaking release (v0.7.0). Please use Quaternion(0, a[1], a[2], a[3]) instead.", :Quaternion)
33+
Quaternion(0, a[1], a[2], a[3])
34+
end
2235

2336
Base.promote_rule(::Type{Quaternion{T}}, ::Type{S}) where {T <: Real, S <: Real} = Quaternion{promote_type(T, S)}
2437
Base.promote_rule(::Type{Quaternion{T}}, ::Type{Complex{S}}) where {T <: Real, S <: Real} = Quaternion{promote_type(T, S)}
2538
Base.promote_rule(::Type{Quaternion{T}}, ::Type{Quaternion{S}}) where {T <: Real, S <: Real} = Quaternion{promote_type(T, S)}
2639

40+
"""
41+
quat(r, [i, j, k])
42+
43+
Convert real numbers or arrays to quaternion. `i, j, k` defaults to zero.
44+
45+
# Examples
46+
```jldoctest
47+
julia> quat(7)
48+
Quaternion{Int64}(7, 0, 0, 0, false)
49+
50+
julia> quat(1.0, 2, 3, 4)
51+
QuaternionF64(1.0, 2.0, 3.0, 4.0, false)
52+
53+
julia> quat([1, 2, 3]) # This output will be changed in the next breaking release for consistency. (#94)
54+
Quaternion{Int64}(0, 1, 2, 3, false)
55+
```
56+
"""
57+
quat
58+
2759
quat(p, v1, v2, v3) = Quaternion(p, v1, v2, v3)
2860
quat(p, v1, v2, v3, n) = Quaternion(p, v1, v2, v3, n)
2961
quat(x) = Quaternion(x)
3062
quat(s, a) = Quaternion(s, a)
3163

64+
"""
65+
real(T::Type{<:Quaternion})
66+
67+
Return the type that represents the real part of a value of type `T`.
68+
e.g: for `T == Quaternion{R}`, returns `R`.
69+
Equivalent to `typeof(real(zero(T)))`.
70+
71+
# Examples
72+
```jldoctest
73+
julia> real(Quaternion{Int})
74+
Int64
75+
```
76+
"""
3277
Base.real(::Type{Quaternion{T}}) where {T} = T
78+
79+
"""
80+
real(q::Quaternion)
81+
82+
Return the real part of the quaternion `q`.
83+
84+
See also: [`imag_part`](@ref), [`quat`](@ref)
85+
86+
# Examples
87+
```jldoctest
88+
julia> real(quat(1,2,3,4))
89+
1
90+
```
91+
"""
3392
Base.real(q::Quaternion) = q.s
93+
94+
"""
95+
real(A::AbstractArray{<:Quaternion})
96+
97+
Return an array containing the real part of each quaternion in `A`.
98+
99+
# Examples
100+
```jldoctest
101+
julia> real([quat(5,6,7,8), 9])
102+
2-element Vector{Int64}:
103+
5
104+
9
105+
```
106+
"""
107+
Base.real(::AbstractArray{<:Quaternion})
108+
109+
"""
110+
imag_part(q::Quaternion{T}) -> NTuple{3, T}
111+
112+
Return the imaginary part of the quaternion `q`.
113+
114+
Note that this function is different from `Base.imag`, which returns `Real` for complex numbers.
115+
116+
See also: [`real`](@ref), [`conj`](@ref).
117+
118+
# Examples
119+
```jldoctest
120+
julia> imag_part(Quaternion(1,2,3,4))
121+
(2, 3, 4)
122+
```
123+
"""
34124
imag_part(q::Quaternion) = (q.v1, q.v2, q.v3)
35125

36126
Base.:/(q::Quaternion, x::Real) = Quaternion(q.s / x, q.v1 / x, q.v2 / x, q.v3 / x)
37127
Base.:*(q::Quaternion, x::Real) = Quaternion(q.s * x, q.v1 * x, q.v2 * x, q.v3 * x)
38128
Base.:*(x::Real, q::Quaternion) = q * x
39129

130+
"""
131+
conj(q::Quaternion)
132+
133+
Compute the quaternion conjugate of a quaternion `q`.
134+
135+
# Examples
136+
```jldoctest
137+
julia> conj(Quaternion(1,2,3,4))
138+
Quaternion{Int64}(1, -2, -3, -4, false)
139+
```
140+
"""
40141
Base.conj(q::Quaternion) = Quaternion(q.s, -q.v1, -q.v2, -q.v3, q.norm)
41142
Base.abs(q::Quaternion) = sqrt(abs2(q))
42143
Base.float(q::Quaternion{T}) where T = convert(Quaternion{float(T)}, q)
@@ -127,9 +228,9 @@ is the extension of `f` to the quaternions, where ``z = a + s i`` is a complex a
127228
128229
See Theorem 5 of [^Sudbery1970] for details.
129230
130-
[^Sudbery1970]
231+
[^Sudbery1970]:
131232
Sudbery (1979). Quaternionic analysis. Mathematical Proceedings of the Cambridge
132-
Philosophical Society,85, pp 199­225
233+
Philosophical Society,85, pp 199225
133234
doi:[10.1017/S030500410005563](https://doi.org/10.1017/S0305004100055638)
134235
"""
135236
function extend_analytic(f, q::Quaternion)
@@ -284,6 +385,29 @@ function rotationmatrix_normalized(q::Quaternion)
284385
xz - sy yz + sx 1 - (xx + yy)]
285386
end
286387

388+
"""
389+
slerp(qa::Quaternion, qb::Quaternion, t::Real)
390+
391+
Spherical linear interpolation (Slerp) between the inputs `qa` and `qb`.
392+
Since the input is normalized inside the function, the absolute value of the return value will be 1.
393+
394+
# Examples
395+
```jldoctest
396+
julia> using Quaternions
397+
398+
julia> qa = Quaternion(1,0,0,0)
399+
Quaternion{Int64}(1, 0, 0, 0, false)
400+
401+
julia> qb = Quaternion(0,1,0,0)
402+
Quaternion{Int64}(0, 1, 0, 0, false)
403+
404+
julia> slerp(qa, qb, 0.6)
405+
QuaternionF64(0.5877852522924731, 0.8090169943749475, 0.0, 0.0, true)
406+
407+
julia> ans ≈ Quaternion(cospi(0.3), sinpi(0.3), 0, 0)
408+
true
409+
```
410+
"""
287411
@inline function slerp(qa0::Quaternion{T}, qb0::Quaternion{T}, t::T) where T<:Real
288412
# http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
289413
iszero(qa0) && throw(DomainError(qa0, "The input quaternion must be non-zero."))

test/Quaternion.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Base.:(/)(a::MyReal, b::Real) = a.val / b
111111
@test quat(1, 2, 3, 4, true).norm == true # respect the .norm input (even if wrong)
112112
@test quat(1, [2, 3, 4]) === Quaternion(1, 2, 3, 4)
113113
@test quat([2, 3, 4]) === Quaternion(0, 2, 3, 4)
114+
@test_deprecated quat([2, 3, 4])
114115
end
115116

116117
@testset "random generation" begin

0 commit comments

Comments
 (0)