我有一个 N 维的 julia 数组和一个切片视图。有没有办法检查这个视图是否连续(在 NumPy 中是
a.flags["F"]
)?看来Base.iscontiguous
不是答案。
a = ones((1,1,1))
t = (1:1, 1:1, 1:1)
v = @view a[t...]
println(Base.iscontiguous(v)) # false
Base.iscontiguous(a[t...]) # MethodError: no method matching iscontiguous(::Array{Float64, 3})
这里我知道底层数据块是连续的,但是有没有办法检查它以获得更复杂的视图?
iscontiguous(A::SubArray) = iscontiguous(typeof(A))
iscontiguous(::Type{<:SubArray}) = false
iscontiguous(::Type{<:FastContiguousSubArray}) = true
你的view
不是
FastContiguousSubArray
:
julia> typeof(v)
SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, UnitRange{Int64}}, false}
哪里
help?> SubArray
search: SubArray BitArray Array DenseArray StridedArray
SubArray{T,N,P,I,L} <: AbstractArray{T,N}
N-dimensional view into a parent array (of type P) with an element type T, restricted by a tuple of indices (of type
I). L is true for types that support fast linear indexing, and false otherwise. [...]
您还可以通过以下方式验证:
julia> Base.viewindexing(v.indices)
IndexCartesian()
查看手册
中的
Linear indexing
部分。