CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
Polynomial operations including fitting, evaluation, arithmetic, and root finding. CuPy provides GPU-accelerated polynomial functionality compatible with NumPy's polynomial interface.
Core polynomial operations for creating, evaluating, and manipulating polynomials on GPU.
def poly(seq_of_zeros):
"""
Find the coefficients of a polynomial with the given sequence of roots.
Parameters:
- seq_of_zeros: cupy.ndarray, sequence of polynomial roots
Returns:
cupy.ndarray: polynomial coefficients in descending powers
"""
def polyval(p, x):
"""
Evaluate a polynomial at specific values.
Parameters:
- p: cupy.ndarray, polynomial coefficients in descending powers
- x: cupy.ndarray, values at which to evaluate the polynomial
Returns:
cupy.ndarray: polynomial values at x
"""
def roots(p):
"""
Return the roots of a polynomial with coefficients given in p.
Parameters:
- p: cupy.ndarray, polynomial coefficients in descending powers
Returns:
cupy.ndarray: roots of the polynomial
"""
def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):
"""
Least squares polynomial fit to data.
Parameters:
- x: cupy.ndarray, x-coordinates of data points
- y: cupy.ndarray, y-coordinates of data points
- deg: int, degree of fitting polynomial
- rcond: float, relative condition number for rank determination
- full: bool, return additional information about the fit
- w: cupy.ndarray, weights to apply to y-coordinates
- cov: bool, return covariance matrix
Returns:
cupy.ndarray or tuple: polynomial coefficients, optionally with fit info
"""Arithmetic operations on polynomials including addition, subtraction, multiplication, and division.
def polyadd(a1, a2):
"""
Find the sum of two polynomials.
Parameters:
- a1: cupy.ndarray, coefficients of first polynomial
- a2: cupy.ndarray, coefficients of second polynomial
Returns:
cupy.ndarray: coefficients of sum polynomial
"""
def polysub(a1, a2):
"""
Difference (subtraction) of two polynomials.
Parameters:
- a1: cupy.ndarray, coefficients of first polynomial
- a2: cupy.ndarray, coefficients of second polynomial
Returns:
cupy.ndarray: coefficients of difference polynomial
"""
def polymul(a1, a2):
"""
Find the product of two polynomials.
Parameters:
- a1: cupy.ndarray, coefficients of first polynomial
- a2: cupy.ndarray, coefficients of second polynomial
Returns:
cupy.ndarray: coefficients of product polynomial
"""
def polydiv(u, v):
"""
Returns the quotient and remainder of polynomial division.
Parameters:
- u: cupy.ndarray, dividend polynomial coefficients
- v: cupy.ndarray, divisor polynomial coefficients
Returns:
tuple: (quotient coefficients, remainder coefficients)
"""Differentiation and integration operations on polynomials.
def polyder(p, m=1):
"""
Return the derivative of the specified order of a polynomial.
Parameters:
- p: cupy.ndarray, polynomial coefficients
- m: int, order of differentiation
Returns:
cupy.ndarray: coefficients of derivative polynomial
"""
def polyint(p, m=1, k=None):
"""
Return an antiderivative (indefinite integral) of a polynomial.
Parameters:
- p: cupy.ndarray, polynomial coefficients
- m: int, order of integration
- k: cupy.ndarray, integration constants
Returns:
cupy.ndarray: coefficients of antiderivative polynomial
"""Object-oriented interface for polynomial operations with convenient methods.
class poly1d:
"""
A one-dimensional polynomial class for GPU arrays.
Parameters:
- c_or_r: cupy.ndarray, polynomial coefficients or roots
- r: bool, if True, c_or_r specifies roots instead of coefficients
- variable: str, variable name for string representation
"""
def __init__(self, c_or_r, r=False, variable=None): ...
def __call__(self, val):
"""Evaluate polynomial at given values."""
def __add__(self, other):
"""Add two polynomials."""
def __sub__(self, other):
"""Subtract two polynomials."""
def __mul__(self, other):
"""Multiply two polynomials."""
def __div__(self, other):
"""Divide two polynomials."""
def __pow__(self, val):
"""Raise polynomial to a power."""
def deriv(self, m=1):
"""Return derivative of polynomial."""
def integ(self, m=1, k=0):
"""Return antiderivative of polynomial."""
@property
def coeffs(self):
"""Polynomial coefficients."""
@property
def order(self):
"""Order/degree of polynomial."""
@property
def roots(self):
"""Roots of polynomial."""Access to CuPy's extended polynomial module for advanced operations.
# Available through cupy.polynomial submodule
class Polynomial:
"""Modern polynomial class with enhanced functionality."""
class Chebyshev:
"""Chebyshev polynomial class."""
class Legendre:
"""Legendre polynomial class."""
class Laguerre:
"""Laguerre polynomial class."""
class Hermite:
"""Hermite polynomial class."""
class HermiteE:
"""Hermite polynomial class (physicist's version)."""import cupy as cp
# Basic polynomial evaluation
coeffs = cp.array([1, -2, 1]) # x^2 - 2x + 1
x = cp.linspace(-2, 2, 100)
y = cp.polyval(coeffs, x)
# Polynomial fitting
x_data = cp.linspace(0, 10, 50)
y_data = 2 * x_data**2 + 3 * x_data + 1 + cp.random.normal(0, 0.5, 50)
fit_coeffs = cp.polyfit(x_data, y_data, deg=2)
# Find roots
p = cp.array([1, -5, 6]) # x^2 - 5x + 6
polynomial_roots = cp.roots(p) # Should be [2, 3]
# Polynomial arithmetic
p1 = cp.array([1, 2, 3]) # x^2 + 2x + 3
p2 = cp.array([1, 1]) # x + 1
sum_poly = cp.polyadd(p1, p2)
product_poly = cp.polymul(p1, p2)
# Using poly1d class
p = cp.poly1d([1, -2, 1]) # x^2 - 2x + 1
print(p(2)) # Evaluate at x=2
derivative = p.deriv() # Get derivative
integral = p.integ() # Get antiderivative
# Create polynomial from roots
roots_array = cp.array([1, 2, 3])
poly_from_roots = cp.poly(roots_array)
# Polynomial calculus
original = cp.array([1, 2, 3, 4]) # x^3 + 2x^2 + 3x + 4
derivative = cp.polyder(original, m=1) # First derivative
second_derivative = cp.polyder(original, m=2) # Second derivative
antiderivative = cp.polyint(derivative) # Should recover original (up to constant)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3