or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-creation.mdcuda-integration.mdcustom-kernels.mddata-types.mdextended-functionality.mdfft.mdindex.mdio-functions.mdlinear-algebra.mdlogic-functions.mdmathematical-functions.mdpolynomial.mdrandom.mdstatistics.mdutilities.md

polynomial.mddocs/

0

# Polynomial Functions

1

2

Polynomial operations including fitting, evaluation, arithmetic, and root finding. CuPy provides GPU-accelerated polynomial functionality compatible with NumPy's polynomial interface.

3

4

## Capabilities

5

6

### Basic Polynomial Operations

7

8

Core polynomial operations for creating, evaluating, and manipulating polynomials on GPU.

9

10

```python { .api }

11

def poly(seq_of_zeros):

12

"""

13

Find the coefficients of a polynomial with the given sequence of roots.

14

15

Parameters:

16

- seq_of_zeros: cupy.ndarray, sequence of polynomial roots

17

18

Returns:

19

cupy.ndarray: polynomial coefficients in descending powers

20

"""

21

22

def polyval(p, x):

23

"""

24

Evaluate a polynomial at specific values.

25

26

Parameters:

27

- p: cupy.ndarray, polynomial coefficients in descending powers

28

- x: cupy.ndarray, values at which to evaluate the polynomial

29

30

Returns:

31

cupy.ndarray: polynomial values at x

32

"""

33

34

def roots(p):

35

"""

36

Return the roots of a polynomial with coefficients given in p.

37

38

Parameters:

39

- p: cupy.ndarray, polynomial coefficients in descending powers

40

41

Returns:

42

cupy.ndarray: roots of the polynomial

43

"""

44

45

def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False):

46

"""

47

Least squares polynomial fit to data.

48

49

Parameters:

50

- x: cupy.ndarray, x-coordinates of data points

51

- y: cupy.ndarray, y-coordinates of data points

52

- deg: int, degree of fitting polynomial

53

- rcond: float, relative condition number for rank determination

54

- full: bool, return additional information about the fit

55

- w: cupy.ndarray, weights to apply to y-coordinates

56

- cov: bool, return covariance matrix

57

58

Returns:

59

cupy.ndarray or tuple: polynomial coefficients, optionally with fit info

60

"""

61

```

62

63

### Polynomial Arithmetic

64

65

Arithmetic operations on polynomials including addition, subtraction, multiplication, and division.

66

67

```python { .api }

68

def polyadd(a1, a2):

69

"""

70

Find the sum of two polynomials.

71

72

Parameters:

73

- a1: cupy.ndarray, coefficients of first polynomial

74

- a2: cupy.ndarray, coefficients of second polynomial

75

76

Returns:

77

cupy.ndarray: coefficients of sum polynomial

78

"""

79

80

def polysub(a1, a2):

81

"""

82

Difference (subtraction) of two polynomials.

83

84

Parameters:

85

- a1: cupy.ndarray, coefficients of first polynomial

86

- a2: cupy.ndarray, coefficients of second polynomial

87

88

Returns:

89

cupy.ndarray: coefficients of difference polynomial

90

"""

91

92

def polymul(a1, a2):

93

"""

94

Find the product of two polynomials.

95

96

Parameters:

97

- a1: cupy.ndarray, coefficients of first polynomial

98

- a2: cupy.ndarray, coefficients of second polynomial

99

100

Returns:

101

cupy.ndarray: coefficients of product polynomial

102

"""

103

104

def polydiv(u, v):

105

"""

106

Returns the quotient and remainder of polynomial division.

107

108

Parameters:

109

- u: cupy.ndarray, dividend polynomial coefficients

110

- v: cupy.ndarray, divisor polynomial coefficients

111

112

Returns:

113

tuple: (quotient coefficients, remainder coefficients)

114

"""

115

```

116

117

### Polynomial Calculus

118

119

Differentiation and integration operations on polynomials.

120

121

```python { .api }

122

def polyder(p, m=1):

123

"""

124

Return the derivative of the specified order of a polynomial.

125

126

Parameters:

127

- p: cupy.ndarray, polynomial coefficients

128

- m: int, order of differentiation

129

130

Returns:

131

cupy.ndarray: coefficients of derivative polynomial

132

"""

133

134

def polyint(p, m=1, k=None):

135

"""

136

Return an antiderivative (indefinite integral) of a polynomial.

137

138

Parameters:

139

- p: cupy.ndarray, polynomial coefficients

140

- m: int, order of integration

141

- k: cupy.ndarray, integration constants

142

143

Returns:

144

cupy.ndarray: coefficients of antiderivative polynomial

145

"""

146

```

147

148

### Polynomial Class

149

150

Object-oriented interface for polynomial operations with convenient methods.

151

152

```python { .api }

153

class poly1d:

154

"""

155

A one-dimensional polynomial class for GPU arrays.

156

157

Parameters:

158

- c_or_r: cupy.ndarray, polynomial coefficients or roots

159

- r: bool, if True, c_or_r specifies roots instead of coefficients

160

- variable: str, variable name for string representation

161

"""

162

163

def __init__(self, c_or_r, r=False, variable=None): ...

164

165

def __call__(self, val):

166

"""Evaluate polynomial at given values."""

167

168

def __add__(self, other):

169

"""Add two polynomials."""

170

171

def __sub__(self, other):

172

"""Subtract two polynomials."""

173

174

def __mul__(self, other):

175

"""Multiply two polynomials."""

176

177

def __div__(self, other):

178

"""Divide two polynomials."""

179

180

def __pow__(self, val):

181

"""Raise polynomial to a power."""

182

183

def deriv(self, m=1):

184

"""Return derivative of polynomial."""

185

186

def integ(self, m=1, k=0):

187

"""Return antiderivative of polynomial."""

188

189

@property

190

def coeffs(self):

191

"""Polynomial coefficients."""

192

193

@property

194

def order(self):

195

"""Order/degree of polynomial."""

196

197

@property

198

def roots(self):

199

"""Roots of polynomial."""

200

```

201

202

### Advanced Polynomial Support

203

204

Access to CuPy's extended polynomial module for advanced operations.

205

206

```python { .api }

207

# Available through cupy.polynomial submodule

208

class Polynomial:

209

"""Modern polynomial class with enhanced functionality."""

210

211

class Chebyshev:

212

"""Chebyshev polynomial class."""

213

214

class Legendre:

215

"""Legendre polynomial class."""

216

217

class Laguerre:

218

"""Laguerre polynomial class."""

219

220

class Hermite:

221

"""Hermite polynomial class."""

222

223

class HermiteE:

224

"""Hermite polynomial class (physicist's version)."""

225

```

226

227

### Usage Examples

228

229

```python

230

import cupy as cp

231

232

# Basic polynomial evaluation

233

coeffs = cp.array([1, -2, 1]) # x^2 - 2x + 1

234

x = cp.linspace(-2, 2, 100)

235

y = cp.polyval(coeffs, x)

236

237

# Polynomial fitting

238

x_data = cp.linspace(0, 10, 50)

239

y_data = 2 * x_data**2 + 3 * x_data + 1 + cp.random.normal(0, 0.5, 50)

240

fit_coeffs = cp.polyfit(x_data, y_data, deg=2)

241

242

# Find roots

243

p = cp.array([1, -5, 6]) # x^2 - 5x + 6

244

polynomial_roots = cp.roots(p) # Should be [2, 3]

245

246

# Polynomial arithmetic

247

p1 = cp.array([1, 2, 3]) # x^2 + 2x + 3

248

p2 = cp.array([1, 1]) # x + 1

249

sum_poly = cp.polyadd(p1, p2)

250

product_poly = cp.polymul(p1, p2)

251

252

# Using poly1d class

253

p = cp.poly1d([1, -2, 1]) # x^2 - 2x + 1

254

print(p(2)) # Evaluate at x=2

255

derivative = p.deriv() # Get derivative

256

integral = p.integ() # Get antiderivative

257

258

# Create polynomial from roots

259

roots_array = cp.array([1, 2, 3])

260

poly_from_roots = cp.poly(roots_array)

261

262

# Polynomial calculus

263

original = cp.array([1, 2, 3, 4]) # x^3 + 2x^2 + 3x + 4

264

derivative = cp.polyder(original, m=1) # First derivative

265

second_derivative = cp.polyder(original, m=2) # Second derivative

266

antiderivative = cp.polyint(derivative) # Should recover original (up to constant)

267

```