a
    a                    @   s6  d dl mZ d dlZd dlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlmZ dd	lmZmZ dd
lmZ g dZd4ddZd5ddZd6ddZd7ddZd8ddZd9ddZdd Zd:dd Zd;d!d"Zd<d#d$Zd=d%d&Z d'e _!d>d(d)Z"d?d*d+Z#d@d,d-Z$dAd.d/Z%dBd0d1Z&dCd2d3Z'dS )D    )warnN)
atleast_1d
atleast_2d   )get_flinalg_funcs)get_lapack_funcs_compute_lwork)LinAlgError_datacopiedLinAlgWarning)_asarray_validated)decomp
decomp_svd)levinson)solvesolve_triangularsolveh_bandedsolve_bandedsolve_toeplitzsolve_circulantinvdetlstsqpinvpinv2pinvhmatrix_balancematmul_toeplitzc                 C   s^   |dk rt d| nd|k r*td|du r6dS |d}||k rZtd|tdd dS )	zB Check arguments during the different steps of the solution phase r   z3LAPACK reported an illegal value in {}-th argument.zMatrix is singular.NEzBIll-conditioned matrix (rcond={:.6g}): result may not be accurate.   
stacklevel)
ValueErrorformatr	   r   r   )ninfolamchrcondr    r(   b/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/linalg/basic.py_solve_check   s    r*   FTgenc
           $      C   s.  d}
t t| |d}tt||d}|jd }|p:t|| }|pHt||}|jd |jd krftd||jd kr|dkr|jdkstd|jdkrt|	 S |j
dkr|dkr|dddf }n|dddf }d}
|rd	}|d
vrtd||dkrt|sd}|dur.tdtdd |jjdv rJtddd}ntddd}td|f}|	rd}d}t|rtdnd}d}|||}|dkrtd||f\}}}|||d\}}}t|| ||||||d\}}t|| ||||d\}}n|dkrjtd||f\}}}t|||}|||||||d \}}}}t|| ||||\}}n|dkrtd!||f\}} }!t|!||}| ||||||d \}}}}t|| ||||\}}nBtd"||f\}"}#|#|||||d#\}}}t|| |"||\}}t|||| |
r*| }|S )$a  
    Solves the linear equation set ``a * x = b`` for the unknown ``x``
    for square ``a`` matrix.

    If the data matrix is known to be a particular type then supplying the
    corresponding string to ``assume_a`` key chooses the dedicated solver.
    The available options are

    ===================  ========
     generic matrix       'gen'
     symmetric            'sym'
     hermitian            'her'
     positive definite    'pos'
    ===================  ========

    If omitted, ``'gen'`` is the default structure.

    The datatype of the arrays define which solver is called regardless
    of the values. In other words, even when the complex array entries have
    precisely zero imaginary parts, the complex solver will be called based
    on the data type of the array.

    Parameters
    ----------
    a : (N, N) array_like
        Square input data
    b : (N, NRHS) array_like
        Input data for the right hand side.
    sym_pos : bool, optional
        Assume `a` is symmetric and positive definite. This key is deprecated
        and assume_a = 'pos' keyword is recommended instead. The functionality
        is the same. It will be removed in the future.
    lower : bool, optional
        If True, only the data contained in the lower triangle of `a`. Default
        is to use upper triangle. (ignored for ``'gen'``)
    overwrite_a : bool, optional
        Allow overwriting data in `a` (may enhance performance).
        Default is False.
    overwrite_b : bool, optional
        Allow overwriting data in `b` (may enhance performance).
        Default is False.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
    assume_a : str, optional
        Valid entries are explained above.
    transposed: bool, optional
        If True, ``a^T x = b`` for real matrices, raises `NotImplementedError`
        for complex matrices (only for True).

    Returns
    -------
    x : (N, NRHS) ndarray
        The solution array.

    Raises
    ------
    ValueError
        If size mismatches detected or input a is not square.
    LinAlgError
        If the matrix is singular.
    LinAlgWarning
        If an ill-conditioned input a is detected.
    NotImplementedError
        If transposed is True and input a is a complex matrix.

    Examples
    --------
    Given `a` and `b`, solve for `x`:

    >>> a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
    >>> b = np.array([2, 4, -1])
    >>> from scipy import linalg
    >>> x = linalg.solve(a, b)
    >>> x
    array([ 2., -2.,  9.])
    >>> np.dot(a, x) == b
    array([ True,  True,  True], dtype=bool)

    Notes
    -----
    If the input b matrix is a 1-D array with N elements, when supplied
    together with an NxN input a, it is assumed as a valid column vector
    despite the apparent size mismatch. This is compatible with the
    numpy.dot() behavior and the returned result is still 1-D array.

    The generic, symmetric, Hermitian and positive definite solutions are
    obtained via calling ?GESV, ?SYSV, ?HESV, and ?POSV routines of
    LAPACK respectively.
    Fcheck_finiter   r   z$Input a needs to be a square matrix.z2Input b has to have same number of rows as input aNTpos)r+   symherr.   z'{} is not a recognized matrix structurer0   r/   zfUse of the "debug" keyword is deprecated and this keyword will be removed in future versions of SciPy.   r    ZfFr&   fdtypedlangeIzWscipy.linalg.solve can currently not solve a^T x = b or a^H x = b for complex matrices.1r+   )gecongetrfgetrsoverwrite_a)transoverwrite_b)norm)heconhesvZ
hesv_lwork)lworklowerr=   r?   )syconsysvZ
sysv_lwork)poconposv)rD   r=   r?   )r   r   r   shaper
   r"   sizenpZasfortranarraycopyndimr#   iscomplexobjr   DeprecationWarningr4   charr   NotImplementedErrorr*   r   ravel)$abZsym_posrD   r=   r?   debugr-   Zassume_aZ
transposedZb_is_1Da1b1r$   r&   r6   r>   r@   Zanormr9   r:   r;   luZipvtr%   xr'   rA   rB   Zhesv_lwrC   rE   rF   Zsysv_lwrG   rH   r(   r(   r)   r   (   s    _

















r   c                 C   sB  |durt dtdd t| |d}t||d}	t|jdksP|jd |jd krXtd|jd |	jd krtd	|j|	j|pt|	|}|rtd
| dddd	||}t
d||	f\}
|jjs|dkr|
||	||||d\}}n|
|j|	|| | |d\}}|dkr|S |dkr0td|d  td|  dS )a  
    Solve the equation `a x = b` for `x`, assuming a is a triangular matrix.

    Parameters
    ----------
    a : (M, M) array_like
        A triangular matrix
    b : (M,) or (M, N) array_like
        Right-hand side matrix in `a x = b`
    lower : bool, optional
        Use only data contained in the lower triangle of `a`.
        Default is to use upper triangle.
    trans : {0, 1, 2, 'N', 'T', 'C'}, optional
        Type of system to solve:

        ========  =========
        trans     system
        ========  =========
        0 or 'N'  a x  = b
        1 or 'T'  a^T x = b
        2 or 'C'  a^H x = b
        ========  =========
    unit_diagonal : bool, optional
        If True, diagonal elements of `a` are assumed to be 1 and
        will not be referenced.
    overwrite_b : bool, optional
        Allow overwriting data in `b` (may enhance performance)
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : (M,) or (M, N) ndarray
        Solution to the system `a x = b`.  Shape of return matches `b`.

    Raises
    ------
    LinAlgError
        If `a` is singular

    Notes
    -----
    .. versionadded:: 0.9.0

    Examples
    --------
    Solve the lower triangular system a x = b, where::

             [3  0  0  0]       [4]
        a =  [2  1  0  0]   b = [2]
             [1  0  1  0]       [4]
             [1  1  1  1]       [2]

    >>> from scipy.linalg import solve_triangular
    >>> a = np.array([[3, 0, 0, 0], [2, 1, 0, 0], [1, 0, 1, 0], [1, 1, 1, 1]])
    >>> b = np.array([4, 2, 4, 2])
    >>> x = solve_triangular(a, b, lower=True)
    >>> x
    array([ 1.33333333, -0.66666667,  2.66666667, -1.33333333])
    >>> a.dot(x)  # Check the result
    array([ 4.,  2.,  4.,  2.])

    NjUse of the "debug" keyword is deprecated and this keyword will be removed in the future versions of SciPy.r1   r    r,   r   r   expected square matrixz(shapes of a {} and b {} are incompatiblezsolve:overwrite_b=)NTC)trtrs)r?   rD   r>   Zunitdiagz1singular matrix: resolution failed at diagonal %dz0illegal value in %dth argument of internal trtrs)r   rO   r   lenrI   r"   r#   r
   printgetr   flagsf_contiguousr]   r	   )rS   rT   r>   rD   Zunit_diagonalr?   rU   r-   rV   rW   r_   rY   r%   r(   r(   r)   r     sB    E"




r   c              	   C   s  |durt dtdd t||dd}t||dd}|jd |jd krNtd	| \}	}
|	|
 d
 |jd krtd|	|
 d
 |jd f |pt||}|jd d
krtj|| d}||d  }|S |	|
  krd
krPn np|pt||}td||f\}|dd
df }|d
ddf }|dddf }|||||||||\}}}}}nftd||f\}tj	d|	 |
 d
 |jd
 f|j
d}|||	dddf< ||	|
||d|d\}}}}|dkr|S |dkrtdtd|  dS )aS  
    Solve the equation a x = b for x, assuming a is banded matrix.

    The matrix a is stored in `ab` using the matrix diagonal ordered form::

        ab[u + i - j, j] == a[i,j]

    Example of `ab` (shape of a is (6,6), `u` =1, `l` =2)::

        *    a01  a12  a23  a34  a45
        a00  a11  a22  a33  a44  a55
        a10  a21  a32  a43  a54   *
        a20  a31  a42  a53   *    *

    Parameters
    ----------
    (l, u) : (integer, integer)
        Number of non-zero lower and upper diagonals
    ab : (`l` + `u` + 1, M) array_like
        Banded matrix
    b : (M,) or (M, K) array_like
        Right-hand side
    overwrite_ab : bool, optional
        Discard data in `ab` (may enhance performance)
    overwrite_b : bool, optional
        Discard data in `b` (may enhance performance)
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : (M,) or (M, K) ndarray
        The solution to the system a x = b. Returned shape depends on the
        shape of `b`.

    Examples
    --------
    Solve the banded system a x = b, where::

            [5  2 -1  0  0]       [0]
            [1  4  2 -1  0]       [1]
        a = [0  1  3  2 -1]   b = [2]
            [0  0  1  2  2]       [2]
            [0  0  0  1  1]       [3]

    There is one nonzero diagonal below the main diagonal (l = 1), and
    two above (u = 2). The diagonal banded form of the matrix is::

             [*  * -1 -1 -1]
        ab = [*  2  2  2  2]
             [5  4  3  2  1]
             [1  1  1  1  *]

    >>> from scipy.linalg import solve_banded
    >>> ab = np.array([[0,  0, -1, -1, -1],
    ...                [0,  2,  2,  2,  2],
    ...                [5,  4,  3,  2,  1],
    ...                [1,  1,  1,  1,  0]])
    >>> b = np.array([0, 1, 2, 2, 3])
    >>> x = solve_banded((1, 2), ab, b)
    >>> x
    array([-2.37288136,  3.93220339, -4.        ,  4.3559322 , -1.3559322 ])

    NrZ   r1   r    T)r-   Z
as_inexactr   &shapes of ab and b are not compatible.r   zfinvalid values for the number of lower and upper diagonals: l+u+1 (%d) does not equal ab.shape[0] (%d)rL   )r   r   )gtsv)gbsvr3   )overwrite_abr?   singular matrixz5illegal value in %d-th argument of internal gbsv/gtsv)r   rO   r   rI   r"   r
   rK   arrayr   zerosr4   r	   )Zl_and_uabrT   rj   r?   rU   r-   rV   rW   ZnlowerZnupperb2rh   dur5   dlZdu2rY   r%   ri   Za2rX   pivr(   r(   r)   r   o  sP    F&

r   c                 C   s2  t | |d}t ||d}|jd |jd kr4td|p@t||}|pNt|| }|jd dkrtd||f\}|r|dddf j}	|dddf }
n&|dddf j}	|dddf  }
||	|
||||\}	}}}n(td	||f\}||||||d
\}}}|dkrtd| |dk r.td|  |S )ai  
    Solve equation a x = b. a is Hermitian positive-definite banded matrix.

    The matrix a is stored in `ab` either in lower diagonal or upper
    diagonal ordered form:

        ab[u + i - j, j] == a[i,j]        (if upper form; i <= j)
        ab[    i - j, j] == a[i,j]        (if lower form; i >= j)

    Example of `ab` (shape of a is (6, 6), `u` =2)::

        upper form:
        *   *   a02 a13 a24 a35
        *   a01 a12 a23 a34 a45
        a00 a11 a22 a33 a44 a55

        lower form:
        a00 a11 a22 a33 a44 a55
        a10 a21 a32 a43 a54 *
        a20 a31 a42 a53 *   *

    Cells marked with * are not used.

    Parameters
    ----------
    ab : (`u` + 1, M) array_like
        Banded matrix
    b : (M,) or (M, K) array_like
        Right-hand side
    overwrite_ab : bool, optional
        Discard data in `ab` (may enhance performance)
    overwrite_b : bool, optional
        Discard data in `b` (may enhance performance)
    lower : bool, optional
        Is the matrix in the lower form. (Default is upper form)
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : (M,) or (M, K) ndarray
        The solution to the system a x = b. Shape of return matches shape
        of `b`.

    Examples
    --------
    Solve the banded system A x = b, where::

            [ 4  2 -1  0  0  0]       [1]
            [ 2  5  2 -1  0  0]       [2]
        A = [-1  2  6  2 -1  0]   b = [2]
            [ 0 -1  2  7  2 -1]       [3]
            [ 0  0 -1  2  8  2]       [3]
            [ 0  0  0 -1  2  9]       [3]

    >>> from scipy.linalg import solveh_banded

    `ab` contains the main diagonal and the nonzero diagonals below the
    main diagonal. That is, we use the lower form:

    >>> ab = np.array([[ 4,  5,  6,  7, 8, 9],
    ...                [ 2,  2,  2,  2, 2, 0],
    ...                [-1, -1, -1, -1, 0, 0]])
    >>> b = np.array([1, 2, 2, 3, 3, 3])
    >>> x = solveh_banded(ab, b, lower=True)
    >>> x
    array([ 0.03431373,  0.45938375,  0.05602241,  0.47759104,  0.17577031,
            0.34733894])


    Solve the Hermitian banded system H x = b, where::

            [ 8   2-1j   0     0  ]        [ 1  ]
        H = [2+1j  5     1j    0  ]    b = [1+1j]
            [ 0   -1j    9   -2-1j]        [1-2j]
            [ 0    0   -2+1j   6  ]        [ 0  ]

    In this example, we put the upper diagonals in the array `hb`:

    >>> hb = np.array([[0, 2-1j, 1j, -2-1j],
    ...                [8,  5,    9,   6  ]])
    >>> b = np.array([1, 1+1j, 1-2j, 0])
    >>> x = solveh_banded(hb, b)
    >>> x
    array([ 0.07318536-0.02939412j,  0.11877624+0.17696461j,
            0.10077984-0.23035393j, -0.00479904-0.09358128j])

    r,   re   r   rf   r1   )ptsvNr   )pbsv)rD   rj   r?   z(%dth leading minor not positive definitez/illegal value in %dth argument of internal pbsv)r   rI   r"   r
   r   realconjr	   )rn   rT   rj   r?   rD   r-   rV   rW   rs   r5   erp   rY   r%   rt   cr(   r(   r)   r     s6    \


r   c           	         s   t |  |dd\}} }}t|ddd |f du rBtd jdkrbtt \}}n.t fdd	t j	d D }|j
| }|S )
a  Solve a Toeplitz system using Levinson Recursion

    The Toeplitz matrix has constant diagonals, with c as its first column
    and r as its first row. If r is not given, ``r == conjugate(c)`` is
    assumed.

    Parameters
    ----------
    c_or_cr : array_like or tuple of (array_like, array_like)
        The vector ``c``, or a tuple of arrays (``c``, ``r``). Whatever the
        actual shape of ``c``, it will be converted to a 1-D array. If not
        supplied, ``r = conjugate(c)`` is assumed; in this case, if c[0] is
        real, the Toeplitz matrix is Hermitian. r[0] is ignored; the first row
        of the Toeplitz matrix is ``[c[0], r[1:]]``. Whatever the actual shape
        of ``r``, it will be converted to a 1-D array.
    b : (M,) or (M, K) array_like
        Right-hand side in ``T x = b``.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (result entirely NaNs) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : (M,) or (M, K) ndarray
        The solution to the system ``T x = b``. Shape of return matches shape
        of `b`.

    See Also
    --------
    toeplitz : Toeplitz matrix

    Notes
    -----
    The solution is computed using Levinson-Durbin recursion, which is faster
    than generic least-squares methods, but can be less numerically stable.

    Examples
    --------
    Solve the Toeplitz system T x = b, where::

            [ 1 -1 -2 -3]       [1]
        T = [ 3  1 -1 -2]   b = [2]
            [ 6  3  1 -1]       [2]
            [10  6  3  1]       [5]

    To specify the Toeplitz matrix, only the first column and the first
    row are needed.

    >>> c = np.array([1, 3, 6, 10])    # First column of T
    >>> r = np.array([1, -1, -2, -3])  # First row of T
    >>> b = np.array([1, 2, 2, 5])

    >>> from scipy.linalg import solve_toeplitz, toeplitz
    >>> x = solve_toeplitz((c, r), b)
    >>> x
    array([ 1.66666667, -1.        , -2.66666667,  2.33333333])

    Check the result by creating the full Toeplitz matrix and
    multiplying it by `x`.  We should get `b`.

    >>> T = toeplitz(c, r)
    >>> T.dot(x)
    array([ 1.,  2.,  2.,  5.])

    T)keep_b_shapere   r   Nz)illegal value, `b` is a required argumentr   c              	      s,   g | ]$}t t d d |f d qS )Nr   )r   rK   ascontiguousarray.0irT   valsr(   r)   
<listcomp>  s   z"solve_toeplitz.<locals>.<listcomp>)_validate_args_for_toeplitz_opsrK   concatenater"   rM   r   rz   Zcolumn_stackrangerI   reshape)	c_or_crrT   r-   rrx   r4   b_shaperY   _r(   r~   r)   r   [  s    G

r   c                 C   sL   |}|dk r||j 7 }d|  kr,|j k r:n n
|j| S td| f d S )Nr   z'%saxis' entry is out of bounds)rM   rI   r"   )anamerS   axisaxr(   r(   r)   _get_axis_len  s    

r   raisere   c                 C   st  t | } td| |}t |}td||}||krHtd| j|jt jjt | || jdd}	t 	|	}
|du r|
j
dd| t t jj }|jdkr|jd |_n
t |}|
|k}t |}|r|d	krtd
nd|	|< t jjt |||jdd}||	 }|r*t j|td|@ }d||< t jj|dd}t | sXt |sX|j}|dkrpt |d|}|S )a  Solve C x = b for x, where C is a circulant matrix.

    `C` is the circulant matrix associated with the vector `c`.

    The system is solved by doing division in Fourier space. The
    calculation is::

        x = ifft(fft(b) / fft(c))

    where `fft` and `ifft` are the fast Fourier transform and its inverse,
    respectively. For a large vector `c`, this is *much* faster than
    solving the system with the full circulant matrix.

    Parameters
    ----------
    c : array_like
        The coefficients of the circulant matrix.
    b : array_like
        Right-hand side matrix in ``a x = b``.
    singular : str, optional
        This argument controls how a near singular circulant matrix is
        handled.  If `singular` is "raise" and the circulant matrix is
        near singular, a `LinAlgError` is raised. If `singular` is
        "lstsq", the least squares solution is returned. Default is "raise".
    tol : float, optional
        If any eigenvalue of the circulant matrix has an absolute value
        that is less than or equal to `tol`, the matrix is considered to be
        near singular. If not given, `tol` is set to::

            tol = abs_eigs.max() * abs_eigs.size * np.finfo(np.float64).eps

        where `abs_eigs` is the array of absolute values of the eigenvalues
        of the circulant matrix.
    caxis : int
        When `c` has dimension greater than 1, it is viewed as a collection
        of circulant vectors. In this case, `caxis` is the axis of `c` that
        holds the vectors of circulant coefficients.
    baxis : int
        When `b` has dimension greater than 1, it is viewed as a collection
        of vectors. In this case, `baxis` is the axis of `b` that holds the
        right-hand side vectors.
    outaxis : int
        When `c` or `b` are multidimensional, the value returned by
        `solve_circulant` is multidimensional. In this case, `outaxis` is
        the axis of the result that holds the solution vectors.

    Returns
    -------
    x : ndarray
        Solution to the system ``C x = b``.

    Raises
    ------
    LinAlgError
        If the circulant matrix associated with `c` is near singular.

    See Also
    --------
    circulant : circulant matrix

    Notes
    -----
    For a 1-D vector `c` with length `m`, and an array `b`
    with shape ``(m, ...)``,

        solve_circulant(c, b)

    returns the same result as

        solve(circulant(c), b)

    where `solve` and `circulant` are from `scipy.linalg`.

    .. versionadded:: 0.16.0

    Examples
    --------
    >>> from scipy.linalg import solve_circulant, solve, circulant, lstsq

    >>> c = np.array([2, 2, 4])
    >>> b = np.array([1, 2, 3])
    >>> solve_circulant(c, b)
    array([ 0.75, -0.25,  0.25])

    Compare that result to solving the system with `scipy.linalg.solve`:

    >>> solve(circulant(c), b)
    array([ 0.75, -0.25,  0.25])

    A singular example:

    >>> c = np.array([1, 1, 0, 0])
    >>> b = np.array([1, 2, 3, 4])

    Calling ``solve_circulant(c, b)`` will raise a `LinAlgError`.  For the
    least square solution, use the option ``singular='lstsq'``:

    >>> solve_circulant(c, b, singular='lstsq')
    array([ 0.25,  1.25,  2.25,  1.25])

    Compare to `scipy.linalg.lstsq`:

    >>> x, resid, rnk, s = lstsq(circulant(c), b)
    >>> x
    array([ 0.25,  1.25,  2.25,  1.25])

    A broadcasting example:

    Suppose we have the vectors of two circulant matrices stored in an array
    with shape (2, 5), and three `b` vectors stored in an array with shape
    (3, 5).  For example,

    >>> c = np.array([[1.5, 2, 3, 0, 0], [1, 1, 4, 3, 2]])
    >>> b = np.arange(15).reshape(-1, 5)

    We want to solve all combinations of circulant matrices and `b` vectors,
    with the result stored in an array with shape (2, 3, 5). When we
    disregard the axes of `c` and `b` that hold the vectors of coefficients,
    the shapes of the collections are (2,) and (3,), respectively, which are
    not compatible for broadcasting. To have a broadcast result with shape
    (2, 3), we add a trivial dimension to `c`: ``c[:, np.newaxis, :]`` has
    shape (2, 1, 5). The last dimension holds the coefficients of the
    circulant matrices, so when we call `solve_circulant`, we can use the
    default ``caxis=-1``. The coefficients of the `b` vectors are in the last
    dimension of the array `b`, so we use ``baxis=-1``. If we use the
    default `outaxis`, the result will have shape (5, 2, 3), so we'll use
    ``outaxis=-1`` to put the solution vectors in the last dimension.

    >>> x = solve_circulant(c[:, np.newaxis, :], b, baxis=-1, outaxis=-1)
    >>> x.shape
    (2, 3, 5)
    >>> np.set_printoptions(precision=3)  # For compact output of numbers.
    >>> x
    array([[[-0.118,  0.22 ,  1.277, -0.142,  0.302],
            [ 0.651,  0.989,  2.046,  0.627,  1.072],
            [ 1.42 ,  1.758,  2.816,  1.396,  1.841]],
           [[ 0.401,  0.304,  0.694, -0.867,  0.377],
            [ 0.856,  0.758,  1.149, -0.412,  0.831],
            [ 1.31 ,  1.213,  1.603,  0.042,  1.286]]])

    Check by solving one pair of `c` and `b` vectors (cf. ``x[1, 1, :]``):

    >>> solve_circulant(c[1], b[1, :])
    array([ 0.856,  0.758,  1.149, -0.412,  0.831])

    rx   rT   z(Shapes of c {} and b {} are incompatiblere   r   Nr(   )r   r   znear singular circulant matrix.r   r3   r   )rK   r   r   r"   r#   rI   fftZrollaxisrM   absmaxfinfoZfloat64epsanyr	   	ones_likeboolifftrN   ru   )rx   rT   ZsingularZtolZcaxisZbaxisZoutaxisZncnbZfcZabs_fcZ
near_zerosZis_near_singularZfbqmaskrY   r(   r(   r)   r     sB     







r   c                 C   s   t | |d}t|jdks.|jd |jd kr6td|pBt|| }td|f\}}}|||d\}}}	|	dkrt||jd }
td|
 }
||||
dd	\}}	|	dkrtd
|	dk rtd|	  |S )a  
    Compute the inverse of a matrix.

    Parameters
    ----------
    a : array_like
        Square matrix to be inverted.
    overwrite_a : bool, optional
        Discard data in `a` (may improve performance). Default is False.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    ainv : ndarray
        Inverse of the matrix `a`.

    Raises
    ------
    LinAlgError
        If `a` is singular.
    ValueError
        If `a` is not square, or not 2D.

    Examples
    --------
    >>> from scipy import linalg
    >>> a = np.array([[1., 2.], [3., 4.]])
    >>> linalg.inv(a)
    array([[-2. ,  1. ],
           [ 1.5, -0.5]])
    >>> np.dot(a, linalg.inv(a))
    array([[ 1.,  0.],
           [ 0.,  1.]])

    r,   r1   r   r   r[   )r:   getrigetri_lworkr<   g)\(?)rC   Zoverwrite_lurk   z7illegal value in %d-th argument of internal getrf|getri)	r   r`   rI   r"   r
   r   r   intr	   )rS   r=   r-   rV   r:   r   r   rX   rr   r%   rC   Zinv_ar(   r(   r)   r     s&    '"

r   c                 C   s|   t | |d}t|jdks.|jd |jd kr6td|pBt|| }td|f\}|||d\}}|dk rxtd|  |S )	a  
    Compute the determinant of a matrix

    The determinant of a square matrix is a value derived arithmetically
    from the coefficients of the matrix.

    The determinant for a 3x3 matrix, for example, is computed as follows::

        a    b    c
        d    e    f = A
        g    h    i

        det(A) = a*e*i + b*f*g + c*d*h - c*e*g - b*d*i - a*f*h

    Parameters
    ----------
    a : (M, M) array_like
        A square matrix.
    overwrite_a : bool, optional
        Allow overwriting data in a (may enhance performance).
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    det : float or complex
        Determinant of `a`.

    Notes
    -----
    The determinant is computed via LU factorization, LAPACK routine z/dgetrf.

    Examples
    --------
    >>> from scipy import linalg
    >>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
    >>> linalg.det(a)
    0.0
    >>> a = np.array([[0,2,3], [4,5,6], [7,8,9]])
    >>> linalg.det(a)
    3.0

    r,   r1   r   r   r[   )r   r<   z5illegal value in %d-th argument of internal det.getrf)r   r`   rI   r"   r
   r   )rS   r=   r-   rV   ZfdetZa_detr%   r(   r(   r)   r     s    ."r   c              	   C   s  t | |d}t ||d}t|jdkr.td|j\}	}
t|jdkrR|jd }nd}|	|jd krztd|	|jd |	dks|
dkrtj|
f|jdd  t||d}|
dkrtjj	|dd	d }n
t
d
}||dt
d
fS |}|du rtj}|dvrtd| t|d| f||f\}}|jjdkr>dnd}|	|
k rt|jdkrtj|
|f|jd}||d|	ddf< ntj|
|jd}||d|	< |}|pt|| }|pt||}|du rt|jj}|dv r(|dkr$t||	|
||}|||||||d\}}}}}}nv|dkr|rft||	|
||\}}||||||dd\}}}}n4t||	|
||\}}}|||||||dd\}}}}|dkrtd|dk rtd| |f tjg |jd}|	|
kr|d|
 }||
krtjt||
d d dd	}|}||||fS |dkrt||	|
||}tj|jd dftjd}||||||dd\}}}}}|dk rtd|  |	|
kr|d|
 }|}|tg |j|dfS dS )a	  
    Compute least-squares solution to equation Ax = b.

    Compute a vector x such that the 2-norm ``|b - A x|`` is minimized.

    Parameters
    ----------
    a : (M, N) array_like
        Left-hand side array
    b : (M,) or (M, K) array_like
        Right hand side array
    cond : float, optional
        Cutoff for 'small' singular values; used to determine effective
        rank of a. Singular values smaller than
        ``rcond * largest_singular_value`` are considered zero.
    overwrite_a : bool, optional
        Discard data in `a` (may enhance performance). Default is False.
    overwrite_b : bool, optional
        Discard data in `b` (may enhance performance). Default is False.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
    lapack_driver : str, optional
        Which LAPACK driver is used to solve the least-squares problem.
        Options are ``'gelsd'``, ``'gelsy'``, ``'gelss'``. Default
        (``'gelsd'``) is a good choice.  However, ``'gelsy'`` can be slightly
        faster on many problems.  ``'gelss'`` was used historically.  It is
        generally slow but uses less memory.

        .. versionadded:: 0.17.0

    Returns
    -------
    x : (N,) or (N, K) ndarray
        Least-squares solution.  Return shape matches shape of `b`.
    residues : (K,) ndarray or float
        Square of the 2-norm for each column in ``b - a x``, if ``M > N`` and
        ``ndim(A) == n`` (returns a scalar if b is 1-D). Otherwise a
        (0,)-shaped array is returned.
    rank : int
        Effective rank of `a`.
    s : (min(M, N),) ndarray or None
        Singular values of `a`. The condition number of a is
        ``abs(s[0] / s[-1])``.

    Raises
    ------
    LinAlgError
        If computation does not converge.

    ValueError
        When parameters are not compatible.

    See Also
    --------
    scipy.optimize.nnls : linear least squares with non-negativity constraint

    Notes
    -----
    When ``'gelsy'`` is used as a driver, `residues` is set to a (0,)-shaped
    array and `s` is always ``None``.

    Examples
    --------
    >>> from scipy.linalg import lstsq
    >>> import matplotlib.pyplot as plt

    Suppose we have the following data:

    >>> x = np.array([1, 2.5, 3.5, 4, 5, 7, 8.5])
    >>> y = np.array([0.3, 1.1, 1.5, 2.0, 3.2, 6.6, 8.6])

    We want to fit a quadratic polynomial of the form ``y = a + b*x**2``
    to this data.  We first form the "design matrix" M, with a constant
    column of 1s and a column containing ``x**2``:

    >>> M = x[:, np.newaxis]**[0, 2]
    >>> M
    array([[  1.  ,   1.  ],
           [  1.  ,   6.25],
           [  1.  ,  12.25],
           [  1.  ,  16.  ],
           [  1.  ,  25.  ],
           [  1.  ,  49.  ],
           [  1.  ,  72.25]])

    We want to find the least-squares solution to ``M.dot(p) = y``,
    where ``p`` is a vector with length 2 that holds the parameters
    ``a`` and ``b``.

    >>> p, res, rnk, s = lstsq(M, y)
    >>> p
    array([ 0.20925829,  0.12013861])

    Plot the data and the fitted curve.

    >>> plt.plot(x, y, 'o', label='data')
    >>> xx = np.linspace(0, 9, 101)
    >>> yy = p[0] + p[1]*xx**2
    >>> plt.plot(xx, yy, label='least squares fit, $y = a + bx^2$')
    >>> plt.xlabel('x')
    >>> plt.ylabel('y')
    >>> plt.legend(framealpha=1, shadow=True)
    >>> plt.grid(alpha=0.25)
    >>> plt.show()

    r,   r1   zInput array a should be 2Dr   r   zGShape mismatch: a and b should have the same number of rows ({} != {}).Nr3   r   )r   )gelsdgelsygelsszLAPACK driver "%s" is not foundz%s_lworkr2   TF)r   r   r   )r=   r?   r   z,SVD did not converge in Linear Least Squaresz.illegal value in %d-th argument of internal %sr   z1illegal value in %d-th argument of internal gelsy)r   r`   rI   r"   r#   rK   rm   Zcommon_typeZlinalgr@   emptyr   default_lapack_driverr   r4   kindr
   r   r   r   r	   asarraysumr   Zint32rl   )rS   rT   condr=   r?   r-   Zlapack_driverrV   rW   mr$   ZnrhsrY   ZresiduesZdriverZlapack_funcZlapack_lworkZ	real_dataro   rC   vsrankZworkr%   ZiworkZrworkZresidsx1Zjptvjr(   r(   r)   r     s    n

&












 



r   r   c                 C   s&  t | |d} tj| ddd\}}}	|jj }
t|}|s@|rNtdt	dd |sV|rr|du rr|du rr|pl|}d}|du r~dn|}|du rt| j
t|
j n|}|dk s|dk rtd	|||  }t||k}|ddd|f }||d|  }||	d|   j}|r||fS |S dS )
a	  
    Compute the (Moore-Penrose) pseudo-inverse of a matrix.

    Calculate a generalized inverse of a matrix using its
    singular-value decomposition ``U @ S @ V`` in the economy mode and picking
    up only the columns/rows that are associated with significant singular
    values.

    If ``s`` is the maximum singular value of ``a``, then the
    significance cut-off value is determined by ``atol + rtol * s``. Any
    singular value below this value is assumed insignificant.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to be pseudo-inverted.
    atol: float, optional
        Absolute threshold term, default value is 0.

        .. versionadded:: 1.7.0

    rtol: float, optional
        Relative threshold term, default value is ``max(M, N) * eps`` where
        ``eps`` is the machine precision value of the datatype of ``a``.

        .. versionadded:: 1.7.0

    return_rank : bool, optional
        If True, return the effective rank of the matrix.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
    cond, rcond : float, optional
        In older versions, these values were meant to be used as ``atol`` with
        ``rtol=0``. If both were given ``rcond`` overwrote ``cond`` and hence
        the code was not correct. Thus using these are strongly discouraged and
        the tolerances above are recommended instead. In fact, if provided,
        atol, rtol takes precedence over these keywords.

        .. versionchanged:: 1.7.0
            Deprecated in favor of ``rtol`` and ``atol`` parameters above and
            will be removed in future versions of SciPy.

        .. versionchanged:: 1.3.0
            Previously the default cutoff value was just ``eps*f`` where ``f``
            was ``1e3`` for single precision and ``1e6`` for double precision.

    Returns
    -------
    B : (N, M) ndarray
        The pseudo-inverse of matrix `a`.
    rank : int
        The effective rank of the matrix. Returned if `return_rank` is True.

    Raises
    ------
    LinAlgError
        If SVD computation does not converge.

    Examples
    --------
    >>> from scipy import linalg
    >>> rng = np.random.default_rng()
    >>> a = rng.standard_normal((9, 6))
    >>> B = linalg.pinv(a)
    >>> np.allclose(a, a @ B @ a)
    True
    >>> np.allclose(B, B @ a @ B)
    True

    r,   F)Zfull_matricesr-   Use of the "cond" and "rcond" keywords are deprecated and will be removed in future versions of SciPy. Use "atol" and "rtol" keywords insteadr1   r    N        &atol and rtol values must be positive.)r   r   Zsvdr4   rP   rD   rK   r   r   rO   rI   r   r   r"   r   rv   r]   )rS   atolrtolreturn_rankr-   r   r'   ur   ZvhtmaxSvalr   Br(   r(   r)   r     s.    J
"r   c                 C   s,   t dtdd |dur|}t| |d||dS )a  
    Compute the (Moore-Penrose) pseudo-inverse of a matrix.

    `scipy.linalg.pinv2` is deprecated since SciPy 1.7.0, use
    `scipy.linalg.pinv` instead for better tolerance control.

    Calculate a generalized inverse of a matrix using its
    singular-value decomposition and including all 'large' singular
    values.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to be pseudo-inverted.
    cond, rcond : float or None
        Cutoff for 'small' singular values; singular values smaller than this
        value are considered as zero. If both are omitted, the default value
        ``max(M,N)*largest_singular_value*eps`` is used where ``eps`` is the
        machine precision value of the datatype of ``a``.

        .. versionchanged:: 1.3.0
            Previously the default cutoff value was just ``eps*f`` where ``f``
            was ``1e3`` for single precision and ``1e6`` for double precision.

    return_rank : bool, optional
        If True, return the effective rank of the matrix.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    B : (N, M) ndarray
        The pseudo-inverse of matrix `a`.
    rank : int
        The effective rank of the matrix. Returned if `return_rank` is True.

    Raises
    ------
    LinAlgError
        If SVD computation does not converge.

    zQscipy.linalg.pinv2 is deprecated since SciPy 1.7.0, use scipy.linalg.pinv insteadr1   r    N)rS   r   r   r   r-   )r   rO   r   )rS   r   r'   r   r-   r(   r(   r)   r   E  s    .
r   c                 C   s   t | |d} tj| |dd\}}	|	jj }
tt|}|sD|rRt	dt
dd |sZ|rv|du rv|du rv|pp|}d}|du rdn|}|du rt| jt|
j n|}|dk s|dk rtd	|||  }t||k}d
||  }|	dd|f }	|	| |	 j }|r|t|fS |S dS )a	  
    Compute the (Moore-Penrose) pseudo-inverse of a Hermitian matrix.

    Calculate a generalized inverse of a copmlex Hermitian/real symmetric
    matrix using its eigenvalue decomposition and including all eigenvalues
    with 'large' absolute value.

    Parameters
    ----------
    a : (N, N) array_like
        Real symmetric or complex hermetian matrix to be pseudo-inverted
    atol: float, optional
        Absolute threshold term, default value is 0.

        .. versionadded:: 1.7.0

    rtol: float, optional
        Relative threshold term, default value is ``N * eps`` where
        ``eps`` is the machine precision value of the datatype of ``a``.

        .. versionadded:: 1.7.0

    lower : bool, optional
        Whether the pertinent array data is taken from the lower or upper
        triangle of `a`. (Default: lower)
    return_rank : bool, optional
        If True, return the effective rank of the matrix.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
    cond, rcond : float, optional
        In older versions, these values were meant to be used as ``atol`` with
        ``rtol=0``. If both were given ``rcond`` overwrote ``cond`` and hence
        the code was not correct. Thus using these are strongly discouraged and
        the tolerances above are recommended instead.  In fact, if provided,
        atol, rtol takes precedence over these keywords.

        .. versionchanged:: 1.7.0
            Deprecated in favor of ``rtol`` and ``atol`` parameters above and
            will be removed in future versions of SciPy.

        .. versionchanged:: 1.3.0
            Previously the default cutoff value was just ``eps*f`` where ``f``
            was ``1e3`` for single precision and ``1e6`` for double precision.

    Returns
    -------
    B : (N, N) ndarray
        The pseudo-inverse of matrix `a`.
    rank : int
        The effective rank of the matrix.  Returned if `return_rank` is True.

    Raises
    ------
    LinAlgError
        If eigenvalue algorithm does not converge.

    Examples
    --------
    >>> from scipy.linalg import pinvh
    >>> rng = np.random.default_rng()
    >>> a = rng.standard_normal((9, 6))
    >>> a = np.dot(a, a.T)
    >>> B = pinvh(a)
    >>> np.allclose(a, a @ B @ a)
    True
    >>> np.allclose(B, B @ a @ B)
    True

    r,   F)rD   r-   r   r1   r    Nr   r   g      ?)r   r   Zeighr4   rP   rD   rK   r   r   r   rO   rI   r   r   r"   rv   r]   r`   )rS   r   r   rD   r   r-   r   r'   r   r   r   r   r   Zabove_cutoffZpsigma_diagr   r(   r(   r)   r   |  s.    I"r   c                 C   s  t t| dd} t j| j s&tdtd| f}|| |||d\}}}}	}
|
dk rdtd|
 t j|	t	d}|	||d	  |||d	 < |	j
td
dd	 }	| jd }t |}||k rt|	|d	 d ddd d	D ]2\}}|| |krq||| |g |||| g< q|dkrZt|	d| D ],\}}||krBq,|||g |||g< q,|rl|||ffS t |}t |||< |t ||ddf fS )a#  
    Compute a diagonal similarity transformation for row/column balancing.

    The balancing tries to equalize the row and column 1-norms by applying
    a similarity transformation such that the magnitude variation of the
    matrix entries is reflected to the scaling matrices.

    Moreover, if enabled, the matrix is first permuted to isolate the upper
    triangular parts of the matrix and, again if scaling is also enabled,
    only the remaining subblocks are subjected to scaling.

    The balanced matrix satisfies the following equality

    .. math::

                        B = T^{-1} A T

    The scaling coefficients are approximated to the nearest power of 2
    to avoid round-off errors.

    Parameters
    ----------
    A : (n, n) array_like
        Square data matrix for the balancing.
    permute : bool, optional
        The selector to define whether permutation of A is also performed
        prior to scaling.
    scale : bool, optional
        The selector to turn on and off the scaling. If False, the matrix
        will not be scaled.
    separate : bool, optional
        This switches from returning a full matrix of the transformation
        to a tuple of two separate 1-D permutation and scaling arrays.
    overwrite_a : bool, optional
        This is passed to xGEBAL directly. Essentially, overwrites the result
        to the data. It might increase the space efficiency. See LAPACK manual
        for details. This is False by default.

    Returns
    -------
    B : (n, n) ndarray
        Balanced matrix
    T : (n, n) ndarray
        A possibly permuted diagonal matrix whose nonzero entries are
        integer powers of 2 to avoid numerical truncation errors.
    scale, perm : (n,) ndarray
        If ``separate`` keyword is set to True then instead of the array
        ``T`` above, the scaling and the permutation vectors are given
        separately as a tuple without allocating the full array ``T``.

    Notes
    -----

    This algorithm is particularly useful for eigenvalue and matrix
    decompositions and in many cases it is already called by various
    LAPACK routines.

    The algorithm is based on the well-known technique of [1]_ and has
    been modified to account for special cases. See [2]_ for details
    which have been implemented since LAPACK v3.5.0. Before this version
    there are corner cases where balancing can actually worsen the
    conditioning. See [3]_ for such examples.

    The code is a wrapper around LAPACK's xGEBAL routine family for matrix
    balancing.

    .. versionadded:: 0.19.0

    Examples
    --------
    >>> from scipy import linalg
    >>> x = np.array([[1,2,0], [9,1,0.01], [1,2,10*np.pi]])

    >>> y, permscale = linalg.matrix_balance(x)
    >>> np.abs(x).sum(axis=0) / np.abs(x).sum(axis=1)
    array([ 3.66666667,  0.4995005 ,  0.91312162])

    >>> np.abs(y).sum(axis=0) / np.abs(y).sum(axis=1)
    array([ 1.2       ,  1.27041742,  0.92658316])  # may vary

    >>> permscale  # only powers of 2 (0.5 == 2^(-1))
    array([[  0.5,   0. ,  0. ],  # may vary
           [  0. ,   1. ,  0. ],
           [  0. ,   0. ,  1. ]])

    References
    ----------
    .. [1] : B.N. Parlett and C. Reinsch, "Balancing a Matrix for
       Calculation of Eigenvalues and Eigenvectors", Numerische Mathematik,
       Vol.13(4), 1969, :doi:`10.1007/BF02165404`

    .. [2] : R. James, J. Langou, B.R. Lowery, "On matrix balancing and
       eigenvector computation", 2014, :arxiv:`1401.5766`

    .. [3] :  D.S. Watkins. A case where balancing is harmful.
       Electron. Trans. Numer. Anal, Vol.23, 2006.

    Tr,   z/The data matrix for balancing should be square.gebal)scalepermuter=   r   zxGEBAL exited with the internal error "illegal value in argument number {}.". See LAPACK documentation for the xGEBAL error codes.r3   r   Frg   Nre   )rK   r   r   equalrI   r"   r   r#   r   floatZastyper   Zarange	enumerateZ
empty_likeZdiag)Ar   r   Zseparater=   r   r   lohiZpsr%   Zscalingr$   permindrY   Zipermr(   r(   r)   r     s>    e


(


r   c           
         s>  t | tr4| \}}t||d }t||d }nt| |d }| }|du r\tdt||d}|j}|jd |jd k}|r|s|jd |jd krtdt|pt|pt|}	|	rtj	ntj
  fdd|||fD \}}}|jdkr|s|d	d}n|jdkr0||jd d	}||| |fS )
a  Validate arguments and format inputs for toeplitz functions

    Parameters
    ----------
    c_or_cr : array_like or tuple of (array_like, array_like)
        The vector ``c``, or a tuple of arrays (``c``, ``r``). Whatever the
        actual shape of ``c``, it will be converted to a 1-D array. If not
        supplied, ``r = conjugate(c)`` is assumed; in this case, if c[0] is
        real, the Toeplitz matrix is Hermitian. r[0] is ignored; the first row
        of the Toeplitz matrix is ``[c[0], r[1:]]``. Whatever the actual shape
        of ``r``, it will be converted to a 1-D array.
    b : (M,) or (M, K) array_like
        Right-hand side in ``T x = b``.
    check_finite : bool
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (result entirely NaNs) if the inputs do contain infinities or NaNs.
    keep_b_shape: bool
        Whether to convert a (M,) dimensional b into a (M, 1) dimensional
        matrix.
    enforce_square: bool, optional
        If True (default), this verifies that the Toeplitz matrix is square.

    Returns
    -------
    r : array
        1d array corresponding to the first row of the Toeplitz matrix.
    c: array
        1d array corresponding to the first column of the Toeplitz matrix.
    b: array
        (M,), (M, 1) or (M, K) dimensional array, post validation,
        corresponding to ``b``.
    dtype: numpy datatype
        ``dtype`` stores the datatype of ``r``, ``c`` and ``b``. If any of
        ``r``, ``c`` or ``b`` are complex, ``dtype`` is ``np.complex128``,
        otherwise, it is ``np.float``.
    b_shape: tuple
        Shape of ``b`` after passing it through ``_asarray_validated``.

    r,   Nz`b` must be an array, not None.r   zIncompatible dimensions.c                 3   s   | ]}t j| d V  qdS )r3   N)rK   r   r{   r3   r(   r)   	<genexpr>      z2_validate_args_for_toeplitz_ops.<locals>.<genexpr>r   re   )
isinstancetupler   rR   	conjugater"   rI   rK   rN   Z
complex128doublerM   r   )
r   rT   r-   ry   enforce_squarerx   r   r   Zis_not_squareZis_cmplxr(   r3   r)   r   |  s*    +
r   c                 C   s@  ddl m }m}m}m} t| ||ddd\}}	}}
}|j\}}t|	}t|}|| d }t|	|ddd f}t	|st	|r||d|d
dd}|||d|d	}||| d|dd
|d
d
f }nJ||d|d
dd}|||d|d	}||| d||dd
|d
d
f }t|dkr.|fn||f}|j
| S )a  Efficient Toeplitz Matrix-Matrix Multiplication using FFT

    This function returns the matrix multiplication between a Toeplitz
    matrix and a dense matrix.

    The Toeplitz matrix has constant diagonals, with c as its first column
    and r as its first row. If r is not given, ``r == conjugate(c)`` is
    assumed.

    Parameters
    ----------
    c_or_cr : array_like or tuple of (array_like, array_like)
        The vector ``c``, or a tuple of arrays (``c``, ``r``). Whatever the
        actual shape of ``c``, it will be converted to a 1-D array. If not
        supplied, ``r = conjugate(c)`` is assumed; in this case, if c[0] is
        real, the Toeplitz matrix is Hermitian. r[0] is ignored; the first row
        of the Toeplitz matrix is ``[c[0], r[1:]]``. Whatever the actual shape
        of ``r``, it will be converted to a 1-D array.
    x : (M,) or (M, K) array_like
        Matrix with which to multiply.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (result entirely NaNs) if the inputs do contain infinities or NaNs.
    workers : int, optional
        To pass to scipy.fft.fft and ifft. Maximum number of workers to use
        for parallel computation. If negative, the value wraps around from
        ``os.cpu_count()``. See scipy.fft.fft for more details.

    Returns
    -------
    T @ x : (M,) or (M, K) ndarray
        The result of the matrix multiplication ``T @ x``. Shape of return
        matches shape of `x`.

    See Also
    --------
    toeplitz : Toeplitz matrix
    solve_toeplitz : Solve a Toeplitz system using Levinson Recursion

    Notes
    -----
    The Toeplitz matrix is embedded in a circulant matrix and the FFT is used
    to efficiently calculate the matrix-matrix product.

    Because the computation is based on the FFT, integer inputs will
    result in floating point outputs.  This is unlike NumPy's `matmul`,
    which preserves the data type of the input.

    This is partly based on the implementation that can be found in [1]_,
    licensed under the MIT license. More information about the method can be
    found in reference [2]_. References [3]_ and [4]_ have more reference
    implementations in Python.

    .. versionadded:: 1.6.0

    References
    ----------
    .. [1] Jacob R Gardner, Geoff Pleiss, David Bindel, Kilian
       Q Weinberger, Andrew Gordon Wilson, "GPyTorch: Blackbox Matrix-Matrix
       Gaussian Process Inference with GPU Acceleration" with contributions
       from Max Balandat and Ruihan Wu. Available online:
       https://github.com/cornellius-gp/gpytorch

    .. [2] J. Demmel, P. Koev, and X. Li, "A Brief Survey of Direct Linear
       Solvers". In Z. Bai, J. Demmel, J. Dongarra, A. Ruhe, and H. van der
       Vorst, editors. Templates for the Solution of Algebraic Eigenvalue
       Problems: A Practical Guide. SIAM, Philadelphia, 2000. Available at:
       http://www.netlib.org/utk/people/JackDongarra/etemplates/node384.html

    .. [3] R. Scheibler, E. Bezzam, I. Dokmanic, Pyroomacoustics: A Python
       package for audio room simulations and array processing algorithms,
       Proc. IEEE ICASSP, Calgary, CA, 2018.
       https://github.com/LCAV/pyroomacoustics/blob/pypi-release/
       pyroomacoustics/adaptive/util.py

    .. [4] Marano S, Edwards B, Ferrari G and Fah D (2017), "Fitting
       Earthquake Spectra: Colored Noise and Incomplete Data", Bulletin of
       the Seismological Society of America., January, 2017. Vol. 107(1),
       pp. 276-291.

    Examples
    --------
    Multiply the Toeplitz matrix T with matrix x::

            [ 1 -1 -2 -3]       [1 10]
        T = [ 3  1 -1 -2]   x = [2 11]
            [ 6  3  1 -1]       [2 11]
            [10  6  3  1]       [5 19]

    To specify the Toeplitz matrix, only the first column and the first
    row are needed.

    >>> c = np.array([1, 3, 6, 10])    # First column of T
    >>> r = np.array([1, -1, -2, -3])  # First row of T
    >>> x = np.array([[1, 10], [2, 11], [2, 11], [5, 19]])

    >>> from scipy.linalg import toeplitz, matmul_toeplitz
    >>> matmul_toeplitz((c, r), x)
    array([[-20., -80.],
           [ -7.,  -8.],
           [  9.,  85.],
           [ 33., 218.]])

    Check the result by creating the full Toeplitz matrix and
    multiplying it by ``x``.

    >>> toeplitz(c, r) @ x
    array([[-20, -80],
           [ -7,  -8],
           [  9,  85],
           [ 33, 218]])

    The full matrix is never formed explicitly, so this routine
    is suitable for very large Toeplitz matrices.

    >>> n = 1000000
    >>> matmul_toeplitz([1] + [0]*(n-1), np.ones(n))
    array([1., 1., 1., ..., 1., 1., 1.])

    r1   )r   r   rfftirfftF)ry   r   r   re   r   )r   workers)r$   r   r   N)r   r   r$   )r   r   r   r   r   rI   r`   rK   r   rN   r   )r   rY   r-   r   r   r   r   r   r   rx   r4   Zx_shaper$   r   ZT_nrowsZT_ncolspZembedded_colZfft_matZfft_xZmat_times_xZreturn_shaper(   r(   r)   r     s4    {



r   )NN)FFFFNTr+   F)r   FFFNT)FFNT)FFFT)T)r   Nre   r   r   )FT)FT)NFFTN)NNFTNN)NNFT)NNTFTNN)TTFF)T)FN)(warningsr   ZnumpyrK   r   r   Zflinalgr   Zlapackr   r   miscr	   r
   r   r   r    r   Z_solve_toeplitzr   __all__r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   <module>   sf   
   
 a  
g  
q  
{
Z	  
 G
M
;  
 K  
l
7  
l  
  
I