If you just want to fill the array with a scalar, fill
is probably the best choice. But it looks like you want something more generalized. Instead of using broadcast
you can use broadcast_arrays
to get the result that (I think) you need.
>>> a = numpy.arange(9).reshape(3, 3) >>> numpy.broadcast_arrays(a, 1)[1] array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
This generalizes any two broadcast forms:
>>> numpy.broadcast_arrays(a, [1, 2, 3])[1] array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
This is not as fast as your ufunc
based ufunc
, but it is still on the same level:
>>> %timeit 1 + a * 0 10000 loops, best of 3: 23.2 us per loop >>> %timeit numpy.broadcast_arrays(a, 1)[1] 10000 loops, best of 3: 52.3 us per loop
But scalars, fill
are still clear front:
>>> %timeit b = numpy.empty_like(a, dtype='i8'); b.fill(1) 100000 loops, best of 3: 6.59 us per loop
Finally, further testing shows that the fastest approach - at least in some cases - is multiplied by ones
:
>>> %timeit numpy.broadcast_arrays(a, numpy.arange(100))[1] 10000 loops, best of 3: 53.4 us per loop >>> %timeit (1 + a * 0) * numpy.arange(100) 10000 loops, best of 3: 45.9 us per loop >>> %timeit b = numpy.ones_like(a, dtype='i8'); b * numpy.arange(100) 10000 loops, best of 3: 28.9 us per loop