# Want to use a function like numpy.broadcast\_to()

**URL:** <https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984>\
**Category:** General Usage\
**Tags:** question, broadcast\
**Created:** [September 21, 2020, 7:15am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984 "2020-09-21T07:15:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abap34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abap34/32/21497_2.png) [@abap34](https://discourse.julialang.org/u/abap34)\
**Post date:** [September 21, 2020, 7:15am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/1 "2020-09-21T07:15:37Z")

</div>

I’m porting some Python code to Julia, and I’d like to use the equivalent of the numpy.broadcast\_to() function in it.

### An idea I’ve tried.

Given an Array `x` and a Tuple `shape`,

- Use ` x .+ zeros(shape)` instead of `broadcast_to(x, shape)`

This is simple and works a little fast, but I don’t think it’s a good solution.

- Implement `broadcast_to` by myself.  
Unfortunately, this worked slower than the first idea I tried.

If you have a function that corresponds to numpy.broadcast\_to, or an implementation that would be helpful, please let me know.

I can’t speak English very well, so I use machine translation.  
Sorry if my English is strange.

Thank you.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 21, 2020, 7:51am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/2 "2020-09-21T07:51:43Z")

</div>

`x` is a scalar, which should be in each element of the resulting n-dimensional array, right?  
There are multiple ways, but this is probably the nicest one:

```julia
fill(x, shape)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 21, 2020, 8:17am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/3 "2020-09-21T08:17:29Z")

</div>

@lungben’s suggestion is very nice: `fill(x, shape)` is, believe, the canonical solution to this. But keep in mind that you often don’t need to instantiate the array at _all_. If you are using `x` in operations with other arrays you would just write

```julia
A .+ x

```

for example. Otherwise, `fill` is the best way.

---

<div class="post-metadata">

**Author:** ![abap34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abap34/32/21497_2.png) [@abap34](https://discourse.julialang.org/u/abap34)\
**Post date:** [September 21, 2020, 9:00am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/4 "2020-09-21T09:00:53Z")

</div>

@lungben @DNF  
I am very sorry.  
I didn’t explain myself well enough.  
The x is an array, not a scalar.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 21, 2020, 9:14am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/5 "2020-09-21T09:14:36Z")

</div>

Could you give an example of where you would like to use `broadcast_to`? I.e. the value of `x` in that case and what you would want to do with it.

> [@lungben](#):
>
> ```julia
> fill(x, shape)
> 
> ```

For non scalar x, this doesn’t match the documented result of `broadcast_to`:

```julia
np.broadcast_to(x, (3, 3))
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 21, 2020, 9:32am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/6 "2020-09-21T09:32:07Z")

</div>

> [@abap34](#):
>
> I am very sorry.

No problem, I see now that your explanation was ok. Perhaps an example would have made it easier to understand.

You could try `repeat`, which is ok in this particular case:

```julia
julia> x = [1,2,3]';

julia> repeat(x, 2, 1)
2×3 Array{Int64,2}:
 1 2 3
 1 2 3

```

But I don’t know of a solution in Base that has exactly the same functionality as `numpy.broadcast_to` (there might be, though). I think this is because you rarely need to actually create the broadcasted array. You just do it lazily by using broadcasted _operations_ instead. Are you sure that is not an acceptable solution? What do you need this for, if I may ask?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 21, 2020, 9:33am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/7 "2020-09-21T09:33:31Z")

</div>

Is this what you are looking for?

```julia
julia> function broadcast_to(x, shape)
           x isa Number && return fill(x, shape)
           repetitions = ntuple(length(shape)) do i
               oldsize, newsize = size(x, i), shape[i]
               if oldsize == 1
                   newsize
               elseif oldsize == newsize
                   1
               else
                   throw(ArgumentError("Sizes $oldsize and $newsize in dimension $i are incompatible!"))
               end
           end
           return repeat(x, repetitions...)
       end
broadcast_to (generic function with 1 method)

julia> broadcast_to(2, (3, 3))
3×3 Array{Int64,2}:
 2 2 2
 2 2 2
 2 2 2

julia> broadcast_to([1,2,3], (3, 3))
3×3 Array{Int64,2}:
 1 1 1
 2 2 2
 3 3 3

```

Just note that as others already pointed out, this is usually not necessary and can be quite slow, because it allocates a new array.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 21, 2020, 9:38am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/9 "2020-09-21T09:38:34Z")

</div>

It’s worth noting that the difference between the python `broadcast_to` and the ones offered here is that in python the returned array is “A readonly view on the original array with the given shape.” rather than featuring copies. This is possible because multi dimensional numpy arrays are just arrays of arrays internally.

> [@abap34](#):
>
> Actually, I’m rewriting a small DeepLerning framework based on Chainer in Julia, and in the process I need broadcast\_to itself.

Do you mind linking to or writing out the exact operation that required `broadcast_to` 😅  
I agree with others in this thread that probably you don’t need it at all, and it’s just the numpy approach seeping into julia.

---

<div class="post-metadata">

**Author:** ![abap34](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abap34/32/21497_2.png) [@abap34](https://discourse.julialang.org/u/abap34)\
**Post date:** [September 21, 2020, 10:30am UTC](https://discourse.julialang.org/t/want-to-use-a-function-like-numpy-broadcast-to/46984/10 "2020-09-21T10:30:07Z")

</div>

@tomerarnon  
Thank you.  
I’ll try to write some code that works with the minimum
