-
-
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
Add diagview
to obtain a view along a diagonal
#56175
Conversation
Seems OK, it's used a lot internally, and this is usually the only reason to want However, it seems like the one-arg form should understand structured matrices, so that e.g. What |
Type-stability is indeed the reason I didn't specialize this for banded matrices. This parallels how |
Why do we want this? |
Mainly for ease of use. Having code that works for |
I think you're suggesting someone may pass |
I was thinking of use cases where julia> using LinearAlgebra
julia> A = rand(4,4)
4×4 Matrix{Float64}:
0.805581 0.957599 0.252321 0.552123
0.663896 0.821837 0.788979 0.110341
0.654359 0.716732 0.0641013 0.0818827
0.903501 0.174516 0.193872 0.933869
julia> @btime Bidiagonal(diag($A), diag($A,1), :U)
82.477 ns (4 allocations: 176 bytes)
4×4 Bidiagonal{Float64, Vector{Float64}}:
0.805581 0.957599 ⋅ ⋅
⋅ 0.821837 0.788979 ⋅
⋅ ⋅ 0.0641013 0.0818827
⋅ ⋅ ⋅ 0.933869
julia> @btime Bidiagonal(diagview($A), diagview($A,1), :U)
52.661 ns (2 allocations: 64 bytes)
4×4 Bidiagonal{Float64, SubArray{Float64, 1, Vector{Float64}, Tuple{StepRange{Int64, Int64}}, true}}:
0.805581 0.957599 ⋅ ⋅
⋅ 0.821837 0.788979 ⋅
⋅ ⋅ 0.0641013 0.0818827
⋅ ⋅ ⋅ 0.933869 The constructor only accepts That said, having a direct view into the parent presents significant optimization opportunities, so I'm open to reconsidering the strict type-stability requirement. |
Ok, fair point. With julia> diagview(A::AbstractMatrix, k::Integer=0) = @view A[diagind(A, k, IndexStyle(A))] # as in PR
julia> diagview(A::UpperTriangular) = diagview(parent(A)) # proposed optimisation
julia> U = UpperTriangular(A);
julia> Bidiagonal(diagview(U), diagview(U,1), :U) # not the most informative error!
ERROR: MethodError: no method matching Bidiagonal(::SubArray{…}, ::SubArray{…}, ::Symbol)
julia> Bidiagonal(diagview(U,0), diagview(U,1), :U) # like this is fine
4×4 Bidiagonal{Float64, SubArray{Float64, 1, UpperTriangular{Float64, Matrix{Float64}}, Tuple{StepRangeLen{CartesianIndex{2}, CartesianIndex{2}, CartesianIndex{2}, Int64}}, false}}:
0.875899 0.396184 ⋅ ⋅
⋅ 0.466412 0.175884 ⋅
⋅ ⋅ 0.0872872 0.324497
⋅ ⋅ ⋅ 0.142097 |
I wonder if |
Gentle bump |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIU, this is good for the time being, and (tempting) specializations could be added separately after careful discussion and consideration, right?
0009fb6
to
1274740
Compare
Just saw this PR, does it make sense to add |
Is there motivation for this? To apply this function recursively, perhaps? |
A function to obtain a view of a diagonal of a matrix is useful, and this is clearly being used widely within
LinearAlgebra
.The implementation here iterates according to the
IndexStyle
of the array:Closes #30250