def in_chebyshev_basis(self): res = poly.Polynomial({poly.ChebyshevVector({}): 1}) for v, p in self: c = poly2cheb([0] * p + [1]) basis = poly.ChebyshevVector.construct_basis([v], p) res *= poly.Polynomial(dict(zip(basis, c))) return res
def C1(n): """ integral related to first order finite Larmor radius effect """ c1 = poly2cheb([0, 0, 0, 0, -1.5, 0, 2]) c2 = nthpoly(2 * n) kernel = chebmul(c1, c2) cint = chebint(kernel) return chebval(1, cint)
def C0(n): """ integral related to zero'th order finite Larmor radius effect """ c1 = poly2cheb([0, 1, 0, -2]) c2 = nthpoly(2 * n) kernel = chebmul(c1, c2) cint = chebint(kernel) return chebval(1, cint)
def conv_poly(P): """ Convert a standard polynomial to a chebyshev polynomial in one dimension. Args: P (): The standard polynomial to be converted. Returns: new_conv (): The chebyshev polynomial. """ conv = C.poly2cheb(P) if conv.size == P.size: return conv else: pad = P.size - conv.size new_conv = np.pad(conv, ((0, pad)), 'constant') return new_conv
def rescale_largest_eig_val(self, lambda_max): """ Rescale the largest eigenvalue to see Tracy-Widom fluctuations .. math:: N^{\\frac{2}{3}} c_v (\\lambda_{\\max} - b_v) where .. math:: c_V = (b_v - a_v)^{-\\frac{1}{3}} \\left(\\sum_{n=1}^{\\infty} k {V'}_k \\right)^{\\frac{2}{3}} with :math:`{V'}_k` being the Chebychev coefficients of .. math:: V'(\\frac{a_v + b_v}{2} + \\frac{b_v - a_v}{2} X) .. seealso:: - Section 3.2 https://arxiv.org/pdf/1210.2199.pdf - :cite:`OlNaTr14` p.5 Equation 2.3 `https://arxiv.org/pdf/1404.0071.pdf <https://arxiv.org/pdf/1404.0071.pdf>`_ - `poly2cheb <https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.chebyshev.poly2cheb.html?>`_ """ a_v, b_v = self.support shift = np.poly1d([0.5 * (b_v - a_v), 0.5 * (b_v + a_v)]) dV_shift = self.V.deriv(m=1)(shift) dV_shift_cheb = poly2cheb(dV_shift.coeffs[::-1]) sum_k_dV_k = sum(k * dV_k for k, dV_k in enumerate(dV_shift_cheb[1:], start=1)) c_v = np.cbrt(sum_k_dV_k**2 / (b_v - a_v)) return self.N**(2 / 3) * c_v * (lambda_max - b_v)
def conv_poly(P): """ Convert a standard polynomial to a Chebyshev polynomial in one dimension. Parameters ---------- P : array_like A one dimensional array_like object that represents the coeff of a power basis polynomial. Returns ------- ndarray A one dimensional array that represents the coeff of a Chebyshev polynomial. """ conv = cheb.poly2cheb(P) if conv.size == P.size: return conv else: pad = P.size - conv.size new_conv = np.pad(conv, ((0, pad)), 'constant') return new_conv
def poly2cheb(pol): from numpy.polynomial.chebyshev import poly2cheb return poly2cheb(pol)
def test_poly2cheb(self) : for i in range(10) : assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
def test_chebint(self) : # check exceptions assert_raises(ValueError, cheb.chebint, [0], .5) assert_raises(ValueError, cheb.chebint, [0], -1) assert_raises(ValueError, cheb.chebint, [0], 1, [0,0]) # test integration of zero polynomial for i in range(2, 5): k = [0]*(i - 2) + [1] res = cheb.chebint([0], m=i, k=k) assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i]) res = cheb.cheb2poly(chebint) assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd for i in range(5) : scl = i + 1 pol = [0]*i + [1] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1) assert_almost_equal(cheb.chebval(-1, chebint), i) # check single integration with integration constant and scaling for i in range(5) : scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2) res = cheb.cheb2poly(chebint) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1) res = cheb.chebint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k]) res = cheb.chebint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1) res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling for i in range(5) : for j in range(2,5) : pol = [0]*i + [1] tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k], scl=2) res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt))
def test_fromroots(self) : roots = [0, .5, 1] p = ch.Chebyshev.fromroots(roots, domain=[0, 1]) res = p.coef tgt = ch.poly2cheb([0, -1, 0, 1]) assert_almost_equal(res, tgt)
def test_roots(self) : p = ch.Chebyshev(ch.poly2cheb([0, -1, 0, 1]), [0, 1]) res = p.roots() tgt = [0, .5, 1] assert_almost_equal(res, tgt)
def test_poly2cheb(self): for i in range(10): assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
def test_chebint(self): # check exceptions assert_raises(ValueError, cheb.chebint, [0], .5) assert_raises(ValueError, cheb.chebint, [0], -1) assert_raises(ValueError, cheb.chebint, [0], 1, [0, 0]) # test integration of zero polynomial for i in range(2, 5): k = [0]*(i - 2) + [1] res = cheb.chebint([0], m=i, k=k) assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i]) res = cheb.cheb2poly(chebint) assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd for i in range(5): scl = i + 1 pol = [0]*i + [1] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1) assert_almost_equal(cheb.chebval(-1, chebint), i) # check single integration with integration constant and scaling for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] chebpol = cheb.poly2cheb(pol) chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2) res = cheb.cheb2poly(chebint) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k for i in range(5): for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] for k in range(j): tgt = cheb.chebint(tgt, m=1) res = cheb.chebint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k for i in range(5): for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k]) res = cheb.chebint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd for i in range(5): for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1) res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling for i in range(5): for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k], scl=2) res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt))
def poly2cheb(pol) : from numpy.polynomial.chebyshev import poly2cheb return poly2cheb(pol)