def _legendre(x_val, num_nodes) -> float:
    """
    Calculates legendre function using recurrent formula for homogeneous polynomials
    """
    if num_nodes == 0:
        return 1
    if num_nodes == 1:
        return x_val

    return (2 * num_nodes - 1) / num_nodes * _legendre(x_val, num_nodes - 1) * x_val - (
        num_nodes - 1
    ) / num_nodes * _legendre(x_val, num_nodes - 2)