Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can numpy einsum() perform outer addition?

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?

like image 300
Bitwise Avatar asked Jul 11 '13 20:07

Bitwise


People also ask

What does numpy Einsum do?

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.

Is numpy Einsum fast?

einsum is clearly faster. Actually, twice as fast as numpy's built-in functions and, well, 6 times faster than loops, in this case.


1 Answers

Another fast alternative to this operation is to use:

np.add.outer(a,b)
like image 150
Saullo G. P. Castro Avatar answered Sep 30 '22 13:09

Saullo G. P. Castro