2017-02-09 5 views
2
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)) 

Я думал, что я классный парень на блоке, создав массив, который мог бы индексировать его длину (я делаю это по определенной причине). Но Джулия, видимо, не нравится:Создание массива со «скрытыми» дополнительными индексами

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 методы, и все, что еще может быть нарушена? Или есть лучший способ сделать это в целом?

ответ

2

Показатели должны возвращать кортеж, как и 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 

Имея indices и size расходятся в размерности массива, хотя, скорее всего, закончится смятения и раздоров. Некоторые функции используют size, тогда как другие используют indices. См. Показание против длины выше.

 Смежные вопросы

  • Нет связанных вопросов^_^