Skip to content

Conversation

@mtfishman
Copy link
Member

Fixes some issues with nested views, for example this now works:

julia> using BlockArrays: Block

julia> using NDTensors.BlockSparseArrays: BlockSparseArray

julia> a = BlockSparseArray{Float64}([2, 3], [2, 3])
typeof(axes) = Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}

Warning: To temporarily circumvent a bug in printing BlockSparseArrays with mixtures of dual and non-dual axes, the types of the dual axes printed below might not be accurate. The types printed above this message are the correct ones.

2×2-blocked 5×5 BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}:
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0
 ──────────┼───────────────
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0

julia> b = @view a[[Block(2), Block(1)], [Block(2), Block(1)]]
5×5 view(::BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}, [3, 4, 5, 1, 2], [3, 4, 5, 1, 2]) with eltype Float64 with indices BlockArrays.BlockedOneTo([3, 5])×BlockArrays.BlockedOneTo([3, 5]):
 0.0  0.0  0.00.0  0.0
 0.0  0.0  0.00.0  0.0
 0.0  0.0  0.00.0  0.0
 ───────────────┼──────────
 0.0  0.0  0.00.0  0.0
 0.0  0.0  0.00.0  0.0

julia> b[Block(1, 1)] .= randn(3, 3)
3×3 view(view(::BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}, [3, 4, 5, 1, 2], [3, 4, 5, 1, 2]), BlockSlice(Block(1),1:3), BlockSlice(Block(1),1:3)) with eltype Float64:
  0.994707   0.611011    0.110323
 -1.42947    0.0876526   0.808757
  0.887843  -0.267044   -0.909812

julia> a
typeof(axes) = Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}

Warning: To temporarily circumvent a bug in printing BlockSparseArrays with mixtures of dual and non-dual axes, the types of the dual axes printed below might not be accurate. The types printed above this message are the correct ones.

2×2-blocked 5×5 BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}:
 0.0  0.00.0        0.0         0.0     
 0.0  0.00.0        0.0         0.0     
 ──────────┼──────────────────────────────────
 0.0  0.00.994707   0.611011    0.110323
 0.0  0.0-1.42947    0.0876526   0.808757
 0.0  0.00.887843  -0.267044   -0.909812

julia> b
5×5 view(::BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}, [3, 4, 5, 1, 2], [3, 4, 5, 1, 2]) with eltype Float64 with indices BlockArrays.BlockedOneTo([3, 5])×BlockArrays.BlockedOneTo([3, 5]):
  0.994707   0.611011    0.1103230.0  0.0
 -1.42947    0.0876526   0.8087570.0  0.0
  0.887843  -0.267044   -0.9098120.0  0.0
 ──────────────────────────────────┼──────────
  0.0        0.0         0.00.0  0.0
  0.0        0.0         0.00.0  0.0

@mtfishman mtfishman merged commit 7b2ada9 into main Jun 18, 2024
@mtfishman mtfishman deleted the BlockSparseArrays_nested_views branch June 18, 2024 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants