
    Ug                        d Z ddlmc mZ ddlZ ed          Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd;dZd;dZd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d;d,Z0d;d-Z1d. Z2d/ Z3d0 Z4d1 Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:d7 Z;d8 Z<d9 Z=d: Z>dS )<z0
Direct wrappers for Fortran `id_dist` backend.
    Nznonzero return codec                     t          j        |           } | j        j        r|                     d          } nt          j        |           } | S )z6
    Same as np.asfortranarray, but ensure a copy
    Forder)npasarrayflagsf_contiguouscopyasfortranarray)As    b/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/scipy/linalg/_interpolative_backend.py_asfortranarray_copyr   (   sH     	
1Aw !FFFa  H    c                 *    t          j        |           S )a  
    Generate standard uniform pseudorandom numbers via a very efficient lagged
    Fibonacci method.

    :param n:
        Number of pseudorandom numbers to generate.
    :type n: int

    :return:
        Pseudorandom numbers.
    :rtype: :class:`numpy.ndarray`
    )_idid_srand)ns    r   r   r   8   s     <??r   c                 V    t          j        |           } t          j        |            dS )z
    Initialize seed values for :func:`id_srand` (any appropriately random
    numbers will do).

    :param t:
        Array of 55 seed values.
    :type t: :class:`numpy.ndarray`
    N)r   r   r   	id_srandi)ts    r   r   r   H   s*     	!AM!r   c                  ,    t          j                     dS )z5
    Reset seed values to their original values.
    N)r   	id_srando r   r   r   r   U   s     MOOOOOr   c                 .    t          j        | ||          S )a|  
    Transform real vector via a composition of Rokhlin's random transform,
    random subselection, and an FFT.

    In contrast to :func:`idd_sfrm`, this routine works best when the length of
    the transformed vector is the power-of-two integer output by
    :func:`idd_frmi`, or when the length is not specified but instead
    determined a posteriori from the output. The returned transformed vector is
    randomly permuted.

    :param n:
        Greatest power-of-two integer satisfying `n <= x.size` as obtained from
        :func:`idd_frmi`; `n` is also the length of the output vector.
    :type n: int
    :param w:
        Initialization array constructed by :func:`idd_frmi`.
    :type w: :class:`numpy.ndarray`
    :param x:
        Vector to be transformed.
    :type x: :class:`numpy.ndarray`

    :return:
        Transformed vector.
    :rtype: :class:`numpy.ndarray`
    )r   idd_frmr   wxs      r   r   r   `       4 ;q!Qr   c                 0    t          j        | |||          S )a  
    Transform real vector via a composition of Rokhlin's random transform,
    random subselection, and an FFT.

    In contrast to :func:`idd_frm`, this routine works best when the length of
    the transformed vector is known a priori.

    :param l:
        Length of transformed vector, satisfying `l <= n`.
    :type l: int
    :param n:
        Greatest power-of-two integer satisfying `n <= x.size` as obtained from
        :func:`idd_sfrmi`.
    :type n: int
    :param w:
        Initialization array constructed by :func:`idd_sfrmi`.
    :type w: :class:`numpy.ndarray`
    :param x:
        Vector to be transformed.
    :type x: :class:`numpy.ndarray`

    :return:
        Transformed vector.
    :rtype: :class:`numpy.ndarray`
    )r   idd_sfrmlr   r   r   s       r   r"   r"   }       4 <1a###r   c                 *    t          j        |           S )aC  
    Initialize data for :func:`idd_frm`.

    :param m:
        Length of vector to be transformed.
    :type m: int

    :return:
        Greatest power-of-two integer `n` satisfying `n <= m`.
    :rtype: int
    :return:
        Initialization array to be used by :func:`idd_frm`.
    :rtype: :class:`numpy.ndarray`
    )r   idd_frmims    r   r'   r'           <??r   c                 ,    t          j        | |          S )a  
    Initialize data for :func:`idd_sfrm`.

    :param l:
        Length of output transformed vector.
    :type l: int
    :param m:
        Length of the vector to be transformed.
    :type m: int

    :return:
        Greatest power-of-two integer `n` satisfying `n <= m`.
    :rtype: int
    :return:
        Initialization array to be used by :func:`idd_sfrm`.
    :rtype: :class:`numpy.ndarray`
    )r   	idd_sfrmir$   r)   s     r   r,   r,          $ =Ar   c                     t          |          }t          j        | |          \  }}}|j        d         }|j                                        d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a real matrix to a specified relative precision.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
       Nr   r   )r   r   iddp_idshapeTravelreshapeepsr   kidxrnormsr   projs          r   r1   r1      z    * 	QA[a((NAsF	
A399;;x1Q3x ((!QqS(==Dc4<r   c                     t          |           } t          j        | |          \  }}| j        d         }| j                                        d|||z
  z                               |||z
  fd          }||fS )aQ  
    Compute ID of a real matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r0   Nr   r   )r   r   iddr_idr2   r3   r4   r5   r   r8   r9   r:   r   r;   s         r   r>   r>      v    $ 	QA+a##KC	
A399;;x1Q3x ((!QqS(==D9r   c                     t          j        |           } |j        dk    rt          j        | ||          S | ddt          j        |          f         S )as  
    Reconstruct matrix from real ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    r   N)r   r   sizer   idd_reconidargsortBr9   r;   s      r   rC   rC      O    $ 	!Ay1}}q#t,,,BJsOO#$$r   c                 ,    t          j        | |          S )a6  
    Reconstruct interpolation matrix from real ID.

    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Interpolation matrix.
    :rtype: :class:`numpy.ndarray`
    )r   idd_reconintr9   r;   s     r   rI   rI          C&&&r   c                 V    t          j        |           } t          j        | ||          S )aN  
    Reconstruct skeleton matrix from real ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   idd_copycolsr   r8   r9   s      r   rM   rM   %  )    $ 	!AAq#&&&r   c                 |    t          j        |           } t          j        | ||          \  }}}}|rt          |||fS )a  
    Convert real ID to SVD.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   
idd_id2svd_RETCODE_ERRORrF   r9   r;   UVSiers          r   rQ   rQ   ?  H    0 	!A>!S$//LAq!S
 a7Nr      c                 <    t          j        | ||||          \  }}|S )a  
    Estimate spectral norm of a real matrix by the randomized power method.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param its:
        Number of power method iterations.
    :type its: int

    :return:
        Spectral norm estimate.
    :rtype: float
    )r   	idd_snorm)r)   r   matvectmatvecitssnormvs          r   r[   r[   b  $    8 }Q7FC88HE1Lr   c           	      6    t          j        | ||||||          S )a0  
    Estimate spectral norm of the difference of two real matrices by the
    randomized power method.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the transpose of the first matrix to a vector, with
        call signature `y = matvect(x)`, where `x` and `y` are the input and
        output vectors, respectively.
    :type matvect: function
    :param matvect2:
        Function to apply the transpose of the second matrix to a vector, with
        call signature `y = matvect2(x)`, where `x` and `y` are the input and
        output vectors, respectively.
    :type matvect2: function
    :param matvec:
        Function to apply the first matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param matvec2:
        Function to apply the second matrix to a vector, with call signature
        `y = matvec2(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec2: function
    :param its:
        Number of power method iterations.
    :type its: int

    :return:
        Spectral norm estimate of matrix difference.
    :rtype: float
    )r   idd_diffsnorm)r)   r   r\   matvect2r]   matvec2r^   s          r   rc   rc     "    N Q7HfgsKKKr   c                 z    t          j        |           } t          j        | |          \  }}}}|rt          |||fS )a  
    Compute SVD of a real matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   iddr_svdrR   r   r8   rT   rU   rV   rW   s         r   rh   rh     F    * 	!A<1%%LAq!S
 a7Nr   c                 l   t          j        |          }|j        \  }}t          j        | |          \  }}}}}}	|	rt
          ||dz
  |||z  z   dz
                               ||fd          }
||dz
  |||z  z   dz
                               ||fd          }||dz
  ||z   dz
           }|
||fS )a  
    Compute SVD of a real matrix to a specified relative precision.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0   r   r   )r   r   r2   r   iddp_svdrR   r5   r7   r   r)   r   r8   iUiViSr   rW   rT   rU   rV   s                r   rl   rl         * 	!A7DAqLa00Ar2r1c
 	"Q$r!A#vax-  !Qs 33A	"Q$r!A#vax-  !Qs 33A	"Q$r!tAv+Aa7Nr   c                 @   t          j        |          }|j        \  }}t          |          \  }}t          j        |d|z  dz   z  |z   dz   d          }t          j        | |||          \  }}}|d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a real matrix to a specified relative precision using random
    sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
       r0   r   r   N)r   r   r2   r'   emptyr   iddp_aidr5   	r7   r   r)   r   n2r   r;   r8   r9   s	            r   ru   ru     s    , 	!A7DAqQKKEB8AqtaxL2%)555D<Q400LAsDAaC>!!1ac(#!66Dc4<r   c                     t          j        |          }|j        \  }}t          |          \  }}t          j        ||z  |dz   |dz   z  z   d          }t          j        | |||          \  }}|S )ae  
    Estimate rank of a real matrix to a specified relative precision using
    random sampling.

    The output rank is typically about 8 higher than the actual rank.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank estimate.
    :rtype: int
    r0   r   r   )r   r   r2   r'   rt   r   idd_estrankr7   r   r)   r   rw   r   rar8   s           r   ry   ry     sv    $ 	!A7DAqQKKEB	!B$!a%"q&))	5	5	5BOCAr**EArHr   c           
      l   t          j        |          }|j        \  }}t          j        |          \  }}t          j        t          t          ||          dz   d|z  d|z  z   dz   z  dt          ||          dz  z  z   d|z  dz   |dz   z            d          }t          j        | |||          \  }}}	}
}}|rt          ||dz
  |||z  z   dz
           
                    ||fd          }||	dz
  |	||z  z   dz
           
                    ||fd          }||
dz
  |
|z   dz
           }|||fS )a  
    Compute SVD of a real matrix to a specified relative precision using random
    sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0            rs   r   r   )r   r   r2   r   r'   rt   maxmin	iddp_asvdrR   r5   r7   r   r)   r   rw   winitr   r8   rn   ro   rp   rW   rT   rU   rV   s                  r   r   r   -  sb   , 	!A7DAqQIB
SAYY]QqS1Q3Y]+bQAo=qS1WrAv	  	 	 	 	A  M#q%;;Ar2r1c
 	"Q$r!A#vax-  !Qs 33A	"Q$r!A#vax-  !Qs 33A	"Q$r!tAv+Aa7Nr   c                    t          j        |dz   d|z  t          ||          dz   z  z   d          }t          j        | ||||          \  }}}}|dk    rt
          |d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a real matrix to a specified relative precision using random
    matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r0   rs   r   r   r   N)r   rt   r   r   iddp_ridrR   r5   )r7   r)   r   r\   r;   r8   r9   rW   s           r   r   r   W  s    < 8AEAaCQQ//s;;;DS!Q>>AsD#
axxAaC>!!1ac(#!66Dc4<r   c                 N    t          j        | |||          \  }}}|rt          |S )aQ  
    Estimate rank of a real matrix to a specified relative precision using
    random matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function

    :return:
        Rank estimate.
    :rtype: int
    )r   idd_findrankrR   )r7   r)   r   r\   r8   r{   rW   s          r   r   r   }  3    0 !#q!W55JAr3
 Hr   c                 6   t          j        | ||||          \  }}}}}	}
|
rt          |	|dz
  |||z  z   dz
                               ||fd          }|	|dz
  |||z  z   dz
                               ||fd          }|	|dz
  ||z   dz
           }|||fS )a  
    Compute SVD of a real matrix to a specified relative precision using random
    matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0   r   r   )r   	iddp_rsvdrR   r5   )r7   r)   r   r\   r]   r8   rn   ro   rp   r   rW   rT   rU   rV   s                 r   r   r         F  M#q!WfEEAr2r1c
 	"Q$r!A#vax-  !Qs 33A	"Q$r!A#vax-  !Qs 33A	"Q$r!tAv+Aa7Nr   c                    t          j        |           } | j        \  }}t          |||          }t	          j        | ||          \  }}||k    rt          j        |||z
  fdd          }n|                    |||z
  fd          }||fS )ag  
    Compute ID of a real matrix to a specified rank using random sampling.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    float64r   dtyper   r   )r   r   r2   	iddr_aidir   iddr_aidrt   r5   r   r8   r)   r   r   r9   r;   s          r   r   r     s    $ 	!A7DAq!QAQ1%%ICAvvxAaC	===||Q!HC|009r   c                 .    t          j        | ||          S )aO  
    Initialize array for :func:`iddr_aid`.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Initialization array to be used by :func:`iddr_aid`.
    :rtype: :class:`numpy.ndarray`
    )r   r   r)   r   r8   s      r   r   r         $ =Aq!!!r   c                 @   t          j        |           } | j        \  }}t          j        d|z  dz   |z  d|z  dz   |z  z   d|dz  z  z   dz   d          }t	          |||          }||d	|j        <   t          j        | ||          \  }}}}	|	d
k    rt          |||fS )a  
    Compute SVD of a real matrix to a specified rank using random sampling.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    rs            r   d   r   r   Nr   )	r   r   r2   rt   r   rB   r   	iddr_asvdrR   
r   r8   r)   r   r   w_rT   rU   rV   rW   s
             r   r   r     s    * 	!A7DAq
!A#(A1r1,r!Q$w6<CHHHA	1a		BAhrwhK=Aq))LAq!S
axxa7Nr   c                     t          j        | |||          \  }}|d|||z
  z                               |||z
  fd          }||fS )a  
    Compute ID of a real matrix to a specified rank using random matrix-vector
    multiplication.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    Nr   r   )r   iddr_ridr5   )r)   r   r\   r8   r9   r;   s         r   r   r   )  W    6 Q7A..ICAaC>!!1ac(#!66D9r   c                 `    t          j        | ||||          \  }}}}|dk    rt          |||fS )a  
    Compute SVD of a real matrix to a specified rank using random matrix-vector
    multiplication.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matvect:
        Function to apply the matrix transpose to a vector, with call signature
        `y = matvect(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvect: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r   )r   	iddr_rsvdrR   )	r)   r   r\   r]   r8   rT   rU   rV   rW   s	            r   r   r   M  s>    F =Aw::LAq!S
axxa7Nr   c                 .    t          j        | ||          S )a  
    Transform complex vector via a composition of Rokhlin's random transform,
    random subselection, and an FFT.

    In contrast to :func:`idz_sfrm`, this routine works best when the length of
    the transformed vector is the power-of-two integer output by
    :func:`idz_frmi`, or when the length is not specified but instead
    determined a posteriori from the output. The returned transformed vector is
    randomly permuted.

    :param n:
        Greatest power-of-two integer satisfying `n <= x.size` as obtained from
        :func:`idz_frmi`; `n` is also the length of the output vector.
    :type n: int
    :param w:
        Initialization array constructed by :func:`idz_frmi`.
    :type w: :class:`numpy.ndarray`
    :param x:
        Vector to be transformed.
    :type x: :class:`numpy.ndarray`

    :return:
        Transformed vector.
    :rtype: :class:`numpy.ndarray`
    )r   idz_frmr   s      r   r   r   z  r    r   c                 0    t          j        | |||          S )a  
    Transform complex vector via a composition of Rokhlin's random transform,
    random subselection, and an FFT.

    In contrast to :func:`idz_frm`, this routine works best when the length of
    the transformed vector is known a priori.

    :param l:
        Length of transformed vector, satisfying `l <= n`.
    :type l: int
    :param n:
        Greatest power-of-two integer satisfying `n <= x.size` as obtained from
        :func:`idz_sfrmi`.
    :type n: int
    :param w:
        Initialization array constructed by :func:`idd_sfrmi`.
    :type w: :class:`numpy.ndarray`
    :param x:
        Vector to be transformed.
    :type x: :class:`numpy.ndarray`

    :return:
        Transformed vector.
    :rtype: :class:`numpy.ndarray`
    )r   idz_sfrmr#   s       r   r   r     r%   r   c                 *    t          j        |           S )aC  
    Initialize data for :func:`idz_frm`.

    :param m:
        Length of vector to be transformed.
    :type m: int

    :return:
        Greatest power-of-two integer `n` satisfying `n <= m`.
    :rtype: int
    :return:
        Initialization array to be used by :func:`idz_frm`.
    :rtype: :class:`numpy.ndarray`
    )r   idz_frmir(   s    r   r   r     r*   r   c                 ,    t          j        | |          S )a  
    Initialize data for :func:`idz_sfrm`.

    :param l:
        Length of output transformed vector.
    :type l: int
    :param m:
        Length of the vector to be transformed.
    :type m: int

    :return:
        Greatest power-of-two integer `n` satisfying `n <= m`.
    :rtype: int
    :return:
        Initialization array to be used by :func:`idz_sfrm`.
    :rtype: :class:`numpy.ndarray`
    )r   	idz_sfrmir-   s     r   r   r     r.   r   c                     t          |          }t          j        | |          \  }}}|j        d         }|j                                        d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a complex matrix to a specified relative precision.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r0   Nr   r   )r   r   idzp_idr2   r3   r4   r5   r6   s          r   r   r     r<   r   c                     t          |           } t          j        | |          \  }}| j        d         }| j                                        d|||z
  z                               |||z
  fd          }||fS )aT  
    Compute ID of a complex matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r0   Nr   r   )r   r   idzr_idr2   r3   r4   r5   r?   s         r   r   r     r@   r   c                     t          j        |           } |j        dk    rt          j        | ||          S | ddt          j        |          f         S )av  
    Reconstruct matrix from complex ID.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Reconstructed matrix.
    :rtype: :class:`numpy.ndarray`
    r   N)r   r   rB   r   idz_reconidrD   rE   s      r   r   r     rG   r   c                 ,    t          j        | |          S )a9  
    Reconstruct interpolation matrix from complex ID.

    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Interpolation matrix.
    :rtype: :class:`numpy.ndarray`
    )r   idz_reconintrJ   s     r   r   r   -  rK   r   c                 V    t          j        |           } t          j        | ||          S )aQ  
    Reconstruct skeleton matrix from complex ID.

    :param A:
        Original matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`

    :return:
        Skeleton matrix.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   idz_copycolsrN   s      r   r   r   ?  rO   r   c                 |    t          j        |           } t          j        | ||          \  }}}}|rt          |||fS )a  
    Convert complex ID to SVD.

    :param B:
        Skeleton matrix.
    :type B: :class:`numpy.ndarray`
    :param idx:
        Column index array.
    :type idx: :class:`numpy.ndarray`
    :param proj:
        Interpolation coefficients.
    :type proj: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   
idz_id2svdrR   rS   s          r   r   r   Y  rX   r   c                 <    t          j        | ||||          \  }}|S )a  
    Estimate spectral norm of a complex matrix by the randomized power method.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param its:
        Number of power method iterations.
    :type its: int

    :return:
        Spectral norm estimate.
    :rtype: float
    )r   	idz_snorm)r)   r   matvecar]   r^   r_   r`   s          r   r   r   |  ra   r   c           	      6    t          j        | ||||||          S )a/  
    Estimate spectral norm of the difference of two complex matrices by the
    randomized power method.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the adjoint of the first matrix to a vector, with
        call signature `y = matveca(x)`, where `x` and `y` are the input and
        output vectors, respectively.
    :type matveca: function
    :param matveca2:
        Function to apply the adjoint of the second matrix to a vector, with
        call signature `y = matveca2(x)`, where `x` and `y` are the input and
        output vectors, respectively.
    :type matveca2: function
    :param matvec:
        Function to apply the first matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param matvec2:
        Function to apply the second matrix to a vector, with call signature
        `y = matvec2(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec2: function
    :param its:
        Number of power method iterations.
    :type its: int

    :return:
        Spectral norm estimate of matrix difference.
    :rtype: float
    )r   idz_diffsnorm)r)   r   r   matveca2r]   re   r^   s          r   r   r     rf   r   c                 z    t          j        |           } t          j        | |          \  }}}}|rt          |||fS )a  
    Compute SVD of a complex matrix to a specified rank.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   idzr_svdrR   ri   s         r   r   r     rj   r   c                 l   t          j        |          }|j        \  }}t          j        | |          \  }}}}}}	|	rt
          ||dz
  |||z  z   dz
                               ||fd          }
||dz
  |||z  z   dz
                               ||fd          }||dz
  ||z   dz
           }|
||fS )a  
    Compute SVD of a complex matrix to a specified relative precision.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0   r   r   )r   r   r2   r   idzp_svdrR   r5   rm   s                r   r   r     rq   r   c                 B   t          j        |          }|j        \  }}t          |          \  }}t          j        |d|z  dz   z  |z   dz   dd          }t          j        | |||          \  }}}|d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a complex matrix to a specified relative precision using
    random sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    rs   r0   
complex128r   r   Nr   )r   r   r2   r   rt   r   idzp_aidr5   rv   s	            r   r   r   
  s    , 	!A7DAqQKKEB8AqtaxL2%)SIIID<Q400LAsDAaC>!!1ac(#!66Dc4<r   c                     t          j        |          }|j        \  }}t          |          \  }}t          j        ||z  |dz   |dz   z  z   dd          }t          j        | |||          \  }}|S )ah  
    Estimate rank of a complex matrix to a specified relative precision using
    random sampling.

    The output rank is typically about 8 higher than the actual rank.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Rank estimate.
    :rtype: int
    r0   r   r   r   )r   r   r2   r   rt   r   idz_estrankrz   s           r   r   r   )  sx    $ 	!A7DAqQKKEB	!B$!a%"q&))S	I	I	IBOCAr**EArHr   c           
         t          j        |          }|j        \  }}t          j        |          \  }}t          j        t          t          ||          dz   d|z  d|z  z   dz   z  dt          ||          dz  z  z   d|z  dz   |dz   z            t           j        d          }t          j	        | |||          \  }}}	}
}}|rt          ||dz
  |||z  z   dz
                               ||fd	          }||	dz
  |	||z  z   dz
                               ||fd	          }||
dz
  |
|z   dz
           }|||fS )
a  
    Compute SVD of a complex matrix to a specified relative precision using
    random sampling.

    :param eps:
        Relative precision.
    :type eps: float
    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0   r}   r~         rs   r   r   r   )r   r   r2   r   r   rt   r   r   r   	idzp_asvdrR   r5   r   s                  r   r   r   G  sf   , 	!A7DAqQIB
SAYY]QqS1Q3Y^,qQA~=qS1WrAv	  	 m3	( 	( 	(A  M#q%;;Ar2r1c
 	"Q$r!A#vax-  !Qs 33A	"Q$r!A#vax-  !Qs 33A	"Q$r!tAv+Aa7Nr   c                 (   t          j        |dz   d|z  t          ||          dz   z  z   t           j        d          }t	          j        | ||||          \  }}}}|rt          |d|||z
  z                               |||z
  fd          }|||fS )a  
    Compute ID of a complex matrix to a specified relative precision using
    random matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function

    :return:
        Rank of ID.
    :rtype: int
    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r0   rs   r   r   Nr   )r   rt   r   r   r   idzp_ridrR   r5   )r7   r)   r   r   r;   r8   r9   rW   s           r   r   r   q  s    < 8	A!SAYY]##m3( ( (D S!Q>>AsD#
 AaC>!!1ac(#!66Dc4<r   c                 N    t          j        | |||          \  }}}|rt          |S )aR  
    Estimate rank of a complex matrix to a specified relative precision using
    random matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function

    :return:
        Rank estimate.
    :rtype: int
    )r   idz_findrankrR   )r7   r)   r   r   r8   r{   rW   s          r   r   r     r   r   c                 6   t          j        | ||||          \  }}}}}	}
|
rt          |	|dz
  |||z  z   dz
                               ||fd          }|	|dz
  |||z  z   dz
                               ||fd          }|	|dz
  ||z   dz
           }|||fS )a  
    Compute SVD of a complex matrix to a specified relative precision using
    random matrix-vector multiplication.

    :param eps:
        Relative precision.
    :type eps: float
    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    r0   r   r   )r   	idzp_rsvdrR   r5   )r7   r)   r   r   r]   r8   rn   ro   rp   r   rW   rT   rU   rV   s                 r   r   r     r   r   c                    t          j        |           } | j        \  }}t          |||          }t	          j        | ||          \  }}||k    rt          j        |||z
  fdd          }n|                    |||z
  fd          }||fS )aj  
    Compute ID of a complex matrix to a specified rank using random sampling.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    r   r   r   r   )r   r   r2   	idzr_aidir   idzr_aidrt   r5   r   s          r   r   r     s    $ 	!A7DAq!QAQ1%%ICAvvxAaCC@@@||Q!HC|009r   c                 .    t          j        | ||          S )aO  
    Initialize array for :func:`idzr_aid`.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Initialization array to be used by :func:`idzr_aid`.
    :rtype: :class:`numpy.ndarray`
    )r   r   r   s      r   r   r     r   r   c                 F   t          j        |           } | j        \  }}t          j        d|z  dz   |z  d|z  dz   |z  z   d|dz  z  z   d|z  z   dz   dd	
          }t	          |||          }||d|j        <   t          j        | ||          \  }}}}	|	rt          |||fS )a  
    Compute SVD of a complex matrix to a specified rank using random sampling.

    :param A:
        Matrix.
    :type A: :class:`numpy.ndarray`
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    rs      r   r   r   
   Z   r   r   r   N)	r   r   r2   rt   r   rB   r   	idzr_asvdrR   r   s
             r   r   r   !  s    * 	!A7DAq
	
1r1!b!|#a1f,r!t3b8#	' 	' 	'A 
1a		BAhrwhK=Aq))LAq!S
 a7Nr   c                     t          j        | |||          \  }}|d|||z
  z                               |||z
  fd          }||fS )a  
    Compute ID of a complex matrix to a specified rank using random
    matrix-vector multiplication.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function
    :param k:
        Rank of ID.
    :type k: int

    :return:
        Column index array.
    :rtype: :class:`numpy.ndarray`
    :return:
        Interpolation coefficients.
    :rtype: :class:`numpy.ndarray`
    Nr   r   )r   idzr_ridr5   )r)   r   r   r8   r9   r;   s         r   r   r   G  r   r   c                 X    t          j        | ||||          \  }}}}|rt          |||fS )a  
    Compute SVD of a complex matrix to a specified rank using random
    matrix-vector multiplication.

    :param m:
        Matrix row dimension.
    :type m: int
    :param n:
        Matrix column dimension.
    :type n: int
    :param matveca:
        Function to apply the matrix adjoint to a vector, with call signature
        `y = matveca(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matveca: function
    :param matvec:
        Function to apply the matrix to a vector, with call signature
        `y = matvec(x)`, where `x` and `y` are the input and output vectors,
        respectively.
    :type matvec: function
    :param k:
        Rank of SVD.
    :type k: int

    :return:
        Left singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Right singular vectors.
    :rtype: :class:`numpy.ndarray`
    :return:
        Singular values.
    :rtype: :class:`numpy.ndarray`
    )r   	idzr_rsvdrR   )	r)   r   r   r]   r8   rT   rU   rV   rW   s	            r   r   r   k  s=    F =Aw::LAq!S
 a7Nr   )rY   )?__doc__scipy.linalg._interpolativelinalg_interpolativer   numpyr   RuntimeErrorrR   r   r   r   r   r   r"   r'   r,   r1   r>   rC   rI   rM   rQ   r[   rc   rh   rl   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s  <  * ) ) ) ) ) ) ) )    344	 	 	    
 
 
       :$ $ $:  $  2  8  2% % %2' ' '$' ' '4  F   @'L 'L 'L 'L\  8  H  >  <# # #T# # #L  D) ) )`  :" " "2  H  H& & &Z     :$ $ $:  $  2  8  2% % %2' ' '$' ' '4  F   @'L 'L 'L 'L\  8  H  >  <# # #T% % %P  D) ) )`  :" " "2  L  H& & & & &r   