From 37b38cdd1eeb055cef2d209e8f4d0e3465fd4c25 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sun, 25 Oct 2026 23:26:28 -0400 Subject: [PATCH] OffDiagonal: Use dense broadcast when other arrays take part Newer LinearAlgebra (JuliaLang/LinearAlgebra.jl#1549) treats `*` as zero preserving whatever its other arguments are, so `A .* M` for an `OffDiagonal` `A` and a dense `M` took the band path. That path broadcasts the band against `M` directly, which throws a `DimensionMismatch`. Only take the band path when every other argument is a scalar or a zero-dimensional array. Co-Authored-By: Claude Opus 5.5 --- src/offdiagonal.jl | 3 +++ test/runtests.jl | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/offdiagonal.jl b/src/offdiagonal.jl index 32a97baa..ebd08699 100644 --- a/src/offdiagonal.jl +++ b/src/offdiagonal.jl @@ -150,6 +150,9 @@ function _offdiagonal_bcindex(bc::Broadcasted) axes(x) == axes(bc) || return nothing k === nothing || k == x.k || return nothing k = x.k + elseif x isa AbstractArray && ndims(x) > 0 + # other arrays do not reduce to the band, even when the result is zero-preserving + return nothing end end k diff --git a/test/runtests.jl b/test/runtests.jl index 77a9dd3e..26c12445 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -467,7 +467,8 @@ end @test eltype(B) == eltype(BM) end # other operations are dense - for (B, BM) in ((cos.(A), cos.(M)), (A .+ 1, M .+ 1), (A .* M, M .* M), (A .+ M, M .+ M), (A .* (1:size(A,1)), M .* (1:size(A,1)))) + for (B, BM) in ((cos.(A), cos.(M)), (A .+ 1, M .+ 1), (A .* M, M .* M), (A .+ M, M .+ M), (A .* (1:size(A,1)), M .* (1:size(A,1))), + ((1:size(A,1)) .* A, (1:size(A,1)) .* M), (A .* permutedims(1:size(A,2)), M .* permutedims(1:size(A,2)))) @test B isa Matrix @test B == BM end