The method will not comply with nested type restrictions

I have this simple method that calculates the weighted average of a set of vectors

function meanw{T <: Number}(x::AbstractArray{AbstractVector{T}, 1}, w::AbstractVector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

but sending cannot match my method when I try to use it.

ERROR: `meanw` has no method matching meanw(::Array{Array{Float64,1},1}, ::Array{Float64,1})

I suspect that I misunderstood how to use type constraints when nesting is involved. How do I rewrite this function according to my collections?

PS

I know that vectors and arrays are the same thing, but differentiation gives a clearer idea of ​​how the function is used.

+4
source share
3 answers

So, if you rewrite your code to use specific types, it works

function meanw{T <: Number}(x::Array{Vector{T}, 1}, w::Vector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

it also works

function meanw{T <: Number}(x::Array{Vector{T}, 1}, w::AbstractVector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

this does not work

function meanw{T <: Number}(x::Array{AbstractVector{T}, 1}, w::AbstractVector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

and it works again

function meanw{T <: Number}(x::AbstractArray{Vector{T}, 1}, w::AbstractVector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

, , Julia http://docs.julialang.org/en/release-0.3/manual/types/#man-parametric-types

:

, Float64 <: Real, {Float64} <: {Real}.

, , ( ). : Point {Float64} Point {Real} , :

function meanw{T <: Number, V <: AbstractVector}(x::AbstractArray{V, 1}, w::AbstractVector{T})
  x̄ = sum(x .* w)
  x̃ = map(z -> z - x̄, x)
  x̄, x̃
end

- ? Julia , , , . , , , , , Julia - .

+4

julia> Vector{Vector} <: Vector{AbstractArray}
false

.

function meanw{T<:Number,S<:Number}(x::Vector{Array{T}}, w::Vector{S})
    x̄ = sum(x .* w)
    x̃ = map(z -> z - x̄, x)
    x̄, x̃
end

, , Float64 x Int w. , - , , , .

+2

Julia - Q & .

julia> VERSION
v"0.6.0-dev.2259"

Julia . ( ). - (v0.6.0 2017-1-16) (, ?).
:

  • ( ) w
  • x

@wallnuss 4 , . :

function meanw{T <: Number}(x::Array{Vector{T}, 1}, w::Vector{T}) info("both are Vector for the same element-type: $T") end

julia> meanw([[1],[2]], [1])
INFO: both are Vector for the same element-type: Int32

, AbstractVector :

julia> (typeof(spzeros(3)) |> supertype |> supertype) <: AbstractVector #=> true

, :

function meanw{T <: Number}(x::Array{Vector{T}, 1}, w::AbstractVector{T}) info("an Array of Vectors and an AbstractVector sub-type both with the same element-type: $T") # end

:

julia> meanw([[1.],[2]], spzeros(3))
INFO: an Array of Vectors and an AbstractVector sub-type both with the same element-type: Float64

:

function meanw{T <: Number}(x::AbstractArray{Vector{T}, 1}, w::AbstractVector{T}) info("an AbstractVector subtype of Vectors and an AbstractVector sub-type both with the same element-type $T") end

:

julia> meanw([spzeros(3) for i=1:3], spzeros(3)) #=> Error
julia> meanw([[1],[2]],[1.]) #=> Error

, , .

function meanw{T <: Number, V <: AbstractVector}(x::AbstractArray{V, 1}, w::AbstractVector{T}) info("an AbstractVector of AbstractVector sub-type with Any element and an AbstractVector sub-type of Number") end

, , , , :

julia> meanw([[""],[2]],[1.]) #=> Not enough restriction for element type of first AbstractVector
INFO: an AbstractVector of AbstractVector sub-type with Any element and an AbstractVector sub-type of Number

:

function meanw{T <: Number, V <: AbstractVector{T}}(x::AbstractArray{V, 1}, w::AbstractVector{T}) info("an AbstractVector of AbstractVector sub-type and an AbstractVector subtype both for same sub-type of Number (triangular)") end

:

julia> methods(meanw)
# 5 methods for generic function "meanw":
meanw{T<:Number}(x::Array{Array{T,1},1}, w::Array{T,1}) in Main at REPL[1]:2
meanw{T<:Number}(x::Array{Array{T,1},1}, w::AbstractArray{T,1}) in Main at REPL[22]:2
meanw{T<:Number}(x::AbstractArray{Array{T,1},1}, w::AbstractArray{T,1}) in Main at REPL[24]:2
meanw{T<:Number,V<:AbstractArray{T<:Number,1}}(x::AbstractArray{V,1}, w::AbstractArray{T,1}) in Main at REPL[41]:2 
meanw{T<:Number,V<:(AbstractArray{T,1} where T)}(x::AbstractArray{V,1}, w::AbstractArray{T,1}) in Main at REPL[39]:2

4- , .

julia> meanw([spzeros(3) for i=1:3], spzeros(3))
INFO: an AbstractVector of AbstractVector sub-type and an AbstractVector subtype both for a same sub-type of Number (triangular)

meanw([[2],[2]],[1.]) - (5). :

function meanw{T <: Number, W <: Number, V <: AbstractVector{W}}(x::AbstractArray{V, 1}, w::AbstractVector{T}) info("an AbstractVector of AbstractVector sub-type and an AbstractVector sub-type both for Number (triangular)") end

julia> meanw([[2],[2]],[1.])
INFO: an AbstractVector of AbstractVector sub-type and an AbstractVector sub-type both for Number (triangular)
+1

All Articles