Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Julia中为数组数组创建方法?_Julia - Fatal编程技术网

如何在Julia中为数组数组创建方法?

如何在Julia中为数组数组创建方法?,julia,Julia,我显然在这里遗漏了一些基本的东西。如何将函数的第二个方法更改为接受c?此外,我更喜欢使用AbstractArray而不是AbstractVector作为类型,这样也不会限制维度,但我想减少潜在的错误源 function f(x::AbstractVector{Int}) println(x) end # f (generic function with 1 method) function f(x::AbstractVector{AbstractVector{Int}}) fo

我显然在这里遗漏了一些基本的东西。如何将函数的第二个方法更改为接受c?此外,我更喜欢使用AbstractArray而不是AbstractVector作为类型,这样也不会限制维度,但我想减少潜在的错误源

function f(x::AbstractVector{Int})
    println(x)
end # f (generic function with 1 method)

function f(x::AbstractVector{AbstractVector{Int}})
    for i in x
        println(i)
    end
end # f (generic function with 2 methods)

a=[1,2,3] # 3-element Array{Int64,1}:
b=[4,5,6] # 3-element Array{Int64,1}:
c=[a,b] # 2-element Array{Array{Int64,1},1}:

typeof(a) <: AbstractVector{Int} # true
typeof(c) <: AbstractVector{AbstractVector{Int}} # false

f(a) # [1, 2, 3]
f(c) # ERROR: MethodError: no method matching f(::Array{Array{Int64,1},1})
引述:

具有不同T值的混凝土点类型决不是彼此的子类型:

julia> struct Point{T} end

julia> Point{Float64} <: Point{Int64}
false

julia> Point{Float64} <: Point{Real}
false
最后一点非常重要:即使引用:

具有不同T值的混凝土点类型决不是彼此的子类型:

julia> struct Point{T} end

julia> Point{Float64} <: Point{Int64}
false

julia> Point{Float64} <: Point{Real}
false

最后一点非常重要:即使是1。为什么Float64是Int64的子类型?我不遵循这个例子,也不了解它与我的第一次类型测试的关系。2.Int是Int64的超类型,就像AbstractVector是向量的超类型一样。我不明白为什么规则会在第三级任意更改,即为什么我不需要1。这个例子可能有点断章取义,因为我只引用了部分章节。我建议您阅读完整部分以了解上下文。2.规则不会改变;在64位系统上,Int===Int64,因此您无法将抽象类型AbstractVector与具体类型Int进行比较。但是,您可以尝试使用抽象整数超类型integer,并查看规则是否与AbstractVector的规则一致。3.我不知道你在问什么;如果您想处理的不仅仅是向量,您可以使用AbstractArray.1。为什么Float64是Int64的子类型?我不遵循这个例子,也不了解它与我的第一次类型测试的关系。2.Int是Int64的超类型,就像AbstractVector是向量的超类型一样。我不明白为什么规则会在第三级任意更改,即为什么我不需要1。这个例子可能有点断章取义,因为我只引用了部分章节。我建议您阅读完整部分以了解上下文。2.规则不会改变;在64位系统上,Int===Int64,因此您无法将抽象类型AbstractVector与具体类型Int进行比较。但是,您可以尝试使用抽象整数超类型integer,并查看规则是否与AbstractVector的规则一致。3.我不知道你在问什么;如果你想处理的不仅仅是向量,你可以使用AbstractArray。
julia> Vector{Vector{Int}} <: AbstractVector{T} where T <: AbstractVector{Int}
true
julia> Vector{Vector{Int}} <: AbstractVector{<:AbstractVector{Int}}
true