Julia 方法将与嵌套类型限制不匹配

Julia 方法将与嵌套类型限制不匹配,julia,dispatch,Julia,Dispatch,我有一个简单的方法,计算向量集合的加权平均值 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 我怀疑我误解了在涉及嵌套时如何使用类型限制。我应该如何重写此函数以匹配我的集合 附言 我知道向量和数组是一样的,但它们之间的区别使函数的使用更

我有一个简单的方法,计算向量集合的加权平均值

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
我怀疑我误解了在涉及嵌套时如何使用类型限制。我应该如何重写此函数以匹配我的集合

附言


我知道向量和数组是一样的,但它们之间的区别使函数的使用更加清晰。

因此,如果您重写代码以使用具体的类型,它就可以工作了

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
函数的意思是{tz-x̄,x)
x̄,x̃
结束
这同样有效

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
函数的意思是{tz-x̄,x)
x̄,x̃
结束
这不管用

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
函数的意思是{tz-x̄,x)
x̄,x̃
结束
这又起作用了

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
函数的意思是{tz-x̄,x)
x̄,x̃
结束
我们这里的问题在Julia手册的

最后一点非常重要:

尽管如此,根本问题是

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

julia>Vector{Vector}julia是一种快速发展的语言,随着每一代人的出现,新的功能都会出现,一些Q&a需要审查

重要

julia> VERSION
v"0.6.0-dev.2259"
Julia的高级功能之一是其调度系统。 . 现在,由于(v0.6.0 2017-1-16),用户可以受益于新的三角调度功能
这里我们需要解决为两个参数编写通用方法的问题:

  • 数字的一维数组(一般向量)
    w
  • 数字的一般向量的一般数组
    x
  • @Wallnus的答案包括4种方法,所有方法都有效。以下是第一种:

    
    
    函数意味着{T StatsBase.jl包有一个支持权重的平均函数。很抱歉,接受时间太晚了。答案符合我的要求,但我认为我意识到我所做的不是正确的事情。以后可能对类型不那么严格更好。
    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
    
    julia> VERSION
    v"0.6.0-dev.2259"
    
    julia> meanw([[1],[2]], [1])
    INFO: both are Vector for the same element-type: Int32
    
    julia> (typeof(spzeros(3)) |> supertype |> supertype) <: AbstractVector #=> true
    
    julia> meanw([[1.],[2]], spzeros(3))
    INFO: an Array of Vectors and an AbstractVector sub-type both with the same element-type: Float64
    
    julia> meanw([spzeros(3) for i=1:3], spzeros(3)) #=> Error
    julia> meanw([[1],[2]],[1.]) #=> Error
    
    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
    
    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
    
    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)
    
    julia> meanw([[2],[2]],[1.])
    INFO: an AbstractVector of AbstractVector sub-type and an AbstractVector sub-type both for Number (triangular)