# Broadcast operations with ranges

**URL:** <https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [April 3, 2018, 3:11pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139 "2018-04-03T15:11:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 3, 2018, 3:11pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/1 "2018-04-03T15:11:37Z")

</div>

Say I want to use a sliding window of width = `N`, such that in the first iteration I would write it as the range `1:N`, and the start and stop values of the range would move `j` units after the `j`-th iteration. My first try would be:

```julia
for j=0:m
    w = j+(1:N)
    # etc
end

```

But testing against Julia 0.7 this issues the following warning:

```julia
Warning: `a::Number + b::AbstractArray` is deprecated, use `broadcast(+, a, b)` instead.

```

Following that advice is bad for performance (at least in Julia 0.6)

```julia
julia> @time 6 + (1:1e7);
  0.000016 seconds (6 allocations: 288 bytes)

julia> @time 6 .+ (1:1e7);
  0.128881 seconds (5.36 k allocations: 76.562 MiB, 2.51% gc time)

```

So I guess I should do:

```julia
for j=0:m
    w = (j+1):(j+N)
    # etc
end

```

with the small extra cost of writing `j` two times (but perhaps the benefit of clearer code).

Am I right? Or is there some other smarter way to do it? Or is this performance issue not happening in 0.7?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 3, 2018, 3:20pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/2 "2018-04-03T15:20:34Z")

</div>

The two things you benchmark dows not compute the same thing

```julia
julia> typeof(6 + (1:1e7)) # range
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}

julia> typeof(broadcast(+, 6, 1:1e7)) # range
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}

julia> typeof(6 .+ (1:1e7)) # array
Array{Float64,1}

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 3, 2018, 3:38pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/3 "2018-04-03T15:38:31Z")

</div>

Thanks, that’s right. So I can rewrite my question as:

Is there a “nicer” (and non-deprecated) way to write `broadcast(+, j, a:b)`, which gives the same result, as `j + (a:b)` did before 0.7?

In this small example I’m using fixed start and stop values. If possible I would also like to do it for shifting a range that is in a variable `x`, whose start and stop values (and perhaps step too) may be “unknown” beforehand.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 3, 2018, 4:54pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/5 "2018-04-03T16:54:01Z")

</div>

I think the answer is: don’t use `()`.

```julia
julia> typeof(6 .+ 1:1e3)                                                                                                                                                
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}                                                                                          

julia> typeof(6 .+ (1:1e3))                                                                                                                                              
Array{Float64,1}                                                                                                                                                         

```

But I wonder why the parenthesis make a difference. Is this a bug?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 3, 2018, 4:57pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/6 "2018-04-03T16:57:25Z")

</div>

That’s just the same as

```julia
(6 .+ 1):1e3

```

`+` has higher precedence than `:`

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 3, 2018, 5:46pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/7 "2018-04-03T17:46:13Z")

</div>

Doh. So, @heliosdrm’s question still stands: How does one add a number to a range and get a range back?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 3, 2018, 6:30pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/8 "2018-04-03T18:30:15Z")

</div>

Right now the answer is in a big PR that changes lots of broadcast functionality. I hope to get back to it soon.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 3, 2018, 6:42pm UTC](https://discourse.julialang.org/t/broadcast-operations-with-ranges/10139/9 "2018-04-03T18:42:04Z")

</div>

To expand on mbaumann, I think this is a weird bug involving the handling of literals.

```julia
r=1:10; z=1.2;
@show z.+r;
#z .+ r = 2.2:1.0:11.2
@show 1.2 .+ r;
#1.2 .+ r = [2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 8.2, 9.2, 10.2, 11.2]
@show broadcast(+, 1.2, r);
#broadcast(+, 1.2, r) = 2.2:1.0:11.2

```

edit: Afaik broadcast-plus is the intended way to shift ranges, modulo transient bugs.
