def _monotone_end_condition(inner_slope, chord_slope):
    """

    Return the "outer" (i.e. first or last) slope given the "inner"
    (i.e. second or penultimate) slope and the slope of the
    corresponding chord.

    """
    # NB: This is a very ad-hoc algorithm meant to minimize the change in slope
    # within the first/last curve segment.  Especially, this should avoid a
    # change from negative to positive acceleration (and vice versa).
    # There might be a better method available!?!
    if chord_slope < 0:
        return -_monotone_end_condition(-inner_slope, -chord_slope)
    assert 0 <= inner_slope <= 3 * chord_slope
    if inner_slope <= chord_slope:
        return 3 * chord_slope - 2 * inner_slope
    else:
        return (3 * chord_slope - inner_slope) / 2