Skip to content

In-place assignment on subviews of bands #251

@miromarszal

Description

@miromarszal

I was wondering what is the recommended way to modify subviews of a band. My first naive try, which perhaps shows that I don't know that much about how array indexing works in julia, was the following:

B = BandedMatrix{Float64}(undef, 5, 5, 0, 0)
B[band(0)] .= 1.0
B
5×5 BandedMatrix{Float64} with bandwidths (0, 0):
 1.0   ⋅    ⋅    ⋅    ⋅ 
  ⋅   1.0   ⋅    ⋅    ⋅ 
  ⋅    ⋅   1.0   ⋅    ⋅ 
  ⋅    ⋅    ⋅   1.0   ⋅ 
  ⋅    ⋅    ⋅    ⋅   1.0
B[band(0)][2:end-1] .= -1.0
 1.0   ⋅    ⋅    ⋅    ⋅ 
  ⋅   1.0   ⋅    ⋅    ⋅ 
  ⋅    ⋅   1.0   ⋅    ⋅ 
  ⋅    ⋅    ⋅   1.0   ⋅ 
  ⋅    ⋅    ⋅    ⋅   1.0

I can get this to work this way though:

view(B, band(0))[2:end-1] .= -1.0
 1.0    ⋅     ⋅     ⋅    ⋅ 
  ⋅   -1.0    ⋅     ⋅    ⋅ 
  ⋅     ⋅   -1.0    ⋅    ⋅ 
  ⋅     ⋅     ⋅   -1.0   ⋅ 
  ⋅     ⋅     ⋅     ⋅   1.0

which seems a bit verbose to me. Is there some shortcut I'm missing? Actually, I think being able to do the following would be very nice:

B[band(0), 2:end-1] .= -1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions