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

Julia 0.6 support / updates to match StaticArrays 0.4.0 #25

Merged
merged 13 commits into from
Apr 3, 2017
Merged
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
email: false
matrix:
allow_failures:
- julia: nightly
- os: osx
# matrix:
# allow_failures:
# - julia: nightly
# - os: osx
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
StaticArrays 0.3.0
julia 0.6-
StaticArrays 0.4.0
2 changes: 1 addition & 1 deletion gen/SimpleSymbolic.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SimpleSymbolic

immutable S
struct S
x::Any
end

Expand Down
14 changes: 7 additions & 7 deletions src/angleaxis_types.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
################################################################################
################################################################################
"""
immutable AngleAxis{T} <: Rotation{3,T}
struct AngleAxis{T} <: Rotation{3,T}
AngleAxis(Θ, x, y, z)

A 3×3 rotation matrix parameterized by a 3D rotation by angle θ about an
Expand All @@ -11,14 +11,14 @@ Note that the axis is not unique for θ = 0, and that this parameterization does
not continuously map the neighbourhood of the null rotation (and therefore
might not be suitable for autodifferentation and optimization purposes).
"""
immutable AngleAxis{T} <: Rotation{3,T}
struct AngleAxis{T} <: Rotation{3,T}
theta::T
axis_x::T
axis_y::T
axis_z::T

# Ensure axis is normalized
function AngleAxis(θ, x, y, z)
function AngleAxis{T}(θ, x, y, z) where T
norm = sqrt(x*x + y*y + z*z)
# Not sure what to do with theta?? Should it become theta * norm ?
new(θ, x/norm, y/norm, z/norm)
Expand All @@ -31,7 +31,7 @@ end

# These 2 functions are enough to satisfy the entire StaticArrays interface:
@inline (::Type{AA}){AA <: AngleAxis}(t::NTuple{9}) = AA(Quat(t))
@inline Base.getindex(aa::AngleAxis, i::Integer) = Quat(aa)[i]
@inline Base.getindex(aa::AngleAxis, i::Int) = Quat(aa)[i]

@inline function Base.convert{R <: RotMatrix}(::Type{R}, aa::AngleAxis)
# Rodrigues' rotation formula.
Expand Down Expand Up @@ -113,14 +113,14 @@ end
################################################################################
################################################################################
"""
immutable RodriguesVec{T} <: Rotation{3,T}
struct RodriguesVec{T} <: Rotation{3,T}
RodriguesVec(sx, sy, sz)

Rodrigues vector parameterization of a 3×3 rotation matrix. The direction of the
vector [sx, sy, sz] defines the axis of rotation, and the rotation angle is
given by its norm.
"""
immutable RodriguesVec{T} <: Rotation{3,T}
struct RodriguesVec{T} <: Rotation{3,T}
sx::T
sy::T
sz::T
Expand All @@ -132,7 +132,7 @@ end

# These 2 functions are enough to satisfy the entire StaticArrays interface:
@inline (::Type{RV}){RV <: RodriguesVec}(t::NTuple{9}) = RV(Quat(t))
@inline Base.getindex(aa::RodriguesVec, i::Integer) = Quat(aa)[i]
@inline Base.getindex(aa::RodriguesVec, i::Int) = Quat(aa)[i]

# define its interaction with other angle representations
@inline Base.convert{R <: RotMatrix}(::Type{R}, rv::RodriguesVec) = convert(R, AngleAxis(rv))
Expand Down
12 changes: 7 additions & 5 deletions src/core_types.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
abstract Rotation{N,T} <: StaticMatrix{T}
abstract type Rotation{N,T} <: StaticMatrix{T}

An abstract type representing `N`-dimensional rotations. More abstractly, they represent
unitary (orthogonal) `N`×`N` matrices.
"""
abstract Rotation{N,T} <: StaticMatrix{T}
abstract type Rotation{N,T} <: StaticMatrix{T} end

Base.@pure StaticArrays.Size{N}(::Type{Rotation{N}}) = Size(N,N)
Base.@pure StaticArrays.Size{N,T}(::Type{Rotation{N,T}}) = Size(N,N)
Expand Down Expand Up @@ -67,15 +67,17 @@ end
################################################################################
################################################################################
"""
immutable RotMatrix{N,T} <: Rotation{N,T}
struct RotMatrix{N,T} <: Rotation{N,T}

A statically-sized, N×N unitary (orthogonal) matrix.

Note: the orthonormality of the input matrix is *not* checked by the constructor.
"""
immutable RotMatrix{N,T,L} <: Rotation{N,T} # which is <: AbstractMatrix{T}
struct RotMatrix{N,T,L} <: Rotation{N,T} # which is <: AbstractMatrix{T}
mat::SMatrix{N, N, T, L} # The final parameter to SMatrix is the "length" of the matrix, 3 × 3 = 9
Copy link
Contributor

@andyferris andyferris Mar 25, 2017

Choose a reason for hiding this comment

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

So should we override the default constructors to fix the ambiguity?

Something like

struct RotMatrix{N,T,L} <: Rotation{N,T} # which is <: AbstractMatrix{T}
    mat::SMatrix{N, N, T, L} # The final parameter to SMatrix is the "length" of the matrix, 3 × 3 = 9
    RotMatrix{N,T,L}(x::AbstractArray) where {N,T,L} = new{N,T,L}(convert(SMatrix{N,N,T,L}, x))
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a stack overflow error:

   [1] macro expansion at /Users/twan/code/julia/RigidBodyDynamics/v0.6/StaticArrays/src/SMatrix.jl:65 [inlined]
   [2] Type at /Users/twan/code/julia/RigidBodyDynamics/v0.6/StaticArrays/src/SMatrix.jl:61 [inlined]
   [3] Type at /Users/twan/code/julia/RigidBodyDynamics/v0.6/Rotations/src/core_types.jl:86 [inlined]
   [4] convert at /Users/twan/code/julia/RigidBodyDynamics/v0.6/StaticArrays/src/convert.jl:7 [inlined]
   [5] Rotations.RotMatrix(::StaticArrays.SMatrix{2,2,Float32,4}) at /Users/twan/code/julia/RigidBodyDynamics/v0.6/StaticArrays/src/convert.jl:4 (repeats 80000 times)

RotMatrix{N,T,L}(x::AbstractArray) where {N,T,L} = new{N,T,L}(convert(SMatrix{N,N,T,L}, x))
end
RotMatrix(x::SMatrix{N,N,T,L}) where {N,T,L} = RotMatrix{N,T,L}(x)

Copy link
Contributor

@andyferris andyferris Mar 25, 2017

Choose a reason for hiding this comment

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

Then we probably need to redefine the default outer constructor (which is lost because we defined an inner constructor).

RotMatrix(x::SMatrix{N.N,T,L}) where {N,T,L} = RotMatrix{N,T,L}(x)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, we could have a series of constructors to populate N, T, etc, so that RotMatrix{2}([1 0; 0 1]) works (maybe useful for REPL...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding

RotMatrix(x::SMatrix{N,N,T,L}) where {N,T,L} = RotMatrix{N,T,L}(x)

in addition to the inner constructor does fix the ambiguity and stack overflow error; sorry, I should have tried that before commenting earlier.

# These functions (plus size) are enough to satisfy the entire StaticArrays interface:
# @inline (::Type{R}){R<:RotMatrix}(t::Tuple) = error("No precise constructor found. Length of input was $(length(t)).")
Expand All @@ -88,7 +90,7 @@ for N = 2:3
@inline (::Type{RotMatrix{$N,T,$L}}){T}(t::NTuple{$L}) = RotMatrix(SMatrix{$N,$N,T}(t))
end
end
Base.@propagate_inbounds Base.getindex(r::RotMatrix, i::Integer) = r.mat[i]
Base.@propagate_inbounds Base.getindex(r::RotMatrix, i::Int) = r.mat[i]

@inline (::Type{RotMatrix})(θ::Real) = RotMatrix(@SMatrix [cos(θ) -sin(θ); sin(θ) cos(θ)])
@inline (::Type{RotMatrix{2}})(θ::Real) = RotMatrix(@SMatrix [cos(θ) -sin(θ); sin(θ) cos(θ)])
Expand Down
3 changes: 2 additions & 1 deletion src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function jacobian(::Type{RotMatrix}, q::Quat)
# then R = RotMatrix(q) = RotMatrix(s * qhat) = s * RotMatrix(qhat)

# get R(q)
R = q[:]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For future reference, this is the only place where JuliaArrays/StaticArrays.jl#128 was causing issues.

Copy link
Member

Choose a reason for hiding this comment

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

Fixed upstream, will need a StaticArrays-0.4.1 release though

# R = q[:] # FIXME: broken with StaticArrays 0.4.0 due to https://github.com/JuliaArrays/StaticArrays.jl/issues/128
R = SVector(convert(Tuple, q))

# solve d(s*R)/dQ (because its easy)
dsRdQ = @SMatrix [ 2*q.w 2*q.x -2*q.y -2*q.z ;
Expand Down
Loading