inversion_ideas.operators.get_diagonal#
- inversion_ideas.operators.get_diagonal(operator)#
Extract diagonal of a linear operator.
Extracts the main diagonal of a square linear operator. If the operator is a dense or sparse array with a
diagonalmethod, the method will be used. ForLinearOperator`s or any other operator that doesn't implement the ``diagonal`method, the diagonal will be computed byNprojections using the unit vectors of the standard basis of \(\mathcal{R}^N\).Important
Extracting the diagonal of a
LinearOperatorrequires computing multiple dot products. This can be quite expensive for large operators.- Parameters:
- operator(n, n) array, sparse array or LinearOperator
Square linear operator from which the diagonal will be extracted.
- Returns:
- diagonal(n,) array
1D array containing the diagonal of the linear operator.
Notes
Consider a \(N \times N\) linear operator \(A\), and let \(\{ e_i \}_1^N\) be the standard basis for \(\mathcal{R}^N\). The \(i\)-th diagonal element of \(A\) (\(a_{ii}\)) can be obtained as:
\[a_{ii} = e_i^T A e_i\]Examples
>>> import numpy as np >>> from scipy.sparse.linalg import aslinearoperator
Build a linear operator from a matrix, and extract its diagonal:
>>> a_matrix = np.array( ... [ ... [1., 2., 3.], ... [4., 5., 6.], ... [7., 8., 9.], ... ] ... ) >>> linop = aslinearoperator(a_matrix) >>> get_diagonal(linop) array([1., 5., 9.])