import math


def _fitfunc1(a, color, FeH, err):
    """Fitting function of *θ* = 5040/|Teff| vs color index and [Fe/H].

    This function calculates *θ* as a function of (color, [Fe/H]) using the
    following relation:

    .. math::
        \\theta = a_0 + a_1X + a_2X^2 + a_3X\mathrm{[Fe/H]} + a_4\mathrm{[Fe/H]}
        + a_5\mathrm{[Fe/H]}^2
    
    where *X* denotes the value of color index.
    The partial deviatives of *θ* with respect to *X* and [Fe/H] are:

    .. math::
        \\frac{\partial\,\\theta}{\partial\,X} = a_1 + 2a_2X
            + a_3\mathrm{[Fe/H]} \\\\
        \\frac{\partial\,\\theta}{\partial\,\mathrm{[Fe/H]}} = a_3X + a_4
            + 2a_5\mathrm{[Fe/H]}
    
    The uncertainty of *θ* are calculated as the quadratic sum of uncertainties
    caused by Δ\ *X* and Δ[Fe/H], and the standard deviation of fitted
    relation:

    .. math::
        \Delta\\theta = \sqrt{
            \left(\\frac{\partial\,\\theta}{\partial\,X}\Delta X\\right)^2 +
            \left(\\frac{\partial\,\\theta}{\partial\,\mathrm{[Fe/H]}}
            \Delta\mathrm{[Fe/H]}\\right)^2 +
            \sigma^2(\\theta)
        }

    Args:
        a (list or tuple): Coefficients.
        color (tuple): Value of color index and its uncertainty.
        FeH (tuple): Metallicity [Fe/H] and its uncertainty.
        err (float): Standard deviation of *θ*.
    Returns:
        tuple: A tuple containing:

            * *float*: *θ* = 5040/|Teff|
            * *float*: Δ\ *θ*

    """
    color, color_err = color
    FeH,   FeH_err   = FeH

    theta = a[0] + a[1]*color + a[2]*color**2 + a[3]*color*FeH + a[4]*FeH + \
            a[5]*FeH**2
    dc = a[1] + 2*a[2]*color + a[3]*FeH
    dm = a[3]*color + a[4] + 2*a[5]*FeH
    dtheta = math.sqrt(err**2 + (dc*color_err)**2 + (dm*FeH_err)**2)
    return theta, dtheta