STAG Python  1.2.1
Spectral Toolkit of Algorithms for Graphs
Loading...
Searching...
No Matches
stag.spectrum Namespace Reference

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.
 

Function Documentation

◆ compute_eigensystem()

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 whichparameter which takes a string and should take one of the following values.

  • SM will return the eigenvalues with smallest magnitude
  • LM will return the eigenvalues with largest magnitude

The following example demonstrates how to compute the 3 largest eigenvectors and eigenvalues of a cycle graph.

import stag.graph
lap = myGraph.normalised_laplacian()
eigenvalues, eigenvectors = stag.spectrum.compute_eigensystem(
lap, 3, 'LM')
Definition: graph.py:1
Graph cycle_graph(n)
Construct a cycle graph on n vertices.
Definition: graph.py:641
Definition: spectrum.py:1
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.
Definition: spectrum.py:42
Parameters
matthe matrix on which to operate
numthe number of eigenvalues and eigenvectors to compute
which(optional) a string indicating which eigenvectors to calculate
Returns
a tuple containing the computed eigenvalues and eigenvectors

◆ compute_eigenvalues()

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 magnitude
  • LM will return the eigenvalues with largest magnitude

If you would like to calculate the eigenvectors and eigenvalues together, then you should instead use stag.spectrum.compute_eigensystem.

Parameters
matthe matrix on which to operate
numthe number of eigenvalues to compute
which(optional) a string indicating which eigenvalues to calculate
Returns
a numpy array containing the computed eigenvalues

◆ compute_eigenvectors()

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 magnitude
  • LM will return the vectors corresponding to eigenvalues with largest magnitude

If you would like to calculate the eigenvectors and eigenvalues together, then you should instead use stag.spectrum.compute_eigensystem.

Parameters
matthe matrix on which to operate
numthe number of eigenvectors to compute
which(optional) a string indicating which eigenvectors to calculate
Returns
a numpy array containing the computed eigenvectors as columns

◆ power_method()

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\).

Parameters
matthe 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.
Returns
the vector \(v_t\) computed by repeated multiplication with \(M\).

◆ rayleigh_quotient()

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}. \]

Parameters
mata sparse matrix \(M \in \mathbb{R}^{n \times n}\).
veca vector \(v \in \mathbb{R}^n\).
Returns
the Rayleigh quotient \(R(M, v)\).