MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / multiply_matrix_vector

Function multiply_matrix_vector

graphs/lanczos_eigenvectors.py:133–162  ·  view source on GitHub ↗

Performs multiplication of a graph's adjacency list representation with a vector. Args: graph: The adjacency list of the graph. vector: A 1D numpy array representing the vector to multiply. Returns: A numpy array representing the product of the adjacency list and th

(
    graph: list[list[int | None]], vector: np.ndarray
)

Source from the content-addressed store, hash-verified

131
132
133def multiply_matrix_vector(
134 graph: list[list[int | None]], vector: np.ndarray
135) -> np.ndarray:
136 """Performs multiplication of a graph's adjacency list representation with a vector.
137
138 Args:
139 graph: The adjacency list of the graph.
140 vector: A 1D numpy array representing the vector to multiply.
141
142 Returns:
143 A numpy array representing the product of the adjacency list and the vector.
144
145 Raises:
146 ValueError: If the vector's length does not match the number of nodes in the
147 graph.
148
149 >>> multiply_matrix_vector([[1, 2], [0, 2], [0, 1]], np.array([1, 1, 1]))
150 array([2., 2., 2.])
151 >>> multiply_matrix_vector([[1, 2], [0, 2], [0, 1]], np.array([0, 1, 0]))
152 array([1., 0., 1.])
153 """
154 num_nodes: int = len(graph)
155 if vector.shape[0] != num_nodes:
156 raise ValueError("Vector length must match the number of nodes in the graph.")
157
158 result: np.ndarray = np.zeros(num_nodes)
159 for node_index, neighbors in enumerate(graph):
160 for neighbor_index in neighbors:
161 result[node_index] += vector[neighbor_index]
162 return result
163
164
165def find_lanczos_eigenvectors(

Callers 1

lanczos_iterationFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected