arrays - 创建具有 "hidden"额外索引的数组

标签 arrays julia

type ExtendedJumpArray{T,T2} <: AbstractArray{Float64,1}
  u::T
  jump_u::T2
end

Base.length(A::ExtendedJumpArray) = length(A.u)
Base.size(A::ExtendedJumpArray) = (length(A),)
function Base.getindex(A::ExtendedJumpArray,i::Int)
  i <= length(A.u) ? A.u[i] : A.jump_u[i-length(A.u)]
end
function Base.setindex!(A::ExtendedJumpArray,v,i::Int)
  i <= length(A.u) ? (A.u[i] = v) : (A.jump_u[i-length(A.u)] = v)
end
similar(A::ExtendedJumpArray) = deepcopy(A)
indices(A::ExtendedJumpArray) = Base.OneTo(length(A.u) + length(A.jump_u))

我认为我是这个领域里最酷的 child ,创建了一个可以索引超过其长度的数组(我这样做是出于特定原因)。但 Julia 显然不喜欢这样:

julia> ExtendedJumpArray([0.2],[-2.0])
Error showing value of type ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}:
ERROR: MethodError: no method matching inds2string(::Int64)
Closest candidates are:
  inds2string(::Tuple{Vararg{AbstractUnitRange,N}}) at show.jl:1485
 in _summary(::ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}, ::Int64) at .\show.jl:1490
 in #showarray#330(::Bool, ::Function, ::IOContext{Base.Terminals.TTYTerminal}, ::ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}, ::Bool) at .\show.jl:1599
 in display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::MIME{Symbol("text/plain")}, ::ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}) at .\REPL.jl:132
 in display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}) at .\REPL.jl:135
 in display(::ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}) at .\multimedia.jl:143
 in print_response(::Base.Terminals.TTYTerminal, ::Any, ::Void, ::Bool, ::Bool, ::Void) at .\REPL.jl:154
 in print_response(::Base.REPL.LineEditREPL, ::Any, ::Void, ::Bool, ::Bool) at .\REPL.jl:139
 in (::Base.REPL.##22#23{Bool,Base.REPL.##33#42{Base.REPL.LineEditREPL,Base.REPL.REPLHistoryProvider},Base.REPL.LineEditREPL,Base.LineEdit.Prompt})(::Base.LineEdit.MIState, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Bool) at .\REPL.jl:652
 in run_interface(::Base.Terminals.TTYTerminal, ::Base.LineEdit.ModalInterface) at .\LineEdit.jl:1579
 in run_frontend(::Base.REPL.LineEditREPL, ::Base.REPL.REPLBackendRef) at .\REPL.jl:903
 in run_repl(::Base.REPL.LineEditREPL, ::Base.##932#933) at .\REPL.jl:188
 in _start() at .\client.jl:360

有没有一种简单的方法可以在不破坏 show 方法以及其他可能被破坏的方法的情况下做到这一点?或者有更好的方法来做到这一点吗?

最佳答案

索引需要返回一个元组,就像size一样。

julia> Base.similar(A::ExtendedJumpArray) = deepcopy(A)

julia> Base.indices(A::ExtendedJumpArray) = (Base.OneTo(length(A.u) + length(A.jump_u)),)

julia> ExtendedJumpArray([0.2],[-2.0])
2-element ExtendedJumpArray{Array{Float64,1},Array{Float64,1}}:
  0.2
 -2.0

julia> length(ans)
1

但是,如果索引大小在数组的维度上不一致,很可能会导致困惑和冲突。有些函数使用size,而其他函数则使用indices。请参阅上面的显示与长度。

关于arrays - 创建具有 "hidden"额外索引的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42126705/

相关文章:

c - 将 *array 分配给指针

arrays - 将 2D 矩阵的 2D 单元(大小一致)转换为 4D matlab double

javascript - 将数字数组按偶数和奇数排序时遇到问题

javascript - 为什么这两个空数组的填充方式不同?

julia:如何读取 bz2 压缩文本文件

io - 将固定宽度字段写入 Julia 中的流?

dataframe - 如何从 Julia 的数据框中获取多个直方图

c++ - 当编译时参数未知时创建 execv 参数数组

io - 如何在 Julia 分布式 for 循环中以非交互方式执行 I/O?

julia - 如何使用 Plots.jl 填充曲线之间的区域?