U
    f»{jZ  ã                   @   s„   d Z ddlZddlZe d¡dd„ ƒZe d¡dd„ ƒZejfdd„Zdd
d„Z	e d¡dd„ ƒZ
ddd„Zddd„Zddd„ZdS )a¬  Utility functions to speed up linear algebraic operations.

In general, things like np.dot and linalg.svd should be used directly
because they are smart about checking for bad values. However, in cases where
things are done repeatedly (e.g., thousands of times on tiny matrices), the
overhead can become problematic from a performance standpoint. Examples:

- Optimization routines:
  - Dipole fitting
  - Sparse solving
  - cHPI fitting
- Inverse computation
  - Beamformers (LCMV/DICS)
  - eLORETA minimum norm

Significant performance gains can be achieved by ensuring that inputs
are Fortran contiguous because that's what LAPACK requires. Without this,
inputs will be memcopied.
é    Nc                 C   s"   ddl m} | |t d| ¡f¡S ©Nr   ©Úlinalg)Úscipyr   Zget_blas_funcsÚnpÚempty)ÚdtypeÚnamesr   © r
   úO/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/utils/linalg.pyÚ_get_blas_funcs!   s    r   c                 C   s:   ddl m} | tjtjfks t‚t d| ¡}| ||f¡S r   )r   r   r   Úfloat64Ú
complex128ÚAssertionErrorr   Úget_lapack_funcs)r   r	   r   Úxr
   r
   r   Ú_get_lapack_funcs'   s    r   c                 C   sx   ddl m} z
|j}W n tk
r0   |j}Y nX t|dƒ\}}|j|f| ždddœŽ}|j|f| ždddœŽ}||fS )zESet up SVD calculations on identical-shape float64/complex128 arrays.r   r   )Úgesdd_lworkÚgesvd_lworkTF)Ú
compute_uvÚfull_matrices)r   r   Z_decomp_svdÚAttributeErrorZ
decomp_svdr   Z_compute_lwork)Úshaper   r   Zdsr   r   Z	sdd_lworkZ	svd_lworkr
   r
   r   Ú
_svd_lwork2   s4    
 ÿÿÿ ÿÿÿ ÿr   Fc           	      C   sˆ   t | jdƒ\}}|| d|d ddd�\}}}}|dkrT|| d|d d|d�\}}}}|dkrhtj d¡‚|dk r~td|  ƒ‚|||fS )	zBMimic scipy.linalg.svd, avoid lwork and get_lapack_funcs overhead.)ÚgesddÚgesvdTr   F)r   Úlworkr   Úoverwrite_aé   zSVD did not convergez1illegal value in %d-th argument of internal gesdd)r   r   r   r   ÚLinAlgErrorÚ
ValueError)	r   r   r   r   r   ÚuÚsÚvÚinfor
   r
   r   Ú_repeated_svdB   s(     ÿ ÿ ÿÿr%   c                 C   sT   ddl m} t d| ¡}| tjkr(d}n| tjks6t‚d}| |f|f¡\}||fS )Nr   r   ZsyevdZheevd)r   r   r   r   r   r   r   r   )r   r   r   ÚdriverZevrr
   r
   r   Ú_get_evdX   s    
r'   Tc           
      C   s†   ddl m} ddlm} |r(|| |d�} t| jƒ\}}|| d|d�\}}}	|	dkrZ||fS |	dk rvtd|	 |f ƒ‚n|d|	 ƒ‚d	S )
aŒ  Efficient wrapper for eigh.

    Parameters
    ----------
    a : ndarray, shape (n_components, n_components)
        The symmetric array operate on.
    overwrite_a : bool
        If True, the contents of a can be overwritten for efficiency.
    check_finite : bool
        If True, check that all elements are finite.

    Returns
    -------
    w : ndarray, shape (n_components,)
        The N eigenvalues, in ascending order, each repeated according to
        its multiplicity.
    v : ndarray, shape (n_components, n_components)
        The normalized eigenvector corresponding to the eigenvalue ``w[i]``
        is the column ``v[:, i]``.
    r   )r   )Ú_asarray_validated)Úcheck_finiter   )Úlowerr   z+illegal value in argument %d of internal %szƒinternal fortran routine failed to converge: %i off-diagonal elements of an intermediate tridiagonal form did not converge to zero.N)Zscipy.linalgr   Zscipy._lib._utilr(   r'   r   r    )
Úar   r)   r   r(   Zevdr&   Úwr#   r$   r
   r
   r   Úeighe   s    ÿýr-   çH¯¼šò×z>c                 C   s   t | |rdnd|dd�S )aV  Compute the sqrt of a positive, semi-definite matrix (or its inverse).

    Parameters
    ----------
    A : ndarray, shape (..., n, n)
        The array to take the square root of.
    rcond : float
        The relative condition number used during reconstruction.
    inv : bool
        If True, compute the inverse of the square root rather than the
        square root itself.

    Returns
    -------
    A_sqrt : ndarray, shape (..., n, n)
        The (possibly inverted) square root of A.
    s : ndarray, shape (..., n)
        The original square root singular values (not inverted).
    ç      à¿ç      à?T)Úreturn_s)Ú_sym_mat_pow)ÚAÚrcondÚinvr
   r
   r   Ú	sqrtm_sym�   s    r6   c           
      C   sÔ   |dkst ‚tj | ¡\}}|ddd…f | }|| k ¡ sFtdƒ‚|dk rTtjnd|||k< |rntj|d< |dkr„tj||d	� |dk r”d
| n|}t ||dtj	dd…f  | 
dd¡ ¡ ¡}	|rÐ|	|f}	|	S )z=Exponentiate Hermitian matrices with optional rank reduction.)éÿÿÿÿr0   r/   .r7   Nz$Matrix is not positive semi-definiter   ).r   )r/   r0   )Úoutg      ð?éþÿÿÿ)r   r   r   r-   Úallr    ÚinfÚsqrtÚmatmulZnewaxisZswapaxesZconj)
r3   Úpowerr4   Zreduce_rankr1   r"   r!   ÚlimitZuse_sr8   r
   r
   r   r2   ¥   s    
,r2   )F)FT)r.   F)r.   FF)Ú__doc__Ú	functoolsÚnumpyr   Ú	lru_cacher   r   r   r   r%   r'   r-   r6   r2   r
   r
   r
   r   Ú<module>   s   





(
