JuliaArrays / JuliaArrays/BlockArrays.jl

Add blockdiagind() indexing function

Open
#130 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
254
Forks
39
PR merge metrics
No merged PRs in 30d

Description

The scalar elements on the diagonals of arrays can be accessed using diagind (https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diagind) to get the AbstractRange of an entire diagonal and operate on it (e.g. doing D[diagind( D, -1 )] .= 1 to set all values on the diagonal 1 below the main to 1). That also works on a BlockArray for accessing the scalar elements on a diagonal. It would be nice to have a similar function for accessing the block diagonal elements (e.g. access entire blocks on a diagonal) instead of just the scalar elements.

Example of desired behavior:

julia> D = BlockArray( randn(4,4), [2, 2], [2, 2] )                                                                                                                                                                                                       
2×2-blocked 4×4 BlockArray{Float64,2}:
  0.456552   0.508835  │  -1.36404    0.0981592
  0.579405   1.1095    │  -0.171994   1.02463  
 ──────────────────────┼───────────────────────
  0.189181  -0.593364  │   0.931672  -0.648214 
 -0.414329   1.02497   │  -0.990573   0.154431 

julia> D[diagind(D,-1)]                                                                                                                                                                                                                                   
3-element Array{Float64,1}:
  0.5794046395673269
 -0.5933636927794533
 -0.9905727125760019

julia> D[blockdiagind(D,-1)]              
2×2 Array{Float64,2}:
  0.189181  -0.593364
 -0.414329   1.02497

Having this available will simplify some operations such as:

 # Create a NxN block matrix with identity matrices of size nx x nx on the diagonal
otherDiag = kron( I(N), to_matrix( mt, I, nx ) )

D = zeros( mt, N, N )
D[diagind( D, -1 )] .= 1    

# Populate the block diagonal 1 below the main block diagonal with the nx x nx matrix sys.A
otherDiag = otherDiag + kron( D, -sys.A )

could become

 # Create a NxN block matrix with identity matrices of size nx x nx on the diagonal
otherDiag = kron( I(N), to_matrix( mt, I, nx ) )

# Populate the block diagonal 1 below the main block diagonal with the nx x nx matrix sys.A
otherDiag[blockdiagind( otherDiag, -1 )] .= -sys.A

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the existing diagind behavior for BlockArray with the requested blockdiagind examples in this issue. Define the block-diagonal indexing entry point and verify that selecting the lower block diagonal returns the expected blocks and supports the shown assignment; add or update tests for those examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.