Skip to content

Commit bda222a

Browse files
authored
Cleanup (#232)
* cleanup Symbol definition * replace `function (*)(...)` with `function Base.:*(...)` * update LaTeX code in docstring * update tests with `@testset` * remove unused function `vee` * add tests for r\v * fix typo * add missing ω * add repeats in kinematics testset * fix testset in principal_value_tests.jl * update threshold in principal_value_tests.jl * replace `for =` with `for in`
1 parent f5106cf commit bda222a

16 files changed

+205
-199
lines changed

src/core_types.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ end
9191
@inline RotMatrix{N,T,L}(t::Tuple) where {N,T,L} = RotMatrix(SArray{Tuple{N,N},T}(t))
9292

9393
# Create aliases RotMatrix2{T} = RotMatrix{2,T,4} and RotMatrix3{T} = RotMatrix{3,T,9}
94-
for N = 2:3
94+
for N in 2:3
9595
L = N*N
9696
RotMatrixN = Symbol(:RotMatrix, N)
9797
@eval begin
@@ -261,7 +261,7 @@ function Base.show(io::IO, ::MIME"text/plain", X::Rotation)
261261
if !isa(X, RotMatrix)
262262
n_fields = length(fieldnames(typeof(X)))
263263
print(io, "(")
264-
for i = 1:n_fields
264+
for i in 1:n_fields
265265
print(io, getfield(X, i))
266266
if i < n_fields
267267
print(io, ", ")
@@ -274,4 +274,3 @@ function Base.show(io::IO, ::MIME"text/plain", X::Rotation)
274274
io = IOContext(io, :typeinfo => eltype(X))
275275
Base.print_array(io, X)
276276
end
277-

src/derivatives.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ end
160160
#######################################################
161161

162162
# Note: this is *not* projected into the orthogonal matrix tangent space.
163-
# can do this by projecting each 3×3 matrix (row of 9) by (jacobian[i] - r * jacabian[i]' * r) / 2 (for i = 1:3)
163+
# can do this by projecting each 3×3 matrix (row of 9) by (jacobian[i] - r * jacabian[i]' * r) / 2 (for i in 1:3)
164164
function jacobian(r::RotMatrix{3}, X::AbstractVector)
165165
@assert length(X) === 3
166166
T = promote_type(eltype(r), eltype(X))

src/euler_types.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#########################
1111

1212
for axis in [:X, :Y, :Z]
13-
RotType = Symbol("Rot" * string(axis))
13+
RotType = Symbol(:Rot, axis)
1414
@eval begin
1515
struct $RotType{T} <: Rotation{3,T}
1616
theta::T
@@ -203,11 +203,11 @@ end
203203
######################
204204

205205
for axis1 in [:X, :Y, :Z]
206-
Rot1Type = Symbol("Rot" * string(axis1))
206+
Rot1Type = Symbol(:Rot, axis1)
207207
for axis2 in filter(axis -> axis != axis1, [:X, :Y, :Z])
208-
Rot2Type = Symbol("Rot" * string(axis2))
209-
RotType = Symbol("Rot" * string(axis1) * string(axis2))
210-
InvRotType = Symbol("Rot" * string(axis2) * string(axis1))
208+
Rot2Type = Symbol(:Rot, axis2)
209+
RotType = Symbol(:Rot, axis1, axis2)
210+
InvRotType = Symbol(:Rot, axis2, axis1)
211211

212212
@eval begin
213213
struct $RotType{T} <: Rotation{3,T}
@@ -474,20 +474,20 @@ end
474474
########################
475475

476476
for axis1 in [:X, :Y, :Z]
477-
Rot1Type = Symbol("Rot" * string(axis1))
477+
Rot1Type = Symbol(:Rot, axis1)
478478
for axis2 in filter(axis -> axis != axis1, [:X, :Y, :Z])
479-
Rot2Type = Symbol("Rot" * string(axis2))
480-
Rot12Type = Symbol("Rot" * string(axis1) * string(axis2))
479+
Rot2Type = Symbol(:Rot, axis2)
480+
Rot12Type = Symbol(:Rot, axis1, axis2)
481481
for axis3 in filter(axis -> axis != axis2, [:X, :Y, :Z])
482-
Rot3Type = Symbol("Rot" * string(axis3))
483-
Rot23Type = Symbol("Rot" * string(axis2) * string(axis3))
484-
Rot13Type = Symbol("Rot" * string(axis1) * string(axis3))
485-
RotType = Symbol("Rot" * string(axis1) * string(axis2) * string(axis3))
486-
InvRotType = Symbol("Rot" * string(axis3) * string(axis2) * string(axis1))
482+
Rot3Type = Symbol(:Rot, axis3)
483+
Rot23Type = Symbol(:Rot, axis2, axis3)
484+
Rot13Type = Symbol(:Rot, axis1, axis3)
485+
RotType = Symbol(:Rot, axis1, axis2, axis3)
486+
InvRotType = Symbol(:Rot, axis3, axis2, axis1)
487487

488488
# Note that axis0 is used only if axis1==axis3
489489
axis0 = setdiff!([:X, :Y, :Z], [axis1, axis2])[1]
490-
Rot0Type = Symbol("Rot" * string(axis0))
490+
Rot0Type = Symbol(:Rot, axis0)
491491

492492
@eval begin
493493
struct $RotType{T} <: Rotation{3,T}

src/mean.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ equivalently minimizes `sum_i=1:n (sin(ϕ[i] / 2))^2`, where `ϕ[i] = rotation_a
3232
function Statistics.mean(qvec::AbstractVector{QuatRotation{T}}, method::Integer = 0) where T
3333
#if (method == 0)
3434
M = zeros(4, 4)
35-
for i = 1:length(qvec)
35+
for i in 1:length(qvec)
3636
q = qvec[i]
3737
Qi = @SVector [q.w, q.x, q.y, q.z] # convert types to ensure we don't get quaternion multiplication
3838
M .+= Qi * (Qi')

src/mrps.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ end
5151
Base.inv(p::MRP) = MRP(-p.x, -p.y, -p.z)
5252

5353
# ~~~~~~~~~~~~~~~ Composition ~~~~~~~~~~~~~~~ #
54-
function (*)(p2::MRP, p1::MRP)
54+
function Base.:*(p2::MRP, p1::MRP)
5555
p2, p1 = params(p2), params(p1)
5656
MRP(((1-p2'p2)*p1 + (1-p1'p1)*p2 - cross(2p1, p2) ) / (1+p1'p1*p2'p2 - 2p1'p2))
5757
end
5858

59-
function (\)(p1::MRP, p2::MRP)
59+
function Base.:\(p1::MRP, p2::MRP)
6060
q1,q2 = params(p1), params(p2)
6161
n1,n2 = q1'q1, q2'q2
6262
θ = 1/((1+n1)*(1+n2))
@@ -70,7 +70,7 @@ function (\)(p1::MRP, p2::MRP)
7070
return MRP(v*M)
7171
end
7272

73-
function (/)(p1::MRP, p2::MRP)
73+
function Base.:/(p1::MRP, p2::MRP)
7474
q1,q2 = params(p1), params(p2)
7575
n1,n2 = q1'q1, q2'q2
7676
θ = 1/((1+n1)*(1+n2))
@@ -86,8 +86,8 @@ end
8686

8787

8888
# ~~~~~~~~~~~~~~~ Rotation ~~~~~~~~~~~~~~~ #
89-
@inline (*)(p::MRP, r::StaticVector) = QuatRotation(p)*r
90-
@inline (\)(p::MRP, r::StaticVector) = QuatRotation(p)\r
89+
@inline Base.:*(p::MRP, r::StaticVector) = QuatRotation(p)*r
90+
@inline Base.:\(p::MRP, r::StaticVector) = QuatRotation(p)\r
9191

9292

9393
# ~~~~~~~~~~~~~~~ Kinematics ~~~~~~~~~~~~~~~ #

src/rodrigues_params.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@ end
4747
Base.inv(p::RodriguesParam) = RodriguesParam(-p.x, -p.y, -p.z)
4848

4949
# ~~~~~~~~~~~~~~~ Composition ~~~~~~~~~~~~~~~ #
50-
function (*)(g2::RodriguesParam, g1::RodriguesParam)
50+
function Base.:*(g2::RodriguesParam, g1::RodriguesParam)
5151
g2 = params(g2)
5252
g1 = params(g1)
5353
RodriguesParam((g2+g1 + g2 × g1)/(1-g2'g1))
5454
end
5555

56-
function (\)(g1::RodriguesParam, g2::RodriguesParam)
56+
function Base.:\(g1::RodriguesParam, g2::RodriguesParam)
5757
g2 = params(g2)
5858
g1 = params(g1)
5959
RodriguesParam((g2-g1 + g2 × g1)/(1+g1'g2))
6060
end
6161

62-
function (/)(g1::RodriguesParam, g2::RodriguesParam)
62+
function Base.:/(g1::RodriguesParam, g2::RodriguesParam)
6363
g2 = params(g2)
6464
g1 = params(g1)
6565
RodriguesParam((g1-g2 + g2 × g1)/(1+g1'g2))
6666
end
6767

6868
# ~~~~~~~~~~~~~~~ Rotation ~~~~~~~~~~~~~~~ #
69-
(*)(g::RodriguesParam, r::StaticVector) = QuatRotation(g)*r
70-
(\)(g::RodriguesParam, r::StaticVector) = inv(QuatRotation(g))*r
69+
Base.:*(g::RodriguesParam, r::StaticVector) = QuatRotation(g)*r
70+
Base.:\(g::RodriguesParam, r::StaticVector) = inv(QuatRotation(g))*r
7171

7272

7373
# ~~~~~~~~~~~~~~~ Kinematics ~~~~~~~~~~~~~~~ #

src/rotation_generator.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ RotMatrixGenerator(x::SMatrix{N,N,T,L}) where {N,T,L} = RotMatrixGenerator{N,T,L
5858
Base.zero(::Type{RotMatrixGenerator}) = error("The dimension of rotation is not specified.")
5959

6060
# These functions (plus size) are enough to satisfy the entire StaticArrays interface:
61-
for N = 2:3
61+
for N in 2:3
6262
L = N*N
6363
RotMatrixGeneratorN = Symbol(:RotMatrixGenerator, N)
6464
@eval begin
@@ -234,7 +234,7 @@ function Base.show(io::IO, ::MIME"text/plain", X::RotationGenerator)
234234
if !isa(X, RotMatrixGenerator)
235235
n_fields = length(fieldnames(typeof(X)))
236236
print(io, "(")
237-
for i = 1:n_fields
237+
for i in 1:n_fields
238238
print(io, getfield(X, i))
239239
if i < n_fields
240240
print(io, ", ")

src/unitquaternion.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Base: *, /, \, exp, , ==
2-
31
"""
42
QuatRotation{T} <: Rotation
53
@@ -305,7 +303,7 @@ rmult(w) * SVector(q)
305303
306304
Sets the output mapping equal to the mapping of `w`
307305
"""
308-
function (*)(q1::QuatRotation, q2::QuatRotation)
306+
function Base.:*(q1::QuatRotation, q2::QuatRotation)
309307
return QuatRotation(q1.q*q2.q)
310308
end
311309

@@ -323,8 +321,8 @@ function Base.:*(q::QuatRotation, r::StaticVector) # must be StaticVector to av
323321
(w^2 - v'v)*r + 2*v*(v'r) + 2*w*cross(v,r)
324322
end
325323

326-
(\)(q1::QuatRotation, q2::QuatRotation) = inv(q1)*q2
327-
(/)(q1::QuatRotation, q2::QuatRotation) = q1*inv(q2)
324+
Base.:\(q1::QuatRotation, q2::QuatRotation) = inv(q1)*q2
325+
Base.:/(q1::QuatRotation, q2::QuatRotation) = q1*inv(q2)
328326

329327
"""
330328
rotation_between(from, to)
@@ -349,14 +347,18 @@ end
349347
350348
The time derivative of the rotation R, according to the definition
351349
352-
``Ṙ = \\lim_{Δt → 0} \\frac{R(t + Δt) - R(t)}{Δt}``
350+
```math
351+
Ṙ = \\lim_{Δt → 0} \\frac{R(t + Δt) - R(t)}{Δt}
352+
```
353353
354354
where `ω` is the angular velocity. This is equivalent to
355355
356-
``Ṙ = \\lim_{Δt → 0} \\frac{R δR - R}{Δt}``
356+
```math
357+
Ṙ = \\lim_{Δt → 0} \\frac{R δR - R}{Δt}
358+
```
357359
358360
where ``δR`` is some small rotation, parameterized by a small rotation ``δθ`` about
359-
an axis ``r``, such that ``lim_{Δt → 0} \\frac{δθ r}{Δt} = ω``
361+
an axis ``r``, such that ``\\lim_{Δt → 0} \\frac{δθ r}{Δt} = ω``
360362
361363
The kinematics are extremely useful when computing the dynamics of rigid bodies, since
362364
`Ṙ = kinematics(R,ω)` is the first-order ODE for the evolution of the attitude dynamics.

src/util.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function perpendicular_vector(vec::SVector{3})
1212
absvec = abs.(vec)
1313
ind1 = argmax(absvec) # index of largest element
1414
tmin = typemin(T)
15-
@inbounds absvec2 = @SVector [ifelse(i == ind1, tmin, absvec[i]) for i = 1 : 3] # set largest element to typemin(T)
15+
@inbounds absvec2 = @SVector [ifelse(i == ind1, tmin, absvec[i]) for i in 1 : 3] # set largest element to typemin(T)
1616
ind2 = argmax(absvec2) # index of second-largest element
1717

1818
# perp[ind1] = -vec[ind2], perp[ind2] = vec[ind1], set remaining element to zero:
1919
@inbounds perpind1 = -vec[ind2]
2020
@inbounds perpind2 = vec[ind1]
2121
tzero = zero(T)
22-
perp = @SVector [ifelse(i == ind1, perpind1, ifelse(i == ind2, perpind2, tzero)) for i = 1 : 3]
22+
perp = @SVector [ifelse(i == ind1, perpind1, ifelse(i == ind2, perpind2, tzero)) for i in 1 : 3]
2323
end
2424

2525
@noinline length_error(v, len) =
@@ -38,10 +38,6 @@ function skew(v::AbstractVector)
3838
-v[2] v[1] 0]
3939
end
4040

41-
function vee(S::AbstractMatrix)
42-
return @SVector [S[3,2], S[1,3], S[2,1]]
43-
end
44-
4541
"""
4642
The element type for a rotation matrix with a given angle type is composed of
4743
trigonometric functions of that type.

test/2d.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ using Unitful
7676
for R in [RotMatrix{2,Float64}, Angle2d{Float64}]
7777
I = one(R)
7878
Random.seed!(0)
79-
for i = 1:repeats
79+
for i in 1:repeats
8080
r = rand(R)
8181
@test isrotation(r)
8282
@test inv(r) == adjoint(r)
@@ -96,7 +96,7 @@ using Unitful
9696
for R in [RotMatrix{2,Float64}, Angle2d{Float64}]
9797
I = one(R)
9898
Random.seed!(0)
99-
for i = 1:repeats
99+
for i in 1:repeats
100100
r = rand(R)
101101
@test norm(r) norm(Matrix(r))
102102
@test normalize(r) normalize(Matrix(r))
@@ -114,7 +114,7 @@ using Unitful
114114
repeats = 100
115115
for R in [RotMatrix{2}, Angle2d]
116116
Random.seed!(0)
117-
for i = 1:repeats
117+
for i in 1:repeats
118118
r = rand(R)
119119
m = SMatrix(r)
120120
v = randn(SVector{2})
@@ -136,7 +136,7 @@ using Unitful
136136
repeats = 100
137137
for R in [RotMatrix{2}, Angle2d]
138138
Random.seed!(0)
139-
for i = 1:repeats
139+
for i in 1:repeats
140140
r = rand(R)
141141
m = SMatrix(r)
142142
v = randn(SVector{2}) * u"m"
@@ -163,7 +163,7 @@ using Unitful
163163
repeats = 100
164164
for R in [RotMatrix{2}, Angle2d]
165165
Random.seed!(0)
166-
for i = 1:repeats
166+
for i in 1:repeats
167167
r1 = rand(R)
168168
m1 = SMatrix(r1)
169169

0 commit comments

Comments
 (0)