Stepping back into Julia after a year’s absence.
I’m using RigibBodyDynamics to model a double pendulum. Simulating the system should return an array of system states, ideally an Array{Float64}
or similar. The type that is actually returned is a
SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Vector{Float64}}
which I’m sure there was a good reason for using, but it can’t just be dropped into functions where one would normally use a Vector{}
, such as for instance plot()
.
So I’m trying to write a convert()
that turns it into an actual vector, and I cannot for the life of me get the type matching system to match my function.
What I’ve done is define:
convert(::Vector{Float64}, x::Vector{SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Vector{Float64}}}) = mapreduce(permutedims, vcat, x)
convert(::Float64, x::SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Vector{Float64}}) = x[1][:]
but when I try to force an implicit conversion, it fails:
julia> typeof(qs)
Vector{SegmentedVector{JointID, Float64, OneTo{JointID}, Vector{Float64}}} (alias for Array{SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Array{Float64, 1}}, 1})
julia> typeof(X)
Vector{Float64} (alias for Array{Float64, 1})
julia> append!(X, qs)
ERROR: MethodError: Cannot `convert` an object of type SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Vector{Float64}} to an object of type Float64
Closest candidates are:
convert(::Type{Float64}, ::Measures.AbsoluteLength)
@ Measures ~/.julia/packages/Measures/PKOxJ/src/length.jl:12
convert(::Type{T}, ::T) where T<:Number
@ Base number.jl:6
convert(::Type{T}, ::Gray) where T<:Real
@ ColorTypes ~/.julia/packages/ColorTypes/1dGw6/src/conversions.jl:113
...
Interestingly, defining something like
conv(x::Vector{SegmentedVector{JointID, Float64, Base.OneTo{JointID}, Vector{Float64}}}) = mapreduce(permutedims, vcat, x)
conv (generic function with 3 methods)
julia> conv(qs)
15002×2 Matrix{Float64}:
0.718177 0.310706
0.716825 0.314462
0.715472 0.318206
0.714121 0.321928
0.712759 0.325665
0.711398 0.329381
0.710036 0.333088
ie doing an explicit conversion, works.
What am I doing wrong?