
    5[g                     ~   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. Z3d/ Z4d0 Z5d1 Z6d2 Z7d3 Z8d4 Z9d5 Z:d6 Z;d7 Z<d8 Z=d9 Z>y);z0
Direct wrappers for Fortran `id_dist` backend.
    Nznonzero return codec                     t        j                  |       } | j                  j                  r| j	                  d      } | S t        j
                  |       } | S )z6
    Same as np.asfortranarray, but ensure a copy
    Forder)npasarrayflagsf_contiguouscopyasfortranarray)As    ^/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/linalg/_interpolative_backend.py_asfortranarray_copyr   (   sL     	

1AwwFFF H 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                 X    t        j                  |       } t        j                  |        y)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MM!r   c                  ,    t        j                          y)z5
    Reset seed values to their original values.
    N)r   	id_srando r   r   r   r   U   s     MMOr   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_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                 2    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                  j                         d|||z
  z   j                  |||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      s    * 	QA[[a(NAsF	
A3399;x1Q3 ((!QqS(=Dc4<r   c                     t        |       } t        j                  | |      \  }}| j                  d   }| j                  j                         d|||z
  z   j                  |||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>      o    $ 	QA++a#KC	
A3399;x1Q3 ((!QqS(=D9r   c                     t        j                  |       } |j                  dkD  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      J    $ 	!Ayy1}q#t,,BJJsO#$$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                 Z    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   ?  B    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   rZ   rZ   b  #    8 }}Q7FC8HE1Lr   c           	      8    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   rb   rb     "    N Q7HfgsKKr   c                 |    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   rg   rg     @    * 	!A<<1%LAq!S
a7Nr   c                 J   t        j                  |      }|j                  \  }}t        j                  | |      \  }}}}}}	|	rt
        ||dz
  |||z  z   dz
   j                  ||fd      }
||dz
  |||z  z   dz
   j                  ||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   rk   rk         * 	!A77DAqLLa0Ar2r1c
	"Q$r!A#vax  !Qs 3A	"Q$r!A#vax  !Qs 3A	"Q$r!tAvAa7Nr   c                 6   t        j                  |      }|j                  \  }}t        |      \  }}t        j                  |d|z  dz   z  |z   dz   d      }t        j                  | |||      \  }}}|d|||z
  z   j                  |||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   rt   rt     s    , 	!A77DAqQKEB88AqtaxL2%)5D<<Q40LAsDAaC>!!1ac(#!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'   rs   r   idd_estrankr7   r   r)   r   rv   r   rar8   s           r   rx   rx     sq    $ 	!A77DAqQKEB	!B$!a%"q&))	5BOOCAr*EArHr   c           
      6   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
   j                  ||fd      }||	dz
  |	||z  z   dz
   j                  ||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            rr   r   r   )r   r   r2   r   r'   rs   maxmin	iddp_asvdrR   r5   r7   r   r)   r   rv   winitr   r8   rm   rn   ro   rW   rT   rU   rV   s                  r   r   r   -  sA   , 	!A77DAqQIB
SAY]QqS1Q3Y]+bQAo=qS1WrAv	 	A  MM#q%;Ar2r1c
	"Q$r!A#vax  !Qs 3A	"Q$r!A#vax  !Qs 3A	"Q$r!tAvAa7Nr   c                     t        j                  |dz   d|z  t        ||      dz   z  z   d      }t        j                  | ||||      \  }}}}|dk7  rt
        |d|||z
  z   j                  |||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   rr   r   r   r   N)r   rs   r   r   iddp_ridrR   r5   )r7   r)   r   r[   r;   r8   r9   rW   s           r   r   r   W  s    < 88AEAaCQQ//s;DS!Q>AsD#
axAaC>!!1ac(#!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   rz   rW   s          r   r   r   }  .    0 !!#q!W5JAr3
Hr   c                    t        j                  | ||||      \  }}}}}	}
|
rt        |	|dz
  |||z  z   dz
   j                  ||fd      }|	|dz
  |||z  z   dz
   j                  ||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   rm   rn   ro   r   rW   rT   rU   rV   s                 r   r   r         F  MM#q!WfEAr2r1c
	"Q$r!A#vax  !Qs 3A	"Q$r!A#vax  !Qs 3A	"Q$r!tAvAa7Nr   c                    t        j                  |       } | j                  \  }}t        |||      }t	        j
                  | ||      \  }}||k(  r!t        j                  |||z
  fdd      }||fS |j                  |||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_aidrs   r5   r   r8   r)   r   r   r9   r;   s          r   r   r     s    $ 	!A77DAq!QAQ1%ICAvxxAaC	= 9 ||Q!HC|09r   c                 0    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                 J   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
k7  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`
    rr            r~   d   r   r   Nr   )	r   r   r2   rs   r   rB   r   	iddr_asvdrR   
r   r8   r)   r   r   w_rT   rU   rV   rW   s
             r   r   r     s    * 	!A77DAq
!A#(A1r1,r!Q$w6<CHA	1a	BAhrwwK==Aq)LAq!S
axa7Nr   c                     t        j                  | |||      \  }}|d|||z
  z   j                  |||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   )  Q    6 Q7A.ICAaC>!!1ac(#!6D9r   c                 ^    t        j                  | ||||      \  }}}}|dk7  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
axa7Nr   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_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                 2    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                  j                         d|||z
  z   j                  |||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                  j                         d|||z
  z   j                  |||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kD  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                 Z    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   |  r`   r   c           	      8    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\   rd   r]   s          r   r   r     re   r   c                 |    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   rh   s         r   r   r     ri   r   c                 J   t        j                  |      }|j                  \  }}t        j                  | |      \  }}}}}}	|	rt
        ||dz
  |||z  z   dz
   j                  ||fd      }
||dz
  |||z  z   dz
   j                  ||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   rl   s                r   r   r     rp   r   c                 8   t        j                  |      }|j                  \  }}t        |      \  }}t        j                  |d|z  dz   z  |z   dz   dd      }t        j                  | |||      \  }}}|d|||z
  z   j                  |||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`
    rr   r0   
complex128r   r   Nr   )r   r   r2   r   rs   r   idzp_aidr5   ru   s	            r   r   r   
  s    , 	!A77DAqQKEB88AqtaxL2%)SID<<Q40LAsDAaC>!!1ac(#!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   rs   r   idz_estrankry   s           r   r   r   )  ss    $ 	!A77DAqQKEB	!B$!a%"q&))S	IBOOCAr*EArHr   c           
      T   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
   j                  ||fd	      }||	dz
  |	||z  z   dz
   j                  ||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}         rr   r   r   r   )r   r   r2   r   r   rs   r   r   r   	idzp_asvdrR   r5   r   s                  r   r   r   G  sG   , 	!A77DAqQIB
SAY]QqS1Q3Y^,qQA~=qS1WrAv	 mm3	(A  MM#q%;Ar2r1c
	"Q$r!A#vax  !Qs 3A	"Q$r!A#vax  !Qs 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   j                  |||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   rr   r   r   Nr   )r   rs   r   r   r   idzp_ridrR   r5   )r7   r)   r   r   r;   r8   r9   rW   s           r   r   r   q  s    < 88	A!SAY]##mm3(D S!Q>AsD#
AaC>!!1ac(#!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   rz   rW   s          r   r   r     r   r   c                    t        j                  | ||||      \  }}}}}	}
|
rt        |	|dz
  |||z  z   dz
   j                  ||fd      }|	|dz
  |||z  z   dz
   j                  ||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   rm   rn   ro   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      }||fS |j                  |||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_aidrs   r5   r   s          r   r   r     s    $ 	!A77DAq!QAQ1%ICAvxxAaCC@ 9 ||Q!HC|09r   c                 0    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                 R   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`
    rr      r   r   r   
   Z   r   r   r   N)	r   r   r2   rs   r   rB   r   	idzr_asvdrR   r   s
             r   r   r   !  s    * 	!A77DAq
	
1r1!b!|#a1f,r!t3b8#	'A 
1a	BAhrwwK==Aq)LAq!S
a7Nr   c                     t        j                  | |||      \  }}|d|||z
  z   j                  |||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  s7    F ==Aw:LAq!S
a7Nr   )   )?__doc__scipy.linalg._interpolativelinalg_interpolativer   numpyr   RuntimeErrorrR   r   r   r   r   r   r"   r'   r,   r1   r>   rC   rI   rM   rQ   rZ   rb   rg   rk   rt   rx   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      sE  < * ) 34	  
 :$:$282%2'$'4F@'L\8H><#T#LD)`:"2HH&Z :$:$282%2'$'4F@'L\8H><#T%PD)`:"2LH&r   