# Broadcast hangs after successful execution over all array elements

**URL:** <https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193>\
**Category:** New to Julia\
**Tags:** question, broadcast\
**Created:** [September 8, 2024, 1:03pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193 "2024-09-08T13:03:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 8, 2024, 1:03pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/1 "2024-09-08T13:03:11Z")

</div>

When I call the function `INSIDE_WHICH_INTVAL` below, it correctly returns `IVL_NUM` for input of type `INTY :: Float64`. When I broadcast the function to cover all elements of an array `INTY_ARRAY` also, as `INSIDE_WHICH_INTVAL.(INTY_ARRAY, Ref(INT_BOU), Ref(N_INTEN))`, it does execute correctly without issues, **but hangs after the last array element is done with**. Any help?

```julia
function INSIDE_WHICH_INTVAL(INTY, INT_BOU, N_INTEN)
    IS_OUTS :: Bool = true
    INT_MIN = INT_BOU[1]
    INT_MAX = INT_BOU[2]
    INT_INC = (INT_MAX - INT_MIN)/ N_INTEN
    IVL_NUM = 0
    I = 1
    while IS_OUTS
        IVL_LOW = INT_MIN + (I - 1)* INT_INC
        IVL_HIH = INT_MIN + (I - 0)* INT_INC
        IS_INSI = (IVL_LOW < INTY) && (INTY < IVL_HIH)
        if IS_INSI == 1
            IVL_NUM = I
            IS_OUTS = false
            break
        end
        I += 1
    end
    println("IVL_NUM = ", IVL_NUM)
    return IVL_NUM
end

I just added the `println` to confirm that `INSIDE_WHICH_INTVAL` does finish evaluating all array elements. 

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 8, 2024, 1:28pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/2 "2024-09-08T13:28:37Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

In particular, tell us exactly how to call your code to see the error.

Also, can I suggest using more descriptive variable names and following the Julia variable naming conventions: [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/)  
This will not only help us understand your code so we can help you, it will help you write maintainable code in the long run.

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 8, 2024, 2:22pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/3 "2024-09-08T14:22:14Z")

</div>

When `INTY` is a scalar `Float64`, here’s a no issues run…

```julia
julia> IMG_BOU
2-element Vector{Float64}:
 0.019607843137254943
 1.0

```

```julia
julia> INTY
0.5529411764705883

```

```julia
julia> N_INTEN
4

```

and here is the call

```julia
julia> INSIDE_WHICH_INTVAL(INTY, IMG_BOU, 4)
IVL_NUM = 3
3

```

When I run with `INTY_ARRAY` a vector say, defined as

```julia
julia> INTY
11-element Vector{Float64}:
 0.45882352941176474
 0.5529411764705883
 0.6313725490196078
 0.7019607843137254
 0.7568627450980392
 0.8196078431372549
 0.8509803921568627
 0.9254901960784314
 1.0
 1.0
 1.0

```

```julia
julia> IVL_VEC = INSIDE_WHICH_INTVAL.(INTY, Ref(IMG_BOU), Ref(N_INTEN))
IVL_NUM = 2
IVL_NUM = 3
IVL_NUM = 3
IVL_NUM = 3
IVL_NUM = 4
IVL_NUM = 4
IVL_NUM = 4
IVL_NUM = 4

```

It hangs after! To test with any other data, just note `0.0 < INTY < 1.0`, `INT_BOU` is a 2- element vector with elements in `(0.0,1.0)`, `N_INTEN` is an `I64` scalar `>0` that specifies the number of intervals **within one of which** `INTY` lies.

Regarding suggestion on style, appearance, kindly bear with me. I’ve been coding in this caps- dominant style for decades, across many languages from Ada 2012 thru MATLAB and now Julia 1.10.4. Indeed I find it lot easier to comprehend and extend a longer set made up of multiple short lines of code than a shorter set with longer lines that’ve lots of nesting and associations. Surely will think over style once I’ve got seasoned enough with Julia.

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 8, 2024, 2:30pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/4 "2024-09-08T14:30:15Z")

</div>

Just occurred to me if `INTY[9] = 1.0` is why it’s hanging. The `while` might after all be unable to figure `IVL_NUM` when any `INTY[k] ==1.0` unlike with elements `1..8` for which `INTY[k] < 1.0`. Will modify the `IS_INSI` assignment and try.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 8, 2024, 4:04pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/5 "2024-09-08T16:04:42Z")

</div>

You need to make sure the number under test is guaranteed to be in one of the tested intervals. It’s difficult to figure out what the code is supposed to be doing because of your choice of variable names, but 1.0 does indeed seem to fall between the cracks.  
It might be better to test only one comparison per loop iteration. For instance, if I read it right, the lower bound should not be necessary, since the limits increase monotonically. You only have to check whether the number is below the increasing upper bound.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 8, 2024, 4:21pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/6 "2024-09-08T16:21:20Z")

</div>

Here’s an example of what I mean. Does this do what you expect your function to do?

```julia
"""
    which_interval(x, grand_interval, num_intervals)
Give which of `num_intervals` subdivisions of the left-open interval `grand_interval` that contains number `x`.
For instance, `which_interval(0.5, (0, 3), 3) == 1`, because `0.5` is in the first third of the interval ``(0, 3]``.

If `x` does not lie within `grand_interval`, returns `nothing`.
"""
function which_interval(x, grand_interval, num_intervals)
    grand_interval[1] < x <= grand_interval[2] || return nothing
    increment = (grand_interval[2]-grand_interval[1])/num_intervals
    for i = 1:num_intervals-1
        x <= grand_interval[1] + increment*i && return i
    end
    return num_intervals
end

```

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 9, 2024, 5:33am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/7 "2024-09-09T05:33:14Z")

</div>

Thanks for suggesting a code, @gustaphe. It was indeed the runtime issue of me not checking for `INTY` being equal to either of the interval’s limits. I also simplified the function, as my bounds (`grand_interval` in your code) would never be outside `[0.0, 1.0]`. The function now reads as follows:

```julia
function INSIDE_WHICH_INTVAL(INTY , N_INTEN)
    IS_OUTS :: Bool = true
    INT_INC = 1.0/ N_INTEN
    IVL_NUM = 0
    I = 1
    while IS_OUTS
        IVL_LOW = (I - 1) * INT_INC
        IVL_HIH = (I - 0) * INT_INC
        IS_INSI = (IVL_LOW <= INTY) && (INTY <= IVL_HIH)
        if IS_INSI == 1
            IVL_NUM = I
            IS_OUTS = false
            break
        end
        I += 1
    end
    return IVL_NUM
end

```

Both lower and upper interval bounds are necessary, as otherwise **all** values `INTY > Lower_Bound` OR all values `INTY < Upper_Bound`could qualify to fire `IS_INSI` as `true`.

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 9, 2024, 5:36am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/8 "2024-09-09T05:36:37Z")

</div>

Just a few runtime stats from the simplified code, to close this discussion:

```julia
julia> IVL_ARR = @time INSIDE_WHICH_INTVAL.(IMG_TST, Ref(N_INTEN));
  6.594793 seconds (4 allocations: 12.810 MiB, 99.75% gc time)

julia> IVL_ARR = @time INSIDE_WHICH_INTVAL.(IMG_TST, Ref(N_INTEN));
  0.017390 seconds (4 allocations: 12.810 MiB)

julia> IVL_ARR = @time INSIDE_WHICH_INTVAL.(IMG_TST, Ref(N_INTEN));
  0.018327 seconds (4 allocations: 12.810 MiB)

julia> IVL_ARR = @time INSIDE_WHICH_INTVAL.(IMG_TST, Ref(N_INTEN));
  0.017548 seconds (4 allocations: 12.810 MiB)

```

The above stats are for the array size and `N_INTEN`:

```julia
julia> size(IMG_TST)
(990, 1696)
N_INTEN
4

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 9, 2024, 8:06am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/9 "2024-09-09T08:06:00Z")

</div>

But as long as you test whether it is inside the full interval before you start the loop, it will only get to the next iteration if it did not pass in the last, meaning that half of your comparisons are superfluous. And that extra complexity is what caused your bug.

Nice you got it working though.

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 9, 2024, 10:50am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/10 "2024-09-09T10:50:46Z")

</div>

Not quite so. `INSIDE_WHICH_INTVAL` is a simple function, similar to returning on which step one is, given the `x` in a flight of stairs of total span `0.0 <= x <= 1.0`; see the sketch here.

 ![IMG-20240909-WA0000](https://global.discourse-cdn.com/julialang/original/3X/e/5/e5e658677ed461edec07331de22ce72cb78fc741.jpeg)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 9, 2024, 3:33pm UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/11 "2024-09-09T15:33:44Z")

</div>

Right, but you only need to check that the number is larger than `0`, and then upper bound checks. Because the only way you’re getting to the loop iteration where the lower bound is `3/8`, is to advance from the iteration where the _upper_ bound was `3/8`. So you have already found that `x !< 3/8`, no need to _also_ check that `x >= 3/8`.

---

<div class="post-metadata">

**Author:** ![Bananthahally](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bananthahally/32/222983_2.png) [@Bananthahally](https://discourse.julialang.org/u/Bananthahally)\
**Post date:** [September 10, 2024, 4:50am UTC](https://discourse.julialang.org/t/broadcast-hangs-after-successful-execution-over-all-array-elements/119193/12 "2024-09-10T04:50:12Z")

</div>

Oh, you’re right, can get going with checking for exceeding only one of the two bounds. But typically `N_INTEN` is quite small, `< 10`, so it wouldn’t matter a lot, in my opinion.
