-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward axes
to the parent for a Diagonal
#50514
Conversation
Seems fine to me. (Note that |
bfc1980
to
f55fa4a
Compare
I think this should be good. @dkarrasch could you take a look? Given the decision in #50530 to treat In particular, currently julia> D = Diagonal(1:∞); D[:,1:3]
ERROR: MethodError: no method matching Int64(::Infinities.InfiniteCardinal{0})
Closest candidates are:
(::Type{T})(::T) where T<:Number
@ Core boot.jl:791
Int64(::Float64)
@ Base float.jl:962
Int64(::Float32)
@ Base float.jl:962
...
Stacktrace:
[1] to_shape(r::Base.OneTo{Infinities.InfiniteCardinal{0}})
@ Base ./abstractarray.jl:847
[2] map
@ Base ./tuple.jl:292 [inlined]
[3] to_shape(dims::Tuple{Base.OneTo{Infinities.InfiniteCardinal{0}}, Base.OneTo{Int64}})
@ Base ./abstractarray.jl:843
[4] similar(a::Diagonal{Int64, InfiniteArrays.InfUnitRange{Int64}}, dims::Tuple{Base.OneTo{Infinities.InfiniteCardinal{0}}, Base.OneTo{Int64}})
@ Base ./abstractarray.jl:828
[5] _unsafe_getindex(::IndexCartesian, ::Diagonal{Int64, InfiniteArrays.InfUnitRange{Int64}}, ::Base.Slice{Base.OneTo{Infinities.InfiniteCardinal{0}}}, ::UnitRange{Int64})
@ Base ./multidimensional.jl:901
[6] _getindex
@ Base ./multidimensional.jl:889 [inlined]
[7] getindex(::Diagonal{Int64, InfiniteArrays.InfUnitRange{Int64}}, ::Function, ::UnitRange{Int64})
@ Base ./abstractarray.jl:1286
[8] top-level scope
@ REPL[5]:1 is broken on master, although this works on v1.9.3. This PR fixes it. |
Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
Unfortunately, this PR worsens the inferred type of julia> v = SVector(1,2,3,4);
julia> D = Diagonal(v)
4×4 Diagonal{Int64, SVector{4, Int64}} with indices SOneTo(4)×SOneTo(4):
1 ⋅ ⋅ ⋅
⋅ 2 ⋅ ⋅
⋅ ⋅ 3 ⋅
⋅ ⋅ ⋅ 4
julia> @inferred axes(v,1) # not inferred concretely
ERROR: return type SOneTo{4} does not match inferred return type Union{SOneTo{4}, Base.OneTo{Int64}}
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] top-level scope
@ REPL[32]:1
julia> @inferred (x -> axes(x,1))(v) # type-inferred
SOneTo(4)
julia> @inferred (x -> axes(x,3))(v)
Base.OneTo(1) |
Given that quite a few packages define
axes(d::Diagonal{T, CustomVector{T}})
asax = parent(d); (ax,ax)
, perhaps it makes sense to have this defined inLinearAlgebra
.After this,
The static sizes are preserved.
This is potentially breaking, asPackages should perhaps figure this out, as the specific method is broken anyway.similar
may be undefined/ambiguous for custom axis types (see JuliaArrays/StructArrays.jl#279), but this may be something to explore in the future.