>>> import numpy.polynomial as P >>> import numpy.polynomial.chebyshev as C >>> P.polyroots((-1,1,-1,1)) # x^3 - x^2 + x - 1 has two complex roots array([ -4.99600361e-16-1.j, -4.99600361e-16+1.j, 1.00000e+00+0.j]) >>> C.chebroots((-1,1,-1,1)) # T3 - T2 + T1 - T0 has only real roots array([ -5.00000000e-01, 2.60860684e-17, 1.00000000e+00]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([-cs[0] / cs[1]]) n = len(cs) - 1 cmat = np.zeros((n, n), dtype=cs.dtype) cmat.flat[1::n + 1] = .5 cmat.flat[n::n + 1] = .5 cmat[1, 0] = 1 cmat[:, -1] -= cs[:-1] * (.5 / cs[-1]) roots = la.eigvals(cmat) roots.sort() return roots # # Chebyshev series class # exec polytemplate.substitute(name='Chebyshev', nick='cheb', domain='[-1,1]')
array([ -1.00000000e+00, -1.38777878e-17, 1.00000000e+00]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1 : return np.array([], dtype=cs.dtype) if len(cs) == 2 : return np.array([-.5*cs[0]/cs[1]]) n = len(cs) - 1 cs /= cs[-1] cmat = np.zeros((n,n), dtype=cs.dtype) cmat[1, 0] = .5 for i in range(1, n): cmat[i - 1, i] = i if i != n - 1: cmat[i + 1, i] = .5 else: cmat[:, i] -= cs[:-1]*.5 roots = la.eigvals(cmat) roots.sort() return roots # # Hermite series class # exec polytemplate.substitute(name='Hermite', nick='herm', domain='[-1,1]')
""" # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1 : return np.array([], dtype=cs.dtype) if len(cs) == 2 : return np.array([-cs[0]/cs[1]]) n = len(cs) - 1 cs /= cs[-1] cmat = np.zeros((n,n), dtype=cs.dtype) cmat[1, 0] = 1 for i in range(1, n): tmp = 2*i + 1 cmat[i - 1, i] = i/tmp if i != n - 1: cmat[i + 1, i] = (i + 1)/tmp else: cmat[:, i] -= cs[:-1]*(i + 1)/tmp roots = la.eigvals(cmat) roots.sort() return roots # # Legendre series class # exec polytemplate.substitute(name='Legendre', nick='leg', domain='[-1,1]')
Parameters ---------- npts : int Number of sample points desired. Returns ------- pts : ndarray The Chebyshev points of the second kind. Notes ----- .. versionadded:: 1.5.0 """ _npts = int(npts) if _npts != npts: raise ValueError("npts must be integer") if _npts < 2: raise ValueError("npts must be >= 2") x = np.linspace(-np.pi, 0, _npts) return np.cos(x) # # Chebyshev series class # exec polytemplate.substitute(name='Chebyshev', nick='cheb', domain='[-1,1]')
""" # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1 : return np.array([], dtype=cs.dtype) if len(cs) == 2 : return np.array([1 + cs[0]/cs[1]]) n = len(cs) - 1 cs /= cs[-1] cmat = np.zeros((n,n), dtype=cs.dtype) cmat[0, 0] = 1 cmat[1, 0] = -1 for i in range(1, n): cmat[i - 1, i] = -i cmat[i, i] = 2*i + 1 if i != n - 1: cmat[i + 1, i] = -(i + 1) else: cmat[:, i] += cs[:-1]*(i + 1) roots = la.eigvals(cmat) roots.sort() return roots # # Laguerre series class # exec polytemplate.substitute(name='Laguerre', nick='lag', domain='[-1,1]')
>>> import numpy.polynomial as P >>> P.polyroots(P.polyfromroots((-1,0,1))) array([-1., 0., 1.]) >>> P.polyroots(P.polyfromroots((-1,0,1))).dtype dtype('float64') >>> j = complex(0,1) >>> P.polyroots(P.polyfromroots((-j,0,j))) array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([-cs[0] / cs[1]]) n = len(cs) - 1 cmat = np.zeros((n, n), dtype=cs.dtype) cmat.flat[n::n + 1] = 1 cmat[:, -1] -= cs[:-1] / cs[-1] roots = la.eigvals(cmat) roots.sort() return roots # # polynomial class # exec polytemplate.substitute(name='Polynomial', nick='poly', domain='[-1,1]')
array([-1., 0., 1.]) >>> P.polyroots(P.polyfromroots((-1,0,1))).dtype dtype('float64') >>> j = complex(0,1) >>> P.polyroots(P.polyfromroots((-j,0,j))) array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1 : return np.array([], dtype=cs.dtype) if len(cs) == 2 : return np.array([-cs[0]/cs[1]]) n = len(cs) - 1 cmat = np.zeros((n,n), dtype=cs.dtype) cmat.flat[n::n+1] = 1 cmat[:,-1] -= cs[:-1]/cs[-1] roots = la.eigvals(cmat) roots.sort() return roots # # polynomial class # exec polytemplate.substitute(name='Polynomial', nick='poly', domain='[-1,1]')
""" # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) <= 1: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([1 + cs[0] / cs[1]]) n = len(cs) - 1 cs /= cs[-1] cmat = np.zeros((n, n), dtype=cs.dtype) cmat[0, 0] = 1 cmat[1, 0] = -1 for i in range(1, n): cmat[i - 1, i] = -i cmat[i, i] = 2 * i + 1 if i != n - 1: cmat[i + 1, i] = -(i + 1) else: cmat[:, i] += cs[:-1] * (i + 1) roots = la.eigvals(cmat) roots.sort() return roots # # Laguerre series class # exec polytemplate.substitute(name='Laguerre', nick='lag', domain='[-1,1]')
Examples -------- >>> import numpy.polynomial.polynomial as poly >>> poly.polyroots(poly.polyfromroots((-1,0,1))) array([-1., 0., 1.]) >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype dtype('float64') >>> j = complex(0,1) >>> poly.polyroots(poly.polyfromroots((-j,0,j))) array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) """ # cs is a trimmed copy [cs] = pu.as_series([cs]) if len(cs) < 2: return np.array([], dtype=cs.dtype) if len(cs) == 2: return np.array([-cs[0] / cs[1]]) m = polycompanion(cs) r = la.eigvals(m) r.sort() return r # # polynomial class # exec polytemplate.substitute(name="Polynomial", nick="poly", domain="[-1,1]")