예제 #1
0
 def test_diff_one(self):
     """
     Derivative of Chebfun(1) close to zero
     """
     one = Chebfun(1.)
     zero = one.differentiate()
     npt.assert_allclose(tools.Zero(tools.xs), 0.)
예제 #2
0
 def test_prune(self):
     N = 10
     coeffs = np.array([1.]+N*[0])
     c0 = Chebfun.from_coeff(coeffs)
     npt.assert_allclose(c0.coefficients(), [1.])
     c1 = Chebfun.from_coeff(coeffs, prune=False)
     npt.assert_allclose(c1.coefficients(), coeffs)
예제 #3
0
 def test_interp_values(self):
     """
     Instanciate Chebfun from interpolation values.
     """
     p2 = Chebfun(self.p.values())
     npt.assert_almost_equal(self.p.coefficients(), p2.coefficients())
     tools.assert_close(self.p, p2)
예제 #4
0
 def test_add(self):
     s = Chebfun.from_function(np.sin)
     c = Chebfun.from_function(np.cos)
     r = c + s
     def expected(x):
         return np.sin(x) + np.cos(x)
     tools.assert_close(r, expected)
예제 #5
0
 def test_truncate(self, N=17):
     """
     Check that the Chebyshev coefficients are properly truncated.
     """
     small = Chebfun.from_function(tools.f, N=N)
     new = Chebfun.from_function(small)
     self.assertEqual(new.size(), small.size(),)
예제 #6
0
 def test_real_imag(self):
     datar = np.random.rand(10)
     datai = np.random.rand(10)
     cc = Chebfun.from_data(datar + 1j*datai)
     cr = Chebfun.from_data(datar)
     ci = Chebfun.from_data(datai)
     tools.assert_close(np.real(cc), cr)
     tools.assert_close(np.imag(cc), ci)
예제 #7
0
 def test_runge(self):
     """
     Test some of the capabilities of operator overloading.
     """
     r = Chebfun.from_function(runge)
     x = Chebfun.basis(1)
     rr = 1./(1+25*x**2)
     tools.assert_close(r, rr, rtol=1e-13)
예제 #8
0
 def test_scalarvectormult(self):
     """
     Possible to multiply scalar with vector chebfun.
     """
     v = Chebfun.from_function(segment)
     s = np.sin(Chebfun.identity())
     m = s * v
     tools.assert_close(m[0], s*v[0])
예제 #9
0
 def test_slice(self):
     """
     Test slicing: f[0] should return the first component.
     """
     s = Chebfun.from_function(segment)
     tools.assert_close(s[0], Chebfun.identity())
     tools.assert_close(s[1], Chebfun(0.))
     tools.assert_close(s[:], s)
예제 #10
0
파일: galerkin.py 프로젝트: nbren12/gnl
def a(p, order):
    if p > 0:
        return Chebfun.from_function(lambda x: sqrt(2)* np.sin(p*x)/p,[0,pi])\
                      .differentiate(order)
    else:
        if order == 1:
            return Chebfun.from_function(lambda x: np.ones_like(x), [0, pi])
        else:
            return Chebfun.from_function(lambda x: np.zeros_like(x), [0, pi])
예제 #11
0
 def test_roots_of_flat_function(self):
     """
     Check roots() does not fail for extremely flat Chebfuns such
     as those representing cumulative distribution functions.
     """
     cdf = Chebfun.from_data(flat_chebfun_vals, domain=[-0.7, 0.7])
     npt.assert_allclose((cdf - 0.05).roots(), 0.1751682246791747)
예제 #12
0
 def test_basis(self, n=4):
     """
     Tn(cos(t)) = cos(nt)
     """
     Tn = Chebfun.basis(n)
     ts = np.linspace(0, 2*np.pi, 100)
     npt.assert_allclose(Tn(np.cos(ts)), np.cos(n*ts))
예제 #13
0
 def test_sum(self):
     """
     Integral of chebfun of x**2 on [-1,1] is 2/3
     """
     p = Chebfun.from_function(Quad)
     i = p.sum()
     npt.assert_array_almost_equal(i,2/3)
예제 #14
0
 def test_highdiff(self):
     """
     Higher order derivatives of exp(x)
     """
     e = Chebfun.from_function(lambda x:np.exp(x))
     e4 = e.differentiate(4)
     tools.assert_close(e4, e)
예제 #15
0
 def test_diffquad(self):
     """
     Derivative of Chebfun(x**2/2) is close to identity function
     """
     self.p = .5*Chebfun.from_function(Quad)
     X = self.p.differentiate()
     tools.assert_close(X, lambda x:x)
예제 #16
0
 def test_nonzero(self):
     """
     nonzero is True for Chebfun(f) and False for Chebfun(0)
     """
     self.assertTrue(self.p)
     mp = Chebfun.from_function(tools.Zero)
     self.assertFalse(mp)
예제 #17
0
 def test_chebyshev_points(self):
     """
     First and last interpolation points are -1 and 1
     """
     N = pow(2,5)
     pts = Chebfun.interpolation_points(N)
     npt.assert_array_almost_equal(pts[[0,-1]],np.array([1.,-1]))
예제 #18
0
def test_init(ufunc):
    xx = Chebfun.from_function(lambda x: x, [0.25, 0.75])
    ff = ufunc(xx)
    assert isinstance(ff, Chebfun)
    result = ff.values()
    expected = ufunc(ff._ui_to_ab(ff.p.xi))
    npt.assert_allclose(result, expected)
예제 #19
0
 def test_zero(self):
     """
     Chebfun for zero has the minimal degree 5
     """
     p = Chebfun.from_function(tools.Zero)
     self.assertEqual(p.size(),
                      1)  # should be equal to the minimum length, 1
예제 #20
0
 def test_highdiff(self):
     """
     Higher order derivatives of exp(x)
     """
     e = Chebfun.from_function(lambda x: np.exp(x))
     e4 = e.differentiate(4)
     tools.assert_close(e4, e)
예제 #21
0
 def test_sum(self):
     """
     Integral of chebfun of x**2 on [-1,1] is 2/3
     """
     p = Chebfun.from_function(Quad)
     i = p.sum()
     npt.assert_array_almost_equal(i, 2 / 3)
예제 #22
0
 def test_basis(self, n=4):
     """
     Tn(cos(t)) = cos(nt)
     """
     Tn = Chebfun.basis(n)
     ts = np.linspace(0, 2 * np.pi, 100)
     npt.assert_allclose(Tn(np.cos(ts)), np.cos(n * ts))
예제 #23
0
    def test_square(self):
        def square(x):
            return self.p(x) * self.p(x)

        sq = Chebfun.from_function(square)
        npt.assert_array_less(0, sq(tools.xs))
        self.sq = sq
예제 #24
0
 def __init__(self, inputdistribution, precision, minexp, maxexp,
              poly_precision):
     '''
 Constructor interpolates the density function using Chebyshev interpolation
 then uses this interpolation to build a PaCal object:
 the self.distribution attribute which contains all the methods we could possibly want
 Inputs:
     inputdistribution: a PaCal object representing the distribution for which we want to compute
                         the rounding error distribution
     precision, minexp, maxexp: specify the low precision environment suing gmpy2
     poly_precision: the number of exact evaluations of the density function used to
                     build the interpolating polynomial representing it
     '''
     self.inputdistribution = inputdistribution
     self.precision = precision
     self.minexp = minexp
     self.maxexp = maxexp
     self.poly_precision = poly_precision
     # Test if the range of floating point number covers enough of the inputdistribution
     x = gmpy2.next_above(gmpy2.inf(-1))
     y = gmpy2.next_below(gmpy2.inf(1))
     coverage = self.inputdistribution.get_piecewise_pdf().integrate(
         float(x), float(y))
     if (1.0 - coverage) > 0.001:
         raise Exception(
             'The range of floating points is too narrow, increase maxexp and increase minexp'
         )
     # Builds the Chebyshev polynomial rerpesentation of the density function
     self.pdf = Chebfun.from_function(lambda t: self.__getpdf(t),
                                      N=self.poly_precision)
     # Creates a PaCal object containing the distribution
     self.distribution = FunDistr(self.pdf, [-1, 1])
예제 #25
0
 def test_chebyshev_points(self):
     """
     First and last interpolation points are -1 and 1
     """
     N = pow(2, 5)
     pts = Chebfun.interpolation_points(N)
     npt.assert_array_almost_equal(pts[[0, -1]], np.array([1., -1]))
예제 #26
0
 def test_roots_of_flat_function(self):
     """
     Check roots() does not fail for extremely flat Chebfuns such
     as those representing cumulative distribution functions.
     """
     cdf = Chebfun.from_data(flat_chebfun_vals, domain=[-0.7, 0.7])
     npt.assert_allclose((cdf-0.05).roots(), 0.1751682246791747)
예제 #27
0
 def test_nonzero(self):
     """
     nonzero is True for Chebfun(f) and False for Chebfun(0)
     """
     self.assertTrue(self.p)
     mp = Chebfun.from_function(tools.Zero)
     self.assertFalse(mp)
예제 #28
0
def test_init(ufunc):
    xx = Chebfun.from_function(lambda x: x,[0.25,0.75])
    ff = ufunc(xx)
    assert isinstance(ff, Chebfun)
    result = ff.values()
    expected = ufunc(ff._ui_to_ab(ff.p.xi))
    npt.assert_allclose(result, expected)
예제 #29
0
 def test_diffquad(self):
     """
     Derivative of Chebfun(x**2/2) is close to identity function
     """
     self.p = .5 * Chebfun.from_function(Quad)
     X = self.p.differentiate()
     tools.assert_close(X, lambda x: x)
예제 #30
0
 def test_integrate(self):
     """
     Integrate exp
     """
     e = Chebfun.from_function(lambda x: np.exp(x))
     antideriv = e.integrate()
     result = antideriv - antideriv(antideriv._domain[0])
     tools.assert_close(result, e - e(antideriv._domain[0]))
예제 #31
0
 def test_integrate(self):
     """
     Integrate exp
     """
     e = Chebfun.from_function(lambda x:np.exp(x))
     antideriv = e.integrate()
     result = antideriv - antideriv(antideriv._domain[0])
     tools.assert_close(result, e - e(antideriv._domain[0]))
예제 #32
0
 def test_diff_x(self):
     """
     First and second derivative of Chebfun(x) are close to one and zero respectively.
     """
     self.p = Chebfun.from_function(tools.Identity)
     one = self.p.differentiate()
     zero = one.differentiate()
     npt.assert_allclose(one(tools.xs), 1.)
     npt.assert_allclose(tools.Zero(tools.xs), 0.)
예제 #33
0
 def test_diff_x(self):
     """
     First and second derivative of Chebfun(x) are close to one and zero respectively.
     """
     self.p = Chebfun.from_function(tools.Identity)
     one = self.p.differentiate()
     zero = one.differentiate()
     npt.assert_allclose(one(tools.xs), 1.)
     npt.assert_allclose(tools.Zero(tools.xs), 0.)
예제 #34
0
 def test_repr(self):
     """
     Repr shows the interpolation values.
     """
     self.skipTest('Representation changed to include domain information')
     p = Chebfun.basis(1)
     s = repr(p)
     expected = '<Chebfun(array([ 1., -1.]))>'
     self.assertEqual(s, expected)
예제 #35
0
 def test_repr(self):
     """
     Repr shows the interpolation values.
     """
     self.skipTest('Representation changed to include domain information')
     p = Chebfun.basis(1)
     s = repr(p)
     expected = '<Chebfun(array([ 1., -1.]))>'
     self.assertEqual(s, expected)
예제 #36
0
 def test_N(self):
     """
     Check initialisation with a fixed N
     """
     N = self.p.size() - 1
     pN = Chebfun.from_function(tools.f, N=N)
     self.assertEqual(len(pN.coefficients()), N + 1)
     self.assertEqual(len(pN.coefficients()), pN.size())
     tools.assert_close(pN, self.p)
     npt.assert_allclose(pN.coefficients(), self.p.coefficients())
예제 #37
0
 def test_dot(self):
     """
     f.0 = 0
     f.1 = f.sum()
     """
     p = Chebfun.from_function(np.sin)
     z = p.dot(Chebfun(0.))
     self.assertAlmostEqual(z, 0.)
     s = p.dot(Chebfun(1.))
     self.assertAlmostEqual(s, p.sum())
예제 #38
0
 def test_add_mistype(self):
     """
     Possible to add a Chebfun and a function
     """
     self.skipTest('not possible to add function and chebfun yet')
     def f(x):
         return np.sin(x)
     c = Chebfun.from_function(f)
     result = c + f
     self.assertIsInstance(result, Chebfun)
예제 #39
0
 def test_N(self):
     """
     Check initialisation with a fixed N
     """
     N = self.p.size() - 1
     pN = Chebfun.from_function(tools.f, N=N)
     self.assertEqual(len(pN.coefficients()), N+1)
     self.assertEqual(len(pN.coefficients()),pN.size())
     tools.assert_close(pN, self.p)
     npt.assert_allclose(pN.coefficients(),self.p.coefficients())
예제 #40
0
def tdata(request):
    index = request.param
    class TData(): pass
    tdata = TData()
    tdata.function = data.IntervalTestData.functions[0]
    tdata.function_d = data.IntervalTestData.first_derivs[0]
    tdata.domain = data.IntervalTestData.domains[index]
    tdata.roots = data.IntervalTestData.roots[0][index]
    tdata.integral = data.IntervalTestData.integrals[0][index]
    tdata.chebfun = Chebfun.from_function(tdata.function, tdata.domain)
    return tdata
예제 #41
0
    def test_add_mistype(self):
        """
        Possible to add a Chebfun and a function
        """
        self.skipTest('not possible to add function and chebfun yet')

        def f(x):
            return np.sin(x)

        c = Chebfun.from_function(f)
        result = c + f
        self.assertIsInstance(result, Chebfun)
예제 #42
0
def test_ufunc(ufunc):
    """
    Check that ufuncs work and give the right result.
    arccosh is not tested
    """
    # transformation from [-1, 1] to [1/4, 3/4]
    trans = lambda x: (x+2)/4
    x2 = Chebfun.from_function(trans)
    cf = ufunc(x2)
    assert isinstance(cf, Chebfun)
    result = cf.values()
    expected = ufunc(trans(cf.p.xi))
    npt.assert_allclose(result, expected)
예제 #43
0
def test_ufunc(ufunc):
    """
    Check that ufuncs work and give the right result.
    arccosh is not tested
    """
    # transformation from [-1, 1] to [1/4, 3/4]
    trans = lambda x: (x + 2) / 4
    x2 = Chebfun.from_function(trans)
    cf = ufunc(x2)
    assert isinstance(cf, Chebfun)
    result = cf.values()
    expected = ufunc(trans(cf.p.xi))
    npt.assert_allclose(result, expected)
예제 #44
0
def tdata(request):
    index = request.param

    class TData():
        pass

    tdata = TData()
    tdata.function = data.IntervalTestData.functions[0]
    tdata.function_d = data.IntervalTestData.first_derivs[0]
    tdata.domain = data.IntervalTestData.domains[index]
    tdata.roots = data.IntervalTestData.roots[0][index]
    tdata.integral = data.IntervalTestData.integrals[0][index]
    tdata.chebfun = Chebfun.from_function(tdata.function, tdata.domain)
    return tdata
예제 #45
0
 def test_from_chebfun(self):
     ce = Chebfun.from_function(tools.f)
     cr = chebfun(ce)
     tools.assert_close(cr, ce)
예제 #46
0
 def test_from_chebcoeffs(self):
     coeffs = np.random.randn(10)
     cr = chebfun(chebcoeff=coeffs)
     ce = Chebfun.from_coeff(coeffs)
     tools.assert_close(cr, ce)
예제 #47
0
 def setUp(self):
     # Construct the O(dx^-16) "spectrally accurate" chebfun p
     self.p = Chebfun.from_function(tools.f)
예제 #48
0
 def test_scalar_mul(self):
     self.assertEqual(self.p1, self.p1)
     self.assertEqual(self.p1 * 1, 1 * self.p1)
     self.assertEqual(self.p1 * 1, self.p1)
     self.assertEqual(0 * self.p1, Chebfun.from_function(tools.Zero))
예제 #49
0
 def setUp(self):
     self.p1 = Chebfun.from_function(tools.f)
     self.p2 = Chebfun.from_function(runge)
예제 #50
0
 def test_underflow(self):
     self.skipTest('mysterious underflow error')
     p = Chebfun.from_function(piecewise_continuous, N=pow(2, 10) - 1)
예제 #51
0
 def test_chebpolyfit(self):
     N = 32
     data = np.random.rand(N - 1, 2)
     coeffs = Chebfun.polyfit(data)
     result = Chebfun.polyval(coeffs)
     npt.assert_allclose(data, result)
예제 #52
0
 def test_chebpolyfitval(self, N=64):
     data = np.random.rand(N - 1, 2)
     computed = Chebfun.polyval(Chebfun.polyfit(data))
     npt.assert_allclose(computed, data)
예제 #53
0
 def test_no_convergence(self):
     with self.assertRaises(Chebfun.NoConvergence):
         Chebfun.from_function(np.sign)
예제 #54
0
 def test_list_init(self):
     c = Chebfun([1.])
     npt.assert_array_almost_equal(c.coefficients(), np.array([1.]))
예제 #55
0
 def test_basis(self, ns=[0, 5]):
     for n in ns:
         c = Chebfun.basis(n)
         npt.assert_array_almost_equal(c.coefficients(),
                                       np.array([0] * n + [1.]))
예제 #56
0
 def test_from_values(self):
     values = np.random.randn(10)
     cr = chebfun(values)
     ce = Chebfun.from_data(values)
     tools.assert_close(cr, ce)
예제 #57
0
 def test_mismatch(self):
     c1 = Chebfun.identity()
     c2 = Chebfun.from_function(lambda x:x, domain=[2,3])
     for op in [operator.add, operator.sub, operator.mul, operator.truediv]:
         with self.assertRaises(Chebfun.DomainMismatch):
             op(c1, c2)
예제 #58
0
 def test_restrict(self):
     x = Chebfun.identity()
     with self.assertRaises(ValueError):
         x.restrict([-2,0])
     with self.assertRaises(ValueError):
         x.restrict([0,2])
예제 #59
0
 def test_from_scalar(self):
     val = np.random.rand()
     cr = chebfun(val)
     ce = Chebfun.from_data([val])
     tools.assert_close(cr, ce)
예제 #60
0
def test_init(ufunc):
    xx = Chebfun.from_function(lambda x: x,[0.25,0.75])
    ff = ufunc(xx)
    assert isinstance(ff, Chebfun)
    result = ff.values()
    expected = ufunc(ff._ui_to_ab(ff.p.xi))
    npt.assert_allclose(result, expected)


#------------------------------------------------------------------------------
# Test the restrict operator
#------------------------------------------------------------------------------

from . import data

@pytest.mark.parametrize('ff', [Chebfun.from_function(tools.f,[-3,4])])
@pytest.mark.parametrize('domain', data.IntervalTestData.domains)
def test_restrict(ff, domain):
    ff_ = ff.restrict(domain)
    xx = tools.map_ui_ab(tools.xs, domain[0],domain[1])
    tools.assert_close(tools.f, ff_, xx)

#------------------------------------------------------------------------------
# Add the arbitrary interval tests
#------------------------------------------------------------------------------

@pytest.fixture(params=list(range(5)))
def tdata(request):
    index = request.param
    class TData(): pass
    tdata = TData()