Taylor
Taylor

Reputation: 127

Element-wise multiplication between all the elements of a vector

What is the efficient way to do element wise mulitplication between all the elements of a numpy array.

If A is a vector of 100 elements then A2 is a vector of 100^2 elements.

Example

Input :

a = [5,2,3,4]

output

a2=[5*5,5*2,5*3,5*4,2*5,2*2,2*3,2*4,3*5,3*2,3*3,3*4,4*5,4*2,4*3,4*4]

Thank you

Upvotes: 1

Views: 66

Answers (1)

Dishin H Goyani
Dishin H Goyani

Reputation: 7723

>>> import numpy as np
>>> np.outer(a,a).reshape(-1)

array([25, 10, 15, 20, 10,  4,  6,  8, 15,  6,  9, 12, 20,  8, 12, 16])

Use numpy.outer - to compute the outer product of two vectors and then numpy.reshape - to get array into expected shape.

Upvotes: 3

Related Questions