import math


def _sabr_implied_vol_hagan_A12(
        underlying, strike, maturity, alpha, beta, rho, nu):
    """_sabr_implied_vol_hagan_A12
    See :py:func:`_sabr_implied_vol_hagan`.

    .. math::
        A_{12}
            & := &
            \left(
                1
                + \\frac{(1 - \\beta)^{2}}{24}
                    \log^{2}\\frac{S}{K}
                + \\frac{(1 - \\beta)^{4}}{1920}
                    \log^{4}\\frac{S}{K}
            \\right)

    :param float underlying:
    :param float strike:
    :param float maturity:
    :param float alpha: must be within :math:`[0, 1]`.
    :param float beta: must be greater than 0.
    :param float rho: must be within :math:`[-1, 1]`.
    :param float nu: volatility of volatility. This must be positive.

    :return: value of factor.
    :rtype: float.
    """
    assert(underlying / strike > 0.0)

    one_minus_beta = 1.0 - beta
    one_minus_beta2 = one_minus_beta ** 2
    one_minus_beta4 = one_minus_beta ** 4
    ln_moneyness = math.log(underlying / strike)
    ln_moneyness2 = ln_moneyness ** 2
    ln_moneyness4 = ln_moneyness ** 4

    term1 = one_minus_beta2 * ln_moneyness2 / 24.0
    term2 = one_minus_beta4 * ln_moneyness4 / 1920.0
    return 1.0 + term1 + term2