方法将不匹配嵌套类型限制 [英] Method will not match with nested type restrictions

查看:19
本文介绍了方法将不匹配嵌套类型限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this simple method which calculates the weighted average of a collection 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 the dispatch 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 have misunderstood how to use type restrictions when there is nesting involved. How should I rewrite this function to match my collections?

附:

我知道向量和数组是一回事,但区别在于更清楚如何使用函数.

I know vectors and arrays are the same thing but the differentiation makes it clearer how the function is used.

推荐答案

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

So if you rewrite your code to use the concrete 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

这也有效

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

这不起作用

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

这又可以工作了

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

我们在这里遇到的问题在 参数类型

the problem we have here is described in the Julia manual under parametric types

最后一点非常重要:

即使 Float64 <: Real 我们没有 Point{Float64} <:点{Real}.

Even though Float64 <: Real we DO NOT have Point{Float64} <: Point{Real}.

换句话说,用类型论的说法,Julia 的类型参数是不变的,而不是协变的(甚至逆变).这是出于实际原因:而任何实例Point{Float64} 在概念上可能类似于 Point{Real} 的一个实例嗯,这两种类型在内存中有不同的表示:

In other words, in the parlance of type theory, Julia’s type parameters are invariant, rather than being covariant (or even contravariant). This is for practical reasons: while any instance of Point{Float64} may conceptually be like an instance of Point{Real} as well, the two types have different representations in memory:

真正起作用的是这个

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 中是出了名的困难.

but the real question is why do you want to do this? The Julia Compiler is clever enough even without type annotations to produce efficient code as long as the inputs are well-typed and the function itself is type-stable. Type annotations are only really needed for multiple-dispatch and as contract to specify what the function can actually handle, but this is notoriously difficult in Julia due to the extensive type system.

这篇关于方法将不匹配嵌套类型限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆