
    (phA                         S r SSKrSSKJrJrJrJrJrJrJ	r	J
r
  SSKJrJr  SSKJrJr  SSKJr  / SQr  SS jrSS jrS rSS jrSS
S	SS.S jjrS rg)zSVD decomposition functions.    N)zerosr_diagdotarccosarcsinwhereclip   )LinAlgError_datacopied)get_lapack_funcs_compute_lwork)_asarray_validated)svdsvdvalsdiagsvdorthsubspace_angles
null_spaceTFgesddc                    [        XS9n[        UR                  5      S:w  a  [        S5      eUR                  u  pxUR                  S:X  Ga  [        [        R                  " SUR                  S95      u  pn[        R                  " USU
R                  S9nU(       au  [        R                  " XgU4U	R                  S9n[        R                  " U5      US'   [        R                  " XhU4UR                  S9n[        R                  " U5      US'   OC[        R                  " XgS4U	R                  S9n[        R                  " USU4UR                  S9nU(       a  XU4$ U$ U=(       d    [        X`5      n[        U[        5      (       d  [        S	5      eUS
;  a  SU S3n[        U5      eU(       a  Xx:  a  Xx4OX4u  nnU(       aH  UU-  [        R                  " [        R                   5      R"                  :  a  [        SU SU S35      eOc[#        UU-  UU-  5      n[#        UU-  UU-  5      [        R                  " [        R                   5      R"                  :  a  [        SU S35      eXUS-   4n[%        UU4SS9u  nn['        UUR                  S   UR                  S   X!S9nU" XbUXS9u  pnnUS:  a  [)        S5      eUS:  a  [        SU* -  5      eU(       a  XU4$ U$ )a
  
Singular Value Decomposition.

Factorizes the matrix `a` into two unitary matrices ``U`` and ``Vh``, and
a 1-D array ``s`` of singular values (real, non-negative) such that
``a == U @ S @ Vh``, where ``S`` is a suitably shaped matrix of zeros with
main diagonal ``s``.

Parameters
----------
a : (M, N) array_like
    Matrix to decompose.
full_matrices : bool, optional
    If True (default), `U` and `Vh` are of shape ``(M, M)``, ``(N, N)``.
    If False, the shapes are ``(M, K)`` and ``(K, N)``, where
    ``K = min(M, N)``.
compute_uv : bool, optional
    Whether to compute also ``U`` and ``Vh`` in addition to ``s``.
    Default is True.
overwrite_a : bool, optional
    Whether to overwrite `a`; may improve performance.
    Default is False.
check_finite : bool, optional
    Whether to check that the input matrix contains only finite numbers.
    Disabling may give a performance gain, but may result in problems
    (crashes, non-termination) if the inputs do contain infinities or NaNs.
lapack_driver : {'gesdd', 'gesvd'}, optional
    Whether to use the more efficient divide-and-conquer approach
    (``'gesdd'``) or general rectangular approach (``'gesvd'``)
    to compute the SVD. MATLAB and Octave use the ``'gesvd'`` approach.
    Default is ``'gesdd'``.

Returns
-------
U : ndarray
    Unitary matrix having left singular vectors as columns.
    Of shape ``(M, M)`` or ``(M, K)``, depending on `full_matrices`.
s : ndarray
    The singular values, sorted in non-increasing order.
    Of shape (K,), with ``K = min(M, N)``.
Vh : ndarray
    Unitary matrix having right singular vectors as rows.
    Of shape ``(N, N)`` or ``(K, N)`` depending on `full_matrices`.

For ``compute_uv=False``, only ``s`` is returned.

Raises
------
LinAlgError
    If SVD computation does not converge.

See Also
--------
svdvals : Compute singular values of a matrix.
diagsvd : Construct the Sigma matrix, given the vector s.

Examples
--------
>>> import numpy as np
>>> from scipy import linalg
>>> rng = np.random.default_rng()
>>> m, n = 9, 6
>>> a = rng.standard_normal((m, n)) + 1.j*rng.standard_normal((m, n))
>>> U, s, Vh = linalg.svd(a)
>>> U.shape,  s.shape, Vh.shape
((9, 9), (6,), (6, 6))

Reconstruct the original matrix from the decomposition:

>>> sigma = np.zeros((m, n))
>>> for i in range(min(m, n)):
...     sigma[i, i] = s[i]
>>> a1 = np.dot(U, np.dot(sigma, Vh))
>>> np.allclose(a, a1)
True

Alternatively, use ``full_matrices=False`` (notice that the shape of
``U`` is then ``(m, n)`` instead of ``(m, m)``):

>>> U, s, Vh = linalg.svd(a, full_matrices=False)
>>> U.shape, s.shape, Vh.shape
((9, 6), (6,), (6, 6))
>>> S = np.diag(s)
>>> np.allclose(a, np.dot(U, np.dot(S, Vh)))
True

>>> s2 = linalg.svd(a, compute_uv=False)
>>> np.allclose(s, s2)
True

check_finite   zexpected matrixr   dtype)r   )shaper   .zlapack_driver must be a string)r   gesvdz/lapack_driver must be "gesdd" or "gesvd", not ""zIndexing a matrix size z x zL would incur integer overflow in LAPACK. Try using numpy.linalg.svd instead.zIndexing a matrix of z[ elements would incur an in integer overflow in LAPACK. Try using numpy.linalg.svd instead._lwork	preferred)ilp64r   )
compute_uvfull_matrices)r$   lworkr%   overwrite_azSVD did not convergez0illegal value in %dth argument of internal gesdd)r   lenr   
ValueErrorsizer   npeyer   
empty_likeidentityr   
isinstancestr	TypeErroriinfoint32maxr   r   r   )ar%   r$   r'   r   lapack_drivera1mnu0s0v0suvmessagemax_mnmin_mnszfuncsgesXdgesXd_lworkr&   infos                           K/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_decomp_svd.pyr   r      s   z 
A	9B
288}*++88DA 
ww!|23
MM"D9bAbhh?A[[^AcFbAbhh?A[[^AcFbAbhh?AbAbhh?A7NH5+b"4KmS))899..CM?RST!!#$5!qff}rxx1555 #:6(#fX NH "H I I 6
 QZV,B1v:q6z*RXXbhh-?-C-CC #8 =H "H I I H45E *%"kJE; ;RXXa[&0OE "5(5PMA!T ax011axK 5! " 	"Qw    c                     [        U SUUS9$ )a  
Compute singular values of a matrix.

Parameters
----------
a : (M, N) array_like
    Matrix to decompose.
overwrite_a : bool, optional
    Whether to overwrite `a`; may improve performance.
    Default is False.
check_finite : bool, optional
    Whether to check that the input matrix contains only finite numbers.
    Disabling may give a performance gain, but may result in problems
    (crashes, non-termination) if the inputs do contain infinities or NaNs.

Returns
-------
s : (min(M, N),) ndarray
    The singular values, sorted in decreasing order.

Raises
------
LinAlgError
    If SVD computation does not converge.

See Also
--------
svd : Compute the full singular value decomposition of a matrix.
diagsvd : Construct the Sigma matrix, given the vector s.

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import svdvals
>>> m = np.array([[1.0, 0.0],
...               [2.0, 3.0],
...               [1.0, 1.0],
...               [0.0, 2.0],
...               [1.0, 0.0]])
>>> svdvals(m)
array([ 4.28091555,  1.63516424])

We can verify the maximum singular value of `m` by computing the maximum
length of `m.dot(u)` over all the unit vectors `u` in the (x,y) plane.
We approximate "all" the unit vectors with a large sample. Because
of linearity, we only need the unit vectors with angles in [0, pi].

>>> t = np.linspace(0, np.pi, 2000)
>>> u = np.array([np.cos(t), np.sin(t)])
>>> np.linalg.norm(m.dot(u), axis=0).max()
4.2809152422538475

`p` is a projection matrix with rank 1. With exact arithmetic,
its singular values would be [1, 0, 0, 0].

>>> v = np.array([0.1, 0.3, 0.9, 0.3])
>>> p = np.outer(v, v)
>>> svdvals(p)
array([  1.00000000e+00,   2.02021698e-17,   1.56692500e-17,
         8.15115104e-34])

The singular values of an orthogonal matrix are all 1. Here, we
create a random orthogonal matrix by using the `rvs()` method of
`scipy.stats.ortho_group`.

>>> from scipy.stats import ortho_group
>>> orth = ortho_group.rvs(4)
>>> svdvals(orth)
array([ 1.,  1.,  1.,  1.])

r   )r$   r'   r   )r   )r5   r'   r   s      rH   r   r      s    P qQK(* *rI   c                     [        U 5      nUR                  R                  n[        U 5      nXQ:X  a$  [        R
                  " U[        XU-
  4US945      $ XR:X  a  [        U[        X-
  U4US94   $ [        S5      e)a'  
Construct the sigma matrix in SVD from singular values and size M, N.

Parameters
----------
s : (M,) or (N,) array_like
    Singular values
M : int
    Size of the matrix whose singular values are `s`.
N : int
    Size of the matrix whose singular values are `s`.

Returns
-------
S : (M, N) ndarray
    The S-matrix in the singular value decomposition

See Also
--------
svd : Singular value decomposition of a matrix
svdvals : Compute singular values of a matrix.

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import diagsvd
>>> vals = np.array([1, 2, 3])  # The array representing the computed svd
>>> diagsvd(vals, 3, 4)
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0]])
>>> diagsvd(vals, 4, 3)
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3],
       [0, 0, 0]])

r   zLength of s must be M or N.)	r   r   charr(   r+   hstackr   r   r)   )r=   MNparttypMorNs         rH   r   r      sx    N 7D
**//Cq6Dyyy$qa%j <=>>	$quaj4455677rI   c                 H   [        U SS9u  p#nUR                  S   UR                  S   peUc6  [        R                  " UR                  5      R
                  [        XV5      -  n[        R                  " USS9U-  n[        R                  " X7:  [        S9nUSS2SU24   n	U	$ )	a  
Construct an orthonormal basis for the range of A using SVD

Parameters
----------
A : (M, N) array_like
    Input array
rcond : float, optional
    Relative condition number. Singular values ``s`` smaller than
    ``rcond * max(s)`` are considered zero.
    Default: floating point eps * max(M,N).

Returns
-------
Q : (M, K) ndarray
    Orthonormal basis for the range of A.
    K = effective rank of A, as determined by rcond

See Also
--------
svd : Singular value decomposition of a matrix
null_space : Matrix null space

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import orth
>>> A = np.array([[2, 0, 0], [0, 5, 0]])  # rank 2 array
>>> orth(A)
array([[0., 1.],
       [1., 0.]])
>>> orth(A.T)
array([[0., 1.],
       [1., 0.],
       [0., 0.]])

F)r%   r   r   N        initialr   )
r   r   r+   finfor   epsr4   amaxsumint)
Arcondr>   r=   vhrN   rO   tolnumQs
             rH   r   r   0  s    L 1E*HA"771:rxx{q}!%%A	1
''!R
 5
(C
&&
$C	!TcT'
AHrI   )r'   r   r6   c                z   [        U SUX4S9u  pVnUR                  S   UR                  S   pUc6  [        R                  " UR                  5      R
                  [        X5      -  n[        R                  " USS9U-  n
[        R                  " Xj:  [        S9nX{S2SS24   R                  R                  5       nU$ )	a  
Construct an orthonormal basis for the null space of A using SVD

Parameters
----------
A : (M, N) array_like
    Input array
rcond : float, optional
    Relative condition number. Singular values ``s`` smaller than
    ``rcond * max(s)`` are considered zero.
    Default: floating point eps * max(M,N).
overwrite_a : bool, optional
    Whether to overwrite `a`; may improve performance.
    Default is False.
check_finite : bool, optional
    Whether to check that the input matrix contains only finite numbers.
    Disabling may give a performance gain, but may result in problems
    (crashes, non-termination) if the inputs do contain infinities or NaNs.
lapack_driver : {'gesdd', 'gesvd'}, optional
    Whether to use the more efficient divide-and-conquer approach
    (``'gesdd'``) or general rectangular approach (``'gesvd'``)
    to compute the SVD. MATLAB and Octave use the ``'gesvd'`` approach.
    Default is ``'gesdd'``.

Returns
-------
Z : (N, K) ndarray
    Orthonormal basis for the null space of A.
    K = dimension of effective null space, as determined by rcond

See Also
--------
svd : Singular value decomposition of a matrix
orth : Matrix range

Examples
--------
1-D null space:

>>> import numpy as np
>>> from scipy.linalg import null_space
>>> A = np.array([[1, 1], [1, 1]])
>>> ns = null_space(A)
>>> ns * np.copysign(1, ns[0,0])  # Remove the sign ambiguity of the vector
array([[ 0.70710678],
       [-0.70710678]])

2-D null space:

>>> from numpy.random import default_rng
>>> rng = default_rng()
>>> B = rng.random((3, 5))
>>> Z = null_space(B)
>>> Z.shape
(5, 2)
>>> np.allclose(B.dot(Z), 0)
True

The basis vectors are orthonormal (up to rounding error):

>>> Z.T.dot(Z)
array([[  1.00000000e+00,   6.92087741e-17],
       [  6.92087741e-17,   1.00000000e+00]])

T)r%   r'   r   r6   r   r   NrT   rU   r   )r   r   r+   rW   r   rX   r4   rY   rZ   r[   Tconj)r\   r]   r'   r   r6   r>   r=   r^   rN   rO   r_   r`   ra   s                rH   r   r   `  s    F 1Dk ,KHA"771:rxx{q}!%%A	1
''!R
 5
(C
&&
$C
46
AHrI   c                 ~   [        U SS9n [        U R                  5      S:w  a  [        SU R                   35      e[	        U 5      nA [        USS9n[        UR                  5      S:w  a  [        SUR                   35      e[        U5      [        U5      :w  a+  [        SUR                  S    SUR                  S    35      e[	        U5      nA[        UR                  R                  5       U5      n[        U5      nUR                  S   UR                  S   :  a  U[        X$5      -
  nO&U[        X4R                  R                  5       5      -
  nAAAUS-  S	:  nUR                  5       (       a  [        [        [        USS
9SS5      5      nOSn[        Xg[        [        USSS2   SS5      5      5      nU$ )a  
Compute the subspace angles between two matrices.

Parameters
----------
A : (M, N) array_like
    The first input array.
B : (M, K) array_like
    The second input array.

Returns
-------
angles : ndarray, shape (min(N, K),)
    The subspace angles between the column spaces of `A` and `B` in
    descending order.

See Also
--------
orth
svd

Notes
-----
This computes the subspace angles according to the formula
provided in [1]_. For equivalence with MATLAB and Octave behavior,
use ``angles[0]``.

.. versionadded:: 1.0

References
----------
.. [1] Knyazev A, Argentati M (2002) Principal Angles between Subspaces
       in an A-Based Scalar Product: Algorithms and Perturbation
       Estimates. SIAM J. Sci. Comput. 23:2008-2040.

Examples
--------
An Hadamard matrix, which has orthogonal columns, so we expect that
the suspace angle to be :math:`\frac{\pi}{2}`:

>>> import numpy as np
>>> from scipy.linalg import hadamard, subspace_angles
>>> rng = np.random.default_rng()
>>> H = hadamard(4)
>>> print(H)
[[ 1  1  1  1]
 [ 1 -1  1 -1]
 [ 1  1 -1 -1]
 [ 1 -1 -1  1]]
>>> np.rad2deg(subspace_angles(H[:, :2], H[:, 2:]))
array([ 90.,  90.])

And the subspace angle of a matrix to itself should be zero:

>>> subspace_angles(H[:, :2], H[:, :2]) <= 2 * np.finfo(float).eps
array([ True,  True], dtype=bool)

The angles between non-orthogonal subspaces are in between these extremes:

>>> x = rng.standard_normal((4, 3))
>>> np.rad2deg(subspace_angles(x[:, :2], x[:, [2]]))
array([ 55.832])  # random
Tr   r   zexpected 2D array, got shape z/A and B must have the same number of rows, got r   z and r   g      ?)r'   g      g      ?rT   N)r   r(   r   r)   r   r   rc   rd   r   anyr   r
   r	   r   )	r\   BQAQBQA_H_QBsigmamask	mu_arcsinthetas	            rH   r   r     s   F 	140A
177|q8	BCC	aB	140A
177|q8	BCC
1vRJHHQK=aggaj\; < 	<	aB	 "$$))+r"GGE 
xx{bhhqk!R!!R)**
B A:Dxxzz4t <c2FG		
 $6$uTrT{C*D#EFELrI   )TTFTr   )FT)N)__doc__numpyr+   r   r   r   r   r   r   r	   r
   _miscr   r   lapackr   r   _decompr   __all__r   r   r   r   r   r    rI   rH   <module>rw      sa    "  C C C , 4 '
P =B)0`FI*X/8h-`KU$K\hrI   