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:
@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
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?
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.
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.
@tomerarnon
Sorry for the delay in getting back to you.
After thinking about it for a while, I’ve found that it’s good enough without using broadcasts.
I’m very sorry for the inconvenience.
Thank you for your kindness and insight.