In numpy, we can perform "outer addition" between two vectors a
and b
like this:
a=np.c_[1,2,3]
b=np.c_[4,5,6]
result=a+b.T # alternatively this can be a.T+b
Is it possible to use einsum
to make the same calculation? Any other fast alternatives? How about if a
equals b
?
Evaluates the Einstein summation convention on the operands. Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion.
einsum is clearly faster. Actually, twice as fast as numpy's built-in functions and, well, 6 times faster than loops, in this case.
Another fast alternative to this operation is to use:
np.add.outer(a,b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With