Beispiel #1
0
 def test_integ(self) :
     p = self.p2.integ()
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 0, scl=.5))
     p = self.p2.integ(1, 1)
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 1, scl=.5))
     p = self.p2.integ(2, [1, 2])
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 2, [1,2], scl=.5))
Beispiel #2
0
 def test_integ(self) :
     p = self.p2.integ()
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 0, scl=.5))
     p = self.p2.integ(lbnd=0)
     assert_almost_equal(p(0), 0)
     p = self.p2.integ(1, 1)
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 1, scl=.5))
     p = self.p2.integ(2, [1, 2])
     assert_almost_equal(p.coef, ch.chebint([1,2,3], 2, [1,2], scl=.5))
Beispiel #3
0
 def _cdf(self, x, *args):
     """
     Return CDF of the polynom function
     """
     coeffs = np.array([c[0] for c in args])
     coeffs = coeffs / coeffs.sum(axis=0)
     coeffs = chebyshev.chebint(coeffs)
     cdf_not_scaled = chebyshev.chebval(x, coeffs) + x + 1 - chebyshev.chebval(-1, coeffs)
     integral_coeffs = chebyshev.chebint(coeffs)
     scale = chebyshev.chebval(1, coeffs) + 2 - chebyshev.chebval(-1, coeffs)
     cdf = np.where(x < -1.0, 0.0, np.where(x > 1.0, 1.0, cdf_not_scaled / scale))
     return cdf
Beispiel #4
0
def chebcdf(pdf, lower_bd, upper_bd, eps=10**(-15)):
    """Get Chebyshev approximation of the CDF.
    
    Parameters
    ----------
    pdf : function, float -> float
        The probability density function (not necessarily normalized). Must take
        floats or ints as input, and return floats as an output.
    lower_bd : float
        Lower bound of the support of the pdf. This parameter allows one to
        manually establish cutoffs for the density.
    upper_bd : float
        Upper bound of the support of the pdf.
    eps: float
        Error tolerance of Chebyshev polynomial fit of PDF.
    
    Returns
    -------
    cdf : function
        The cumulative density function of the (normalized version of the)
        provided pdf. The function cdf() takes an iterable of floats or doubles
        as an argument, and returns an iterable of floats of the same length.
    """
    if not (np.isfinite(lower_bd) and np.isfinite(upper_bd)):
        raise ValueError('Bounds must be finite.')
    if not lower_bd < upper_bd:
        raise ValueError('Lower bound must be less than upper bound.')

    x,coeffs = adaptive_chebfit(pdf, lower_bd, upper_bd, eps)
    int_coeffs = chebint(coeffs)
    # offset and scale so that it goes from 0 to 1, i.e. is a true CDF.
    offset = chebval(lower_bd, int_coeffs)
    scale = chebval(upper_bd, int_coeffs) - chebval(lower_bd, int_coeffs)
    cdf = lambda x: (chebval(x,int_coeffs) - offset) / scale
    return cdf
Beispiel #5
0
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)
Beispiel #6
0
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)
Beispiel #7
0
 def test_chebder(self) :
     # check exceptions
     assert_raises(ValueError, ch.chebder, [0], -1)
     # check that zeroth deriviative does nothing
     for i in range(5) :
         tgt = [1] + [0]*i
         res = ch.chebder(tgt, m=0)
         assert_equal(trim(res), trim(tgt))
     # check that derivation is the inverse of integration
     for i in range(5) :
         for j in range(2,5) :
             tgt = [1] + [0]*i
             res = ch.chebder(ch.chebint(tgt, m=j), m=j)
             assert_almost_equal(trim(res), trim(tgt))
     # check derivation with scaling
     for i in range(5) :
         for j in range(2,5) :
             tgt = [1] + [0]*i
             res = ch.chebder(ch.chebint(tgt, m=j, scl=2), m=j, scl=.5)
             assert_almost_equal(trim(res), trim(tgt))
Beispiel #8
0
 def test_chebder(self):
     # check exceptions
     assert_raises(ValueError, ch.chebder, [0], -1)
     # check that zeroth deriviative does nothing
     for i in range(5):
         tgt = [1] + [0] * i
         res = ch.chebder(tgt, m=0)
         assert_equal(trim(res), trim(tgt))
     # check that derivation is the inverse of integration
     for i in range(5):
         for j in range(2, 5):
             tgt = [1] + [0] * i
             res = ch.chebder(ch.chebint(tgt, m=j), m=j)
             assert_almost_equal(trim(res), trim(tgt))
     # check derivation with scaling
     for i in range(5):
         for j in range(2, 5):
             tgt = [1] + [0] * i
             res = ch.chebder(ch.chebint(tgt, m=j, scl=2), m=j, scl=.5)
             assert_almost_equal(trim(res), trim(tgt))
Beispiel #9
0
 def _pdf(self, x, *args):
     """
     Return PDF of the polynom function
     """
     # TODO Fix support for large args
     coeffs = np.array([c[0] for c in args])
     coeffs = coeffs / coeffs.sum(axis=0)
     # +1 so that the pdf is always positive
     pdf_not_normed = chebyshev.chebval(x, coeffs) + 1
     integral_coeffs = chebyshev.chebint(coeffs)
     norm = chebyshev.chebval(1, integral_coeffs) - chebyshev.chebval(-1, integral_coeffs) + 2
     pdf = np.where(np.abs(x) > 1.0, 0.0, pdf_not_normed / norm)
     return pdf
Beispiel #10
0
    def test_chebint_axis(self):
        # check that axis keyword works
        c2d = np.random.random((3, 4))

        tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
        res = cheb.chebint(c2d, axis=0)
        assert_almost_equal(res, tgt)

        tgt = np.vstack([cheb.chebint(c) for c in c2d])
        res = cheb.chebint(c2d, axis=1)
        assert_almost_equal(res, tgt)

        tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
        res = cheb.chebint(c2d, k=3, axis=1)
        assert_almost_equal(res, tgt)
Beispiel #11
0
    def test_chebint_axis(self):
        # check that axis keyword works
        c2d = np.random.random((3, 4))

        tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
        res = cheb.chebint(c2d, axis=0)
        assert_almost_equal(res, tgt)

        tgt = np.vstack([cheb.chebint(c) for c in c2d])
        res = cheb.chebint(c2d, axis=1)
        assert_almost_equal(res, tgt)

        tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
        res = cheb.chebint(c2d, k=3, axis=1)
        assert_almost_equal(res, tgt)
Beispiel #12
0
    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))
Beispiel #13
0
    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))
Beispiel #14
0


if __name__ == '__main__':

	Ny = 201
	Ly = 1.

	# ys = Ly * np.linspace(0, 1, Ny)
	# ys = Ly * (1 - np.cos(.5*np.pi*np.linspace(0,1,Ny)))
	ys = cb.chebpts2(Ny)

	# D1, D2, I2 = Schemes.compact6(ys)
	# D1, D2, I2 = Schemes.center2(ys)
	D1, D2, I1 = Schemes.cheb(ys)

	# us = np.sin(2*np.pi*ys) * np.cos(np.pi*ys**2)
	us = (ys - ys[0]) * (ys[-1] - ys)

	print(cb.chebval(ys, cb.chebint(cb.chebfit(ys, us, len(ys)-1), lbnd=ys[0]))[-1])
	print(np.diag(I1) @ us * (ys[-1]-ys[0]))

	Schemes.check(ys, us, D1, D2)







Beispiel #15
0
def chebint(cs, m=1, k=[], lbnd=0, scl=1):
    from numpy.polynomial.chebyshev import chebint
    return chebint(cs, m, k, lbnd, scl)
Beispiel #16
0
def chebint(cs, m=1, k=[], lbnd=0, scl=1):
    from numpy.polynomial.chebyshev import chebint
    return chebint(cs, m, k, lbnd, scl)
Beispiel #17
0
def indefInt(A, s=1):
    return np_cheb.chebint(A, scl=s)