§
    fŠtj$8  ã                   óš   — d dl mZ d dlmZ d dlmZmZmZ d dl	m
c mZ d dlmZ d dlmZ ddlmZ 	 	 ddddœd„Zd„ Zd„ Zd„ Z	 	 dd„ZdS )é    )Úfloat_factorial)Únumpy)Úarray_namespaceÚxp_swapaxesÚ	xp_deviceN)Ú
convolve1d)Ú
_polyutilsé   )Ú
axis_sliceç      ð?Úconv)ÚxpÚdevicec                óR  — || k    rt          d¦  «        ‚t          | d¦  «        \  }}	|€|	dk    r|dz
  }n|}d|cxk    r| k     sn t          d¦  «        ‚|dvrt          d¦  «        ‚|€t          n!t          |                     d¦  «        ¦  «        }||k    r|                     | |j        |¬	¦  «        }
|
S |                     | | |z
  |j        |¬	¦  «        }|d
k    r|                     |¦  «        }| 	                    |                     |dz   |j        |¬	¦  «        d¦  «        }||z  }|                     |dz   |j        |¬	¦  «        }t          j        ||¦  «                             t          |¦  «        ||z  z  ¦  «        }t          j        |||¬¦  «        \  }
}}}|
S )a	  Compute the coefficients for a 1-D Savitzky-Golay FIR filter.

    Parameters
    ----------
    window_length : int
        The length of the filter window (i.e., the number of coefficients).
    polyorder : int
        The order of the polynomial used to fit the samples.
        `polyorder` must be less than `window_length`.
    deriv : int, optional
        The order of the derivative to compute. This must be a
        nonnegative integer. The default is 0, which means to filter
        the data without differentiating.
    delta : float, optional
        The spacing of the samples to which the filter will be applied.
        This is only used if deriv > 0.
    pos : int or None, optional
        If pos is not None, it specifies evaluation position within the
        window. The default is the middle of the window.
    use : str, optional
        Either 'conv' or 'dot'. This argument chooses the order of the
        coefficients. The default is 'conv', which means that the
        coefficients are ordered to be used in a convolution. With
        use='dot', the order is reversed, so the filter is applied by
        dotting the coefficients with the data set.

    Returns
    -------
    coeffs : 1-D ndarray
        The filter coefficients.

    See Also
    --------
    savgol_filter

    Notes
    -----
    .. versionadded:: 0.14.0

    References
    ----------
    A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of Data by
    Simplified Least Squares Procedures. Analytical Chemistry, 1964, 36 (8),
    pp 1627-1639.
    Jianwen Luo, Kui Ying, and Jing Bai. 2005. Savitzky-Golay smoothing and
    differentiation filter for even number data. Signal Process.
    85, 7 (July 2005), 1429-1434.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.signal import savgol_coeffs
    >>> savgol_coeffs(5, 2)
    array([-0.08571429,  0.34285714,  0.48571429,  0.34285714, -0.08571429])
    >>> savgol_coeffs(5, 2, deriv=1)
    array([ 2.00000000e-01,  1.00000000e-01,  2.07548111e-16, -1.00000000e-01,
           -2.00000000e-01])

    Note that use='dot' simply reverses the coefficients.

    >>> savgol_coeffs(5, 2, pos=3)
    array([ 0.25714286,  0.37142857,  0.34285714,  0.17142857, -0.14285714])
    >>> savgol_coeffs(5, 2, pos=3, use='dot')
    array([-0.14285714,  0.17142857,  0.34285714,  0.37142857,  0.25714286])
    >>> savgol_coeffs(4, 2, pos=3, deriv=1, use='dot')
    array([0.45,  -0.85,  -0.65,  1.05])

    `x` contains data from the parabola x = t**2, sampled at
    t = -1, 0, 1, 2, 3.  `c` holds the coefficients that will compute the
    derivative at the last position.  When dotted with `x` the result should
    be 6.

    >>> x = np.array([1, 0, 1, 4, 9])
    >>> c = savgol_coeffs(5, 2, pos=4, deriv=1, use='dot')
    >>> c.dot(x)
    6.0
    z*polyorder must be less than window_length.é   Nr   g      à?z4pos must be nonnegative and less than window_length.)r   Údotz`use` must be 'conv' or 'dot'©Údtyper   r   r
   ©éÿÿÿÿr
   ©r   )Ú
ValueErrorÚdivmodÚ	np_compatr   ÚemptyÚzerosÚfloat64ÚarangeÚflipÚreshapeÚxpxÚatÚsetr   Ú_puÚ_lstsq)Úwindow_lengthÚ	polyorderÚderivÚdeltaÚposÚuser   r   ÚhalflenÚremÚcoeffsÚxÚorderÚAÚyÚ_s                   úZ/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/signal/_savitzky_golay.pyÚsavgol_coeffsr5      sÙ  € ðx �MÒ!Ð!ÝÐEÑFÔFÐFå˜-¨Ñ+Ô+�L€GˆSà
€{Ø�!Š8ˆ8Ø˜C‘-ˆCˆCàˆCà�Ð$Ð$Ò$Ð$�}Ò$Ð$Ð$Ð$Ýð *ñ +ô +ð 	+ð �/Ð!Ð!ÝÐ8Ñ9Ô9Ð9ð �j�ˆ¥o°b·h²h¸q±k´kÑ&BÔ&B€BàˆyÒÐØ—’˜-¨r¬zÀ&�ÑIÔIˆØˆð 	�	Š	�3�$˜¨Ñ+°2´:Àfˆ	ÑMÔM€Aà
ˆf‚}€}à�GŠG�A‰JŒJˆà�JŠJØ
�	Š	�)˜a‘- r¤z¸&ˆ	ÑAÔAÀ7ñô €Eð 	
ˆU‰
€Að 	�Š�˜Q‘ b¤j¸ˆÑ@Ô@€Aõ 	Œˆq�%ÑÔ×Ò�_¨UÑ3Ô3°uÀ±~ÑFÑGÔG€Aõ ”j  A¨"Ð-Ñ-Ô-�O€FˆAˆq�!à€Mó    c          
      ó°  — |dk    r| }nÌ| j         d         }||k    r |                     | dd…df         ¦  «        }n™|                     | d| …df         d¬¦  «        }t          |¦  «        D ]e}|                     ||z
  dz
  ||z
  dz
  d| j        t          | ¦  «        ¬¦  «        }||                     |||z
  fd	| j        dz
  z  z   ¦  «        z  }Œf|}|S )
aH  Differentiate polynomials represented with coefficients.

    p must be a 1-D or 2-D array.  In the 2-D case, each column gives
    the coefficients of a polynomial; the first row holds the coefficients
    associated with the highest power. m must be a nonnegative integer.
    (numpy.polyder doesn't handle the 2-D case.)
    r   Nr
   .T)Úcopyr   r   )r
   )	ÚshapeÚ
zeros_likeÚasarrayÚranger   r   r   r    Úndim)ÚpÚmr   ÚresultÚnÚdpÚkÚrngs           r4   Ú_polyderrE   ›   sø   € ð 	ˆA‚v€vØˆˆàŒG�AŒJˆØ�Š6ˆ6Ø—]’] 1 R a R¨ W¤:Ñ.Ô.ˆFˆFà—’˜A˜c ˜r˜c 3˜hœK¨d�Ñ3Ô3ˆBÝ˜1‘X”Xð Fð F�Ø—i’iØ˜‘E˜A‘I˜q 1™u q™y¨"°A´GÅIÈaÁLÄLð  ñ ô �ð �b—j’j  q¨1¡u h°¸¼À!¹Ñ1DÑ&DÑEÔEÑE��ØˆFØ€Mr6   c
           
      óê  — t          | ¦  «        }
t          | |||¬¦  «        }|dk    s|| j         k    r|}d}nt          ||d|
¦  «        }d}|
                     ||j        d         df¦  «        }t          j        |
                     d||z
  | j	        t          | ¦  «        ¬¦  «        |||
¬¦  «        }|dk    rt          |||
¬¦  «        }|
                     ||z
  ||z
  |j	        t          |¦  «        ¬¦  «        }t          j        ||
                     |d¦  «        |
¬¦  «        ||z  z  }t          |	j        ¦  «        }||         |d         c|d<   ||<   |
                     |||z
  g|d	d
…         ¢R ¦  «        }|rt          |d||
¦  «        }t          d
¦  «        g|	j        z  }t          ||¦  «        ||<   t          j        |	t#          |¦  «        ¦  «                             |¦  «        }	|	S )aE  
    Given an N-d array `x` and the specification of a slice of `x` from
    `window_start` to `window_stop` along `axis`, create an interpolating
    polynomial of each 1-D slice, and evaluate that polynomial in the slice
    from `interp_start` to `interp_stop`. Put the result into the
    corresponding slice of `y`.
    )ÚstartÚstopÚaxisr   FTr   r   r   r   r
   N)r   r   r=   r   r    r9   r$   Úpolyfitr   r   r   rE   ÚpolyvalÚlistÚslicer!   r"   Útupler#   )r/   Úwindow_startÚwindow_stopÚinterp_startÚinterp_stoprI   r'   r(   r)   r2   r   Úx_edgeÚxx_edgeÚswappedÚpoly_coeffsÚiÚvaluesÚshpÚy_slices                      r4   Ú	_fit_edger[   µ   s  € õ 
˜Ñ	Ô	€Bõ ˜ °KÀdÐKÑKÔK€FØˆq‚y€y�D˜QœV˜G’O�OØˆØˆˆå˜f d¨A¨rÑ2Ô2ˆØˆØ�jŠj˜ 7¤=°Ô#3°RÐ"8Ñ9Ô9€Gõ ”+Ø
�	Š	Øˆ{˜\Ñ)°´ÅÈ1ÁÄð 	ñ 	
ô 	
à�I "ðñ ô €Kð ˆq‚y€yÝ˜{¨E°bÐ9Ñ9Ô9ˆð 	�	Š	Ø�|Ñ# [°<Ñ%?ØÔ­	°+Ñ(>Ô(>ð 	ñ 	ô 	€Aõ Œ[˜ b§j¢j°°GÑ&<Ô&<ÀÐDÑDÔDÈÐQVÉÑW€Fõ ˆqŒw‰-Œ-€CØ˜Dœ	 3 q¤6Ð€Cˆ�FˆC�‰IØ�ZŠZ˜ ¨|Ñ!;Ð F¸cÀ!À"À"¼gÐ FÐ FÑGÔG€FØð 2Ý˜V Q¨¨bÑ1Ô1ˆå�T‰{Œ{ˆm˜aœfÑ$€GÝ˜,¨Ñ4Ô4€GˆD�MÝŒˆq•%˜‘.”.Ñ!Ô!×%Ò% fÑ-Ô-€Aà€Hr6   c                 ó–   — |dz  }t          | d|d||||||¦
  «
        }| j        |         }t          | ||z
  |||z
  ||||||¦
  «
        }|S )zÆ
    Use polynomial interpolation of x at the low and high ends of the axis
    to fill in the halflen values in y.

    This function just calls _fit_edge twice, once for each end of the axis.
    r   r   )r[   r9   )	r/   r&   r'   r(   r)   rI   r2   r,   rA   s	            r4   Ú_fit_edges_polyfitr]   ì   sq   € ð ˜qÑ €GÝ�!�Q˜ q¨'°4Ø˜  qñ	*ô 	*€Aà	Œ�Œ€AÝ�!�Q˜Ñ&¨¨1¨w©;¸¸4Ø˜  qñ	*ô 	*€Að €Hr6   r   Úinterpç        c           
      óô  — |dvrt          d¦  «        ‚t          | ¦  «        }|                     | ¦  «        } | j        |j        k    r+| j        |j        k    r|                     | |j        ¦  «        } t          |||||t          | ¦  «        ¬¦  «        }	|dk    rI|| j	        |         k    rt          d¦  «        ‚t          | |	|d¬¦  «        }
t          | ||||||
¦  «        }
nt          | |	|||¬¦  «        }
|
S )	aŠ   Apply a Savitzky-Golay filter to an array.

    This is a 1-D filter. If `x`  has dimension greater than 1, `axis`
    determines the axis along which the filter is applied.

    Parameters
    ----------
    x : array_like
        The data to be filtered. If `x` is not a single or double precision
        floating point array, it will be converted to type ``numpy.float64``
        before filtering.
    window_length : int
        The length of the filter window (i.e., the number of coefficients).
        If `mode` is 'interp', `window_length` must be less than or equal
        to the size of `x`.
    polyorder : int
        The order of the polynomial used to fit the samples.
        `polyorder` must be less than `window_length`.
    deriv : int, optional
        The order of the derivative to compute. This must be a
        nonnegative integer. The default is 0, which means to filter
        the data without differentiating.
    delta : float, optional
        The spacing of the samples to which the filter will be applied.
        This is only used if deriv > 0. Default is 1.0.
    axis : int, optional
        The axis of the array `x` along which the filter is to be applied.
        Default is -1.
    mode : str, optional
        Must be 'mirror', 'constant', 'nearest', 'wrap' or 'interp'. This
        determines the type of extension to use for the padded signal to
        which the filter is applied.  When `mode` is 'constant', the padding
        value is given by `cval`.  See the Notes for more details on 'mirror',
        'constant', 'wrap', and 'nearest'.
        When the 'interp' mode is selected (the default), no extension
        is used.  Instead, a degree `polyorder` polynomial is fit to the
        last `window_length` values of the edges, and this polynomial is
        used to evaluate the last `window_length // 2` output values.
    cval : scalar, optional
        Value to fill past the edges of the input if `mode` is 'constant'.
        Default is 0.0.

    Returns
    -------
    y : ndarray, same shape as `x`
        The filtered data.

    See Also
    --------
    savgol_coeffs

    Notes
    -----
    Details on the `mode` options:

        'mirror':
            Repeats the values at the edges in reverse order. The value
            closest to the edge is not included.
        'nearest':
            The extension contains the nearest input value.
        'constant':
            The extension contains the value given by the `cval` argument.
        'wrap':
            The extension contains the values from the other end of the array.

    For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8], and
    `window_length` is 7, the following shows the extended data for
    the various `mode` options (assuming `cval` is 0)::

        mode       |   Ext   |         Input          |   Ext
        -----------+---------+------------------------+---------
        'mirror'   | 4  3  2 | 1  2  3  4  5  6  7  8 | 7  6  5
        'nearest'  | 1  1  1 | 1  2  3  4  5  6  7  8 | 8  8  8
        'constant' | 0  0  0 | 1  2  3  4  5  6  7  8 | 0  0  0
        'wrap'     | 6  7  8 | 1  2  3  4  5  6  7  8 | 1  2  3

    .. versionadded:: 0.14.0

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.signal import savgol_filter
    >>> np.set_printoptions(precision=2)  # For compact display.
    >>> x = np.array([2, 2, 5, 2, 1, 0, 1, 4, 9])

    Filter with a window length of 5 and a degree 2 polynomial.  Use
    the defaults for all other parameters.

    >>> savgol_filter(x, 5, 2)
    array([1.66, 3.17, 3.54, 2.86, 0.66, 0.17, 1.  , 4.  , 9.  ])

    Note that the last five values in x are samples of a parabola, so
    when mode='interp' (the default) is used with polyorder=2, the last
    three values are unchanged. Compare that to, for example,
    `mode='nearest'`:

    >>> savgol_filter(x, 5, 2, mode='nearest')
    array([1.74, 3.03, 3.54, 2.86, 0.66, 0.17, 1.  , 4.6 , 7.97])

    )ÚmirrorÚconstantÚnearestr^   Úwrapz@mode must be 'mirror', 'constant', 'nearest' 'wrap' or 'interp'.)r(   r)   r   r   r^   zOIf mode is 'interp', window_length must be less than or equal to the size of x.rb   )rI   Úmode)rI   re   Úcval)r   r   r;   r   r   Úfloat32Úastyper5   r   r9   r   r]   )r/   r&   r'   r(   r)   rI   re   rf   r   r.   r2   s              r4   Úsavgol_filterri   ý   s+  € ðL ÐFÐFÐFÝð /ñ 0ô 0ð 	0õ 
˜Ñ	Ô	€BØ
�
Š
�1‰Œ€Aà„w�"”*ÒÐ ¤¨B¬JÒ!6Ð!6Ø�IŠI�a˜œÑ$Ô$ˆåØ�y¨°U¸rÍ)ÐTUÉ,Ì,ðñ ô €Fð ˆxÒÐØ˜1œ7 4œ=Ò(Ð(Ýð ?ñ @ô @ð @õ �q˜& t°*Ð=Ñ=Ô=ˆÝ˜q -°¸EÀ5È$ÐPQÑRÔRˆˆõ �q˜& t°$¸TÐBÑBÔBˆà€Hr6   )r   r   Nr   )r   r   r   r^   r_   )Úscipy._lib._utilr   Úscipy._lib.array_api_compatr   r   Úscipy._lib._array_apir   r   r   Úscipy._lib.array_api_extraÚ_libÚarray_api_extrar!   Úscipy.ndimager   Úscipy.signalr	   r$   Ú_arraytoolsr   r5   rE   r[   r]   ri   © r6   r4   ú<module>rt      s'  ðØ ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø :Ð :Ð :Ð :Ð :Ð :Ø IÐ IÐ IÐ IÐ IÐ IÐ IÐ IÐ IÐ IØ (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à $Ð $Ð $Ð $Ð $Ð $Ø *Ð *Ð *Ð *Ð *Ð *Ø #Ð #Ð #Ð #Ð #Ð #ð EIØðMØ$(°ðMð Mð Mð Mð Mð`ð ð ð43ð 3ð 3ðnð ð ð" ?BØ/2ðBð Bð Bð Bð Bð Br6   