JuliaArrays/LazyArrays.jl

Cached BandedMatrices give wrong entries and break bandedness

TSGut opened this issue · 4 comments

TSGut commented

@dlfivefifty I was looking for a minimum working example of the fill-in bug mentioned in this PR but I came across something else in the process:

julia> A = BandedMatrix(0=>ones(10),1 => zeros(9))
10×10 BandedMatrix{Float64} with bandwidths (0, 1):
 1.0  0.0                                
     1.0  0.0                            
         1.0  0.0                        
             1.0  0.0                    
                 1.0  0.0                
                     1.0  0.0            
                         1.0  0.0        
                             1.0  0.0    
                                 1.0  0.0
                                     1.0

julia> B = cache(A) # huh?
10×10 CachedArray{Float64, 2, BandedMatrix{Float64, Matrix{Float64}, Base.OneTo{Int64}}, BandedMatrix{Float64, Matrix{Float64}, Base.OneTo{Int64}}}:
 1.0  0.0                                
     1.0  0.0                            
         0.0  0.0                        
             0.0  0.0                    
                 0.0  0.0                
                     0.0  0.0            
                         0.0  0.0        
                             0.0  0.0    
                                 0.0  0.0
                                     1.0

julia> B[1:10,1:10] # still wrong.. and broke bandedness
10×10 Matrix{Float64}:
 1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0

blocking the output I get what is expected (minus the breaking bandedness):

julia> A = BandedMatrix(0=>ones(10),1 => zeros(9))
10×10 BandedMatrix{Float64} with bandwidths (0, 1):
 1.0  0.0                                
     1.0  0.0                            
         1.0  0.0                        
             1.0  0.0                    
                 1.0  0.0                
                     1.0  0.0            
                         1.0  0.0        
                             1.0  0.0    
                                 1.0  0.0
                                     1.0

julia> B = cache(A);

julia> B[1:10,1:10]
10×10 Matrix{Float64}:
 1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0

This is probably entirely unrelated but I would still like to know what's happening here

TSGut commented

While I don't know why this interaction happens when display() is called, it seems that this bug is not present when LazyBandedMatrices.jl is also in use, which is the intended way of interfacing BandedMatrices with LazyArrays.

This is still strange behavior so I will leave it open for now I guess?

We can change LazyBandedMatrices.jl into a Julia v1.9 extension in the future

This is fixed in v2.0

julia> using LazyArrays, BandedMatrices

julia> A = BandedMatrix(0=>ones(10),1 => zeros(9))
10×10 BandedMatrix{Float64} with bandwidths (0, 1):
 1.0  0.0                                
     1.0  0.0                            
         1.0  0.0                        
             1.0  0.0                    
                 1.0  0.0                
                     1.0  0.0            
                         1.0  0.0        
                             1.0  0.0    
                                 1.0  0.0
                                     1.0

julia> B = cache(A) # huh?
10×10 LazyArrays.CachedArray{Float64, 2, BandedMatrix{Float64, Matrix{Float64}, Base.OneTo{Int64}}, BandedMatrix{Float64, Matrix{Float64}, Base.OneTo{Int64}}}:
 1.0  0.0                                
     1.0  0.0                            
         1.0  0.0                        
             1.0  0.0                    
                 1.0  0.0                
                     1.0  0.0            
                         1.0  0.0        
                             1.0  0.0    
                                 1.0  0.0
                                     1.0

julia> B[1:10,1:10]
10×10 BandedMatrix{Float64} with bandwidths (0, 1):
 1.0  0.0                                
     1.0  0.0                            
         1.0  0.0                        
             1.0  0.0                    
                 1.0  0.0                
                     1.0  0.0            
                         1.0  0.0        
                             1.0  0.0    
                                 1.0  0.0
                                     1.0