Search code examples
pythonnetworkxeigenvector

How do I calculate the dot product between one vector and a list of vectors?


I have a vector and the Eigenvectors of the matrix H, id like to find the dot product on V1 with every vector in the array.

I want to multiply the vector PhiZero with every eigenvector that's calculated for H

import numpy as np
import scipy.linalg as la
import math
import networkx as nx

Alpha = []
n=3
p=0.5
G = nx.gnp_random_graph(n,p) 
A = nx.to_numpy_matrix(G)
w = np.zeros(shape=(n,n))
w[1,2] = 1
gamma = 1/(n*p)
H = (-w) - (gamma * A)

ive chosen a random position in w,

evals, evecs = la.eig(H)
PhiZero = np.reciprocal(math.sqrt(n)) * np.ones((n,1), dtype=int)

i've tried to calculate it two ways, the first way, I get a 3x3 matrix

Alpha = np.dot(PhiZero.transpose(), evecs)

the other way, I tried it with a for loop:

for y in evecs:
    alphaJ = np.dot(PhiZero.transpose(), evecs) 
    Alpha.append(alphaJ)

i've taken the transpose PhiZero to make the dimensions align with evecs (1x3 & 3x1)


Solution

  • In your second approach, should you not have:

    alphaJ = np.dot(PhiZero.transpose(), y)

    rather than

    alphaJ = np.dot(PhiZero.transpose(), evecs) ?

    If I try your first example, it works:

    import numpy as np
    import scipy.linalg as la
    import math
    import networkx as nx
    
    Alpha = []
    n=3
    p=0.5
    G = nx.gnp_random_graph(n,p) 
    A = nx.to_numpy_matrix(G)
    w = np.zeros(shape=(n,n))
    w[1,2] = 1
    gamma = 1/(n*p)
    H = (-w) - (gamma * A)
    
    evals, evecs = la.eig(H)
    PhiZero = np.reciprocal(math.sqrt(n)) * np.ones((n,1), dtype=int)
    
    Alpha = np.dot(PhiZero.transpose(), evecs)
    
    print(Alpha)
    

    gives

    [[ 0.95293215 -0.32163376  0.03179978]]
    

    Are you sure you get a 3x3 if you run this?