![]() |
STAG Python
1.2.1
Spectral Toolkit of Algorithms for Graphs
|
Functions | |
Tuple[np.ndarray, np.ndarray] | compute_eigensystem (scipy.sparse.spmatrix mat, int num, str which='SM') |
Compute the eigenvalues and eigenvectors of a given matrix. | |
np.ndarray | compute_eigenvalues (scipy.sparse.spmatrix mat, int num, str which='SM') |
Compute the eigenvalues of a given matrix. | |
np.ndarray | compute_eigenvectors (scipy.sparse.spmatrix mat, int num, str which='SM') |
Compute the eigenvectors of a given matrix. | |
np.ndarray | power_method (scipy.sparse.spmatrix mat, int num_iterations=None, np.ndarray initial_vector=None) |
Apply the power method to compute the dominant eigenvector of a matrix. | |
float | rayleigh_quotient (scipy.sparse.spmatrix mat, np.ndarray vec) |
Compute the Rayleigh quotient of the given vector and matrix. | |
Tuple[np.ndarray, np.ndarray] stag.spectrum.compute_eigensystem | ( | scipy.sparse.spmatrix | mat, |
int | num, | ||
str | which = 'SM' |
||
) |
Compute the eigenvalues and eigenvectors of a given matrix.
By default, this will compute the eigenvalues of smallest magnitude. This default can be overridden by the which
parameter which takes a string and should take one of the following values.
SM
will return the eigenvalues with smallest magnitudeLM
will return the eigenvalues with largest magnitudeThe following example demonstrates how to compute the 3 largest eigenvectors and eigenvalues of a cycle graph.
mat | the matrix on which to operate |
num | the number of eigenvalues and eigenvectors to compute |
which | (optional) a string indicating which eigenvectors to calculate |
np.ndarray stag.spectrum.compute_eigenvalues | ( | scipy.sparse.spmatrix | mat, |
int | num, | ||
str | which = 'SM' |
||
) |
Compute the eigenvalues of a given matrix.
By default, this will compute the eigenvalues of smallest magnitude. This default can be overridden by the which parameter which takes a string and should be one of the following.
SM
will return the eigenvalues with smallest magnitudeLM
will return the eigenvalues with largest magnitudeIf you would like to calculate the eigenvectors and eigenvalues together, then you should instead use stag.spectrum.compute_eigensystem.
mat | the matrix on which to operate |
num | the number of eigenvalues to compute |
which | (optional) a string indicating which eigenvalues to calculate |
np.ndarray stag.spectrum.compute_eigenvectors | ( | scipy.sparse.spmatrix | mat, |
int | num, | ||
str | which = 'SM' |
||
) |
Compute the eigenvectors of a given matrix.
By default, this will compute the eigenvectors corresponding to the eigenvalues of smallest magnitude. This default can be overridden by the which parameter which takes a string and should be one of the following.
SM
will return the vectors corresponding to eigenvalues with smallest magnitudeLM
will return the vectors corresponding to eigenvalues with largest magnitudeIf you would like to calculate the eigenvectors and eigenvalues together, then you should instead use stag.spectrum.compute_eigensystem.
mat | the matrix on which to operate |
num | the number of eigenvectors to compute |
which | (optional) a string indicating which eigenvectors to calculate |
np.ndarray stag.spectrum.power_method | ( | scipy.sparse.spmatrix | mat, |
int | num_iterations = None , |
||
np.ndarray | initial_vector = None |
||
) |
Apply the power method to compute the dominant eigenvector of a matrix.
Given a matrix \(M\), an initial vector \(v_0\), and a number of iterations \(t\), the power method calculates the vector
\[ v_t = M^t v_0, \]
which is close to the eigenvector of \(M\) with largest eigenvalue.
The running time of the power method is \(O(t \cdot \mathrm{nnz}(M))\), where \(\mathrm{nnz}(M)\) is the number of non-zero elements in the matrix \(M\).
mat | the matrix \(M\) on which to operate. |
num_iterations | (optional) the number of iterations of the power method to apply. It this argument is omitted, \(O(\log(n))\) iterations are used which results in a vector whose Rayleigh quotient is a \((1 - \epsilon)\) approximation of the dominant eigenvalue. |
initial_vector | (optional) the initial vector to use for the power iteration. If this argument is omitted, a random unit vector will be used. |
float stag.spectrum.rayleigh_quotient | ( | scipy.sparse.spmatrix | mat, |
np.ndarray | vec | ||
) |
Compute the Rayleigh quotient of the given vector and matrix.
Given a matrix \(M\), the Rayleigh quotient of vector \(v\) is
\[ R(M, v) = \frac{v^\top M v}{v^\top v}. \]
mat | a sparse matrix \(M \in \mathbb{R}^{n \times n}\). |
vec | a vector \(v \in \mathbb{R}^n\). |