TAGS :Viewed: 10 - Published at: a few seconds ago

[ What is the best vectorization method here? ]

I am wondering what would be the best way to vectorize the following formula:

c= Sum(u(i)*<u(i),y>/v(i) )

<.,.> means dot product of two matrix.

let say we have a matrix K= U*Diag(w)*U^-1 (w and u are eigenvalues and eigenvectors of matrix k of size nxn) . and y is a vector of size n.

so if :

k=np.array([[1,2,3],[2,3,4],[2,7,8]])
y=np.array([1,4,5])
w,u=np.linalg.eigh(k)

then :

w=array([ -2.02599523,   0.47346124,  13.552534  ])


u=array([[-0.18897996,  0.95770742,  0.21698634],
        [ 0.82245177,  0.03363605,  0.5678395 ],
       [-0.53652554, -0.28577109,  0.79402471]])

This is how I implemented it:

uDoty=np.dot(u,y)
div=np.divide(y,w)

div=np.divide(uDoty,w)
r=np.tile(div,(len(u),1))
a=u*r.T
c=sum(a) 

But it actually It doesn't look nice to me.So is there any suggestion?

Answer 1


You can avoid using np.tile with some broadcasting:

U = np.dot(u, y)
d = U/w
a = u*d[:,None]
c = a.sum()