o
    Uݢg
                     @  s`   d Z ddlmZ ddlZddlZddlmZ dddZ	dd Z
d	d
 Z					ddddZdS )a9  Copyright 2013 Michael Kane and Bryan Lewis.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    )annotationsNFc                 C  s   |j dksJ t| r2|r t||   d d df S | t|  d d df S |r@t|  |	 S t| |	 S )N   r   )
ndimspissparse
csr_matrixdot	transposetoarraynpasarrayravel)Axt r   g/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/analysis/irlb.pymult   s   
$$r   c                 C  s   |  |}| t|| S )zOrthogonalize a vector or matrix Y against the columns of the matrix X.

    This function requires that the column dimension of Y is less than X and
    that Y and X have the same number of rows.
    )r   r   )YXZdotYr   r   r   orthog)   s   
r   c                 C  s.   dt tj }| |krd|  S td dS )N   g      ?z9Ill-conditioning encountered, result accuracy may be poorg        )r   finfofloatepswarningswarn)r   Zeps2r   r   r   invcheck4   s
   
r   -C6?2   r   
np.ndarrayninttolr   maxitcenternp.ndarray | Nonescalereturn3tuple[np.ndarray, np.ndarray, np.ndarray, int, int]c                  C  s  t j|}|durt|t jstd|dur"t|t js"td|}| jd }	| jd }t|	|dk r9tdt|d d	| t| jf}
d}d}d}|}d}t 	||
f}t 	|	|
f}t 	|df}t 	|
|
f}|
||dddf< |dddf t j| |dddf< ||k r|dkr|}|dd|f }|dur|| }t| ||dd|f< |d7 }|dur|dd|f t || |dd|f< |dkrt|dd|f |ddd|f |dd|f< t j|dd|f }t|}||dd|f  |dd|f< ||
k rCt| |dd|f d
d}|d7 }|dur*|| }|durIt |dd|f | }|durE|| }|| }|||dd|f   }t||ddd|d f }t j|}t|}|| }||
d k r4||dd|d f< ||||f< ||||d f< |dd|d f }|dur|| }t| ||dd|d f< |d7 }|dur|dd|d f t || |dd|d f< |dd|d f ||dd|f   |dd|d f< t|dd|d f |ddd|d f |dd|d f< t j|dd|d f }t|}||dd|d f  |dd|d f< n||||f< |d7 }||
k st j|}||d |
d ddf  }|dk rc|d d }n
t|d d |f}tt |d| || k }||k rt|| |}t||
d	 }nnu|ddd|
f |d  ddd|f |ddd|f< ||dd|f< t 	|
|
f}t|D ]}|d | |||f< q|d| |d||f< |ddd|
f |d ddd|f |ddd|f< |d7 }||k s|ddd|
f |d ddd|f }|ddd|
f |d  ddd|f }||d d| |||fS )a   Estimate a few of the largest singular values and corresponding singular.

    vectors of matrix using the implicitly restarted Lanczos bidiagonalization
    method of Baglama and Reichel, see:

    Augmented Implicitly Restarted Lanczos Bidiagonalization Methods,
    J. Baglama and L. Reichel, SIAM J. Sci. Comput. 2005

    Keyword Arguments:
    tol   -- An estimation tolerance. Smaller means more accurate estimates.
    maxit -- Maximum number of Lanczos iterations allowed.

    Given an input matrix A of dimension j * k, and an input desired number
    of singular values n, the function returns a tuple X with five entries:

    X[0] A j * nu matrix of estimated left singular vectors.
    X[1] A vector of length nu of estimated singular values.
    X[2] A k * nu matrix of estimated right singular vectors.
    X[3] The number of Lanczos iterations run.
    X[4] The number of matrix-vector products run.

    The algorithm estimates the truncated singular value decomposition:
    A.dot(X[2]) = X[0]*X[1].
    Nzcenter must be a numpy.ndarrayzscale must be a numpy.ndarrayr   r   r   z&The input matrix must be at least 2x2.      T)r   )r   randomRandomState
isinstancendarray	TypeErrorshapemin
ValueErrorzerosrandnlinalgnormr   r   r   r   sumsvdmaxabsr	   range) r   r!   r#   r$   r%   r'   random_statersnumZm_bZmproditjksmaxVWFBZVJsZsinvsubfnZfninvZVJp1SRconvlUr   r   r   irlb=   s   !

(
(2 





08>*
2

B>.f2rQ   )F)r   r   NNr   )r   r    r!   r"   r#   r   r$   r"   r%   r&   r'   r&   r(   r)   )__doc__
__future__r   r   numpyr   scipy.sparsesparser   r   r   r   rQ   r   r   r   r   <module>   s    
