
    M/Ph                     :    d Z ddlZg dZd
dZd Zd Z	 	 dd	ZdS )z*
Linear Algebra solvers and other helpers
    N)logdet_symmstationary_solvetransf_constraintsmatrix_sqrtFc                    ddl m} |r,t          j        | | j        k              st          d          |                    | d          \  }}dt          j        t          j        |	                                                    z  S )z
    Return log(det(m)) asserting positive definiteness of m.

    Parameters
    ----------
    m : array_like
        2d array that is positive-definite (and symmetric)

    Returns
    -------
    logdet : float
        The log-determinant of m.
    r   linalgzm is not symmetric.T)lower   )
scipyr	   npallT
ValueError
cho_factorsumlogdiagonal)m
check_symmr	   c_s        X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tools/linalg.pyr   r   
   s      4va13h 	42333Qd++DAqRVBF1::<<(())))    c           
      0   | dd         }|j         }|j         dk    r|dddf         }|ddddf         }t          dt          |                    D ].}| d|         ddd         }||ddf         t          j        ||          z
  dt          j        ||ddd                   z
  z  }|t          j        |ddd         |          z
  }t          j        ||dddf         fd          }|t          |          dz
  k    r n{| |         }	|	t          j        ||          z
  dt          j        ||ddd                   z
  z  }|||ddd         z  z
  }t          j        |t          j        |         f          }0|dk    r|dddf         }|S )aH  
    Solve a linear system for a Toeplitz correlation matrix.

    A Toeplitz correlation matrix represents the covariance of a
    stationary series with unit variance.

    Parameters
    ----------
    r : array_like
        A vector describing the coefficient matrix.  r[0] is the first
        band next to the diagonal, r[1] is the second band, etc.
    b : array_like
        The right-hand side for which we are solving, i.e. we solve
        Tx = b and return b, where T is the Toeplitz coefficient matrix.

    Returns
    -------
    The solution to the linear system.
    r      N)axis)ndimrangelenr   dotouterconcatenater_)
rbdbdimxjrfazrns
             r   r   r       s   * 
1Q3B
&Cv{{aaagJ	!A#qqq&	A1c!ff + +qsVDDbD\q!!!tWrvb!}}$RVB44R4-A-A)ABDDbD1%%%NAqqqqz?333A
??EqT"&R.. QBtttH)=)=%=>2ddd8O^QaM**
axxaaadGHr   c                     ddl m} | j        d         }|                    t	          j        |                     \  }}|dd|df         }|S )a  use QR to get transformation matrix to impose constraint

    Parameters
    ----------
    constraints : ndarray, 2-D
        restriction matrix with one constraints in rows

    Returns
    -------
    transf : ndarray
        transformation matrix to reparameterize so that constraint is
        imposed

    Notes
    -----
    This is currently and internal helper function for GAM.
    API not stable and will most likely change.

    The code for this function was taken from patsy spline handling, and
    corresponds to the reparameterization used by Wood in R's mgcv package.

    See Also
    --------
    statsmodels.base._constraints.TransformRestriction : class to impose
        constraints by reparameterization used by `_fit_constrained`.
    r   r   N)r   r	   shapeqrr   	transpose)constraintsr	   r   qr   transfs         r   r   r   P   s\    8 !A99R\+..//DAqqqq!""uXFMr   V瞯<c                 X   t           j                            |           \  }}}t          j        || k               rddl}|                    d           |s||k    }	d|||k     <   n||k     }	d|||k    <   t          j        ||	                   }
|rdt          j        ||	                   z  }
|rKt          j        |dd|	f         t          j        t          j        |
          ||	                             }n-t          j        t          j        |
          ||	                   }|S )a  matrix square root for symmetric matrices

    Usage is for decomposing a covariance function S into a square root R
    such that

        R' R = S if inverse is False, or
        R' R = pinv(S) if inverse is True

    Parameters
    ----------
    mat : array_like, 2-d square
        symmetric square matrix for which square root or inverse square
        root is computed.
        There is no checking for whether the matrix is symmetric.
        A warning is issued if some singular values are negative, i.e.
        below the negative of the threshold.
    inverse : bool
        If False (default), then the matrix square root is returned.
        If inverse is True, then the matrix square root of the inverse
        matrix is returned.
    full : bool
        If full is False (default, then the square root has reduce number
        of rows if the matrix is singular, i.e. has singular values below
        the threshold.
    nullspace : bool
        If nullspace is true, then the matrix square root of the null space
        of the matrix is returned.
    threshold : float
        Singular values below the threshold are dropped.

    Returns
    -------
    msqrt : ndarray
        matrix square root or square root of inverse matrix.
    r   Nz!some singular values are negativer   )	r   r	   svdanywarningswarnsqrtr"   diag)matinversefull	nullspace	thresholdusvr;   masksqrt_sr'   s               r   r   r   t   s   L immC  GAq!	va9*n ;9::: 9}!i-9}!i-WQtWF &RWQtW%%% -F1QQQW:rvbgfooqw??@@F276??AdG,,Hr   )F)FFFr7   )__doc__numpyr   __all__r   r   r   r    r   r   <module>rM      s          * * * *,- - -`! ! !H ;@: : : : : :r   