I’m still new to julia but one of my favorite aspects is dotted broadcasting syntax with my own custom and fast functions. However, in many cases I still find it cumbersome to use this functionality compared to my usual numpy syntax, at least with my current knowledge of julia.
Often arrays don’t have matching shapes for broadcasting because their dimensions don’t correspond. To make them fit, you can use None or np.newaxis:
a = np.random.rand(3, 5)
b = np.random.rand(4, 6)
c = a[:, None, :, None] + b[None, :, None, :]
c.shape == (3, 4, 5, 6)
In my opinion this is easy to write and read. I think in julia I’d have to use reshape
. But for this I need to know all dimension sizes and can’t just use colons if I mean the whole dimension:
sa = size(a)
sb = size(b)
c = reshape(a, (sa[1], 1, sa[2], 1)) .+ reshape(b, (1, sb[1], 1, sb[2]))
Is there a better way to do this that I’m not aware of?
Best,
Julius