def bernstein(n, k, t): """k:sup:`th` of `n` + 1 Bernstein basis polynomials of degree `n`. A weighted linear combination of these basis polynomials is called a Bernstein polynomial [wikipedia2017k]_. Notes ----- When constructing Bezier curves, the weights are simply the coordinates of the control points of the curve. Parameters ---------- n : int The degree of the polynomial. k : int The number of the basis polynomial. t : float The variable. Returns ------- float The value of the Bernstein basis polynomial at `t`. """ if k < 0: return 0 if k > n: return 0 return binomial_coefficient(n, k) * t**k * (1 - t)**(n - k)
def test_binomial(n, k, result): assert binomial_coefficient(n, k) == pytest.approx(result)
def test_binomial_raises_when_input_is_invalid(n, k): with pytest.raises(ValueError): binomial_coefficient(n, k)