
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - interpolate.BPoly() Function
scipy.interpolate.BPoly() is a function in SciPy used to create and evaluate piecewise polynomial functions known as Bernstein polynomials. It allows users to define a polynomial piecewise function based on given coefficients and knots.
This function is particularly useful for interpolation and curve fitting by providing a smooth and continuous representation of data. The BPoly function supports various operations such as evaluating the polynomial at specific points by calculating derivatives and integrating over specified intervals. It is especially valuable in applications requiring high degrees of flexibility and control over polynomial behavior by maintaining numerical stability across intervals.
Syntax
Following is the syntax of the function scipy.interpolate.BPoly() to represent piecewise polynomials −
BPoly(c, x, extrapolate=None, axis=0)
Parameters
Below are the parameters of the scipy.interpolate.BPoly() function −
- c(array-like, shape(k,m)): The coefficients of the polynomial pieces. Each row corresponds to a polynomial, and each column corresponds to a coefficient for decreasing powers of x.
- x(array-like, shape(k,)):The breakpoints or knots where the polynomial pieces are defined. The array must be strictly increasing.
- axis(int, optional): The axis along which the polynomial pieces are defined. This is useful when dealing with multidimensional data.
- extrapolate(bool, optional): Determines whether to allow extrapolation outside the breakpoints. If True, the polynomial will be extended linearly beyond the specified knots. If False, it will return NaN for values outside the range. If None, the default behavior is used based on the value of x.
Return Value
The scipy.interpolate.BPoly() function returns an instance of the BPoly class which represents the piecewise polynomial defined by the coefficients and the breakpoints.
Piecewise Polynomial with BPoly
Following is the example of scipy.interpolate.BPoly() function which is used to create and evaluate the piecewise polynomial. In this example we define a simple piecewise polynomial and evaluate it at several points. In this example we will define a piecewise polynomial with two segments, evaluate it over a range of x-values (including points outside the breakpoints) and visualize the result −
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import BPoly # Define breakpoints (knots) x = np.array([0, 1, 2]) # Breakpoints # Define coefficients for the polynomial pieces # Each row corresponds to a polynomial, ordered by decreasing power of x c = np.array([[1, 0], # Polynomial for segment 1: P1(x) = 1 (constant) [0, 1]]) # Polynomial for segment 2: P2(x) = x - 1 (linear) # Create the piecewise polynomial bp = BPoly(c, x, extrapolate=True) # Allow extrapolation # Generate new x values for evaluation, including points outside the breakpoints x_new = np.linspace(-1, 3, 100) # Range includes -1 to 3 y_new = bp(x_new) # Evaluate the piecewise polynomial # Plot the piecewise polynomial plt.plot(x_new, y_new, label='Piecewise Polynomial', color='blue') plt.scatter(x, [1, 0, 1], color='red', label='Breakpoints', zorder=5) plt.xlabel('x') plt.ylabel('y') plt.title('Piecewise Polynomial using BPoly') plt.legend() plt.grid() plt.axhline(0, color='black', lw=0.5, ls='--') # Horizontal line at y=0 plt.axvline(0, color='black', lw=0.5, ls='--') # Vertical line at x=0 plt.show()
Here is the output of the scipy.interpolate.BPoly() function basic example −

Quadratic and Linear Segments
Quadratic and linear segments can be combined to create a piecewise polynomial function. Each segment can have different properties by allowing for flexibility in modeling various shapes or behaviors. Here's how we can define and evaluate a piecewise polynomial consisting of both quadratic and linear segments −
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import BPoly # Define breakpoints breakpoints = np.array([0, 1, 2]) # Breakpoints for the polynomial pieces # Define coefficients for the polynomial pieces # Ensure each coefficient set corresponds to one segment and has consistent lengths coefficients = np.array([[1, -1, 0], # Coefficients for quadratic segment (1*x^2 - 1*x + 0) [0, -2, 4]]) # Coefficients for linear segment adjusted to fit a quadratic format # Create the piecewise polynomial bp = BPoly(c=coefficients.T, x=breakpoints) # Generate new x values for evaluation x_new = np.linspace(-1, 3, 100) # Range includes points outside the breakpoints y_new = bp(x_new) # Plot the piecewise polynomial plt.plot(x_new, y_new, label='Piecewise Polynomial', color='blue') # Calculate and display breakpoints values for plotting breakpoint_values = [bp(val) for val in breakpoints] plt.scatter(breakpoints, breakpoint_values, color='red', label='Breakpoints', zorder=5) # Show breakpoints plt.xlabel('x') plt.ylabel('y') plt.title('Piecewise Polynomial: Quadratic and Linear Segments Using BPoly') plt.legend() plt.grid() plt.axhline(0, color='black', lw=0.5, ls='--') # Horizontal line at y=0 plt.axvline(0, color='black', lw=0.5, ls='--') # Vertical line at x=0 plt.show()
Here is the output of the scipy.interpolate.BPoly() function basic example −
