# OffsetArray of size 1 from elementwise multiplication

**URL:** <https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135>\
**Category:** General Usage\
**Tags:** question, broadcast, offsetarrays\
**Created:** [September 6, 2024, 3:59pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135 "2024-09-06T15:59:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liamh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamh/32/208541_2.png) [@liamh](https://discourse.julialang.org/u/liamh)\
**Post date:** [September 6, 2024, 3:59pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135/1 "2024-09-06T15:59:05Z")

</div>

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.

```julia
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.

```julia
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)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 7, 2024, 9:06pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135/2 "2024-09-07T21:06:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 8, 2024, 1:03pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135/3 "2024-09-08T13:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![liamh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamh/32/208541_2.png) [@liamh](https://discourse.julialang.org/u/liamh)\
**Post date:** [September 9, 2024, 2:57pm UTC](https://discourse.julialang.org/t/offsetarray-of-size-1-from-elementwise-multiplication/119135/4 "2024-09-09T14:57:53Z")

</div>

Thanks for the responses. I have created [this issue](https://github.com/JuliaArrays/OffsetArrays.jl/issues/360) in OffsetArrays.jl.
