OffsetArray of size 1 from elementwise multiplication

When I elementwise multiply an offset array by a regular array, I get the result I expect - the resulting array is offset properly - except when the size is 1.

In this example, the input array dimension is 6:10, and the multiplication result is 6:10 in the first dimension. This is as I expect, and all dimensions of size greater than 1 result in the same first dimension as the input.

rp
5-element OffsetArray(::Vector{Float64}, 6:10) with eltype Float64 with indices 6:10:
 1.3901009231535904e17
 2.6179379426980558e20
 3.167716484040217e23
 2.6806873684655975e26
 1.4178453339725972e29

csmlon
1×2 adjoint(::Vector{Float64}) with eltype Float64:
 -0.594981  0.80374

rp .* csmlon
5×2 OffsetArray(::Matrix{Float64}, 6:10, 1:2) with eltype Float64 with indices 6:10×1:2:
 -8.27084e16  1.11728e17
 -1.55762e20  2.10414e20
 -1.88473e23  2.54602e23
 -1.59496e26  2.15458e26
 -8.43591e28  1.13958e29

However, when the input array is 10:10, the multiplication result is 1:1 in the first dimension. I expect 10:10 as the first dimension, consistent with the size > 1 case.

rp
1-element OffsetArray(::Vector{Float64}, 10:10) with eltype Float64 with indices 10:10:
 2.3234323987486906e28

csmlon
1×2 adjoint(::Vector{Float64}) with eltype Float64:
 0.873366  -0.487065

rp .* csmlon
5×2 OffsetArray(::Matrix{Float64}, 6:10, 1:2) with eltype Float64 with indices 6:10×1:2:
 -8.27084e16  1.11728e17
 -1.55762e20  2.10414e20
 -1.88473e23  2.54602e23
 -1.59496e26  2.15458e26
 -8.43591e28  1.13958e29


csmlon
1×2 adjoint(::Vector{Float64}) with eltype Float64:
 0.873366  -0.487065

rp .* csmlon
1×2 OffsetArray(::Matrix{Float64}, 1:1, 1:2) with eltype Float64 with indices 1:1×1:2:
 2.02921e28  -1.13166e28

The only resolution I can think of is to call OffsetArray(...) on the result in the size=1 case. Is this a bug?

Julia Version 1.10.1 (2024-02-13)

2 Likes

Yeah, type stability issues like this one are usually considered as bugs. Try reporting it to the OffsetArrays.jl Github page.

This appears to be more of a Base broadcasting issue, where it chooses one axis over the other when both contain only one element.

1 Like

Thanks for the responses. I have created this issue in OffsetArrays.jl.