# Is there a way to De-Broadcast?

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281
**Category:** General Usage
**Tags:** question
**Created:** [August 24, 2022, 6:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281 "2022-08-24T18:05:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [August 24, 2022, 6:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281/1 "2022-08-24T18:05:55Z")

</div>

I have come across cases of functions (e.g. the functions in Splines2, and elsewhere) where vectors are required as inputs, and where these same functions fail for scalar input (where conversion to/from 1-dim vector is required)

Short of implementing a script for the special case of isa( ,Number)==true (converting to 1-dim vector, evaluating, and then converting back to scalar) is there a more elegant way to implement this ‘De-Broadcasting’?

Thanks for any help

---

<div class="post-metadata">

### Author: ![RayleighLord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayleighlord/32/33592_2.png) [@RayleighLord](https://discourse.julialang.org/u/RayleighLord)
#### Post date: [August 24, 2022, 6:13pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281/2 "2022-08-24T18:13:04Z")

</div>

I think what you want to do can be achieved by overloading the function via Multiple Dispatch. That is, implement a method of the function that takes a scalar as an input and makes the transformation to a vector so it then can be used by the method that only accepts a vector as input.

As a silly example, say you have a function that adds all the components of a vector. If you pass a scalar to that function, since there is nothing to sum, you expect the function to return that scalar. Therefore, we could have

```julia
sum_vec(x::AbstractVector) = sum(x) # This would throw an error for an scalar
sum_vec(x::Number) = sum_vec([x,]) # This works for scalars

```

As you can see, with the second method definition I insert the scalar into a 1 dimensional vector with 1 element which executes the first method definition of the function `sum_vec`.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 24, 2022, 6:15pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281/3 "2022-08-24T18:15:00Z")

</div>

> [@compleat](#):
>
> where vectors are required as inputs

just wrap your input in `[]`?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [August 24, 2022, 8:59pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281/4 "2022-08-24T20:59:50Z")

</div>

The preferred way to _not_ broadcast an input is to wrap it in an iterable. The common choices are `Ref(x)`, `[x]`, `(x,)`. For example, `in.(1:4,Ref(3:6))` is comparable to `[in(z,3:6) for z in 1:4]`.

---

<div class="post-metadata">

### Author: ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)
#### Post date: [August 24, 2022, 9:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-de-broadcast/86281/5 "2022-08-24T21:17:30Z")

</div>

Hi All,  
Thanks - I think I will go with the multiple dispatch solution [ I was wondering if there were an  
easier way.]  
The tricky part is that the function is going to be called by a routine that I have no control over.

Thanks again
