BPOLY
Overview
The BPOLY
function constructs a piecewise polynomial in the Bernstein basis, allowing for smooth interpolation between specified breakpoints using Bernstein polynomials. This is useful in numerical analysis, computer graphics, and engineering for representing curves and surfaces with desirable properties such as smoothness and stability. The polynomial between each pair of breakpoints and is expressed as:
where is the degree of the polynomial, are the Bernstein coefficients for interval , and is the Bernstein basis polynomial:
For more details, see the SciPy BPoly documentation and Wikipedia: Bernstein polynomial .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=BPOLY(c, x, x_eval, [extrapolate])
c
(2D list, required): Bernstein coefficients. 2D list of shape (degree+1, number of intervals). Each row is a degree, each column is an interval.x
(2D list, required): Breakpoints. 2D column vector of shape (number of intervals + 1, 1), must be sorted in increasing or decreasing order.x_eval
(2D list, required): Points at which to evaluate the polynomial. 2D column vector of shape (n, 1).extrapolate
(bool or str, optional, default=True): Whether to extrapolate outside the breakpoints. If ‘periodic’, uses periodic extrapolation.
The function returns a 2D list of evaluated values (float), or an error message (string) if the input is invalid.
Examples
Example 1: Quadratic Bernstein Polynomial
This example constructs a quadratic polynomial on the interval [0, 1, 2, 3] with coefficients 1, 2, 3, and evaluates it at the breakpoints.
Inputs:
c | x | x_eval | extrapolate | ||
---|---|---|---|---|---|
1 | 2 | 3 | 0 | 0 | True |
1 | 1 | ||||
2 | 2 | ||||
3 | 3 |
Excel formula:
=BPOLY({1,2,3}, {0;1;2;3}, {0;1;2;3})
Expected output:
Result |
---|
1 |
2 |
3 |
3 |
Example 2: Cubic Bernstein Polynomial with Two Intervals
Inputs:
c | x | x_eval | extrapolate | |||
---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 0 | 0 | True |
1 | 2 | 3 | 4 | 0.25 | 0.25 | |
0.5 | 0.5 | |||||
0.75 | 0.75 | |||||
1 | 1 |
Excel formula:
=BPOLY({0,1,2,3;1,2,3,4}, {0;0.25;0.5;0.75;1}, {0;0.25;0.5;0.75;1})
Expected output:
Result |
---|
0 |
1 |
2 |
3 |
4 |
Example 3: No Extrapolation
Inputs:
c | x | x_eval | extrapolate | ||
---|---|---|---|---|---|
1 | 2 | 3 | 0 | 0 | False |
1 | 1 | ||||
2 | 2 | ||||
3 | 3 |
Excel formula:
=BPOLY({1,2,3}, {0;1;2;3}, {0;1;2;3}, FALSE)
Expected output:
Result |
---|
1 |
2 |
3 |
3 |
Example 4: Periodic Extrapolation
Inputs:
c | x | x_eval | extrapolate | ||
---|---|---|---|---|---|
1 | 2 | 3 | 0 | 0 | ”periodic” |
1 | 1 | ||||
2 | 2 | ||||
3 | 3 |
Excel formula:
=BPOLY({1,2,3}, {0;1;2;3}, {0;1;2;3}, "periodic")
Expected output:
Result |
---|
1 |
2 |
3 |
1 |
Python Code
from scipy.interpolate import BPoly as scipy_bpoly
def bpoly(c, x, x_eval, extrapolate=True):
"""
Construct a piecewise polynomial in the Bernstein basis and evaluate it at specified points.
Args:
c: 2D list of Bernstein coefficients, shape (degree+1, number of intervals).
x: 2D list of breakpoints, shape (number of intervals+1, 1).
x_eval: 2D list of points at which to evaluate the polynomial, shape (n, 1).
extrapolate: Bool or str, optional. Whether to extrapolate outside the breakpoints. If 'periodic', uses periodic extrapolation. Default is True.
Returns:
2D list of evaluated values (float), or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate c
if not isinstance(c, list) or not all(isinstance(row, list) for row in c):
return "Invalid input: c must be a 2D list."
if len(c) == 0 or len(c[0]) == 0:
return "Invalid input: c must not be empty."
n_intervals = len(c[0])
for row in c:
if len(row) != n_intervals:
return "Invalid input: All rows in c must have the same length."
# Validate x
if not isinstance(x, list) or not all(isinstance(row, list) and len(row) == 1 for row in x):
return "Invalid input: x must be a 2D column vector (list of lists, each with one value)."
if len(x) != n_intervals + 1:
return "Invalid input: x must have (number of intervals + 1) rows."
# Validate x_eval
if not isinstance(x_eval, list) or not all(isinstance(row, list) and len(row) == 1 for row in x_eval):
return "Invalid input: x_eval must be a 2D column vector (list of lists, each with one value)."
# Validate extrapolate
if not (isinstance(extrapolate, bool) or (isinstance(extrapolate, str) and extrapolate == "periodic")):
return "Invalid input: extrapolate must be a bool or 'periodic'."
try:
import numpy as np
c_arr = np.array(c)
x_arr = np.array(x).flatten()
x_eval_arr = np.array(x_eval).flatten()
poly = scipy_bpoly(c_arr, x_arr, extrapolate=extrapolate)
y = poly(x_eval_arr)
y_2d = [[float(val)] for val in y]
except Exception as e:
return f"scipy.interpolate.BPoly error: {e}"
return y_2d
Live Notebook
Edit this function in a live notebook .