a
    ߙfb                    @   s  d Z ddlZddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZ g d	Zd
ej ZeeejjZdeded  ZG dd de
ZG dd deZG dd de
ZG dd de
ZG dd de
Z G dd de
Z!G dd de
Z"G dd de
Z#G dd de#Z$G dd de#Z%G d d! d!e#Z&G d"d# d#e#Z'G d$d% d%e'Z(G d&d' d'e'Z)G d(d) d)e'Z*G d*d+ d+e
Z+G d,d- d-eZ,G d.d/ d/e
Z-G d0d1 d1e
Z.G d2d3 d3e
Z/G d4d5 d5eZ0G d6d7 d7eZ1G d8d9 d9eZ2G d:d; d;eZ3G d<d= d=e
Z4G d>d? d?eZ5G d@dA dAe
Z6G dBdC dCeZ7G dDdE dEe
Z8G dFdG dGeZ9G dHdI dIeZ:G dJdK dKe
Z;G dLdM dMeZ<G dNdO dOeZ=G dPdQ dQe
Z>G dRdS dSe
Z?G dTdU dUe
Z@dS )VzMathematical models.    N)units)Quantity
UnitsError   )Fittable1DModelFittable2DModel)	ParameterInputParameterError)ellipse_extent)#
AiryDisk2DMoffat1DMoffat2DBox1DBox2DConst1DConst2D	Ellipse2DDisk2D
Gaussian1D
Gaussian2DLinear1D	Lorentz1DRickerWavelet1DRickerWavelet2DRedshiftScaleFactorMultiplyPlanar2DScaleSersic1DSersic2DShiftSine1DCosine1D	Tangent1D	ArcSine1DArcCosine1DArcTangent1DTrapezoid1DTrapezoidDisk2DRing2DVoigt1DKingProjectedAnalytic1DExponential1DLogarithmic1D          @c                   @   s|   e Zd ZdZedddZedddZededfdd	ZdddZ	e
dd Zedd Zedd Ze
dd Zdd ZdS )r   a  
    One dimensional Gaussian model.

    Parameters
    ----------
    amplitude : float or `~astropy.units.Quantity`.
        Amplitude (peak value) of the Gaussian - for a normalized profile
        (integrating to 1), set amplitude = 1 / (stddev * np.sqrt(2 * np.pi))
    mean : float or `~astropy.units.Quantity`.
        Mean of the Gaussian.
    stddev : float or `~astropy.units.Quantity`.
        Standard deviation of the Gaussian with FWHM = 2 * stddev * np.sqrt(2 * np.log(2)).

    Notes
    -----
    Either all or none of input ``x``, ``mean`` and ``stddev`` must be provided
    consistently with compatible units or as unitless numbers.

    Model formula:

        .. math:: f(x) = A e^{- \frac{\left(x - x_{0}\right)^{2}}{2 \sigma^{2}}}

    Examples
    --------
    >>> from astropy.modeling import models
    >>> def tie_center(model):
    ...         mean = 50 * model.stddev
    ...         return mean
    >>> tied_parameters = {'mean': tie_center}

    Specify that 'mean' is a tied parameter in one of two ways:

    >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3,
    ...                             tied=tied_parameters)

    or

    >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3)
    >>> g1.mean.tied
    False
    >>> g1.mean.tied = tie_center
    >>> g1.mean.tied
    <function tie_center at 0x...>

    Fixed parameters:

    >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3,
    ...                        fixed={'stddev': True})
    >>> g1.stddev.fixed
    True

    or

    >>> g1 = models.Gaussian1D(amplitude=10, mean=5, stddev=.3)
    >>> g1.stddev.fixed
    False
    >>> g1.stddev.fixed = True
    >>> g1.stddev.fixed
    True

    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Gaussian1D

        plt.figure()
        s1 = Gaussian1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()

    See Also
    --------
    Gaussian2D, Box1D, Moffat1D, Lorentz1D
    r   z&Amplitude (peak value) of the Gaussiandefaultdescriptionr   zPosition of peak (Gaussian)Nz"Standard deviation of the Gaussianr1   boundsr2         @c                 C   s    | j }|| j }|| || fS )aU  
        Tuple defining the default ``bounding_box`` limits,
        ``(x_low, x_high)``

        Parameters
        ----------
        factor : float
            The multiple of `stddev` used to define the limits.
            The default is 5.5, corresponding to a relative error < 1e-7.

        Examples
        --------
        >>> from astropy.modeling.models import Gaussian1D
        >>> model = Gaussian1D(mean=0, stddev=2)
        >>> model.bounding_box
        ModelBoundingBox(
            intervals={
                x: Interval(lower=-11.0, upper=11.0)
            }
            model=Gaussian1D(inputs=('x',))
            order='C'
        )

        This range can be set directly (see: `Model.bounding_box
        <astropy.modeling.Model.bounding_box>`) or by using a different factor,
        like:

        >>> model.bounding_box = model.bounding_box(factor=2)
        >>> model.bounding_box
        ModelBoundingBox(
            intervals={
                x: Interval(lower=-4.0, upper=4.0)
            }
            model=Gaussian1D(inputs=('x',))
            order='C'
        )
        )meanstddevselffactorZx0dx r<   Alib/python3.9/site-packages/astropy/modeling/functional_models.pybounding_box~   s    '
zGaussian1D.bounding_boxc                 C   s
   | j t S )z$Gaussian full width at half maximum.)r7   GAUSSIAN_SIGMA_TO_FWHMr9   r<   r<   r=   fwhm   s    zGaussian1D.fwhmc                 C   s"   |t d| | d  |d   S )z,
        Gaussian1D model function.
              r.   npexp)x	amplituder6   r7   r<   r<   r=   evaluate   s    zGaussian1D.evaluatec                 C   s\   t d|d  | | d  }|| | |  |d  }|| | | d  |d  }|||gS )z8
        Gaussian1D model function derivatives.
        rB   r.      rC   )rF   rG   r6   r7   d_amplitudeZd_meanZd_stddevr<   r<   r=   	fit_deriv   s    zGaussian1D.fit_derivc                 C   s"   | j jd u rd S | jd | j jiS Nr   )r6   unitinputsr@   r<   r<   r=   input_units   s    zGaussian1D.input_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r6   r7   rG   rN   outputsr9   Zinputs_unitZoutputs_unitr<   r<   r=   _parameter_units_for_data_units   s    z*Gaussian1D._parameter_units_for_data_units)r5   )__name__
__module____qualname____doc__r   rG   r6   FLOAT_EPSILONr7   r>   propertyrA   staticmethodrH   rK   rO   rS   r<   r<   r<   r=   r   "   s   T
,




r   c                       s   e Zd ZdZedddZedddZedddZedddZedd	dZ	ed
ddZ
ejejejddddf fdd	Zedd Zedd ZdddZedd Zedd Zedd Zdd Z  ZS )r   aJ  
    Two dimensional Gaussian model.

    Parameters
    ----------
    amplitude : float or `~astropy.units.Quantity`.
        Amplitude (peak value) of the Gaussian.
    x_mean : float or `~astropy.units.Quantity`.
        Mean of the Gaussian in x.
    y_mean : float or `~astropy.units.Quantity`.
        Mean of the Gaussian in y.
    x_stddev : float or `~astropy.units.Quantity` or None.
        Standard deviation of the Gaussian in x before rotating by theta. Must
        be None if a covariance matrix (``cov_matrix``) is provided. If no
        ``cov_matrix`` is given, ``None`` means the default value (1).
    y_stddev : float or `~astropy.units.Quantity` or None.
        Standard deviation of the Gaussian in y before rotating by theta. Must
        be None if a covariance matrix (``cov_matrix``) is provided. If no
        ``cov_matrix`` is given, ``None`` means the default value (1).
    theta : float or `~astropy.units.Quantity`, optional.
        The rotation angle as an angular quantity
        (`~astropy.units.Quantity` or `~astropy.coordinates.Angle`)
        or a value in radians (as a float). The rotation angle
        increases counterclockwise. Must be `None` if a covariance matrix
        (``cov_matrix``) is provided. If no ``cov_matrix`` is given,
        `None` means the default value (0).
    cov_matrix : ndarray, optional
        A 2x2 covariance matrix. If specified, overrides the ``x_stddev``,
        ``y_stddev``, and ``theta`` defaults.

    Notes
    -----
    Either all or none of input ``x, y``, ``[x,y]_mean`` and ``[x,y]_stddev``
    must be provided consistently with compatible units or as unitless numbers.

    Model formula:

        .. math::

            f(x, y) = A e^{-a\left(x - x_{0}\right)^{2}  -b\left(x - x_{0}\right)
            \left(y - y_{0}\right)  -c\left(y - y_{0}\right)^{2}}

    Using the following definitions:

        .. math::
            a = \left(\frac{\cos^{2}{\left (\theta \right )}}{2 \sigma_{x}^{2}} +
            \frac{\sin^{2}{\left (\theta \right )}}{2 \sigma_{y}^{2}}\right)

            b = \left(\frac{\sin{\left (2 \theta \right )}}{2 \sigma_{x}^{2}} -
            \frac{\sin{\left (2 \theta \right )}}{2 \sigma_{y}^{2}}\right)

            c = \left(\frac{\sin^{2}{\left (\theta \right )}}{2 \sigma_{x}^{2}} +
            \frac{\cos^{2}{\left (\theta \right )}}{2 \sigma_{y}^{2}}\right)

    If using a ``cov_matrix``, the model is of the form:
        .. math::
            f(x, y) = A e^{-0.5 \left(\vec{x} - \vec{x}_{0}\right)^{T} \Sigma^{-1} \left(\vec{x} - \vec{x}_{0}\right)}

    where :math:`\vec{x} = [x, y]`, :math:`\vec{x}_{0} = [x_{0}, y_{0}]`,
    and :math:`\Sigma` is the covariance matrix:

        .. math::
            \Sigma = \left(\begin{array}{ccc}
            \sigma_x^2               & \rho \sigma_x \sigma_y \\
            \rho \sigma_x \sigma_y   & \sigma_y^2
            \end{array}\right)

    :math:`\rho` is the correlation between ``x`` and ``y``, which should
    be between -1 and +1.  Positive correlation corresponds to a
    ``theta`` in the range 0 to 90 degrees.  Negative correlation
    corresponds to a ``theta`` in the range of 0 to -90 degrees.

    See [1]_ for more details about the 2D Gaussian function.

    See Also
    --------
    Gaussian1D, Box2D, Moffat2D

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Gaussian_function
    r   zAmplitude of the Gaussianr0   r   z(Peak position (along x axis) of Gaussianz(Peak position (along y axis) of Gaussianz1Standard deviation of the Gaussian (along x axis)z1Standard deviation of the Gaussian (along y axis)        zNRotation angle either as a float (in radians) or a |Quantity| angle (optional)Nc              	      s  |d u r@|d u r| j jj}|d u r,| j jj}|d u r| j jj}n~|d usX|d usX|d ur`tdt|}|jdkr|t	dtj
|\}	}
t|	\}}|
d d df }t|d |d }|di  |d dtd f |d dtd f t jf ||||||d	| d S )
Nz3Cannot specify both cov_matrix and x/y_stddev/theta)r.   r.   zCovariance matrix must be 2x2r   r   r4   x_stddevy_stddev)rG   x_meany_meanr\   r]   theta)	__class__r\   r1   r]   r`   r	   rD   arrayshape
ValueErrorZlinalgZeigsqrtZarctan2
setdefaultrX   super__init__)r9   rG   r^   r_   r\   r]   r`   Z
cov_matrixkwargsZeig_valsZeig_vecsZy_vecra   r<   r=   rh   *  s2    



zGaussian2D.__init__c                 C   s
   | j t S )z)Gaussian full width at half maximum in X.)r\   r?   r@   r<   r<   r=   x_fwhmO  s    zGaussian2D.x_fwhmc                 C   s
   | j t S )z)Gaussian full width at half maximum in Y.)r]   r?   r@   r<   r<   r=   y_fwhmT  s    zGaussian2D.y_fwhmr5   c                 C   sj   || j  }|| j }| jjdu r*| jj}n| jj}t|||\}}| j| | j| f| j| | j| ffS )a  
        Tuple defining the default ``bounding_box`` limits in each dimension,
        ``((y_low, y_high), (x_low, x_high))``

        The default offset from the mean is 5.5-sigma, corresponding
        to a relative error < 1e-7. The limits are adjusted for rotation.

        Parameters
        ----------
        factor : float, optional
            The multiple of `x_stddev` and `y_stddev` used to define the limits.
            The default is 5.5.

        Examples
        --------
        >>> from astropy.modeling.models import Gaussian2D
        >>> model = Gaussian2D(x_mean=0, y_mean=0, x_stddev=1, y_stddev=2)
        >>> model.bounding_box
        ModelBoundingBox(
            intervals={
                x: Interval(lower=-5.5, upper=5.5)
                y: Interval(lower=-11.0, upper=11.0)
            }
            model=Gaussian2D(inputs=('x', 'y'))
            order='C'
        )

        This range can be set directly (see: `Model.bounding_box
        <astropy.modeling.Model.bounding_box>`) or by using a different factor
        like:

        >>> model.bounding_box = model.bounding_box(factor=2)
        >>> model.bounding_box
        ModelBoundingBox(
            intervals={
                x: Interval(lower=-2.0, upper=2.0)
                y: Interval(lower=-4.0, upper=4.0)
            }
            model=Gaussian2D(inputs=('x', 'y'))
            order='C'
        )
        N)r\   r]   r`   Zquantityvaluer
   r_   r^   )r9   r:   abr`   r;   dyr<   r<   r=   r>   Y  s    ,


zGaussian2D.bounding_boxc                 C   s   t |d }t |d }	t d| }
|d }|d }| | }|| }d|| |	|   }d|
| |
|   }d|	| ||   }|t ||d  || |  ||d     S )z!Two dimensional Gaussian functionr.   r/         ?rD   cossinrE   )rF   yrG   r^   r_   r\   r]   r`   cost2sint2sin2txstd2ystd2xdiffydiffrn   ro   cr<   r<   r=   rH     s    
zGaussian2D.evaluatec           )      C   s  t |}t |}	t |d }
t |d }t d| }t d| }|d }|d }|d }|d }| | }|| }|d }|d }d|
| ||   }d|| ||   }d|| |
|   }|t || || |  ||    }|	| d| d|   }|
 | }| | }|| ||  }| | }|| }| } | | }!|
 | }"|| }#|d| | ||   }$||| d| |   }%||| || |  |!|    }&||| || |  |"|    }'||| || |  | |    }(|#|$|%|&|'|(gS )zGTwo dimensional Gaussian function derivative with respect to parametersr.   r/   rI   rq         ?rr   ))rF   ru   rG   r^   r_   r\   r]   r`   costsintrv   rw   Zcos2trx   ry   rz   Zxstd3Zystd3r{   r|   Zxdiff2Zydiff2rn   ro   r}   gZ	da_dthetaZda_dx_stddevZda_dy_stddevZ	db_dthetaZdb_dx_stddevZdb_dy_stddevZ	dc_dthetaZdc_dx_stddevZdc_dy_stddevZdg_dAZ
dg_dx_meanZ
dg_dy_meanZdg_dx_stddevZdg_dy_stddevZ	dg_dthetar<   r<   r=   rK     sd    











zGaussian2D.fit_derivc                 C   s<   | j jd u r| jjd u rd S | jd | j j| jd | jjiS Nr   r   )r^   rM   r_   rN   r@   r<   r<   r=   rO     s
    zGaussian2D.input_unitsc                 C   sj   || j d  || j d  kr$td|| j d  || j d  || j d  || j d  tj|| jd  dS )Nr   r   (Units of 'x' and 'y' inputs should match)r^   r_   r\   r]   r`   rG   rN   r   uZradrQ   rR   r<   r<   r=   rS     s    z*Gaussian2D._parameter_units_for_data_units)r5   )rT   rU   rV   rW   r   rG   r^   r_   r\   r]   r`   r1   rh   rY   rk   rl   r>   rZ   rH   rK   rO   rS   __classcell__r<   r<   rj   r=   r      s.   S%


7

.
r   c                   @   sh   e Zd ZdZedddZdZdZedd Z	edd	 Z
ed
d Zedd Zedd Zdd ZdS )r    zv
    Shift a coordinate.

    Parameters
    ----------
    offset : float
        Offset to add to a coordinate.
    r   zOffset to add to a modelr0   Tc                 C   s"   | j jd u rd S | jd | j jiS rL   )offsetrM   rN   r@   r<   r<   r=   rO     s    zShift.input_unitsc                    sR      }| jd9  _z
 j W n ty2   Y n0 t fdd jD |_|S )z,One dimensional inverse Shift model functionc                 3   s   | ]}  | jV  qd S N)rH   r   .0rF   r@   r<   r=   	<genexpr>      z Shift.inverse.<locals>.<genexpr>)copyr   r>   NotImplementedErrortupler9   invr<   r@   r=   inverse  s    
zShift.inversec                 C   s   | | S )z$One dimensional Shift model functionr<   )rF   r   r<   r<   r=   rH   
  s    zShift.evaluatec                 C   s   | S )z=Evaluate the implicit term (x) of one dimensional Shift modelr<   )rF   r<   r<   r=   sum_of_implicit_terms  s    zShift.sum_of_implicit_termsc                 G   s   t | }|gS )z@One dimensional Shift model derivative with respect to parameterrD   	ones_like)rF   paramsZd_offsetr<   r<   r=   rK     s    
zShift.fit_derivc                 C   s   d|| j d  iS )Nr   r   rQ   rR   r<   r<   r=   rS     s    z%Shift._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   r   linear_has_inverse_bounding_boxrY   rO   r   rZ   rH   r   rK   rS   r<   r<   r<   r=   r      s   	




r    c                   @   sh   e Zd ZdZedddZdZdZdZdZ	dZ
edd Zedd	 Zed
d Zedd Zdd ZdS )r   a   
    Multiply a model by a dimensionless factor.

    Parameters
    ----------
    factor : float
        Factor by which to scale a coordinate.

    Notes
    -----

    If ``factor`` is a `~astropy.units.Quantity` then the units will be
    stripped before the scaling operation.

    r   z Factor by which to scale a modelr0   Tc                 C   s"   | j jd u rd S | jd | j jiS rL   )r:   rM   rN   r@   r<   r<   r=   rO   9  s    zScale.input_unitsc                    sT      }d j |_z
 j W n ty0   Y n 0 t fdd j D |_|S )z,One dimensional inverse Scale model functionr   c                 3   s   | ]}  | jV  qd S r   rH   r:   r   r@   r<   r=   r   J  r   z Scale.inverse.<locals>.<genexpr>r   r:   r>   r   r   r   r<   r@   r=   r   ?  s    
zScale.inversec                 C   s   t |tjr|j}||  S )z$One dimensional Scale model function)
isinstancer   r   rm   rF   r:   r<   r<   r=   rH   N  s    zScale.evaluatec                 G   s
   | }|gS )z@One dimensional Scale model derivative with respect to parameterr<   rF   r   Zd_factorr<   r<   r=   rK   V  s    zScale.fit_derivc                 C   s   d|| j d  iS Nr:   r   r   rR   r<   r<   r=   rS   ]  s    z%Scale._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   r:   r   fittableZ_input_units_strictZ _input_units_allow_dimensionlessr   rY   rO   r   rZ   rH   rK   rS   r<   r<   r<   r=   r     s    



r   c                   @   sT   e Zd ZdZedddZdZdZdZe	dd Z
edd	 Zed
d Zdd ZdS )r   z
    Multiply a model by a quantity or number.

    Parameters
    ----------
    factor : float
        Factor by which to multiply a coordinate.
    r   z#Factor by which to multiply a modelr0   Tc                    sT      }d j |_z
 j W n ty0   Y n 0 t fdd j D |_|S )z/One dimensional inverse multiply model functionr   c                 3   s   | ]}  | jV  qd S r   r   r   r@   r<   r=   r   |  r   z#Multiply.inverse.<locals>.<genexpr>r   r   r<   r@   r=   r   q  s    
zMultiply.inversec                 C   s   ||  S )z'One dimensional multiply model functionr<   r   r<   r<   r=   rH     s    zMultiply.evaluatec                 G   s
   | }|gS )zCOne dimensional multiply model derivative with respect to parameterr<   r   r<   r<   r=   rK     s    zMultiply.fit_derivc                 C   s   d|| j d  iS r   r   rR   r<   r<   r=   rS     s    z(Multiply._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   r:   r   r   r   rY   r   rZ   rH   rK   rS   r<   r<   r<   r=   r   a  s   	


r   c                   @   sD   e Zd ZdZedddZdZedd Zedd	 Z	e
d
d ZdS )r   z
    One dimensional redshift scale factor model.

    Parameters
    ----------
    z : float
        Redshift value.

    Notes
    -----
    Model formula:

        .. math:: f(x) = x (1 + z)
    ZRedshiftr   )r2   r1   Tc                 C   s   d| |  S )z2One dimensional RedshiftScaleFactor model functionr   r<   )rF   zr<   r<   r=   rH     s    zRedshiftScaleFactor.evaluatec                 C   s
   | }|gS )z4One dimensional RedshiftScaleFactor model derivativer<   )rF   r   Zd_zr<   r<   r=   rK     s    zRedshiftScaleFactor.fit_derivc                    s\      }dd j  d |_z
 j W n ty8   Y n 0 t fdd j D |_|S )z!Inverse RedshiftScaleFactor modelr~   c                 3   s   | ]}  | jV  qd S r   )rH   r   r   r@   r<   r=   r     r   z.RedshiftScaleFactor.inverse.<locals>.<genexpr>)r   r   r>   r   r   r   r<   r@   r=   r     s    
zRedshiftScaleFactor.inverseN)rT   rU   rV   rW   r   r   r   rZ   rH   rK   rY   r   r<   r<   r<   r=   r     s   

r   c                   @   sX   e Zd ZdZedddZedddZedddZdZe	d	d
 Z
edd Zdd ZdS )r   a  
    One dimensional Sersic surface brightness profile.

    Parameters
    ----------
    amplitude : float
        Surface brightness at r_eff.
    r_eff : float
        Effective (half-light) radius
    n : float
        Sersic Index.

    See Also
    --------
    Gaussian1D, Moffat1D, Lorentz1D

    Notes
    -----
    Model formula:

    .. math::

        I(r)=I_e\exp\left\{-b_n\left[\left(\frac{r}{r_{e}}\right)^{(1/n)}-1\right]\right\}

    The constant :math:`b_n` is defined such that :math:`r_e` contains half the total
    luminosity, and can be solved for numerically.

    .. math::

        \Gamma(2n) = 2\gamma (b_n,2n)

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        from astropy.modeling.models import Sersic1D
        import matplotlib.pyplot as plt

        plt.figure()
        plt.subplot(111, xscale='log', yscale='log')
        s1 = Sersic1D(amplitude=1, r_eff=5)
        r=np.arange(0, 100, .01)

        for n in range(1, 10):
             s1.n = n
             plt.plot(r, s1(r), color=str(float(n) / 15))

        plt.axis([1e-1, 30, 1e-2, 1e3])
        plt.xlabel('log Radius')
        plt.ylabel('log Surface Brightness')
        plt.text(.25, 1.5, 'n=1')
        plt.text(.25, 300, 'n=10')
        plt.xticks([])
        plt.yticks([])
        plt.show()

    References
    ----------
    .. [1] http://ned.ipac.caltech.edu/level5/March05/Graham/Graham2.html
    r   Surface brightness at r_effr0   Effective (half-light) radius   Sersic IndexNc                 C   sL   | j du rddlm} || _ |t|  d| d || d|  d   S )z(One dimensional Sersic profile function.Nr   gammaincinvr.   rq   r   )_gammaincinvscipy.specialr   rD   rE   )clsrrG   r_effnr   r<   r<   r=   rH     s    
$zSersic1D.evaluatec                 C   s"   | j jd u rd S | jd | j jiS rL   )r   rM   rN   r@   r<   r<   r=   rO     s    zSersic1D.input_unitsc                 C   s   || j d  || jd  dS )Nr   )r   rG   rP   rR   r<   r<   r=   rS     s    z(Sersic1D._parameter_units_for_data_units)rT   rU   rV   rW   r   rG   r   r   r   classmethodrH   rY   rO   rS   r<   r<   r<   r=   r     s   ?


r   c                   @   sH   e Zd ZdZedddZedddZedddZedd	 Z	d
d Z
dS )_Trigonometric1Da
  
    Base class for one dimensional trigonometric and inverse trigonometric models

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude
    frequency : float
        Oscillation frequency
    phase : float
        Oscillation phase
    r   zOscillation amplituder0   zOscillation frequencyr   zOscillation phasec                 C   s&   | j jd u rd S | jd d| j j iS )Nr   r~   )	frequencyrM   rN   r@   r<   r<   r=   rO   /  s    z_Trigonometric1D.input_unitsc                 C   s"   || j d  d || jd  dS Nr   r   )r   rG   rP   rR   r<   r<   r=   rS   5  s    z0_Trigonometric1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   phaserY   rO   rS   r<   r<   r<   r=   r     s   
r   c                   @   s4   e Zd ZdZedd Zedd Zedd ZdS )	r!   al  
    One dimensional Sine model.

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude
    frequency : float
        Oscillation frequency
    phase : float
        Oscillation phase

    See Also
    --------
    ArcSine1D, Cosine1D, Tangent1D, Const1D, Linear1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = A \sin(2 \pi f x + 2 \pi p)

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Sine1D

        plt.figure()
        s1 = Sine1D(amplitude=1, frequency=.25)
        r=np.arange(0, 10, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([0, 10, -5, 5])
        plt.show()
    c                 C   s.   t ||  |  }t|tr |j}|t| S )z#One dimensional Sine model function)TWOPIr   r   rm   rD   rt   rF   rG   r   r   argumentr<   r<   r=   rH   h  s    
zSine1D.evaluatec                 C   sl   t t| |  t|  }t|  | t t| |  t|   }t| t t| |  t|   }|||gS )z%One dimensional Sine model derivative)rD   rt   r   rs   rF   rG   r   r   rJ   d_frequencyd_phaser<   r<   r=   rK   u  s    
zSine1D.fit_derivc                 C   s   t | j| j| jdS )zOne dimensional inverse of SinerG   r   r   )r$   rG   r   r   r@   r<   r<   r=   r     s    zSine1D.inverseN	rT   rU   rV   rW   rZ   rH   rK   rY   r   r<   r<   r<   r=   r!   :  s   -


r!   c                   @   s4   e Zd ZdZedd Zedd Zedd ZdS )	r"   ar  
    One dimensional Cosine model.

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude
    frequency : float
        Oscillation frequency
    phase : float
        Oscillation phase

    See Also
    --------
    ArcCosine1D, Sine1D, Tangent1D, Const1D, Linear1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = A \cos(2 \pi f x + 2 \pi p)

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Cosine1D

        plt.figure()
        s1 = Cosine1D(amplitude=1, frequency=.25)
        r=np.arange(0, 10, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([0, 10, -5, 5])
        plt.show()
    c                 C   s.   t ||  |  }t|tr |j}|t| S )z%One dimensional Cosine model function)r   r   r   rm   rD   rs   r   r<   r<   r=   rH     s    
zCosine1D.evaluatec                 C   sp   t t| |  t|  }t|  | t t| |  t|    }t| t t| |  t|    }|||gS )z'One dimensional Cosine model derivative)rD   rs   r   rt   r   r<   r<   r=   rK     s    
zCosine1D.fit_derivc                 C   s   t | j| j| jdS )z!One dimensional inverse of Cosiner   )r%   rG   r   r   r@   r<   r<   r=   r     s    zCosine1D.inverseNr   r<   r<   r<   r=   r"     s   -


r"   c                   @   s<   e Zd ZdZedd Zedd Zedd Zdd	 Z	d
S )r#   a  
    One dimensional Tangent model.

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude
    frequency : float
        Oscillation frequency
    phase : float
        Oscillation phase

    See Also
    --------
    Sine1D, Cosine1D, Const1D, Linear1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = A \tan(2 \pi f x + 2 \pi p)

    Note that the tangent function is undefined for inputs of the form
    pi/2 + n*pi for all integers n. Thus thus the default bounding box
    has been restricted to:

        .. math:: [(-1/4 - p)/f, (1/4 - p)/f]

    which is the smallest interval for the tangent function to be continuous
    on.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Tangent1D

        plt.figure()
        s1 = Tangent1D(amplitude=1, frequency=.25)
        r=np.arange(0, 10, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([0, 10, -5, 5])
        plt.show()
    c                 C   s.   t ||  |  }t|tr |j}|t| S )z&One dimensional Tangent model function)r   r   r   rm   rD   tanr   r<   r<   r=   rH     s    
zTangent1D.evaluatec                 C   sb   dt t| |  t|  d  }t t| |  t|  }t|  | | }t| | }|||gS )z(One dimensional Tangent model derivativer   r.   )rD   rs   r   r   )rF   rG   r   r   ZsecrJ   r   r   r<   r<   r=   rK     s
    "zTangent1D.fit_derivc                 C   s   t | j| j| jdS )z"One dimensional inverse of Tangentr   )r&   rG   r   r   r@   r<   r<   r=   r   #  s    zTangent1D.inversec                 C   s<   d| j  | j d| j  | j g}| jjdur8|| jj }|S )a
        Tuple defining the default ``bounding_box`` limits,
        ``(x_low, x_high)``
        g      пg      ?N)r   r   rM   )r9   Zbboxr<   r<   r=   r>   )  s     zTangent1D.bounding_boxN)
rT   rU   rV   rW   rZ   rH   rK   rY   r   r>   r<   r<   r<   r=   r#     s   6



r#   c                   @   s$   e Zd ZdZedd Zdd ZdS )_InverseTrigonometric1DzE
    Base class for one dimensional inverse trigonometric models
    c                 C   s"   | j jd u rd S | jd | j jiS rL   )rG   rM   rN   r@   r<   r<   r=   rO   <  s    z#_InverseTrigonometric1D.input_unitsc                 C   s"   || j d  d || jd  dS r   rQ   rN   rR   r<   r<   r=   rS   B  s    z7_InverseTrigonometric1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   rY   rO   rS   r<   r<   r<   r=   r   7  s   
r   c                   @   s<   e Zd ZdZedd Zedd Zdd Zedd	 Z	d
S )r$   aR  
    One dimensional ArcSine model returning values between -pi/2 and pi/2
    only.

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude for corresponding Sine
    frequency : float
        Oscillation frequency for corresponding Sine
    phase : float
        Oscillation phase for corresponding Sine

    See Also
    --------
    Sine1D, ArcCosine1D, ArcTangent1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = ((arcsin(x / A) / 2pi) - p) / f

    The arcsin function being used for this model will only accept inputs
    in [-A, A]; otherwise, a runtime warning will be thrown and the result
    will be NaN. To avoid this, the bounding_box has been properly set to
    accommodate this; therefore, it is recommended that this model always
    be evaluated with the ``with_bounding_box=True`` option.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import ArcSine1D

        plt.figure()
        s1 = ArcSine1D(amplitude=1, frequency=.25)
        r=np.arange(-1, 1, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([-1, 1, -np.pi/2, np.pi/2])
        plt.show()
    c                 C   s2   | | }t |tr|j}t|t }|| | S )z&One dimensional ArcSine model function)r   r   rm   rD   arcsinr   )rF   rG   r   r   r   Zarc_siner<   r<   r=   rH   |  s
    	
zArcSine1D.evaluatec                 C   sh   |  t | |d  td| | d    }|t| | t   |d  }d| t| j }|||gS )z(One dimensional ArcSine model derivativer.   r   r   )r   rD   re   r   onesrc   r   r<   r<   r=   rK     s    ,zArcSine1D.fit_derivc                 C   s   d| j  d| j  fS r   r   r   rG   r@   r<   r<   r=   r>     s    zArcSine1D.bounding_boxc                 C   s   t | j| j| jdS )z"One dimensional inverse of ArcSiner   )r!   rG   r   r   r@   r<   r<   r=   r     s    zArcSine1D.inverseN
rT   rU   rV   rW   rZ   rH   rK   r>   rY   r   r<   r<   r<   r=   r$   G  s   4

r$   c                   @   s<   e Zd ZdZedd Zedd Zdd Zedd	 Z	d
S )r%   aF  
    One dimensional ArcCosine returning values between 0 and pi only.


    Parameters
    ----------
    amplitude : float
        Oscillation amplitude for corresponding Cosine
    frequency : float
        Oscillation frequency for corresponding Cosine
    phase : float
        Oscillation phase for corresponding Cosine

    See Also
    --------
    Cosine1D, ArcSine1D, ArcTangent1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = ((arccos(x / A) / 2pi) - p) / f

    The arccos function being used for this model will only accept inputs
    in [-A, A]; otherwise, a runtime warning will be thrown and the result
    will be NaN. To avoid this, the bounding_box has been properly set to
    accommodate this; therefore, it is recommended that this model always
    be evaluated with the ``with_bounding_box=True`` option.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import ArcCosine1D

        plt.figure()
        s1 = ArcCosine1D(amplitude=1, frequency=.25)
        r=np.arange(-1, 1, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([-1, 1, 0, np.pi])
        plt.show()
    c                 C   s2   | | }t |tr|j}t|t }|| | S )z(One dimensional ArcCosine model function)r   r   rm   rD   arccosr   rF   rG   r   r   r   Zarc_cosr<   r<   r=   rH     s
    	
zArcCosine1D.evaluatec                 C   sf   | t | |d  td| | d    }|t| | t   |d  }d| t| j }|||gS )z*One dimensional ArcCosine model derivativer.   r   r   )r   rD   re   r   r   rc   r   r<   r<   r=   rK     s    *zArcCosine1D.fit_derivc                 C   s   d| j  d| j  fS r   r   r@   r<   r<   r=   r>     s    zArcCosine1D.bounding_boxc                 C   s   t | j| j| jdS )z$One dimensional inverse of ArcCosiner   )r"   rG   r   r   r@   r<   r<   r=   r     s    zArcCosine1D.inverseNr   r<   r<   r<   r=   r%     s   4

r%   c                   @   s4   e Zd ZdZedd Zedd Zedd ZdS )	r&   a  
    One dimensional ArcTangent model returning values between -pi/2 and
    pi/2 only.

    Parameters
    ----------
    amplitude : float
        Oscillation amplitude for corresponding Tangent
    frequency : float
        Oscillation frequency for corresponding Tangent
    phase : float
        Oscillation phase for corresponding Tangent

    See Also
    --------
    Tangent1D, ArcSine1D, ArcCosine1D


    Notes
    -----
    Model formula:

        .. math:: f(x) = ((arctan(x / A) / 2pi) - p) / f

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import ArcTangent1D

        plt.figure()
        s1 = ArcTangent1D(amplitude=1, frequency=.25)
        r=np.arange(-10, 10, .01)

        for amplitude in range(1,4):
             s1.amplitude = amplitude
             plt.plot(r, s1(r), color=str(0.25 * amplitude), lw=2)

        plt.axis([-10, 10, -np.pi/2, np.pi/2])
        plt.show()
    c                 C   s2   | | }t |tr|j}t|t }|| | S )z)One dimensional ArcTangent model function)r   r   rm   rD   arctanr   r   r<   r<   r=   rH   0  s
    	
zArcTangent1D.evaluatec                 C   sb   |  t | |d  d| | d    }|t| | t   |d  }d| t| j }|||gS )z+One dimensional ArcTangent model derivativer.   r   r   )r   rD   r   r   rc   r   r<   r<   r=   rK   @  s    &zArcTangent1D.fit_derivc                 C   s   t | j| j| jdS )z%One dimensional inverse of ArcTangentr   )r#   rG   r   r   r@   r<   r<   r=   r   I  s    zArcTangent1D.inverseNr   r<   r<   r<   r=   r&     s   .

r&   c                   @   sd   e Zd ZdZedddZedddZdZedd	 Z	ed
d Z
edd Zedd Zdd ZdS )r   a(  
    One dimensional Line model.

    Parameters
    ----------
    slope : float
        Slope of the straight line

    intercept : float
        Intercept of the straight line

    See Also
    --------
    Const1D

    Notes
    -----
    Model formula:

        .. math:: f(x) = a x + b
    r   zSlope of the straight liner0   r   zIntercept of the straight lineTc                 C   s   ||  | S )z#One dimensional Line model functionr<   )rF   slope	interceptr<   r<   r=   rH   j  s    zLinear1D.evaluatec                 G   s   | }t | }||gS )z@One dimensional Line model derivative with respect to parametersr   )rF   r   Zd_sloped_interceptr<   r<   r=   rK   p  s    
zLinear1D.fit_derivc                 C   s&   | j d }| j | j  }| j||dS )Nr   )r   r   )r   r   ra   )r9   Z	new_slopeZnew_interceptr<   r<   r=   r   x  s    
zLinear1D.inversec                 C   s6   | j jd u r| jjd u rd S | jd | j j| jj iS rL   )r   rM   r   rN   r@   r<   r<   r=   rO   ~  s    zLinear1D.input_unitsc                 C   s,   || j d  || j d  || jd   dS )Nr   )r   r   r   rR   r<   r<   r=   rS     s    z(Linear1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   r   r   r   rZ   rH   rK   rY   r   rO   rS   r<   r<   r<   r=   r   P  s   



r   c                   @   sX   e Zd ZdZedddZedddZedddZdZe	d	d
 Z
e	dd Zdd ZdS )r   a9  
    Two dimensional Plane model.

    Parameters
    ----------
    slope_x : float
        Slope of the plane in X

    slope_y : float
        Slope of the plane in Y

    intercept : float
        Z-intercept of the plane

    Notes
    -----
    Model formula:

        .. math:: f(x, y) = a x + b y + c
    r   zSlope of the plane in Xr0   zSlope of the plane in Yr   zZ-intercept of the planeTc                 C   s   ||  ||  | S )z$Two dimensional Plane model functionr<   )rF   ru   slope_xslope_yr   r<   r<   r=   rH     s    zPlanar2D.evaluatec                 G   s   | }|}t | }|||gS )zATwo dimensional Plane model derivative with respect to parametersr   )rF   ru   r   Z	d_slope_xZ	d_slope_yr   r<   r<   r=   rK     s    
zPlanar2D.fit_derivc                 C   s(   |d |d |d  |d |d  dS )Nr   rF   ru   )r   r   r   r<   rR   r<   r<   r=   rS     s    z(Planar2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   r   r   r   r   rZ   rH   rK   rS   r<   r<   r<   r=   r     s   

r   c                   @   sj   e Zd ZdZedddZedddZedddZedd	 Z	ed
d Z
dddZedd Zdd ZdS )r   a  
    One dimensional Lorentzian model.

    Parameters
    ----------
    amplitude : float or `~astropy.units.Quantity`.
        Peak value - for a normalized profile (integrating to 1),
        set amplitude = 2 / (np.pi * fwhm)
    x_0 : float or `~astropy.units.Quantity`.
        Position of the peak
    fwhm : float or `~astropy.units.Quantity`.
        Full width at half maximum (FWHM)

    See Also
    --------
    Gaussian1D, Box1D, RickerWavelet1D

    Notes
    -----
    Either all or none of input ``x``, position ``x_0`` and ``fwhm`` must be provided
    consistently with compatible units or as unitless numbers.

    Model formula:

    .. math::

        f(x) = \frac{A \gamma^{2}}{\gamma^{2} + \left(x - x_{0}\right)^{2}}

    where :math:`\gamma` is half of given FWHM.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Lorentz1D

        plt.figure()
        s1 = Lorentz1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()
    r   z
Peak valuer0   r   Position of the peakzFull width at half maximumc                 C   s(   ||d d  | | d |d d   S )z)One dimensional Lorentzian model functionr/   r.   r<   )rF   rG   x_0rA   r<   r<   r=   rH     s    
zLorentz1D.evaluatec                 C   sj   |d |d | | d   }|| d|  d|   |d | | d   }d| | | d|  }|||gS )zFOne dimensional Lorentzian model derivative with respect to parametersr.   r   r<   )rF   rG   r   rA   rJ   d_x_0Zd_fwhmr<   r<   r=   rK     s    zLorentz1D.fit_deriv   c                 C   s    | j }|| j }|| || fS )ar  Tuple defining the default ``bounding_box`` limits,
        ``(x_low, x_high)``.

        Parameters
        ----------
        factor : float
            The multiple of FWHM used to define the limits.
            Default is chosen to include most (99%) of the
            area under the curve, while still showing the
            central feature of interest.

        )r   rA   r8   r<   r<   r=   r>     s    
zLorentz1D.bounding_boxc                 C   s"   | j jd u rd S | jd | j jiS rL   r   rM   rN   r@   r<   r<   r=   rO     s    zLorentz1D.input_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r   rA   rG   rP   rR   r<   r<   r=   rS     s    z)Lorentz1D._parameter_units_for_data_unitsN)r   )rT   rU   rV   rW   r   rG   r   rA   rZ   rH   rK   r>   rY   rO   rS   r<   r<   r<   r=   r     s   4

	

r   c                       s   e Zd ZdZedddZedddZedej ddZ	ee
dd	dZeejZee
dZee
dej Zejded
Zejded
ZdZejeje	jejdf fdd	Zdd Zdd Zdd Zedd Zdd ZedddZ  Z S )r*   a  
    One dimensional model for the Voigt profile.

    Parameters
    ----------
    x_0 : float or `~astropy.units.Quantity`
        Position of the peak
    amplitude_L : float or `~astropy.units.Quantity`.
        The Lorentzian amplitude (peak of the associated Lorentz function)
        - for a normalized profile (integrating to 1), set
        amplitude_L = 2 / (np.pi * fwhm_L)
    fwhm_L : float or `~astropy.units.Quantity`
        The Lorentzian full width at half maximum
    fwhm_G : float or `~astropy.units.Quantity`.
        The Gaussian full width at half maximum
    method : str, optional
        Algorithm for computing the complex error function; one of
        'Humlicek2' (default, fast and generally more accurate than ``rtol=3.e-5``) or
        'Scipy', alternatively 'wofz' (requires ``scipy``, almost as fast and
        reference in accuracy).

    See Also
    --------
    Gaussian1D, Lorentz1D

    Notes
    -----
    Either all or none of input ``x``, position ``x_0`` and the ``fwhm_*`` must be provided
    consistently with compatible units or as unitless numbers.
    Voigt function is calculated as real part of the complex error function computed from either
    Humlicek's rational approximations (JQSRT 21:309, 1979; 27:437, 1982) following
    Schreier 2018 (MNRAS 479, 3068; and ``hum2zpf16m`` from his cpfX.py module); or
    `~scipy.special.wofz` (implementing 'Faddeeva.cc').

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        from astropy.modeling.models import Voigt1D
        import matplotlib.pyplot as plt

        plt.figure()
        x = np.arange(0, 10, 0.01)
        v1 = Voigt1D(x_0=5, amplitude_L=10, fwhm_L=0.5, fwhm_G=0.9)
        plt.plot(x, v1(x))
        plt.show()
    r   r   r0   r   zThe Lorentzian amplituder.   z)The Lorentzian full width at half maximumz'The Gaussian full width at half maximum)ZdtypeN	humlicek2c                    sx   t | dv r$ddlm} || _n*t | dkr>| j| _ntd| d| jj| _t	 j
f ||||d| d S )N)wofzZscipyr   )r   r   z2Not a valid method for Voigt1D Faddeeva function: .)r   amplitude_Lfwhm_Lfwhm_G)strlowerr   r   	_faddeeva_hum2zpf16crd   rT   methodrg   rh   )r9   r   r   r   r   r   ri   r   rj   r<   r=   rh   d  s    

zVoigt1D.__init__c                 C   s@   |j | jj kr(tj|| jdddr(| jS | || _|| _| jS )zCall complex error (Faddeeva) function w(z) implemented by algorithm `method`;
        cache results for consecutive calls from `evaluate`, `fit_deriv`.g+=gV瞯<)ZrtolZatol)rc   _last_zrD   Zallclose_last_wr   )r9   r   r<   r<   r=   
_wrap_wofzr  s    zVoigt1D._wrap_wofzc                 C   sB   t d||  d|  | j | }| |j| j | | | S )z@One dimensional Voigt function scaled to Lorentz peak amplitude.r.                 ?)rD   
atleast_1dsqrt_ln2r   real
sqrt_ln2pi)r9   rF   r   r   r   r   r   r<   r<   r=   rH   ~  s    $zVoigt1D.evaluatec           
      C   s   | j | }td||  d|  | }| || | | | j }d| | d| | |  }	|	j d | |j| |j| |	j|  |j |d||  |	j ||	j    | gS )zLDerivative of the one dimensional Voigt function with respect to parameters.r.   r   y               @)r   rD   r   r   sqrt_pir   imag)
r9   rF   r   r   r   r   sr   wZdwdzr<   r<   r=   rK     s    
*zVoigt1D.fit_derivc                 C   s"   | j jd u rd S | jd | j jiS rL   r   r@   r<   r<   r=   rO     s    zVoigt1D.input_unitsc                 C   s6   || j d  || j d  || j d  || jd  dS )Nr   )r   r   r   r   rP   rR   r<   r<   r=   rS     s
    z'Voigt1D._parameter_units_for_data_units      $@c                 C   s  t g d}t g d}dt t j }| |  }d| || d   d||d    }t | j|k rt| j| j |k }| t | d }|| }	|d	 | |d
  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  | |d  }
|	|d
  |	 |d  |	 |d  |	 |d  |	 |d  |	 |d  |	 |d  |	 |d  }t 	|||
|  |S )a  Complex error function w(z) for z = x + iy combining Humlicek's rational approximations:

        |x| + y > 10:  Humlicek (JQSRT, 1982) rational approximation for region II;
        else:          Humlicek (JQSRT, 1979) rational approximation with n=16 and delta=y0=1.35

        Version using a mask and np.place;
        single complex argument version of Franz Schreier's cpfX.hum2zpf16m.
        Originally licensed under a 3-clause BSD style license - see
        https://atmos.eoc.dlr.de/tools/lbl4IR/cpfX.py
        )gO@y       tgˤo7	y        pd,Ag"YYAy       gi"U&y        mA6@gGb@y       {3qglL}`sy        p@g:@qF@y       JJe{LAgE|_y        BP?)g@r[   g    r[   g   @
Ar[   g    2r[   g    @r[   g    Sr[   g     T@r[   g      Nr[   r~   r~   r   g]/M?g      ?g      @y        ?               
   	               r   rI   r.   r   r   )
rD   rb   re   pianyr   absr   whereZplace)r   r   ZAAZbbZ
sqrt_piinvZzzr   maskZZZZZnumerZdenomr<   r<   r=   r     s    	
$:>zVoigt1D._hum2zpf16c)r   )!rT   rU   rV   rW   r   r   r   rD   r   r   logr   re   r   r   r   zeroscomplexr   floatr   r   r1   rh   r   rH   rK   rY   rO   rS   rZ   r   r   r<   r<   rj   r=   r*   !  s<   2



r*   c                   @   sL   e Zd ZdZedddZdZedd Zedd	 Z	e
d
d Zdd ZdS )r   a  
    One dimensional Constant model.

    Parameters
    ----------
    amplitude : float
        Value of the constant function

    See Also
    --------
    Const2D

    Notes
    -----
    Model formula:

        .. math:: f(x) = A

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Const1D

        plt.figure()
        s1 = Const1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()
    r   Value of the constant functionr0   Tc                 C   sX   |j dkr(tj| dd} | |  n|tj| dd } t|trTt| |jddS | S )z'One dimensional Constant model functionr   FZsubokrM   r   	sizerD   Z
empty_likeZfillitemr   r   r   rM   )rF   rG   r<   r<   r=   rH     s    

zConst1D.evaluatec                 C   s   t | }|gS )zDOne dimensional Constant model derivative with respect to parametersr   )rF   rG   rJ   r<   r<   r=   rK     s    
zConst1D.fit_derivc                 C   s   d S r   r<   r@   r<   r<   r=   rO     s    zConst1D.input_unitsc                 C   s   d|| j d  iS NrG   r   r   rR   r<   r<   r=   rS      s    z'Const1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   rZ   rH   rK   rY   rO   rS   r<   r<   r<   r=   r     s   (


r   c                   @   s@   e Zd ZdZedddZdZedd Ze	dd	 Z
d
d ZdS )r   z
    Two dimensional Constant model.

    Parameters
    ----------
    amplitude : float
        Value of the constant function

    See Also
    --------
    Const1D

    Notes
    -----
    Model formula:

        .. math:: f(x, y) = A
    r   r   r0   Tc                 C   sX   |j dkr(tj| dd} | |  n|tj| dd } t|trTt| |jddS | S )z'Two dimensional Constant model functionr   Fr  r  r  )rF   ru   rG   r<   r<   r=   rH   ;  s    

zConst2D.evaluatec                 C   s   d S r   r<   r@   r<   r<   r=   rO   L  s    zConst2D.input_unitsc                 C   s   d|| j d  iS r  r   rR   r<   r<   r=   rS   P  s    z'Const2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   rZ   rH   rY   rO   rS   r<   r<   r<   r=   r   $  s   

r   c                   @   s   e Zd ZdZedddZedddZedddZedddZedd	dZ	edd
dZ
edd Zedd Zedd Zdd ZdS )r   aA  
    A 2D Ellipse model.

    Parameters
    ----------
    amplitude : float
        Value of the ellipse.

    x_0 : float
        x position of the center of the disk.

    y_0 : float
        y position of the center of the disk.

    a : float
        The length of the semimajor axis.

    b : float
        The length of the semiminor axis.

    theta : float
        The rotation angle in radians of the semimajor axis.  The
        rotation angle increases counterclockwise from the positive x
        axis.

    See Also
    --------
    Disk2D, Box2D

    Notes
    -----
    Model formula:

    .. math::

        f(x, y) = \left \{
                    \begin{array}{ll}
                      \mathrm{amplitude} & : \left[\frac{(x - x_0) \cos
                        \theta + (y - y_0) \sin \theta}{a}\right]^2 +
                        \left[\frac{-(x - x_0) \sin \theta + (y - y_0)
                        \cos \theta}{b}\right]^2  \leq 1 \\
                      0 & : \mathrm{otherwise}
                    \end{array}
                  \right.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        from astropy.modeling.models import Ellipse2D
        from astropy.coordinates import Angle
        import matplotlib.pyplot as plt
        import matplotlib.patches as mpatches
        x0, y0 = 25, 25
        a, b = 20, 10
        theta = Angle(30, 'deg')
        e = Ellipse2D(amplitude=100., x_0=x0, y_0=y0, a=a, b=b,
                      theta=theta.radian)
        y, x = np.mgrid[0:50, 0:50]
        fig, ax = plt.subplots(1, 1)
        ax.imshow(e(x, y), origin='lower', interpolation='none', cmap='Greys_r')
        e2 = mpatches.Ellipse((x0, y0), 2*a, 2*b, theta.degree, edgecolor='red',
                              facecolor='none')
        ax.add_patch(e2)
        plt.show()
    r   zValue of the ellipser0   r   z%X position of the center of the disk.z%Y position of the center of the disk.z The length of the semimajor axisz The length of the semiminor axiszQThe rotation angle in radians of the semimajor axis (Positive - counterclockwise)c                 C   s   | | }|| }	t |}
t |}||
 |	|  }||  |	|
  }|| d || d  dk}t |g|g}t|trt||jddS |S )z'Two dimensional Ellipse model function.r.   r~   Fr  )rD   rs   rt   selectr   r   rM   )rF   ru   rG   r   y_0rn   ro   r`   ZxxZyyr   r   Z
numerator1Z
numerator2Z
in_ellipseresultr<   r<   r=   rH     s    


zEllipse2D.evaluatec                 C   sL   | j }| j}| jj}t|||\}}| j| | j| f| j| | j| ffS zu
        Tuple defining the default ``bounding_box`` limits.

        ``((y_low, y_high), (x_low, x_high))``
        )rn   ro   r`   rm   r
   r  r   )r9   rn   ro   r`   r;   rp   r<   r<   r=   r>     s    zEllipse2D.bounding_boxc                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r   rM   rN   r  r@   r<   r<   r=   rO     s
    zEllipse2D.input_unitsc                 C   sj   || j d  || j d  kr$td|| j d  || j d  || j d  || j d  tj|| jd  dS )Nr   r   r   )r   r  rn   ro   r`   rG   r   rR   r<   r<   r=   rS     s    z)Ellipse2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  rn   ro   r`   rZ   rH   rY   r>   rO   rS   r<   r<   r<   r=   r   T  s   E


r   c                   @   sl   e Zd ZdZedddZedddZedddZedddZe	d	d
 Z
edd Zedd Zdd ZdS )r   af  
    Two dimensional radial symmetric Disk model.

    Parameters
    ----------
    amplitude : float
        Value of the disk function
    x_0 : float
        x position center of the disk
    y_0 : float
        y position center of the disk
    R_0 : float
        Radius of the disk

    See Also
    --------
    Box2D, TrapezoidDisk2D

    Notes
    -----
    Model formula:

        .. math::

            f(r) = \left \{
                     \begin{array}{ll}
                       A & : r \leq R_0 \\
                       0 & : r > R_0
                     \end{array}
                   \right.
    r   zValue of disk functionr0   r   z X position of center of the diskz Y position of center of the diskzRadius of the diskc                 C   sN   | | d || d  }t ||d kg|g}t|trJt||jddS |S )z#Two dimensional Disk model functionr.   Fr  )rD   r  r   r   rM   )rF   ru   rG   r   r  R_0rrr	  r<   r<   r=   rH     s
    
zDisk2D.evaluatec                 C   s0   | j | j | j | j f| j| j | j| j ffS r
  )r  r  r   r@   r<   r<   r=   r>     s    zDisk2D.bounding_boxc                 C   s<   | j jd u r| jjd u rd S | jd | j j| jd | jjiS r   r   rM   r  rN   r@   r<   r<   r=   rO     s
    zDisk2D.input_unitsc                 C   sZ   || j d  || j d  kr$td|| j d  || j d  || j d  || jd  dS )Nr   r   r   )r   r  r  rG   rN   r   rQ   rR   r<   r<   r=   rS     s    z&Disk2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r  rZ   rH   rY   r>   rO   rS   r<   r<   r<   r=   r     s    




r   c                       s   e Zd ZdZedddZedddZedddZedddZedd	dZ	ej
ej
ej
d
d
d
f fdd	Zedd Zedd Zedd Zdd Z  ZS )r)   a7  
    Two dimensional radial symmetric Ring model.

    Parameters
    ----------
    amplitude : float
        Value of the disk function
    x_0 : float
        x position center of the disk
    y_0 : float
        y position center of the disk
    r_in : float
        Inner radius of the ring
    width : float
        Width of the ring.
    r_out : float
        Outer Radius of the ring. Can be specified instead of width.

    See Also
    --------
    Disk2D, TrapezoidDisk2D

    Notes
    -----
    Model formula:

        .. math::

            f(r) = \left \{
                     \begin{array}{ll}
                       A & : r_{in} \leq r \leq r_{out} \\
                       0 & : \text{else}
                     \end{array}
                   \right.

    Where :math:`r_{out} = r_{in} + r_{width}`.
    r   zValue of the disk functionr0   r   zX position of center of disczY position of center of disczInner radius of the ringzWidth of the ringNc                    sn  |d u r*|d u r*|d u r*| j j}| jj}n|d urL|d u rL|d u rL| jj}n|d u rv|d urv|d u rv| j j}|| }n|d u r|d u r|d ur| j j}n~|d ur|d ur|d u r|| }n\|d u r|d ur|d ur|| }n:|d ur|d ur|d urt||| krtdt|dk s6t|dk rLtd|d|dt jf |||||d| d S )NzWidth must be r_out - r_inr   zr_in=z and width=z must both be >=0)rG   r   r  r_inwidth)r  r1   r  rD   r   r	   rg   rh   )r9   rG   r   r  r  r  Zr_outri   rj   r<   r=   rh   S  s0    





 
zRing2D.__init__c           
      C   sf   | | d || d  }t ||d k||| d k}t |g|g}	t|trbt|	|jddS |	S )z$Two dimensional Ring model function.r.   Fr  rD   logical_andr  r   r   rM   )
rF   ru   rG   r   r  r  r  r  Zr_ranger	  r<   r<   r=   rH   o  s     
zRing2D.evaluatec                 C   s4   | j | j }| j| | j| f| j| | j| ffS n
        Tuple defining the default ``bounding_box``.

        ``((y_low, y_high), (x_low, x_high))``
        )r  r  r  r   r9   Zdrr<   r<   r=   r>   {  s    zRing2D.bounding_boxc                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO     s
    zRing2D.input_unitsc                 C   sf   || j d  || j d  kr$td|| j d  || j d  || j d  || j d  || jd  dS )Nr   r   r   )r   r  r  r  rG   r  rR   r<   r<   r=   rS     s    z&Ring2D._parameter_units_for_data_units)rT   rU   rV   rW   r   rG   r   r  r  r  r1   rh   rZ   rH   rY   r>   rO   rS   r   r<   r<   rj   r=   r)   &  s"   &


r)   c                   @   sl   e Zd ZdZedddZedddZedddZedd	 Z	e
d
d Ze
dd Ze
dd Zdd ZdS )r   a  
    One dimensional Box model.

    Parameters
    ----------
    amplitude : float
        Amplitude A
    x_0 : float
        Position of the center of the box function
    width : float
        Width of the box

    See Also
    --------
    Box2D, TrapezoidDisk2D

    Notes
    -----
    Model formula:

      .. math::

            f(x) = \left \{
                     \begin{array}{ll}
                       A & : x_0 - w/2 \leq x \leq x_0 + w/2 \\
                       0 & : \text{else}
                     \end{array}
                   \right.

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Box1D

        plt.figure()
        s1 = Box1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            s1.width = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()
    r   zAmplitude Ar0   r   z"Position of center of box functionzWidth of the boxc                 C   s6   t | ||d  k| ||d  k}t |g|gdS )z"One dimensional Box model functionr/   r   )rD   r  r  )rF   rG   r   r  Zinsider<   r<   r=   rH     s    $zBox1D.evaluatec                 C   s   | j d }| j| | j| fS zc
        Tuple defining the default ``bounding_box`` limits.

        ``(x_low, x_high))``
        r.   )r  r   r9   r;   r<   r<   r=   r>     s    
zBox1D.bounding_boxc                 C   s"   | j jd u rd S | jd | j jiS rL   r   r@   r<   r<   r=   rO     s    zBox1D.input_unitsc                 C   s"   | j jd u rd S | jd | j jiS rL   )rG   rM   rQ   r@   r<   r<   r=   return_units  s    zBox1D.return_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r   r  rG   rP   rR   r<   r<   r=   rS     s    z%Box1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  rZ   rH   rY   r>   rO   r  rS   r<   r<   r<   r=   r     s   4



r   c                   @   sx   e Zd ZdZedddZedddZedddZedddZedd	dZ	e
d
d Zedd Zedd Zdd ZdS )r   a  
    Two dimensional Box model.

    Parameters
    ----------
    amplitude : float
        Amplitude
    x_0 : float
        x position of the center of the box function
    x_width : float
        Width in x direction of the box
    y_0 : float
        y position of the center of the box function
    y_width : float
        Width in y direction of the box

    See Also
    --------
    Box1D, Gaussian2D, Moffat2D

    Notes
    -----
    Model formula:

      .. math::

            f(x, y) = \left \{
                     \begin{array}{ll}
            A : & x_0 - w_x/2 \leq x \leq x_0 + w_x/2 \text{ and} \\
                & y_0 - w_y/2 \leq y \leq y_0 + w_y/2 \\
            0 : & \text{else}
                     \end{array}
                   \right.

    r   Z	Amplituder0   r   z,X position of the center of the box functionz,Y position of the center of the box functionzWidth in x direction of the boxzWidth in y direction of the boxc           
      C   s   t | ||d  k| ||d  k}t |||d  k|||d  k}t t ||g|gd}	t|tr|t|	|jddS |	S )z"Two dimensional Box model functionr/   r   Fr  r  )
rF   ru   rG   r   r  x_widthy_widthZx_rangeZy_ranger	  r<   r<   r=   rH   %	  s    
zBox2D.evaluatec                 C   s<   | j d }| jd }| j| | j| f| j| | j| ffS )r  r.   )r  r  r  r   )r9   r;   rp   r<   r<   r=   r>   4	  s
    

zBox2D.bounding_boxc                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO   B	  s
    zBox2D.input_unitsc                 C   sB   || j d  || j d  || j d  || j d  || jd  dS )Nr   r   )r   r  r  r  rG   rP   rR   r<   r<   r=   rS   I	  s    z%Box2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r  r  rZ   rH   rY   r>   rO   rS   r<   r<   r<   r=   r     s   $


r   c                   @   sl   e Zd ZdZedddZedddZedddZedddZe	d	d
 Z
edd Zedd Zdd ZdS )r'   ae  
    One dimensional Trapezoid model.

    Parameters
    ----------
    amplitude : float
        Amplitude of the trapezoid
    x_0 : float
        Center position of the trapezoid
    width : float
        Width of the constant part of the trapezoid.
    slope : float
        Slope of the tails of the trapezoid

    See Also
    --------
    Box1D, Gaussian1D, Moffat1D

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Trapezoid1D

        plt.figure()
        s1 = Trapezoid1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            s1.width = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()
    r   Amplitude of the trapezoidr0   r   z Center position of the trapezoidz'Width of constant part of the trapezoidzSlope of the tails of trapezoidc                 C   s   ||d  }||d  }|||  }|||  }t | |k| |k }	t | |k| |k }
t | |k| |k }|| |  }|}|||   }t |	|
|g|||g}t|trt||jddS |S )z(One dimensional Trapezoid model functionr/   Fr  r  )rF   rG   r   r  r   Zx2Zx3Zx1Zx4Zrange_aZrange_bZrange_cZval_aZval_bZval_cr	  r<   r<   r=   rH   	  s    
zTrapezoid1D.evaluatec                 C   s*   | j d | j| j  }| j| | j| fS r  )r  rG   r   r   r  r<   r<   r=   r>   	  s    zTrapezoid1D.bounding_boxc                 C   s"   | j jd u rd S | jd | j jiS rL   r   r@   r<   r<   r=   rO   	  s    zTrapezoid1D.input_unitsc                 C   sD   || j d  || j d  || jd  || j d   || jd  dS )Nr   )r   r  r   rG   rP   rR   r<   r<   r=   rS   	  s
    z+Trapezoid1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r   rZ   rH   rY   r>   rO   rS   r<   r<   r<   r=   r'   Q	  s   )


r'   c                   @   sx   e Zd ZdZedddZedddZedddZedddZedd	dZ	e
d
d Zedd Zedd Zdd ZdS )r(   a  
    Two dimensional circular Trapezoid model.

    Parameters
    ----------
    amplitude : float
        Amplitude of the trapezoid
    x_0 : float
        x position of the center of the trapezoid
    y_0 : float
        y position of the center of the trapezoid
    R_0 : float
        Radius of the constant part of the trapezoid.
    slope : float
        Slope of the tails of the trapezoid in x direction.

    See Also
    --------
    Disk2D, Box2D
    r   r  r0   r   z)X position of the center of the trapezoidz)Y position of the center of the trapezoidz$Radius of constant part of trapezoidz*Slope of tails of trapezoid in x directionc                 C   s   t | | d || d  }||k}t ||k||||  k}	|}
||||   }t ||	g|
|g}t|trt||jddS |S )z-Two dimensional Trapezoid Disk model functionr.   Fr  )rD   re   r  r  r   r   rM   )rF   ru   rG   r   r  r  r   r   Zrange_1Zrange_2Zval_1Zval_2r	  r<   r<   r=   rH   	  s    
zTrapezoidDisk2D.evaluatec                 C   s:   | j | j| j  }| j| | j| f| j| | j| ffS r  )r  rG   r   r  r   r  r<   r<   r=   r>   	  s    zTrapezoidDisk2D.bounding_boxc                 C   s<   | j jd u r| jjd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO   	  s
    zTrapezoidDisk2D.input_unitsc                 C   sh   |d |d krt d|| jd  || jd  || jd  || jd  || jd   || jd  dS )NrF   ru   r   r   )r   r  r  r   rG   )r   rN   rQ   rR   r<   r<   r=   rS   	  s    z/TrapezoidDisk2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r  r   rZ   rH   rY   r>   rO   rS   r<   r<   r<   r=   r(   	  s   


r(   c                   @   s^   e Zd ZdZedddZedddZedddZedd	 Z	dddZ
edd Zdd ZdS )r   a  
    One dimensional Ricker Wavelet model (sometimes known as a "Mexican Hat"
    model).

    .. note::

        See https://github.com/astropy/astropy/pull/9445 for discussions
        related to renaming of this model.

    Parameters
    ----------
    amplitude : float
        Amplitude
    x_0 : float
        Position of the peak
    sigma : float
        Width of the Ricker wavelet

    See Also
    --------
    RickerWavelet2D, Box1D, Gaussian1D, Trapezoid1D

    Notes
    -----
    Model formula:

    .. math::

        f(x) = {A \left(1 - \frac{\left(x - x_{0}\right)^{2}}{\sigma^{2}}\right)
        e^{- \frac{\left(x - x_{0}\right)^{2}}{2 \sigma^{2}}}}

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import RickerWavelet1D

        plt.figure()
        s1 = RickerWavelet1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            s1.width = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -2, 4])
        plt.show()
    r   Amplitude (peak) valuer0   r   r   Width of the Ricker waveletc                 C   s4   | | d d|d   }|dd|   t |  S )z-One dimensional Ricker Wavelet model functionr.   r   rC   )rF   rG   r   sigmaZxx_wwr<   r<   r=   rH   8
  s    zRickerWavelet1D.evaluater   c                 C   s    | j }|| j }|| || fS )zTuple defining the default ``bounding_box`` limits,
        ``(x_low, x_high)``.

        Parameters
        ----------
        factor : float
            The multiple of sigma used to define the limits.

        )r   r  r8   r<   r<   r=   r>   ?
  s    

zRickerWavelet1D.bounding_boxc                 C   s"   | j jd u rd S | jd | j jiS rL   r   r@   r<   r<   r=   rO   N
  s    zRickerWavelet1D.input_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r   r  rG   rP   rR   r<   r<   r=   rS   T
  s    z/RickerWavelet1D._parameter_units_for_data_unitsN)r   )rT   rU   rV   rW   r   rG   r   r  rZ   rH   r>   rY   rO   rS   r<   r<   r<   r=   r   	  s   6


r   c                   @   s`   e Zd ZdZedddZedddZedddZedddZe	d	d
 Z
edd Zdd ZdS )r   a  
    Two dimensional Ricker Wavelet model (sometimes known as a "Mexican Hat"
    model).

    .. note::

        See https://github.com/astropy/astropy/pull/9445 for discussions
        related to renaming of this model.

    Parameters
    ----------
    amplitude : float
        Amplitude
    x_0 : float
        x position of the peak
    y_0 : float
        y position of the peak
    sigma : float
        Width of the Ricker wavelet

    See Also
    --------
    RickerWavelet1D, Gaussian2D

    Notes
    -----
    Model formula:

    .. math::

        f(x, y) = A \left(1 - \frac{\left(x - x_{0}\right)^{2}
        + \left(y - y_{0}\right)^{2}}{\sigma^{2}}\right)
        e^{\frac{- \left(x - x_{0}\right)^{2}
        - \left(y - y_{0}\right)^{2}}{2 \sigma^{2}}}
    r   r  r0   r   X position of the peakY position of the peakr  c                 C   s<   | | d || d  d|d   }|d|  t |  S )z-Two dimensional Ricker Wavelet model functionr.   r   rC   )rF   ru   rG   r   r  r  Zrr_wwr<   r<   r=   rH   
  s    $zRickerWavelet2D.evaluatec                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO   
  s
    zRickerWavelet2D.input_unitsc                 C   sZ   || j d  || j d  kr$td|| j d  || j d  || j d  || jd  dS )Nr   r   r   )r   r  r  rG   r  rR   r<   r<   r=   rS   
  s    z/RickerWavelet2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r  rZ   rH   rY   rO   rS   r<   r<   r<   r=   r   Z
  s   $

r   c                   @   sh   e Zd ZdZedddZedddZedddZedddZd	Z	d	Z
ed
d Zedd Zdd Zd	S )r   a  
    Two dimensional Airy disk model.

    Parameters
    ----------
    amplitude : float
        Amplitude of the Airy function.
    x_0 : float
        x position of the maximum of the Airy function.
    y_0 : float
        y position of the maximum of the Airy function.
    radius : float
        The radius of the Airy disk (radius of the first zero).

    See Also
    --------
    Box2D, TrapezoidDisk2D, Gaussian2D

    Notes
    -----
    Model formula:

        .. math:: f(r) = A \left[\frac{2 J_1(\frac{\pi r}{R/R_z})}{\frac{\pi r}{R/R_z}}\right]^2

    Where :math:`J_1` is the first order Bessel function of the first
    kind, :math:`r` is radial distance from the maximum of the Airy
    function (:math:`r = \sqrt{(x - x_0)^2 + (y - y_0)^2}`), :math:`R`
    is the input ``radius`` parameter, and :math:`R_z =
    1.2196698912665045`).

    For an optical system, the radius of the first zero represents the
    limiting angular resolution and is approximately 1.22 * lambda / D,
    where lambda is the wavelength of the light and D is the diameter of
    the aperture.

    See [1]_ for more details about the Airy disk.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Airy_disk
    r   z+Amplitude (peak value) of the Airy functionr0   r   r   r!  z;The radius of the Airy disk (radius of first zero crossing)Nc                 C   s   | j du r6ddlm}m} |ddd tj | _ || _t|| d || d  || j   }	t|	t	rt|	
tj}	t|	j}
tj|	|	dk  }d| | | d |
|	dk< t|t	rt	|
tjdd}
|
|9 }
|
S )	z#Two dimensional Airy model functionNr   )j1jn_zerosr   r.   r/   F)r   )_rzr   r"  r#  rD   r   _j1re   r   r   Zto_valuer   Zdimensionless_unscaledr   rc   )r   rF   ru   rG   r   r  radiusr"  r#  r   r   Zrtr<   r<   r=   rH   
  s    
(

zAiryDisk2D.evaluatec                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO   
  s
    zAiryDisk2D.input_unitsc                 C   sZ   || j d  || j d  kr$td|| j d  || j d  || j d  || jd  dS )Nr   r   r   )r   r  r&  rG   r  rR   r<   r<   r=   rS   
  s    z*AiryDisk2D._parameter_units_for_data_units)rT   rU   rV   rW   r   rG   r   r  r&  r$  r%  r   rH   rY   rO   rS   r<   r<   r<   r=   r   
  s   *

r   c                   @   sx   e Zd ZdZedddZedddZedddZedddZe	d	d
 Z
edd Zedd Ze	dd Zdd ZdS )r   a  
    One dimensional Moffat model.

    Parameters
    ----------
    amplitude : float
        Amplitude of the model.
    x_0 : float
        x position of the maximum of the Moffat model.
    gamma : float
        Core width of the Moffat model.
    alpha : float
        Power index of the Moffat model.

    See Also
    --------
    Gaussian1D, Box1D

    Notes
    -----
    Model formula:

    .. math::

        f(x) = A \left(1 + \frac{\left(x - x_{0}\right)^{2}}{\gamma^{2}}\right)^{- \alpha}

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        import matplotlib.pyplot as plt

        from astropy.modeling.models import Moffat1D

        plt.figure()
        s1 = Moffat1D()
        r = np.arange(-5, 5, .01)

        for factor in range(1, 4):
            s1.amplitude = factor
            s1.width = factor
            plt.plot(r, s1(r), color=str(0.25 * factor), lw=2)

        plt.axis([-5, 5, -1, 4])
        plt.show()
    r   zAmplitude of the modelr0   r   z%X position of maximum of Moffat modelzCore width of Moffat modelPower index of the Moffat modelc                 C   s(   dt | j t dd| j  d  S z
        Moffat full width at half maximum.
        Derivation of the formula is available in
        `this notebook by Yoonsoo Bach <https://nbviewer.jupyter.org/github/ysbach/AO_2017/blob/master/04_Ground_Based_Concept.ipynb#1.2.-Moffat>`_.
        r/   r~   rD   r   gammare   alphar@   r<   r<   r=   rA   7  s    zMoffat1D.fwhmc                 C   s   |d| | | d  |   S )z%One dimensional Moffat model functionr   r.   r<   )rF   rG   r   r*  r+  r<   r<   r=   rH   @  s    zMoffat1D.evaluatec           
      C   s   d| | d |d   }||  }d| | | |  | ||d   }d| | | | d  | ||d   }| | t | }	||||	gS )zBOne dimensional Moffat model derivative with respect to parametersr   r.   rI   rD   r   )
rF   rG   r   r*  r+  Zfacd_Ar   d_gammad_alphar<   r<   r=   rK   F  s    
$
zMoffat1D.fit_derivc                 C   s"   | j jd u rd S | jd | j jiS rL   r   r@   r<   r<   r=   rO   R  s    zMoffat1D.input_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r   r*  rG   rP   rR   r<   r<   r=   rS   X  s    z(Moffat1D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r*  r+  rY   rA   rZ   rH   rK   rO   rS   r<   r<   r<   r=   r      s   1



r   c                   @   s   e Zd ZdZedddZedddZedddZedddZedd	dZ	e
d
d Zedd Zedd Ze
dd Zdd ZdS )r   ak  
    Two dimensional Moffat model.

    Parameters
    ----------
    amplitude : float
        Amplitude of the model.
    x_0 : float
        x position of the maximum of the Moffat model.
    y_0 : float
        y position of the maximum of the Moffat model.
    gamma : float
        Core width of the Moffat model.
    alpha : float
        Power index of the Moffat model.

    See Also
    --------
    Gaussian2D, Box2D

    Notes
    -----
    Model formula:

    .. math::

        f(x, y) = A \left(1 + \frac{\left(x - x_{0}\right)^{2} +
        \left(y - y_{0}\right)^{2}}{\gamma^{2}}\right)^{- \alpha}
    r   z#Amplitude (peak value) of the modelr0   r   z-X position of the maximum of the Moffat modelz-Y position of the maximum of the Moffat modelzCore width of the Moffat modelr'  c                 C   s(   dt | j t dd| j  d  S r(  r)  r@   r<   r<   r=   rA     s    zMoffat2D.fwhmc                 C   s2   | | d || d  |d  }|d| |   S )z%Two dimensional Moffat model functionr.   r   r<   )rF   ru   rG   r   r  r*  r+  rr_ggr<   r<   r=   rH     s     zMoffat2D.evaluatec                 C   s   | | d || d  |d  }d| |  }d| | | | |  |d d|   }	d| | | ||  |d d|   }
| | t d|  }d| | | | |d|   }||	|
||gS )zBTwo dimensional Moffat model derivative with respect to parametersr.   r   r,  )rF   ru   rG   r   r  r*  r+  r0  r-  r   Zd_y_0r/  r.  r<   r<   r=   rK     s     
zMoffat2D.fit_derivc                 C   s4   | j jd u rd S | jd | j j| jd | jjiS d S r   r  r@   r<   r<   r=   rO     s
    zMoffat2D.input_unitsc                 C   sZ   || j d  || j d  kr$td|| j d  || j d  || j d  || jd  dS )Nr   r   r   )r   r  r*  rG   r  rR   r<   r<   r=   rS     s    z(Moffat2D._parameter_units_for_data_unitsN)rT   rU   rV   rW   r   rG   r   r  r*  r+  rY   rA   rZ   rH   rK   rO   rS   r<   r<   r<   r=   r   ^  s   



r   c                   @   s   e Zd ZdZedddZedddZedddZedd	dZedd
dZ	edddZ
edddZdZedd Zedd Zdd ZdS )r   a  
    Two dimensional Sersic surface brightness profile.

    Parameters
    ----------
    amplitude : float
        Surface brightness at r_eff.
    r_eff : float
        Effective (half-light) radius
    n : float
        Sersic Index.
    x_0 : float, optional
        x position of the center.
    y_0 : float, optional
        y position of the center.
    ellip : float, optional
        Ellipticity.
    theta : float, optional
        Rotation angle in radians, counterclockwise from
        the positive x-axis.

    See Also
    --------
    Gaussian2D, Moffat2D

    Notes
    -----
    Model formula:

    .. math::

        I(x,y) = I(r) = I_e\exp\left\{-b_n\left[\left(\frac{r}{r_{e}}\right)^{(1/n)}-1\right]\right\}

    The constant :math:`b_n` is defined such that :math:`r_e` contains half the total
    luminosity, and can be solved for numerically.

    .. math::

        \Gamma(2n) = 2\gamma (2n,b_n)

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        from astropy.modeling.models import Sersic2D
        import matplotlib.pyplot as plt

        x,y = np.meshgrid(np.arange(100), np.arange(100))

        mod = Sersic2D(amplitude = 1, r_eff = 25, n=4, x_0=50, y_0=50,
                       ellip=.5, theta=-1)
        img = mod(x, y)
        log_img = np.log10(img)


        plt.figure()
        plt.imshow(log_img, origin='lower', interpolation='nearest',
                   vmin=-1, vmax=2)
        plt.xlabel('x')
        plt.ylabel('y')
        cbar = plt.colorbar()
        cbar.set_label('Log Brightness', rotation=270, labelpad=25)
        cbar.set_ticks([-1, 0, 1, 2], update_ticks=True)
        plt.show()

    References
    ----------
    .. [1] http://ned.ipac.caltech.edu/level5/March05/Graham/Graham2.html
    r   r   r0   r   r   r   r   zX position of the centerzY position of the centerZEllipticityz5Rotation angle in radians (counterclockwise-positive)Nc
                 C   s   | j du rddlm}
 |
| _ |  d| d}|d| |  }}t|	t|	 }}|| | || |  }||  | || |  }t|| d || d  }|t| |d|  d   S )z(Two dimensional Sersic profile function.Nr   r   r/   rq   r   r.   )r   r   r   rD   rs   rt   re   rE   )r   rF   ru   rG   r   r   r   r  ellipr`   r   Zbnrn   ro   Z	cos_thetaZ	sin_thetaZx_majZx_minr   r<   r<   r=   rH     s    
zSersic2D.evaluatec                 C   s0   | j jd u rd S | jd | j j| jd | jjiS r   r  r@   r<   r<   r=   rO     s
    zSersic2D.input_unitsc                 C   s^   || j d  || j d  kr$td|| j d  || j d  || j d  tj|| jd  dS )Nr   r   r   )r   r  r   r`   rG   r   rR   r<   r<   r=   rS      s    z(Sersic2D._parameter_units_for_data_units)rT   rU   rV   rW   r   rG   r   r   r   r  r1  r`   r   r   rH   rY   rO   rS   r<   r<   r<   r=   r     s   H

r   c                   @   s   e Zd ZdZededfddZededfddZededfddZe	d	d
 Z
edd Zedd Ze	dd Ze	dd Zdd ZdS )r+   a  
    Projected (surface density) analytic King Model.


    Parameters
    ----------
    amplitude : float
        Amplitude or scaling factor.
    r_core : float
        Core radius (f(r_c) ~ 0.5 f_0)
    r_tide : float
        Tidal radius.


    Notes
    -----

    This model approximates a King model with an analytic function. The derivation of this
    equation can be found in King '62 (equation 14). This is just an approximation of the
    full model and the parameters derived from this model should be taken with caution.
    It usually works for models with a concentration (c = log10(r_t/r_c) parameter < 2.

    Model formula:

    .. math::

        f(x) = A r_c^2  \left(\frac{1}{\sqrt{(x^2 + r_c^2)}} -
        \frac{1}{\sqrt{(r_t^2 + r_c^2)}}\right)^2

    Examples
    --------
    .. plot::
        :include-source:

        import numpy as np
        from astropy.modeling.models import KingProjectedAnalytic1D
        import matplotlib.pyplot as plt

        plt.figure()
        rt_list = [1, 2, 5, 10, 20]
        for rt in rt_list:
            r = np.linspace(0.1, rt, 100)

            mod = KingProjectedAnalytic1D(amplitude = 1, r_core = 1., r_tide = rt)
            sig = mod(r)


            plt.loglog(r, sig/sig[0], label='c ~ {:0.2f}'.format(mod.concentration))

        plt.xlabel("r")
        plt.ylabel(r"$\sigma/\sigma_0$")
        plt.legend()
        plt.show()

    References
    ----------
    .. [1] https://ui.adsabs.harvard.edu/abs/1962AJ.....67..471K
    r   NzAmplitude or scaling factorr3   zCore Radiusr.   zTidal Radiusc                 C   s   t t | j| j S )z)Concentration parameter of the king model)rD   Zlog10r   r_tider_corer@   r<   r<   r=   concentrationm  s    z%KingProjectedAnalytic1D.concentrationc                 C   sh   ||d  dt | d |d   dt |d |d    d  }| |k| dk B }|| d ||< |S )z/
        Analytic King model function.
        r.   r   r   r[   rD   re   )rF   rG   r3  r2  r	  r4   r<   r<   r=   rH   r  s    "z KingProjectedAnalytic1D.evaluatec              	   C   s  |d dt | d |d   dt |d |d    d  }d| |d  ||d |d  d  ||d | d  d    dt |d | d   dt |d |d     d| | dt |d | d   dt |d |d    d   }d| |d  | dt |d | d   dt |d |d     |d |d  d  }| |k| dk B }|| d ||< || d ||< || d ||< |||gS )z;
        Analytic King model function derivatives.
        r.   r   g      ?r~   r   r5  )rF   rG   r3  r2  rJ   Zd_r_coreZd_r_tider4   r<   r<   r=   rK     s8    $2"z!KingProjectedAnalytic1D.fit_derivc                 C   s   d| j  d| j  fS )z
        Tuple defining the default ``bounding_box`` limits.

        The model is not defined for r > r_tide.

        ``(r_low, r_high)``
        r   r   )r2  r@   r<   r<   r=   r>     s    
z$KingProjectedAnalytic1D.bounding_boxc                 C   s"   | j jd u rd S | jd | j jiS rL   )r3  rM   rN   r@   r<   r<   r=   rO     s    z#KingProjectedAnalytic1D.input_unitsc                 C   s*   || j d  || j d  || jd  dS )Nr   )r3  r2  rG   rP   rR   r<   r<   r=   rS     s    z7KingProjectedAnalytic1D._parameter_units_for_data_units)rT   rU   rV   rW   r   rX   rG   r3  r2  rY   r4  rZ   rH   rK   r>   rO   rS   r<   r<   r<   r=   r+   -  s   ;




r+   c                   @   sj   e Zd ZdZeddZeddZedd Zedd Z	e
dd	 Zejd
d Ze
dd Zdd ZdS )r-   z
    One dimensional logarithmic model.

    Parameters
    ----------
    amplitude : float, optional
    tau : float, optional

    See Also
    --------
    Exponential1D, Gaussian1D
    r   r1   c                 C   s   |t | |  S r   r,  rF   rG   taur<   r<   r=   rH     s    zLogarithmic1D.evaluatec                 C   s*   t | | }t | j||  }||gS r   )rD   r   r   rc   rF   rG   r8  rJ   Zd_taur<   r<   r=   rK     s    zLogarithmic1D.fit_derivc                 C   s   | j }| j}t||dS N)rG   r8  )r8  rG   r,   r9   Znew_amplitudeZnew_taur<   r<   r=   r     s    zLogarithmic1D.inversec                 C   s   t |dkrtdd S )Nr   !0 is not an allowed value for taurD   allrd   r9   valr<   r<   r=   r8    s    zLogarithmic1D.tauc                 C   s"   | j jd u rd S | jd | j jiS rL   r8  rM   rN   r@   r<   r<   r=   rO     s    zLogarithmic1D.input_unitsc                 C   s   || j d  || jd  dS Nr   )r8  rG   rP   rR   r<   r<   r=   rS     s    z-Logarithmic1D._parameter_units_for_data_unitsNrT   rU   rV   rW   r   rG   r8  rZ   rH   rK   rY   r   Z	validatorrO   rS   r<   r<   r<   r=   r-     s   






r-   c                   @   sj   e Zd ZdZeddZeddZedd Zedd Z	e
dd	 Zejd
d Ze
dd Zdd ZdS )r,   z
    One dimensional exponential model.

    Parameters
    ----------
    amplitude : float, optional
    tau : float, optional

    See Also
    --------
    Logarithmic1D, Gaussian1D
    r   r6  c                 C   s   |t | |  S r   rC   r7  r<   r<   r=   rH     s    zExponential1D.evaluatec                 C   s6   t | | }| | |d   t | |  }||gS )z& Derivative with respect to parametersr.   rC   r9  r<   r<   r=   rK     s     zExponential1D.fit_derivc                 C   s   | j }| j}t||dS r:  )r8  rG   r-   r;  r<   r<   r=   r     s    zExponential1D.inversec                 C   s   t |dkrtddS )z tau cannot be 0r   r<  Nr=  r?  r<   r<   r=   r8    s    zExponential1D.tauc                 C   s"   | j jd u rd S | jd | j jiS rL   rA  r@   r<   r<   r=   rO     s    zExponential1D.input_unitsc                 C   s   || j d  || jd  dS rB  rP   rR   r<   r<   r=   rS     s    z-Exponential1D._parameter_units_for_data_unitsNrC  r<   r<   r<   r=   r,     s   






r,   )ArW   ZnumpyrD   Zastropyr   r   Zastropy.unitsr   r   corer   r   
parametersr   r	   Zutilsr
   __all__r   r   r   ZfinfoZfloat32ZtinyrX   re   r   r?   r   r   r    r   r   r   r   r   r!   r"   r#   r   r$   r%   r&   r   r   r   r*   r   r   r   r   r)   r   r   r'   r(   r   r   r   r   r   r   r+   r-   r,   r<   r<   r<   r=   <module>   sj   

 ,  :B/2[MMc]]O90h 8L0 Ov^W`L]Db^Xw 1