/
polynomial.py
46 lines (34 loc) · 1.27 KB
/
polynomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import cupy
def polyvander(x, deg):
"""Computes the Vandermonde matrix of given degree.
Args:
x (cupy.ndarray): array of points
deg (int): degree of the resulting matrix.
Returns:
cupy.ndarray: The Vandermonde matrix
.. seealso:: :func:`numpy.polynomial.polynomial.polyvander`
"""
deg = cupy.polynomial.polyutils._deprecate_as_int(deg, 'deg')
if deg < 0:
raise ValueError('degree must be non-negative')
if x.ndim == 0:
x = x.ravel()
dtype = cupy.float64 if x.dtype.kind in 'biu' else x.dtype
out = x ** cupy.arange(deg + 1, dtype=dtype).reshape((-1,) + (1,) * x.ndim)
return cupy.moveaxis(out, 0, -1)
def polycompanion(c):
"""Computes the companion matrix of c.
Args:
c (cupy.ndarray): 1-D array of polynomial coefficients
ordered from low to high degree.
Returns:
cupy.ndarray: Companion matrix of dimensions (deg, deg).
.. seealso:: :func:`numpy.polynomial.polynomial.polycompanion`
"""
[c] = cupy.polynomial.polyutils.as_series([c])
deg = c.size - 1
if deg == 0:
raise ValueError('Series must have maximum degree of at least 1.')
matrix = cupy.eye(deg, k=-1, dtype=c.dtype)
matrix[:, -1] -= c[:-1] / c[-1]
return matrix