Say I’ve coded up a type called
Field, and defined
broadcast over these objects so that I can multiply them, i.e.
f1 = Field() f2 = Field() f1 .* f2 .* f1 # this works correctly
Now I would also like to be able to work with
Fields, in particular I want matrix multiplication to “use the broadcasted * operator”, and to do so efficiently. By that I mean,
x = [f1,f2]' y = [f1,f2] # I want to be able to write this: x * y + f1 # and have this be effectively evaluated: x .* y .+ x .* y .+ f1
In particular, I want to make sure there’s one single broadcast over everything, so that even the
x * y + f1 form does not allocate any temporary arrays.
Can anyone think of a good way to do this?
My best idea so far involves writing a custom
@generated broadcast function, and then always writing
@. x * y + f1. From inside the generated broadcast function I then have access to both the expression
x * y + f1, and the types of the arguments, x, y and f1. Hence in theory I have everything I need to transform the call into what is needed. However, I’m not the most keen on what it might take to code up the logic for this transformation so it will work generally. Is there perhaps an easier way?
Thanks for any advice.