o
    Ö­jÀ+  ã                   @   s´   d Z dgZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ e	je
jejejdœZe	je
jejejdœZdddœZG dd„ dƒZ				ddd„ZdS )a�  
Python wrapper for PROPACK
--------------------------

PROPACK is a collection of Fortran routines for iterative computation
of partial SVDs of large matrices or linear operators.

Based on BSD licensed pypropack project:
  http://github.com/jakevdp/pypropack
  Author: Jake Vanderplas <vanderplas@astro.washington.edu>

PROPACK source is BSD licensed, and available at
  http://soi.stanford.edu/~rmunk/PROPACK/
Ú_svdpé    N)Úaslinearoperator)ÚLinAlgErroré   )Ú	_spropack)Ú	_dpropack)Ú	_cpropack)Ú	_zpropack)ÚfÚdÚFÚDÚLÚS)ÚLMÚSMc                   @   s8   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd
S )Ú_AProdzŒ
    Wrapper class for linear operator

    The call signature of the __call__ method matches the callback of
    the PROPACK routines.
    c                 C   s6   zt |ƒ| _W d S  ty   t t |¡ƒ| _Y d S w ©N)r   ÚAÚ	TypeErrorÚnpÚasarray)Úselfr   © r   úV/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/sparse/linalg/_svdp.pyÚ__init__9   s
   ÿz_AProd.__init__c                 C   s8   |dkr| j  |¡|d d …< d S | j  |¡|d d …< d S )NÚn)r   ÚmatvecÚrmatvec)r   ÚtransaÚmr   ÚxÚyÚsparmÚiparmr   r   r   Ú__call__?   s   z_AProd.__call__c                 C   s   | j jS r   )r   Úshape©r   r   r   r   r&   E   s   z_AProd.shapec              	   C   s:   z| j jW S  ty   | j  t | j jd ¡¡j Y S w )Nr   )r   ÚdtypeÚAttributeErrorr   r   Úzerosr&   r'   r   r   r   r(   I   s
   
 ÿz_AProd.dtypeN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   Úpropertyr&   r(   r   r   r   r   r   2   s    
r   r   TFçü©ñÒMb`?c           +      C   sÞ  |du rt dƒ‚| ¡ }|dvrt dƒ‚|s|dkrt dƒ‚t| ƒ}|jj}z
t| }t| }W n( tyX   t 	tj
d|d�¡rHt t¡j}nt t¡j}t| }t| }Y nw |j\}}|d	k si|t||ƒkrmt d
ƒ‚|du rud| }|du r{d}t|d	 |d	 |ƒ}||k r”t d|› d|› d�ƒ‚|r˜dnd}|rždnd}tj||d	 fd|d�}tj||fd|d�}|du rã|j|d�|dd…df< t 	tj
d|d�¡râ|dd…df  d|j|d� 7  < nz
||dd…df< W n t yû   t d|› �ƒ‚w |
du �r
t t |¡j¡}
|du �rt |¡jd }|�rItj|
|||f| ¡ d�}|du �r0|| }|t|| ||ƒk�r?t dƒ‚|dk �rHt dƒ‚ntj|
||f| ¡ d�}tjtt|ƒƒtt|ƒƒfdd�}d}|�sn|�r—|| d|  d| |  d td| | d|  d |t||ƒ ƒ } d | }!n!|| d|  d!| |  d t|| d| d ƒ } d!| d	 }!tj
| | ¡ d�}"tj
|!tjd�}#tj
d	| ¡ d�}$tj
d	tjd�}%| ¡ �ròtj
|| d"|  |d�}&|"|&|#f}'n|"|#f}'|�r|t| |||||||||||	g|'¢|‘|‘|$‘|%‘R Ž \}}(})}}*n||||||||||	g	|'¢|‘|‘|$‘|%‘R Ž \}}(})}}*|*dk�rFtd#|*› d$�ƒ‚|*dk �rVtd%|› d&|› d'�ƒ‚|dd…d|…f |(|dd…d|…f  ¡ j|)fS )(ax  
    Compute the singular value decomposition of a linear operator using PROPACK

    Parameters
    ----------
    A : array_like, sparse matrix, or LinearOperator
        Operator for which SVD will be computed.  If `A` is a LinearOperator
        object, it must define both ``matvec`` and ``rmatvec`` methods.
    k : int
        Number of singular values/vectors to compute
    which : {"LM", "SM"}
        Which singular triplets to compute:
        - 'LM': compute triplets corresponding to the `k` largest singular
                values
        - 'SM': compute triplets corresponding to the `k` smallest singular
                values
        `which='SM'` requires `irl_mode=True`.  Computes largest singular
        values by default.
    irl_mode : bool, optional
        If `True`, then compute SVD using IRL (implicitly restarted Lanczos)
        mode.  Default is `True`.
    kmax : int, optional
        Maximal number of iterations / maximal dimension of the Krylov
        subspace. Default is ``10 * k``.
    compute_u : bool, optional
        If `True` (default) then compute left singular vectors, `u`.
    compute_v : bool, optional
        If `True` (default) then compute right singular vectors, `v`.
    tol : float, optional
        The desired relative accuracy for computed singular values.
        If not specified, it will be set based on machine precision.
    v0 : array_like, optional
        Starting vector for iterations: must be of length ``A.shape[0]``.
        If not specified, PROPACK will generate a starting vector.
    full_output : bool, optional
        If `True`, then return sigma_bound.  Default is `False`.
    delta : float, optional
        Level of orthogonality to maintain between Lanczos vectors.
        Default is set based on machine precision.
    eta : float, optional
        Orthogonality cutoff.  During reorthogonalization, vectors with
        component larger than `eta` along the Lanczos vector will be purged.
        Default is set based on machine precision.
    anorm : float, optional
        Estimate of ``||A||``.  Default is ``0``.
    cgs : bool, optional
        If `True`, reorthogonalization is done using classical Gram-Schmidt.
        If `False` (default), it is done using modified Gram-Schmidt.
    elr : bool, optional
        If `True` (default), then extended local orthogonality is enforced
        when obtaining singular vectors.
    min_relgap : float, optional
        The smallest relative gap allowed between any shift in IRL mode.
        Default is ``0.001``.  Accessed only if ``irl_mode=True``.
    shifts : int, optional
        Number of shifts per restart in IRL mode.  Default is determined
        to satisfy ``k <= min(kmax-shifts, m, n)``.  Must be
        >= 0, but choosing 0 might lead to performance degradation.
        Accessed only if ``irl_mode=True``.
    maxiter : int, optional
        Maximum number of restarts in IRL mode.  Default is ``1000``.
        Accessed only if ``irl_mode=True``.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator state. When `rng` is None, a new
        `numpy.random.Generator` is created using entropy from the
        operating system. Types other than `numpy.random.Generator` are
        passed to `numpy.random.default_rng` to instantiate a ``Generator``.

    Returns
    -------
    u : ndarray
        The `k` largest (``which="LM"``) or smallest (``which="SM"``) left
        singular vectors, ``shape == (A.shape[0], 3)``, returned only if
        ``compute_u=True``.
    sigma : ndarray
        The top `k` singular values, ``shape == (k,)``
    vt : ndarray
        The `k` largest (``which="LM"``) or smallest (``which="SM"``) right
        singular vectors, ``shape == (3, A.shape[1])``, returned only if
        ``compute_v=True``.
    sigma_bound : ndarray
        the error bounds on the singular values sigma, returned only if
        ``full_output=True``.

    Nz:`rng` must be a normalized numpy.random.Generator instance>   r   r   z#`which` must be either 'LM' or 'SM'r   z#`which`='SM' requires irl_mode=Truer   )r(   r   z.k must be positive and not greater than m or né
   iè  z3kmax must be greater than or equal to k, but kmax (z) < k (ú)r"   r   r   )Úorderr(   )Úsizey              ð?zv0 must be of length g      è?z0shifts must satisfy k <= min(kmax-shifts, m, n)!zshifts must be >= 0!Úié   é	   é   é   é   é   é   é    z#An invariant subspace of dimension z was found.zk=z0 singular triplets did not converge within kmax=z iterations)Ú
ValueErrorÚupperr   r(   ÚcharÚ_lansvd_irl_dictÚ_lansvd_dictÚKeyErrorr   ÚiscomplexobjÚemptyÚcomplexÚfloatr&   Úminr*   ÚuniformÚsqrtÚfinfoÚepsÚarrayÚlowerÚintÚboolÚmaxÚint32ÚisupperÚ_which_converterr   ÚconjÚT)+r   ÚkÚwhichÚirl_modeÚkmaxÚ	compute_uÚ	compute_vÚv0Úfull_outputÚtolÚdeltaÚetaÚanormÚcgsÚelrÚ
min_relgapÚshiftsÚmaxiterÚrngÚaprodÚtypÚ
lansvd_irlÚlansvdr    r   ÚjobuÚjobvÚuÚvÚdoptionÚioptionÚ	blocksizeÚlworkÚliworkÚworkÚiworkÚdparmr$   ÚzworkÚworksÚsigmaÚbndÚinfor   r   r   r   Q   sì   Yù
	ÿÿÿ$€ÿ



ÿ"	 þ
6

þþþýýýÿÿÿÿÿ

ÿ
ÿÿ2)r   TNTTNFr   NNr   FTr0   NNN)r.   Ú__all__Únumpyr   Úscipy.sparse.linalgr   Úscipy.linalgr   Ú_propackr   r   r   r	   ÚslansvdÚdlansvdÚclansvdÚzlansvdrB   Úslansvd_irlÚdlansvd_irlÚclansvd_irlÚzlansvd_irlrA   rT   r   r   r   r   r   r   Ú<module>   s8    ü	üþý