
    5[g                         d dl Z d dlZd dlmZ g dZddZd ZddZe	fdZ
d Zd	 Zd
 Zd ZddZd ZddZddZddZddZd Zd ZddZy)    N)
as_strided)toeplitz	circulanthankelhadamardlesliekron
block_diag	companionhelmerthilbert
invhilbertpascal	invpascaldftfiedlerfiedler_companionconvolution_matrixc                    t        j                  |       j                         } || j                         }n#t        j                  |      j                         }t        j                  | ddd   |dd f      }t        |       t        |      f}|j                  d   }t        |t        |       dz
  d || |f      j                         S )a  
    Construct a Toeplitz 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 : array_like
        First column of the matrix.  Whatever the actual shape of `c`, it
        will be converted to a 1-D array.
    r : array_like, optional
        First row of the matrix. If None, ``r = conjugate(c)`` is assumed;
        in this case, if c[0] is real, the result is a Hermitian matrix.
        r[0] is ignored; the first row of the returned matrix is
        ``[c[0], r[1:]]``.  Whatever the actual shape of `r`, it will be
        converted to a 1-D array.

    Returns
    -------
    A : (len(c), len(r)) ndarray
        The Toeplitz matrix. Dtype is the same as ``(c[0] + r[0]).dtype``.

    See Also
    --------
    circulant : circulant matrix
    hankel : Hankel matrix
    solve_toeplitz : Solve a Toeplitz system.

    Notes
    -----
    The behavior when `c` or `r` is a scalar, or when `c` is complex and
    `r` is None, was changed in version 0.8.0. The behavior in previous
    versions was undocumented and is no longer supported.

    Examples
    --------
    >>> from scipy.linalg import toeplitz
    >>> toeplitz([1,2,3], [1,4,5,6])
    array([[1, 4, 5, 6],
           [2, 1, 4, 5],
           [3, 2, 1, 4]])
    >>> toeplitz([1.0, 2+3j, 4-1j])
    array([[ 1.+0.j,  2.-3.j,  4.+1.j],
           [ 2.+3.j,  1.+0.j,  2.-3.j],
           [ 4.-1.j,  2.+3.j,  1.+0.j]])

    N   r   shapestrides)	npasarrayravel	conjugateconcatenatelenr   r   copycrvalsout_shpns        Y/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/linalg/_special_matrices.pyr   r      s    d 	

1AyKKMJJqM! >>1TrT7AabE*+D!fc!fnGQAd3q6!89oWr1gFKKMM    c                    t        j                  |       j                         } t        j                  | ddd   | ddd   f      }t	        |       }|j
                  d   }t        ||dz
  d ||f| |f      j                         S )aT  
    Construct a circulant matrix.

    Parameters
    ----------
    c : (N,) array_like
        1-D array, the first column of the matrix.

    Returns
    -------
    A : (N, N) ndarray
        A circulant matrix whose first column is `c`.

    See Also
    --------
    toeplitz : Toeplitz matrix
    hankel : Hankel matrix
    solve_circulant : Solve a circulant system.

    Notes
    -----
    .. versionadded:: 0.8.0

    Examples
    --------
    >>> from scipy.linalg import circulant
    >>> circulant([1, 2, 3])
    array([[1, 3, 2],
           [2, 1, 3],
           [3, 2, 1]])

    Nr   r   r   r   )r   r   r   r   r    r   r   r!   )r#   c_extLr'   s       r(   r   r   P   s    B 	

1ANNAddGQu"uX./EAAaAeAaCDk!Q1"aAFFHHr)   c                 x   t        j                  |       j                         } |t        j                  |       }n#t        j                  |      j                         }t        j                  | |dd f      }t        |       t        |      f}|j                  d   }t        ||||f      j                         S )a  
    Construct a Hankel matrix.

    The Hankel matrix has constant anti-diagonals, with `c` as its
    first column and `r` as its last row. If `r` is not given, then
    `r = zeros_like(c)` is assumed.

    Parameters
    ----------
    c : array_like
        First column of the matrix. Whatever the actual shape of `c`, it
        will be converted to a 1-D array.
    r : array_like, optional
        Last row of the matrix. If None, ``r = zeros_like(c)`` is assumed.
        r[0] is ignored; the last row of the returned matrix is
        ``[c[-1], r[1:]]``. Whatever the actual shape of `r`, it will be
        converted to a 1-D array.

    Returns
    -------
    A : (len(c), len(r)) ndarray
        The Hankel matrix. Dtype is the same as ``(c[0] + r[0]).dtype``.

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

    Examples
    --------
    >>> from scipy.linalg import hankel
    >>> hankel([1, 17, 99])
    array([[ 1, 17, 99],
           [17, 99,  0],
           [99,  0,  0]])
    >>> hankel([1,2,3,4], [4,7,7,8,9])
    array([[1, 2, 3, 4, 7],
           [2, 3, 4, 7, 7],
           [3, 4, 7, 7, 8],
           [4, 7, 7, 8, 9]])

    Nr   r   r   )	r   r   r   
zeros_liker   r    r   r   r!   r"   s        r(   r   r   y   s    V 	

1AyMM!JJqM! >>1ae*%D!fc!fnGQAd'Aq6:??AAr)   c                 R   | dk  rd}nt        t        j                  | d            }d|z  | k7  rt        d      t	        j
                  dgg|      }t        d|      D ]D  }t	        j                  t	        j                  ||f      t	        j                  || f      f      }F |S )a  
    Construct an Hadamard matrix.

    Constructs an n-by-n Hadamard matrix, using Sylvester's
    construction. `n` must be a power of 2.

    Parameters
    ----------
    n : int
        The order of the matrix. `n` must be a power of 2.
    dtype : dtype, optional
        The data type of the array to be constructed.

    Returns
    -------
    H : (n, n) ndarray
        The Hadamard matrix.

    Notes
    -----
    .. versionadded:: 0.8.0

    Examples
    --------
    >>> from scipy.linalg import hadamard
    >>> hadamard(2, dtype=complex)
    array([[ 1.+0.j,  1.+0.j],
           [ 1.+0.j, -1.-0.j]])
    >>> hadamard(4)
    array([[ 1,  1,  1,  1],
           [ 1, -1,  1, -1],
           [ 1,  1, -1, -1],
           [ 1, -1, -1,  1]])

    r   r      z9n must be an positive integer, and n must be a power of 2dtype)	intmathlog
ValueErrorr   arrayrangevstackhstack)r'   r2   lg2His        r(   r   r      s    P 	1u$((1a.!Cx1} ( ) 	) 	1#e$A 1c]IIryy!Q("))QG*<=>  Hr)   c           
      "   t        j                  |       } t        j                  |      }| j                  dk7  rt        d      |j                  dk7  rt        d      | j                  |j                  dz   k7  rt        d      |j                  dk(  rt        d      | d   |d   z   }| j                  }t        j
                  ||f|j                        }| |d<   ||t        t        d|            t        t        d|dz
              f<   |S )a  
    Create a Leslie matrix.

    Given the length n array of fecundity coefficients `f` and the length
    n-1 array of survival coefficients `s`, return the associated Leslie
    matrix.

    Parameters
    ----------
    f : (N,) array_like
        The "fecundity" coefficients.
    s : (N-1,) array_like
        The "survival" coefficients, has to be 1-D.  The length of `s`
        must be one less than the length of `f`, and it must be at least 1.

    Returns
    -------
    L : (N, N) ndarray
        The array is zero except for the first row,
        which is `f`, and the first sub-diagonal, which is `s`.
        The data-type of the array will be the data-type of ``f[0]+s[0]``.

    Notes
    -----
    .. versionadded:: 0.8.0

    The Leslie matrix is used to model discrete-time, age-structured
    population growth [1]_ [2]_. In a population with `n` age classes, two sets
    of parameters define a Leslie matrix: the `n` "fecundity coefficients",
    which give the number of offspring per-capita produced by each age
    class, and the `n` - 1 "survival coefficients", which give the
    per-capita survival rate of each age class.

    References
    ----------
    .. [1] P. H. Leslie, On the use of matrices in certain population
           mathematics, Biometrika, Vol. 33, No. 3, 183--212 (Nov. 1945)
    .. [2] P. H. Leslie, Some further notes on the use of matrices in
           population mathematics, Biometrika, Vol. 35, No. 3/4, 213--245
           (Dec. 1948)

    Examples
    --------
    >>> from scipy.linalg import leslie
    >>> leslie([0.1, 2.0, 1.0, 0.1], [0.2, 0.8, 0.7])
    array([[ 0.1,  2. ,  1. ,  0.1],
           [ 0.2,  0. ,  0. ,  0. ],
           [ 0. ,  0.8,  0. ,  0. ],
           [ 0. ,  0. ,  0.7,  0. ]])

    r   z$Incorrect shape for f.  f must be 1Dz$Incorrect shape for s.  s must be 1DzVIncorrect lengths for f and s.  The length of s must be one less than the length of f.r   z#The length of s must be at least 1.r1   	r   
atleast_1dndimr6   sizezerosr2   listr8   )fstmpr'   as        r(   r   r      s    h 	aA
aAvv{?@@vv{?@@vv! H I 	Ivv{>??
A$1+C	A
!Qsyy)AAaD23Ad5A;eAq1uo../Hr)   c                 |   | j                   dk(  s|j                   dk(  rW| j                  d   |j                  d   z  }| j                  d   |j                  d   z  }t        j                  | ||f      S | j                  d   s t        j
                  | | j                        } |j                  d   s t        j
                  ||j                        }t        j                  | |      }|j                  | j                  |j                  z         }t        j                  t        j                  |d      d      S )a}  
    Kronecker product.

    The result is the block matrix::

        a[0,0]*b    a[0,1]*b  ... a[0,-1]*b
        a[1,0]*b    a[1,1]*b  ... a[1,-1]*b
        ...
        a[-1,0]*b   a[-1,1]*b ... a[-1,-1]*b

    Parameters
    ----------
    a : (M, N) ndarray
        Input array
    b : (P, Q) ndarray
        Input array

    Returns
    -------
    A : (M*P, N*Q) ndarray
        Kronecker product of `a` and `b`.

    Examples
    --------
    >>> from numpy import array
    >>> from scipy.linalg import kron
    >>> kron(array([[1,2],[3,4]]), array([[1,1,1]]))
    array([[1, 1, 1, 2, 2, 2],
           [3, 3, 3, 4, 4, 4]])

    r   r   )r   
CONTIGUOUSaxis)rB   r   r   
empty_likeflagsreshapeouterr   )rH   bmr'   os        r(   r	   r	   3  s    B 	vv{affkGGAJ#GGAJ#}}Qq!f--77< JJq!''"77< JJq!''"
AA			!''AGG#$A>>"..3!<<r)   c                     | dk(  rg f} | D cg c]  }t        j                  |       } }t        t        |             D cg c]  }| |   j                  dkD  s| }}|rt        d|z        t        j                  | D cg c]  }|j                   c}      }t        j                  | D cg c]  }|j                   c} }t        j                  t        j                  |d      |      }d\  }}	t        |      D ]&  \  }
\  }}| |
   ||||z   |	|	|z   f<   ||z  }|	|z  }	( |S c c}w c c}w c c}w c c}w )a  
    Create a block diagonal matrix from provided arrays.

    Given the inputs `A`, `B` and `C`, the output will have these
    arrays arranged on the diagonal::

        [[A, 0, 0],
         [0, B, 0],
         [0, 0, C]]

    Parameters
    ----------
    A, B, C, ... : array_like, up to 2-D
        Input arrays.  A 1-D array or array_like sequence of length `n` is
        treated as a 2-D array with shape ``(1,n)``.

    Returns
    -------
    D : ndarray
        Array with `A`, `B`, `C`, ... on the diagonal. `D` has the
        same dtype as `A`.

    Notes
    -----
    If all the input arrays are square, the output is known as a
    block diagonal matrix.

    Empty sequences (i.e., array-likes of zero size) will not be ignored.
    Noteworthy, both [] and [[]] are treated as matrices with shape ``(1,0)``.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import block_diag
    >>> A = [[1, 0],
    ...      [0, 1]]
    >>> B = [[3, 4, 5],
    ...      [6, 7, 8]]
    >>> C = [[7]]
    >>> P = np.zeros((2, 0), dtype='int32')
    >>> block_diag(A, B, C)
    array([[1, 0, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0],
           [0, 0, 3, 4, 5, 0],
           [0, 0, 6, 7, 8, 0],
           [0, 0, 0, 0, 0, 7]])
    >>> block_diag(A, P, B, C)
    array([[1, 0, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0],
           [0, 0, 3, 4, 5, 0],
           [0, 0, 6, 7, 8, 0],
           [0, 0, 0, 0, 0, 7]])
    >>> block_diag(1.0, [2, 3], [[4, 5], [6, 7]])
    array([[ 1.,  0.,  0.,  0.,  0.],
           [ 0.,  2.,  3.,  0.,  0.],
           [ 0.,  0.,  0.,  4.,  5.],
           [ 0.,  0.,  0.,  6.,  7.]])

     r0   zFarguments in the following positions have dimension greater than 2: %sr   rK   r1   )r   r   )r   
atleast_2dr8   r    rA   r6   r7   r   result_typer2   rC   sum	enumerate)arrsrH   kbad_argsshapesarr	out_dtypeoutr$   r#   r=   rrccs                r(   r
   r
   b  sF   | rzu&*+dBMM!dD+ T+@+atAw||a/?+H@ .089 : 	: XX-1qww-.Fd ;dsd ;<I
((266&q)
;CDAq (8B"&q'Aa"fHaBh	R	R ) J! ,@
 . ;s   D-D2D2D7/D<c           
         t        j                  |       } | j                  dk7  rt        d      | j                  dk  rt        d      | d   dk(  rt        d      | dd  d| d   z  z  }| j                  }t        j
                  |dz
  |dz
  f|j                  	      }||d<   d|t        t        d|dz
              t        t        d|dz
              f<   |S )
aW  
    Create a companion matrix.

    Create the companion matrix [1]_ associated with the polynomial whose
    coefficients are given in `a`.

    Parameters
    ----------
    a : (N,) array_like
        1-D array of polynomial coefficients. The length of `a` must be
        at least two, and ``a[0]`` must not be zero.

    Returns
    -------
    c : (N-1, N-1) ndarray
        The first row of `c` is ``-a[1:]/a[0]``, and the first
        sub-diagonal is all ones.  The data-type of the array is the same
        as the data-type of ``1.0*a[0]``.

    Raises
    ------
    ValueError
        If any of the following are true: a) ``a.ndim != 1``;
        b) ``a.size < 2``; c) ``a[0] == 0``.

    Notes
    -----
    .. versionadded:: 0.8.0

    References
    ----------
    .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*.  Cambridge, UK:
        Cambridge University Press, 1999, pp. 146-7.

    Examples
    --------
    >>> from scipy.linalg import companion
    >>> companion([1, -10, 31, -30])
    array([[ 10., -31.,  30.],
           [  1.,   0.,   0.],
           [  0.,   1.,   0.]])

    r   z6Incorrect shape for `a`.  `a` must be one-dimensional.r0   z%The length of `a` must be at least 2.r   z.The first coefficient in `a` must not be zero.N      ?r1   r?   )rH   	first_rowr'   r#   s       r(   r   r     s    X 	aAvv{ , - 	- 	vvz@AAtqyIJJ12#!*%I	A
!a%Qy7AAaD67Ad5AE?T%1q5/223Hr)   c                    t        j                  t        j                  | | f      d      t        j                  t        j                  |             z
  }t        j                  |       t        j                  d| dz         z  }d|d<   | |d<   |t        j
                  |      ddt         j                  f   z  }|r|S |dd S )a1  
    Create an Helmert matrix of order `n`.

    This has applications in statistics, compositional or simplicial analysis,
    and in Aitchison geometry.

    Parameters
    ----------
    n : int
        The size of the array to create.
    full : bool, optional
        If True the (n, n) ndarray will be returned.
        Otherwise the submatrix that does not include the first
        row will be returned.
        Default: False.

    Returns
    -------
    M : ndarray
        The Helmert matrix.
        The shape is (n, n) or (n-1, n) depending on the `full` argument.

    Examples
    --------
    >>> from scipy.linalg import helmert
    >>> helmert(5, full=True)
    array([[ 0.4472136 ,  0.4472136 ,  0.4472136 ,  0.4472136 ,  0.4472136 ],
           [ 0.70710678, -0.70710678,  0.        ,  0.        ,  0.        ],
           [ 0.40824829,  0.40824829, -0.81649658,  0.        ,  0.        ],
           [ 0.28867513,  0.28867513,  0.28867513, -0.8660254 ,  0.        ],
           [ 0.2236068 ,  0.2236068 ,  0.2236068 ,  0.2236068 , -0.89442719]])

    r   r   r   N)r   trilonesdiagarangesqrtnewaxis)r'   fullr<   dH_fulls        r(   r   r     s    D 	A$rwwryy|'<<A
		!ryyAaC((AAaDAaDArzzM**Fabzr)   c                 t    ddt        j                  d| z  dz
        z   z  }t        |d|  || dz
  d       }|S )a  
    Create a Hilbert matrix of order `n`.

    Returns the `n` by `n` array with entries `h[i,j] = 1 / (i + j + 1)`.

    Parameters
    ----------
    n : int
        The size of the array to create.

    Returns
    -------
    h : (n, n) ndarray
        The Hilbert matrix.

    See Also
    --------
    invhilbert : Compute the inverse of a Hilbert matrix.

    Notes
    -----
    .. versionadded:: 0.10.0

    Examples
    --------
    >>> from scipy.linalg import hilbert
    >>> hilbert(3)
    array([[ 1.        ,  0.5       ,  0.33333333],
           [ 0.5       ,  0.33333333,  0.25      ],
           [ 0.33333333,  0.25      ,  0.2       ]])

    rd   r0   r   N)r$   )r   rj   r   )r'   valueshs      r(   r   r   "  sF    B C"))AEAI../FvbqzVAEF^,AHr)   c                    ddl m} |r| dkD  rt        }n!t        j                  }nt        j
                  }t        j                  | | f|      }t        |       D ]v  }t        d|dz         D ]b  }||z   }d|z  |dz   z   || |z   | |z
  dz
  |      z   || |z   | |z
  dz
  |      z   ||||      dz  z  |||f<   ||k7  sW|||f   |||f<   d x |S )	aH  
    Compute the inverse of the Hilbert matrix of order `n`.

    The entries in the inverse of a Hilbert matrix are integers. When `n`
    is greater than 14, some entries in the inverse exceed the upper limit
    of 64 bit integers. The `exact` argument provides two options for
    dealing with these large integers.

    Parameters
    ----------
    n : int
        The order of the Hilbert matrix.
    exact : bool, optional
        If False, the data type of the array that is returned is np.float64,
        and the array is an approximation of the inverse.
        If True, the array is the exact integer inverse array. To represent
        the exact inverse when n > 14, the returned array is an object array
        of long integers. For n <= 14, the exact inverse is returned as an
        array with data type np.int64.

    Returns
    -------
    invh : (n, n) ndarray
        The data type of the array is np.float64 if `exact` is False.
        If `exact` is True, the data type is either np.int64 (for n <= 14)
        or object (for n > 14). In the latter case, the objects in the
        array will be long integers.

    See Also
    --------
    hilbert : Create a Hilbert matrix.

    Notes
    -----
    .. versionadded:: 0.10.0

    Examples
    --------
    >>> from scipy.linalg import invhilbert
    >>> invhilbert(4)
    array([[   16.,  -120.,   240.,  -140.],
           [ -120.,  1200., -2700.,  1680.],
           [  240., -2700.,  6480., -4200.],
           [ -140.,  1680., -4200.,  2800.]])
    >>> invhilbert(4, exact=True)
    array([[   16,  -120,   240,  -140],
           [ -120,  1200, -2700,  1680],
           [  240, -2700,  6480, -4200],
           [ -140,  1680, -4200,  2800]], dtype=int64)
    >>> invhilbert(16)[7,7]
    4.2475099528537506e+19
    >>> invhilbert(16, exact=True)[7,7]
    42475099528537378560

    r   comb   r1   r   r   exactr0   )scipy.specialru   objectr   int64float64emptyr8   )r'   rx   ru   r2   invhr=   jrF   s           r(   r   r   H  s    p #r6EHHE

88QF%(D1Xq!a%AAA!)q1u-q1ua!eaiu=>q1ua!eaiu=> q!51Q67DAJ Av!!Q$ZQT
 !  Kr)   c                    ddl m} |dvrt        d      |r| dk\  r/t        j                  | | ft
              }|j                  d       n't        j                  | | ft        j                        }t        |       D ]%  }t        |dz         D ]  } |||d	      |||f<    ' n |t        j                  d
| d
| f    }|dk(  r|}|S |dk(  r|j                  }|S t        j                  ||j                        }|S )at  
    Returns the n x n Pascal matrix.

    The Pascal matrix is a matrix containing the binomial coefficients as
    its elements.

    Parameters
    ----------
    n : int
        The size of the matrix to create; that is, the result is an n x n
        matrix.
    kind : str, optional
        Must be one of 'symmetric', 'lower', or 'upper'.
        Default is 'symmetric'.
    exact : bool, optional
        If `exact` is True, the result is either an array of type
        numpy.uint64 (if n < 35) or an object array of Python long integers.
        If `exact` is False, the coefficients in the matrix are computed using
        `scipy.special.comb` with `exact=False`. The result will be a floating
        point array, and the values in the array will not be the exact
        coefficients, but this version is much faster than `exact=True`.

    Returns
    -------
    p : (n, n) ndarray
        The Pascal matrix.

    See Also
    --------
    invpascal

    Notes
    -----
    See https://en.wikipedia.org/wiki/Pascal_matrix for more information
    about Pascal matrices.

    .. versionadded:: 0.11.0

    Examples
    --------
    >>> from scipy.linalg import pascal
    >>> pascal(4)
    array([[ 1,  1,  1,  1],
           [ 1,  2,  3,  4],
           [ 1,  3,  6, 10],
           [ 1,  4, 10, 20]], dtype=uint64)
    >>> pascal(4, kind='lower')
    array([[1, 0, 0, 0],
           [1, 1, 0, 0],
           [1, 2, 1, 0],
           [1, 3, 3, 1]], dtype=uint64)
    >>> pascal(50)[-1, -1]
    25477612258980856902730428600
    >>> from scipy.special import comb
    >>> comb(98, 49, exact=True)
    25477612258980856902730428600

    r   rt   	symmetriclowerupperz-kind must be 'symmetric', 'lower', or 'upper'#   r1   r   Trw   Nr   r   )ry   ru   r6   r   r}   rz   fillrC   uint64r8   ogridTdot)r'   kindrx   ru   L_nr=   r   ps           r(   r   r     s    x #22HII7((Aq60CHHQK((Aq63CqA1q5\ AT2AqD	 "  BHHRaR!V$%w H 
EE H FF3Hr)   c           
         ddl m} |dvrt        d      |dk(  r|r| dkD  rt        }n!t        j
                  }nt        j                  }t	        j                  | | f|      }t        |       D ]s  }t        d|dz         D ]_  }d}t        | |z
        D ](  }	| |||	z   |	|	       |||	z   ||	z   |z
  |	      z  z  }* d
||z
  z  |z  |||f<   ||k7  sT|||f   |||f<   a u |S t        | ||      }|j                  t        j                  k(  r|j                  t        j
                        }|t        d
t	        j                  |       z        j                  |j                        z  }|S )a  
    Returns the inverse of the n x n Pascal matrix.

    The Pascal matrix is a matrix containing the binomial coefficients as
    its elements.

    Parameters
    ----------
    n : int
        The size of the matrix to create; that is, the result is an n x n
        matrix.
    kind : str, optional
        Must be one of 'symmetric', 'lower', or 'upper'.
        Default is 'symmetric'.
    exact : bool, optional
        If `exact` is True, the result is either an array of type
        ``numpy.int64`` (if `n` <= 35) or an object array of Python integers.
        If `exact` is False, the coefficients in the matrix are computed using
        `scipy.special.comb` with `exact=False`. The result will be a floating
        point array, and for large `n`, the values in the array will not be the
        exact coefficients.

    Returns
    -------
    invp : (n, n) ndarray
        The inverse of the Pascal matrix.

    See Also
    --------
    pascal

    Notes
    -----

    .. versionadded:: 0.16.0

    References
    ----------
    .. [1] "Pascal matrix", https://en.wikipedia.org/wiki/Pascal_matrix
    .. [2] Cohen, A. M., "The inverse of a Pascal matrix", Mathematical
           Gazette, 59(408), pp. 111-112, 1975.

    Examples
    --------
    >>> from scipy.linalg import invpascal, pascal
    >>> invp = invpascal(5)
    >>> invp
    array([[  5, -10,  10,  -5,   1],
           [-10,  30, -35,  19,  -4],
           [ 10, -35,  46, -27,   6],
           [ -5,  19, -27,  17,  -4],
           [  1,  -4,   6,  -4,   1]])

    >>> p = pascal(5)
    >>> p.dot(invp)
    array([[ 1.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  0.,  1.]])

    An example of the use of `kind` and `exact`:

    >>> invpascal(5, kind='lower', exact=False)
    array([[ 1., -0.,  0., -0.,  0.],
           [-1.,  1., -0.,  0., -0.],
           [ 1., -2.,  1., -0.,  0.],
           [-1.,  3., -3.,  1., -0.],
           [ 1., -4.,  6., -4.,  1.]])

    r   rt   r   z/'kind' must be 'symmetric', 'lower' or 'upper'.r   "   r1   r   rw   r   )r   rx   )ry   ru   r6   rz   r   r{   r|   r}   r8   r   r2   r   viewr   rj   astype)
r'   r   rx   ru   dtinvpr=   r   vr[   s
             r(   r   r     ss   P #22JKK{2vXXBxxAb)qA1a!e_q1uAa!eQe4tAE1q519BG8I I IA & !AE]Q.QT
6!%adDAJ % * K ad%0::" 99RXX&D 	"ryy|+,33DJJ??Kr)   c                 L   |dvrt        d|d      t        j                  dt        j                  z  t        j                  |       z  | z        j                  dd      }|t        j                  |       z  }|dk(  r|t        j                  |       z  }|S |dk(  r|| z  }|S )	a  
    Discrete Fourier transform matrix.

    Create the matrix that computes the discrete Fourier transform of a
    sequence [1]_. The nth primitive root of unity used to generate the
    matrix is exp(-2*pi*i/n), where i = sqrt(-1).

    Parameters
    ----------
    n : int
        Size the matrix to create.
    scale : str, optional
        Must be None, 'sqrtn', or 'n'.
        If `scale` is 'sqrtn', the matrix is divided by `sqrt(n)`.
        If `scale` is 'n', the matrix is divided by `n`.
        If `scale` is None (the default), the matrix is not normalized, and the
        return value is simply the Vandermonde matrix of the roots of unity.

    Returns
    -------
    m : (n, n) ndarray
        The DFT matrix.

    Notes
    -----
    When `scale` is None, multiplying a vector by the matrix returned by
    `dft` is mathematically equivalent to (but much less efficient than)
    the calculation performed by `scipy.fft.fft`.

    .. versionadded:: 0.14.0

    References
    ----------
    .. [1] "DFT matrix", https://en.wikipedia.org/wiki/DFT_matrix

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import dft
    >>> np.set_printoptions(precision=2, suppress=True)  # for compact output
    >>> m = dft(5)
    >>> m
    array([[ 1.  +0.j  ,  1.  +0.j  ,  1.  +0.j  ,  1.  +0.j  ,  1.  +0.j  ],
           [ 1.  +0.j  ,  0.31-0.95j, -0.81-0.59j, -0.81+0.59j,  0.31+0.95j],
           [ 1.  +0.j  , -0.81-0.59j,  0.31+0.95j,  0.31-0.95j, -0.81+0.59j],
           [ 1.  +0.j  , -0.81+0.59j,  0.31-0.95j,  0.31+0.95j, -0.81-0.59j],
           [ 1.  +0.j  ,  0.31+0.95j, -0.81+0.59j, -0.81-0.59j,  0.31-0.95j]])
    >>> x = np.array([1, 2, 3, 0, 3])
    >>> m @ x  # Compute the DFT of x
    array([ 9.  +0.j  ,  0.12-0.81j, -2.12+3.44j, -2.12-3.44j,  0.12+0.81j])

    Verify that ``m @ x`` is the same as ``fft(x)``.

    >>> from scipy.fft import fft
    >>> fft(x)     # Same result as m @ x
    array([ 9.  +0.j  ,  0.12-0.81j, -2.12+3.44j, -2.12-3.44j,  0.12+0.81j])
    )Nsqrtnr'   z%scale must be None, 'sqrtn', or 'n'; z is not valid.y              r   r   r   r'   )r6   r   exppirj   rO   r4   rk   )r'   scaleomegasrR   s       r(   r   r   Y  s    t ((@!9N4 5 	5 VVC"%%K"))A,.23;;BBF"))A,A	TYYq\ H 
#	QHr)   c                 >   t        j                  |       } | j                  dk7  rt        d      | j                  dk(  rt        j
                  g t              S | j                  dk(  rt        j
                  dgg      S t        j                  | dddf   | z
        S )a  Returns a symmetric Fiedler matrix

    Given an sequence of numbers `a`, Fiedler matrices have the structure
    ``F[i, j] = np.abs(a[i] - a[j])``, and hence zero diagonals and nonnegative
    entries. A Fiedler matrix has a dominant positive eigenvalue and other
    eigenvalues are negative. Although not valid generally, for certain inputs,
    the inverse and the determinant can be derived explicitly as given in [1]_.

    Parameters
    ----------
    a : (n,) array_like
        coefficient array

    Returns
    -------
    F : (n, n) ndarray

    See Also
    --------
    circulant, toeplitz

    Notes
    -----

    .. versionadded:: 1.3.0

    References
    ----------
    .. [1] J. Todd, "Basic Numerical Mathematics: Vol.2 : Numerical Algebra",
        1977, Birkhauser, :doi:`10.1007/978-3-0348-7286-7`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import det, inv, fiedler
    >>> a = [1, 4, 12, 45, 77]
    >>> n = len(a)
    >>> A = fiedler(a)
    >>> A
    array([[ 0,  3, 11, 44, 76],
           [ 3,  0,  8, 41, 73],
           [11,  8,  0, 33, 65],
           [44, 41, 33,  0, 32],
           [76, 73, 65, 32,  0]])

    The explicit formulas for determinant and inverse seem to hold only for
    monotonically increasing/decreasing arrays. Note the tridiagonal structure
    and the corners.

    >>> Ai = inv(A)
    >>> Ai[np.abs(Ai) < 1e-12] = 0.  # cleanup the numerical noise for display
    >>> Ai
    array([[-0.16008772,  0.16666667,  0.        ,  0.        ,  0.00657895],
           [ 0.16666667, -0.22916667,  0.0625    ,  0.        ,  0.        ],
           [ 0.        ,  0.0625    , -0.07765152,  0.01515152,  0.        ],
           [ 0.        ,  0.        ,  0.01515152, -0.03077652,  0.015625  ],
           [ 0.00657895,  0.        ,  0.        ,  0.015625  , -0.00904605]])
    >>> det(A)
    15409151.999999998
    >>> (-1)**(n-1) * 2**(n-2) * np.diff(a).prod() * (a[-1] - a[0])
    15409152

    r   zInput 'a' must be a 1D array.r   r1           N)r   r@   rA   r6   rB   r7   floatabs)rH   s    r(   r   r     s    @ 	aAvv{899vv{xx%((	
1xx"vva4j1n%%r)   c                    t        j                  |       } | j                  dk7  rt        d      | j                  dk  rQ| j                  dk(  r!t        j
                  | | d   z  d    gg      S t        j
                  g | j                        S | d   dk(  rt        d      | | d   z  } | j                  dz
  }t        j                  ||f| j                        }d	|t        d
|d      t        d|dz
  d      f<   | d
dd    |t        d|d      t        d|dz
  d      f<   d	|t        d|dz
  d      t        d|d      f<   | ddd    |t        d|dz
  d      t        d|d      f<   | d    dg|ddgdf<   |S )a   Returns a Fiedler companion matrix

    Given a polynomial coefficient array ``a``, this function forms a
    pentadiagonal matrix with a special structure whose eigenvalues coincides
    with the roots of ``a``.

    Parameters
    ----------
    a : (N,) array_like
        1-D array of polynomial coefficients in descending order with a nonzero
        leading coefficient. For ``N < 2``, an empty array is returned.

    Returns
    -------
    c : (N-1, N-1) ndarray
        Resulting companion matrix

    See Also
    --------
    companion

    Notes
    -----
    Similar to `companion` the leading coefficient should be nonzero. In the case
    the leading coefficient is not 1, other coefficients are rescaled before
    the array generation. To avoid numerical issues, it is best to provide a
    monic polynomial.

    .. versionadded:: 1.3.0

    References
    ----------
    .. [1] M. Fiedler, " A note on companion matrices", Linear Algebra and its
        Applications, 2003, :doi:`10.1016/S0024-3795(03)00548-2`

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import fiedler_companion, eigvals
    >>> p = np.poly(np.arange(1, 9, 2))  # [1., -16., 86., -176., 105.]
    >>> fc = fiedler_companion(p)
    >>> fc
    array([[  16.,  -86.,    1.,    0.],
           [   1.,    0.,    0.,    0.],
           [   0.,  176.,    0., -105.],
           [   0.,    1.,    0.,    0.]])
    >>> eigvals(fc)
    array([7.+0.j, 5.+0.j, 3.+0.j, 1.+0.j])

    r   zInput 'a' must be a 1-D array.r0   r   r   r1   r   zLeading coefficient is zero.rd      N)	r   r@   rA   r6   rB   r7   r2   rC   r8   )rH   r'   r#   s      r(   r   r     s   f 	aAvv{9::vv{66Q;88!A$|m_-..xx!''**trz788	!A$A	
A
!Qqww'A*,AeAq!neAqsA&&'+,QTT7(AeAq!neAqsA&&'*,AeAqsAaA&'+,QTT7(AeAqsAaA&'qTE1:Aq!faiLHr)   c                    |dk  rt        d      t        j                  |       } | j                  dk7  rt        d      | j                  dk(  rt        d      |dvrt        d      t        j
                  | d|dz
  fd      }t        j
                  | d	d	d
   d|dz
  fd      }|dk(  rIt        |t        |             dz
  }|dz  }||z
  }||t        |      |z
   }|| |z
  t        |      |z
   }	nN|dk(  rAt        |t        |             dz
  }|}||t        |      |z
   }|| |z
  t        |      |z
   }	n|}|| d	 }	t        ||	      S )a  
    Construct a convolution matrix.

    Constructs the Toeplitz matrix representing one-dimensional
    convolution [1]_.  See the notes below for details.

    Parameters
    ----------
    a : (m,) array_like
        The 1-D array to convolve.
    n : int
        The number of columns in the resulting matrix.  It gives the length
        of the input to be convolved with `a`.  This is analogous to the
        length of `v` in ``numpy.convolve(a, v)``.
    mode : str
        This is analogous to `mode` in ``numpy.convolve(v, a, mode)``.
        It must be one of ('full', 'valid', 'same').
        See below for how `mode` determines the shape of the result.

    Returns
    -------
    A : (k, n) ndarray
        The convolution matrix whose row count `k` depends on `mode`::

            =======  =========================
             mode    k
            =======  =========================
            'full'   m + n -1
            'same'   max(m, n)
            'valid'  max(m, n) - min(m, n) + 1
            =======  =========================

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

    Notes
    -----
    The code::

        A = convolution_matrix(a, n, mode)

    creates a Toeplitz matrix `A` such that ``A @ v`` is equivalent to
    using ``convolve(a, v, mode)``.  The returned array always has `n`
    columns.  The number of rows depends on the specified `mode`, as
    explained above.

    In the default 'full' mode, the entries of `A` are given by::

        A[i, j] == (a[i-j] if (0 <= (i-j) < m) else 0)

    where ``m = len(a)``.  Suppose, for example, the input array is
    ``[x, y, z]``.  The convolution matrix has the form::

        [x, 0, 0, ..., 0, 0]
        [y, x, 0, ..., 0, 0]
        [z, y, x, ..., 0, 0]
        ...
        [0, 0, 0, ..., x, 0]
        [0, 0, 0, ..., y, x]
        [0, 0, 0, ..., z, y]
        [0, 0, 0, ..., 0, z]

    In 'valid' mode, the entries of `A` are given by::

        A[i, j] == (a[i-j+m-1] if (0 <= (i-j+m-1) < m) else 0)

    This corresponds to a matrix whose rows are the subset of those from
    the 'full' case where all the coefficients in `a` are contained in the
    row.  For input ``[x, y, z]``, this array looks like::

        [z, y, x, 0, 0, ..., 0, 0, 0]
        [0, z, y, x, 0, ..., 0, 0, 0]
        [0, 0, z, y, x, ..., 0, 0, 0]
        ...
        [0, 0, 0, 0, 0, ..., x, 0, 0]
        [0, 0, 0, 0, 0, ..., y, x, 0]
        [0, 0, 0, 0, 0, ..., z, y, x]

    In the 'same' mode, the entries of `A` are given by::

        d = (m - 1) // 2
        A[i, j] == (a[i-j+d] if (0 <= (i-j+d) < m) else 0)

    The typical application of the 'same' mode is when one has a signal of
    length `n` (with `n` greater than ``len(a)``), and the desired output
    is a filtered signal that is still of length `n`.

    For input ``[x, y, z]``, this array looks like::

        [y, x, 0, 0, ..., 0, 0, 0]
        [z, y, x, 0, ..., 0, 0, 0]
        [0, z, y, x, ..., 0, 0, 0]
        [0, 0, z, y, ..., 0, 0, 0]
        ...
        [0, 0, 0, 0, ..., y, x, 0]
        [0, 0, 0, 0, ..., z, y, x]
        [0, 0, 0, 0, ..., 0, z, y]

    .. versionadded:: 1.5.0

    References
    ----------
    .. [1] "Convolution", https://en.wikipedia.org/wiki/Convolution

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import convolution_matrix
    >>> A = convolution_matrix([-1, 4, -2], 5, mode='same')
    >>> A
    array([[ 4, -1,  0,  0,  0],
           [-2,  4, -1,  0,  0],
           [ 0, -2,  4, -1,  0],
           [ 0,  0, -2,  4, -1],
           [ 0,  0,  0, -2,  4]])

    Compare multiplication by `A` with the use of `numpy.convolve`.

    >>> x = np.array([1, 2, 0, -3, 0.5])
    >>> A @ x
    array([  2. ,   6. ,  -1. , -12.5,   8. ])

    Verify that ``A @ x`` produced the same result as applying the
    convolution function.

    >>> np.convolve([-1, 4, -2], x, mode='same')
    array([  2. ,   6. ,  -1. , -12.5,   8. ])

    For comparison to the case ``mode='same'`` shown above, here are the
    matrices produced by ``mode='full'`` and ``mode='valid'`` for the
    same coefficients and size.

    >>> convolution_matrix([-1, 4, -2], 5, mode='full')
    array([[-1,  0,  0,  0,  0],
           [ 4, -1,  0,  0,  0],
           [-2,  4, -1,  0,  0],
           [ 0, -2,  4, -1,  0],
           [ 0,  0, -2,  4, -1],
           [ 0,  0,  0, -2,  4],
           [ 0,  0,  0,  0, -2]])

    >>> convolution_matrix([-1, 4, -2], 5, mode='valid')
    array([[-2,  4, -1,  0,  0],
           [ 0, -2,  4, -1,  0],
           [ 0,  0, -2,  4, -1]])
    r   zn must be a positive integer.r   z;convolution_matrix expects a one-dimensional array as inputzlen(a) must be at least 1.)rm   validsamez8'mode' argument must be one of ('full', 'valid', 'same')constantNr   r   r0   r   )	r6   r   r   rA   rB   padminr    r   )
rH   r'   modeazraztrimtbtecol0row0s
             r(   r   r   ;  sz   h 	Av899


1Avv{ * + 	+vv{566,,FH 	H 
Aqs8Z	(B
&&4R41ac(J
/Cv~1c!f~!1WBY"SWRZ A2b5S"%	CF^a"SWRZ A2b5S"%A23xD$r)   )N)F)r   T)rm   )r4   numpyr   numpy.lib.stride_tricksr   __all__r   r   r   r3   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   rU   r)   r(   <module>r      s      .A<N~&IR6Br  6rEP,=^Pf=@*Z#LJZSlk\DNJ&ZK\t r)   