|
| 1 | +# This should remained pinned to version 1.2 and not updated like other |
| 2 | +# externals. |
| 3 | +"""Copyright (c) 2001-2002 Enthought, Inc. 2003-2019, SciPy Developers. |
| 4 | +All rights reserved. |
| 5 | +
|
| 6 | +Redistribution and use in source and binary forms, with or without |
| 7 | +modification, are permitted provided that the following conditions |
| 8 | +are met: |
| 9 | +
|
| 10 | +1. Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | +2. Redistributions in binary form must reproduce the above |
| 14 | + copyright notice, this list of conditions and the following |
| 15 | + disclaimer in the documentation and/or other materials provided |
| 16 | + with the distribution. |
| 17 | +
|
| 18 | +3. Neither the name of the copyright holder nor the names of its |
| 19 | + contributors may be used to endorse or promote products derived |
| 20 | + from this software without specific prior written permission. |
| 21 | +
|
| 22 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | +""" |
| 34 | + |
| 35 | +import numpy as np |
| 36 | +import scipy.linalg.decomp as decomp |
| 37 | + |
| 38 | + |
| 39 | +def pinvh(a, cond=None, rcond=None, lower=True, return_rank=False, |
| 40 | + check_finite=True): |
| 41 | + """ |
| 42 | + Compute the (Moore-Penrose) pseudo-inverse of a Hermitian matrix. |
| 43 | +
|
| 44 | + Copied in from scipy==1.2.2, in order to preserve the default choice of the |
| 45 | + `cond` and `above_cutoff` values which determine which values of the matrix |
| 46 | + inversion lie below threshold and are so set to zero. Changes in scipy 1.3 |
| 47 | + resulted in a smaller default threshold and thus slower convergence of |
| 48 | + dependent algorithms in some cases (see Sklearn github issue #14055). |
| 49 | +
|
| 50 | + Calculate a generalized inverse of a Hermitian or real symmetric matrix |
| 51 | + using its eigenvalue decomposition and including all eigenvalues with |
| 52 | + 'large' absolute value. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + a : (N, N) array_like |
| 57 | + Real symmetric or complex hermetian matrix to be pseudo-inverted |
| 58 | + cond, rcond : float or None |
| 59 | + Cutoff for 'small' eigenvalues. |
| 60 | + Singular values smaller than rcond * largest_eigenvalue are considered |
| 61 | + zero. |
| 62 | +
|
| 63 | + If None or -1, suitable machine precision is used. |
| 64 | + lower : bool, optional |
| 65 | + Whether the pertinent array data is taken from the lower or upper |
| 66 | + triangle of a. (Default: lower) |
| 67 | + return_rank : bool, optional |
| 68 | + if True, return the effective rank of the matrix |
| 69 | + check_finite : bool, optional |
| 70 | + Whether to check that the input matrix contains only finite numbers. |
| 71 | + Disabling may give a performance gain, but may result in problems |
| 72 | + (crashes, non-termination) if the inputs do contain infinities or NaNs. |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------- |
| 76 | + B : (N, N) ndarray |
| 77 | + The pseudo-inverse of matrix `a`. |
| 78 | + rank : int |
| 79 | + The effective rank of the matrix. Returned if return_rank == True |
| 80 | +
|
| 81 | + Raises |
| 82 | + ------ |
| 83 | + LinAlgError |
| 84 | + If eigenvalue does not converge |
| 85 | +
|
| 86 | + Examples |
| 87 | + -------- |
| 88 | + >>> from scipy.linalg import pinvh |
| 89 | + >>> a = np.random.randn(9, 6) |
| 90 | + >>> a = np.dot(a, a.T) |
| 91 | + >>> B = pinvh(a) |
| 92 | + >>> np.allclose(a, np.dot(a, np.dot(B, a))) |
| 93 | + True |
| 94 | + >>> np.allclose(B, np.dot(B, np.dot(a, B))) |
| 95 | + True |
| 96 | +
|
| 97 | + """ |
| 98 | + a = decomp._asarray_validated(a, check_finite=check_finite) |
| 99 | + s, u = decomp.eigh(a, lower=lower, check_finite=False) |
| 100 | + |
| 101 | + if rcond is not None: |
| 102 | + cond = rcond |
| 103 | + if cond in [None, -1]: |
| 104 | + t = u.dtype.char.lower() |
| 105 | + factor = {'f': 1E3, 'd': 1E6} |
| 106 | + cond = factor[t] * np.finfo(t).eps |
| 107 | + |
| 108 | + # For Hermitian matrices, singular values equal abs(eigenvalues) |
| 109 | + above_cutoff = (abs(s) > cond * np.max(abs(s))) |
| 110 | + psigma_diag = 1.0 / s[above_cutoff] |
| 111 | + u = u[:, above_cutoff] |
| 112 | + |
| 113 | + B = np.dot(u * psigma_diag, np.conjugate(u).T) |
| 114 | + |
| 115 | + if return_rank: |
| 116 | + return B, len(psigma_diag) |
| 117 | + else: |
| 118 | + return B |
0 commit comments