
    `i0                         d dl Z d dlZd dlZd dlZd dlmZ d dlZd Zd Z	ed             Z
ed             Zed             Zd Zd	 Zd
 ZddZd Zd ZdS )    N)RankWarningc                 B     d  fd}t          j        |           S )Nc                 F   t          | t          j                  r| j        S t          j        |           rt          j        |           S t          | t          j                  r0t          j        |           } | j        dk    r| S t          d          t          d          )N   z)Multidimensional inputs are not supportedzUnsupported type)

isinstancecupypoly1d_coeffsisscalar
atleast_1dndarrayndim
ValueError	TypeErrorxs    k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/lib/_routines_poly.py_get_coeffsz'_wraps_polyroutine.<locals>._get_coeffs   s    a%% 	9= 	&?1%%%a&& 	J""Av{{HIII*+++    c                     fd| D             } | }t          d | D                       r|S t          |t          j                  rt          j        |          S t          |t
                    rt          d |D                       S J )Nc                 &    g | ]} |          S  r   ).0r   r   s     r   
<listcomp>z7_wraps_polyroutine.<locals>.wrapper.<locals>.<listcomp>   s!    ///Q++a..///r   c              3   L   K   | ]}t          |t          j                   V   d S )N)r   r   r	   r   r   s     r   	<genexpr>z6_wraps_polyroutine.<locals>.wrapper.<locals>.<genexpr>   s1      <<!:a---<<<<<<r   c                 6    g | ]}t          j        |          S r   )r   r	   r   s     r   r   z7_wraps_polyroutine.<locals>.wrapper.<locals>.<listcomp>!   s     666Q$+a..666r   )allr   r   r   r	   tuple)argscoeffsoutr   funcs      r   wrapperz#_wraps_polyroutine.<locals>.wrapper   s    ////$///dFm<<t<<<<< 	Jc4<(( 	$;s###c5!! 	866#666777ur   )	functoolsupdate_wrapper)r$   r%   r   s   ` @r   _wraps_polyroutiner(      sF    
, 
, 
,
 
 
 
 
 
 #GT222r   c                    | }|j         dk    r|j        d         |j        d         k    rl|j        d         dk    r[t          j        ||                                j                  r t          j                            |          }n]t          d          |j         dk    r4|	                    t          j
        |j        j                  d          }nt          d          |j        dk    rdS d|j        dz
                                  z  }t          j        |df|j                  }|d	d	df                             d           t          j        ||d	|j        df         
           |dk    rA|dz  }t          j        j                            |d	|         ||d	         d          }|dk    A|dd	|j        dz   f         S )a  Computes the coefficients of a polynomial with the given roots sequence.

    Args:
        seq_of_zeros (cupy.ndarray): a sequence of polynomial roots.

    Returns:
        cupy.ndarray: polynomial coefficients from highest to lowest degree.

    .. warning::

        This function doesn't support general 2d square arrays currently.
        Only complex Hermitian and real symmetric 2d arrays are allowed.

    .. seealso:: :func:`numpy.poly`

       r   r   KOnly complex Hermitian and real symmetric 2d arrays are supported currentlyFcopyz.Input must be 1d or non-empty square 2d array.g      ?N)r#   full)r   shaper   array_equalconjTlinalgeigvalshNotImplementedErrorastypemintypecodedtypecharr   size
bit_lengthzerosfillnegative_mathmisc_fft_convolve)seq_of_zerosr   r:   as       r   polyrD   '   s   " 	Av{{qwqzQWQZ//AGAJ!OOAqvvxxz** 	3$$Q''AA% '2 3 3 3 
1HHT%agl33%H@@IJJJv{{s!'')))D
D!9ag&&AaaadGLLOOOM!7AF7A:''''
((qyJO))!ETE(AdeeHfEE (( Q!^r   c                     | j         |j         k     r|| }} t          j        || j         |j         z
  df          }|                    t          j        | |          d          }|| z  }|S )a<  Computes the sum of two polynomials.

    Args:
        a1 (scalar, cupy.ndarray or cupy.poly1d): first input polynomial.
        a2 (scalar, cupy.ndarray or cupy.poly1d): second input polynomial.

    Returns:
        cupy.ndarray or cupy.poly1d: The sum of the inputs.

    .. seealso:: :func:`numpy.polyadd`

    r   Fr,   )r:   r   padr6   result_typea1a2r#   s      r   polyaddrK   R   sk     
wRB
(2"')1-
.
.C
**T%b"--E*
:
:C2ICJr   c                    | j         d         |j         d         k    r`t          j        | |j         d         | j         d         z
  df          }|                    t          j        | |          d          }||z  }net          j        || j         d         |j         d         z
  df          }|                    t          j        | |          d          }|d|z  | z
  z  }|S )aJ  Computes the difference of two polynomials.

    Args:
        a1 (scalar, cupy.ndarray or cupy.poly1d): first input polynomial.
        a2 (scalar, cupy.ndarray or cupy.poly1d): second input polynomial.

    Returns:
        cupy.ndarray or cupy.poly1d: The difference of the inputs.

    .. seealso:: :func:`numpy.polysub`

    r   Fr,   r*   )r/   r   rF   r6   rG   rH   s      r   polysubrM   h   s     
x{bhqk!!hrBHQK"(1+5q9::jj)"b11j>>r	hrBHQK"(1+5q9::jj)"b11j>>q3w|Jr   c                    t          j        | d          } t          j        |d          }| j        dk    rt          j        dg| j                  } |j        dk    rt          j        dg|j                  }t          j        | |          S )aD  Computes the product of two polynomials.

    Args:
        a1 (scalar, cupy.ndarray or cupy.poly1d): first input polynomial.
        a2 (scalar, cupy.ndarray or cupy.poly1d): second input polynomial.

    Returns:
        cupy.ndarray or cupy.poly1d: The product of the inputs.

    .. seealso:: :func:`numpy.polymul`

    f)trimr   g        )r   
trim_zerosr:   arrayr8   convolve)rI   rJ   s     r   polymulrT      s~     
#	&	&	&B	#	&	&	&B	w!||Zbh''	w!||Zbh''=R   r   c           	          |dk    rdS |dk    r| S |dz  dk    r&t          t          j        | |           |dz            S t          j        | t          t          j        | |           |dz
  dz                      S )Nr   r   r*   )_polypowr   rS   )r   ns     r   _polypow_directrX      s{    AvvqAvv1uzza++Q!V444=HT]1a%8%81q5Q,GGHHHr   c                 ,   |dk    rdS |dk    r| S t           j        j                            | | d          }|dk    rt	          | |          S |dk    r| j        j        dk    r#t           j        j        t           j        j        }}n"t           j        j	        t           j        j
        }}| j        dz
  |z  dz   }t          j        j                            |          } || |          }t          j        |||          } |||          }	|	d |         S J )Nr   r   r.   directfftc)r   r?   r@   _choose_conv_methodrX   r8   kindr[   ifftrfftirfftr:   cupyxscipynext_fast_lenpower)
r   rW   methodr[   r_   out_sizer:   fxfyys
             r   rV   rV      s    AvvqAvvZ_00Av>>Fq!$$$	57<3dhmCCtx~CFQJ!#a'{,,X66SD\\ZAr""DTNN(|ur   c                     | j         j        dk    r!|                     t          j        d          S |                     t          j        d          S )Nr\   Fr,   )r8   r^   r6   numpy
complex128float64r   s    r   _polyfit_typecastro      sB    w|sxx(ux55588EM8...r   Fc                    | j         j        dk    r|j         j        dk    rt          d          |j         t          j        k    rt          d          t          |           } t          |          }t          |          }|dk     rt          d          | j
        dk    rt          d          | j        dk    rt          d	          |j
        dk     s|j
        d
k    rt          d          | j        |j        d         k    rt          d          t          j        j                            | |          dddddf         }|}|st          |          }|j
        dk    rt          d          |j        | j        k    rt          d          ||dddf         z  }|j
        d
k    r|dddf         }||z  }|&| j        t          j        | j                   j        z  }t          j        t          j        |                              d                    }	||	z  }t          j                            |||          \  }
}}}|j
        dk    r|	                    dd          }	|
|	z  }
|dz   }||k    r |sd}t1          j        |t4          d           |r+|j         j        dk    rt          j        |          }|
||||fS |rt          j                            t          j        |j        |                    }|t          j        |	|	          z  }|dk    rd}n(| j        |k    r|| j        |z
  z  }nt          d          |j
        dk    r|d         }|
||z  fS |
S )a  Returns the least squares fit of polynomial of degree deg
    to the data y sampled at x.

    Args:
        x (cupy.ndarray): x-coordinates of the sample points of shape (M,).
        y (cupy.ndarray): y-coordinates of the sample points of shape
            (M,) or (M, K).
        deg (int): degree of the fitting polynomial.
        rcond (float, optional): relative condition number of the fit.
            The default value is ``len(x) * eps``.
        full (bool, optional): indicator of the return value nature.
            When False (default), only the coefficients are returned.
            When True, diagnostic information is also returned.
        w (cupy.ndarray, optional): weights applied to the y-coordinates
            of the sample points of shape (M,).
        cov (bool or str, optional): if given, returns the coefficients
            along with the covariance matrix.

    Returns:
        cupy.ndarray or tuple:
        p (cupy.ndarray of shape (deg + 1,) or (deg + 1, K)):
            Polynomial coefficients from highest to lowest degree
        residuals, rank, singular_values, rcond         (cupy.ndarray, int, cupy.ndarray, float):
            Present only if ``full=True``.
            Sum of squared residuals of the least-squares fit,
            rank of the scaled Vandermonde coefficient matrix,
            its singular values, and the specified value of ``rcond``.
        V (cupy.ndarray of shape (M, M) or (M, M, K)):
            Present only if ``full=False`` and ``cov=True``.
            The covariance matrix of the polynomial coefficient estimates.

    .. warning::

        cupy.exceptions.RankWarning: The rank of the coefficient matrix in the
        least-squares fit is deficient. It is raised if ``full=False``.

    .. seealso:: :func:`numpy.polyfit`

    ebz0float16 x and bool y are not currently supportedzfloat16 y are not supportedr   zexpected deg >= 0r   zexpected 1D vector for xzexpected non-empty vector for xr*   zexpected 1D or 2D array for yz$expected x and y to have same lengthNz expected a 1-d array for weightsz(expected w and y to have the same length)axisz!Polyfit may be poorly conditioned   )
stacklevelr\   unscaledzJthe number of data points must exceed order to scale the covariance matrix.N) r8   r9   r^   r5   rl   float16r   ro   intr   r   r:   r/   r   
polynomial
polyvanderfinfoepssqrtsquaresumr3   lstsqreshapewarningswarnr   absoluteinvdotr2   outer)r   rj   degrcondr.   wcovlhsrhsscaler\   residsranksordermsgbasefactors                     r   polyfitr      s}   R 	w|sqw|s22! #9 : : 	:w%-5666!A!A
c((C
Qww,---v{{2333v{{9:::vzzQVaZZ7888v>???
/
$
/
/3
7
744R4
@C
C}a  6Q;;>???6QVFGGGqDz8q==!!!T'
Aq}AG,,00It{3'',,!,4455E5LC**3U;;AvtQvzzb!$$JA!GEu}}T}1c;15555 )<##]6**F&$5((
  {txs3344
5%(((*FFVe^^qv~.FF ? @ @ @ 6Q;;	?D$-Hr   c                 :   t          | t          j                  r| j        } t          | t          j                  r| j        dk    rt          d          | j        dk    rt          d          t          |t          j                  rt          j	        |j        d          }t          j        t          j
        dg|                    }t          j        t          j
        dg|                    }| ddd         D ]}|||z  z   }||z  }|S t          j	        | j                            d          |          }|                     |d	          } | j        dk    rt          j        |j        |          S |t          j        k    r |                                 |z  | d         z   S t          j        |          st          j        ||          d
         }|t          j        | j        |          z  }| ddd         |z                      d|          S )a  Evaluates a polynomial at specific values.

    Args:
        p (cupy.ndarray or cupy.poly1d): input polynomial.
        x (scalar, cupy.ndarray): values at which the polynomial
        is evaluated.

    Returns:
        cupy.ndarray or cupy.poly1d: polynomial evaluated at x.

    .. warning::

        This function doesn't currently support poly1d values to evaluate.

    .. seealso:: :func:`numpy.polyval`

    r   z%p must be 1d ndarray or poly1d objectr   zp must be 1d array)r8   Nrs   Fr,   rx   )rt   r8   )r   r   r	   r"   r   r   r   r   rl   rG   rR   r8   typer6   r:   r<   r/   bool_anyr   asarrayaranger   )pr   r8   resprodr\   s         r   polyvalr   4  s   $ !T[!! Ha&& A!&A++?@@@vzz-...!T[!! !!(A..k$*aS66677{4:qc7778844R4 	 	Aq.C!8DD
agll1ooq11E	U##Av{{z!'5)))uuww{QrU""= 4L%(((3	T[u----AdddGaK"E222r   c                 n   t          | t          j                  r| j        } | j        j        dk    rt          d          | j        dk    rt          d          | j	        dk     rt          j
        g           S t          j        j                            | ddd         g          \  } | j	        dk     rt          j
        g           S | j	        dk    rJ| d          | d         z  d         }| d         dk    r$|j                            t           j                  }|S t          j        j                            |           }t          j        ||                                j                  r t          j                            |          }nt          d	          |                    | j                  S )
a!  Computes the roots of a polynomial with given coefficients.

    Args:
        p (cupy.ndarray or cupy.poly1d): polynomial coefficients.

    Returns:
        cupy.ndarray: polynomial roots.

    .. warning::

        This function doesn't support currently polynomial coefficients
        whose companion matrices are general 2d square arrays. Only those
        with complex Hermitian or real symmetric 2d arrays are allowed.

        The current `cupy.roots` doesn't guarantee the order of results.

    .. seealso:: :func:`numpy.roots`

    rr   z boolean inputs are not supportedr   z"0-dimensional input is not allowedr*   Nrs   r   r+   )r   r   r	   r"   r8   r^   r5   r   r   r:   rR   r{   	polyutils	as_seriesrealr6   rl   rn   polycompanionr0   r1   r2   r3   r4   )r   r#   cmatrixs      r   rootsr   a  su   ( !T[!! Hw|s!"DEEEv{{<===vzzz"~~
/
#
-
-q2wi
8
8CQvzzz"~~v{{!uqt|T"Q4199(//%-00C
o(66q99G!122 /k""7++! #. / / 	/ ::agr   )NFNF)r&   r   rl   r   cupy.exceptionsr   cupyx.scipy.fftrb   r(   rD   rK   rM   rT   rX   rV   ro   r   r   r   r   r   r   <module>r      s/          ' ' ' ' ' '    3 3 38( ( (V   *   0 ! ! !,I I I  2/ / /p p p pf*3 *3 *3Z, , , , ,r   