Skip to Content

PPOLY

Overview

The PPOLY function constructs a piecewise polynomial in the power basis, defined by a set of polynomial coefficients and breakpoints. This is useful for representing and evaluating polynomials that are defined piecewise over different intervals, such as in interpolation, curve fitting, and numerical analysis. The polynomial between each pair of breakpoints x[i]x[i] and x[i+1]x[i+1] is given by:

S(x)=m=0kc[m,i](xx[i])kmS(x) = \sum_{m=0}^{k} c[m, i] \cdot (x - x[i])^{k-m}

where kk is the degree of the polynomial, c[m,i]c[m, i] are the coefficients for the ii-th interval, and x[i]x[i] are the breakpoints. For more details, see the SciPy PPoly documentation.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=PPOLY(c, x, [eval_points], [extrapolate])
  • c (2D list, required): Coefficient matrix of shape (degree+1, n_intervals). Each column contains the coefficients for one interval, ordered from highest to lowest degree. Must be a 2D list. 1D lists are not supported.
  • x (list, required): List of breakpoints of length n_intervals+1. Must be a 2D list (column vector). 1D lists are not supported.
  • eval_points (2D list, optional, default=x): Points at which to evaluate the piecewise polynomial. If not provided, the breakpoints are used. Must be a 2D list. 1D lists are not supported.
  • extrapolate (bool, optional, default=True): Whether to extrapolate outside the breakpoints.

The function returns a 2D list of evaluated values at the specified points, or an error message (string) if the input is invalid.

Examples

Example 1: Quadratic Piecewise Polynomial

This example constructs a quadratic piecewise polynomial with two intervals and evaluates it at the breakpoints.

Inputs:

cxeval_pointsextrapolate
1000True
0110.5
-1121

Excel formula:

=PPOLY({1,0;0,1;-1,1}, {0;1;2}, {0;0.5;1}, TRUE)

Expected output:

Result
-1
-0.75
1

This means the polynomial evaluates to -1 at the left endpoint, -0.75 at 0.5, and 1 at the right endpoint.

Example 2: Linear Piecewise Polynomial

This example constructs a linear piecewise polynomial and evaluates it at several points.

Inputs:

cxeval_pointsextrapolate
2100False
0110.5
21

Excel formula:

=PPOLY({2,1;0,1}, {0;1;2}, {0;0.5;1}, FALSE)

Expected output:

Result
0
1
1

Example 3: Default Evaluation at Breakpoints

This example omits the eval_points argument, so the polynomial is evaluated at the breakpoints.

Inputs:

cxextrapolate
120True
011
2

Excel formula:

=PPOLY({1,2;0,1}, {0;1;2}, , TRUE)

Expected output:

Result
0
1
3

Python Code

from scipy.interpolate import PPoly as scipy_ppoly import numpy as np def ppoly(c, x, eval_points=None, extrapolate=True): """ Construct and evaluate a piecewise polynomial in the power basis. Args: c: 2D list of coefficients (degree+1, n_intervals). Must be a 2D list. x: 2D list of breakpoints (n_intervals+1, 1). Must be a 2D list (column vector). eval_points: 2D list of points to evaluate at (default: x). Must be a 2D list. extrapolate: Whether to extrapolate outside breakpoints (default: True). Returns: 2D list of evaluated values, or 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." k = len(c) - 1 n_intervals = len(c[0]) if any(len(row) != n_intervals for row in c): 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) for row in x): return "Invalid input: x must be a 2D list (column vector)." if len(x) != n_intervals + 1: return "Invalid input: x must be a 2D list of length n_intervals+1." # Validate eval_points if eval_points is None: eval_points = x if not (isinstance(eval_points, list) and all(isinstance(row, list) for row in eval_points)): return "Invalid input: eval_points must be a 2D list." eval_points_flat = [item for sublist in eval_points for item in sublist] try: c_np = np.array(c, dtype=float) x_np = np.array([row[0] for row in x], dtype=float) eval_np = np.array(eval_points_flat, dtype=float) pp = scipy_ppoly(c_np, x_np, extrapolate=extrapolate) result = pp(eval_np) except Exception as e: return f"scipy.PPoly error: {e}" # Handle extrapolation if not extrapolate: mask = (eval_np < x_np[0]) | (eval_np > x_np[-1]) result = np.where(mask, np.nan, result) # Format output as 2D list out = [] idx = 0 for row in eval_points: out_row = [] for _ in row: val = result[idx] if np.isnan(val): out_row.append("") else: out_row.append(round(float(val), 6)) idx += 1 out.append(out_row) return out

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on