U
    d»{je  ã                   @   s    d dl Zddd„Zd	dd„ZdS )
é    NTé   c                 C   sÞ   t  | ¡} t  |¡}| jdkr&tdƒ‚|jdkr8tdƒ‚|dkrL|sLtdƒ‚|r^t  | ¡g}ng }td|d ƒD ]H}| t  dt j | | |  ¡¡ | t  	dt j | | |  ¡¡ qpt  
|¡}|dk	rÔ|| }t  |¡S )a  Compute the Lomb-Scargle design matrix at the given frequency

    This is the matrix X such that the periodic model at the given frequency
    can be expressed :math:`\hat{y} = X \theta`.

    Parameters
    ----------
    t : array-like, shape=(n_times,)
        times at which to compute the design matrix
    frequency : float
        frequency for the design matrix
    dy : float or array-like, optional
        data uncertainties: should be broadcastable with `t`
    bias : bool (default=True)
        If true, include a bias column in the matrix
    nterms : int (default=1)
        Number of Fourier terms to include in the model

    Returns
    -------
    X : ndarray, shape=(n_times, n_parameters)
        The design matrix, where n_parameters = bool(bias) + 2 * nterms
    r   zt should be one dimensionalr   zfrequency must be a scalarz cannot have nterms=0 and no biasé   N)ÚnpÚasarrayÚndimÚ
ValueErrorÚ	ones_likeÚrangeÚappendÚsinÚpiÚcosZvstackZ	transpose)ÚtÚ	frequencyÚdyÚbiasÚntermsÚcolsÚiZXT© r   ú~/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/timeseries/periodograms/lombscargle/implementations/mle.pyÚdesign_matrix   s$    



"$
r   c                 C   sü   t tj| ||fƒ\} }}|dkr,t |¡}n
t |¡}t |¡}| jdkrRtdƒ‚|jdkrdtdƒ‚|jdkrvtdƒ‚|r |d }t ||¡| ¡  }	||	 }nd}	t| ||||d�}
tj	 
t |
j|
¡t |
j|| ¡¡}t||||d	�}|	t ||¡ S )
aÒ  Compute the Lomb-Scargle model fit at a given frequency

    Parameters
    ----------
    t, y, dy : float or array-like
        The times, observations, and uncertainties to fit
    frequency : float
        The frequency at which to compute the model
    t_fit : float or array-like
        The times at which the fit should be computed
    center_data : bool (default=True)
        If True, center the input data before applying the fit
    fit_mean : bool (default=True)
        If True, include the bias as part of the model
    nterms : int (default=1)
        The number of Fourier terms to include in the fit

    Returns
    -------
    y_fit : ndarray
        The model fit evaluated at each value of t_fit
    Nr   z"t, y, dy should be one dimensionalzt_fit should be one dimensionalr   zfrequency should be a scalarg       À)r   r   r   )r   r   )Úmapr   r   r   r   r   ÚdotÚsumr   ZlinalgZsolveÚT)r   Úyr   r   Zt_fitZcenter_dataZfit_meanr   ÚwZy_meanÚXZ	theta_MLEZX_fitr   r   r   Úperiodic_fit7   s(    





&r   )NTr   )TTr   )Únumpyr   r   r   r   r   r   r   Ú<module>   s   
3