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

Function find_lanczos_eigenvectors

graphs/lanczos_eigenvectors.py:165–193  ·  view source on GitHub ↗

Computes the largest eigenvalues and their corresponding eigenvectors using the Lanczos method. Args: graph: The graph as a list of adjacency lists. num_eigenvectors: Number of largest eigenvalues and eigenvectors to compute. Returns: A tuple containing:

(
    graph: list[list[int | None]], num_eigenvectors: int
)

Source from the content-addressed store, hash-verified

163
164
165def find_lanczos_eigenvectors(
166 graph: list[list[int | None]], num_eigenvectors: int
167) -> tuple[np.ndarray, np.ndarray]:
168 """Computes the largest eigenvalues and their corresponding eigenvectors using the
169 Lanczos method.
170
171 Args:
172 graph: The graph as a list of adjacency lists.
173 num_eigenvectors: Number of largest eigenvalues and eigenvectors to compute.
174
175 Returns:
176 A tuple containing:
177 - eigenvalues: 1D array of the largest eigenvalues in descending order.
178 - eigenvectors: 2D array where each column is an eigenvector corresponding
179 to an eigenvalue.
180
181 Raises:
182 ValueError: If the graph format is invalid or num_eigenvectors is out of bounds.
183
184 >>> eigenvalues, eigenvectors = find_lanczos_eigenvectors(
185 ... [[1, 2], [0, 2], [0, 1]], 2
186 ... )
187 >>> len(eigenvalues) == 2 and eigenvectors.shape[1] == 2
188 True
189 """
190 validate_adjacency_list(graph)
191 tridiagonal_matrix, orthonormal_basis = lanczos_iteration(graph, num_eigenvectors)
192 eigenvalues, eigenvectors = np.linalg.eigh(tridiagonal_matrix)
193 return eigenvalues[::-1], np.dot(orthonormal_basis, eigenvectors[:, ::-1])
194
195
196def main() -> None:

Callers

nothing calls this directly

Calls 2

validate_adjacency_listFunction · 0.85
lanczos_iterationFunction · 0.85

Tested by

no test coverage detected