import math


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

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

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

    one_minus_beta_half = (1.0 - beta) / 2.0
    three_plus_beta_half = (3.0 + beta) / 2.0
    ln_moneyness = math.log(underlying / strike)

    factor1 = (strike ** one_minus_beta_half) * nu / alpha
    factor2 = underlying ** (-three_plus_beta_half)

    term1 = (beta ** 2 - 1.0) * ln_moneyness / 4.0
    return factor1 * factor2 * (term1 - beta)