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)
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(),)
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])
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)
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)
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)
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)
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)
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)
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]))
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)
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])
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.)
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())
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())
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)
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
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)
def test_vectorized(self): fv = np.vectorize(tools.f) p = Chebfun.from_function(fv)
def test_no_convergence(self): with self.assertRaises(Chebfun.NoConvergence): Chebfun.from_function(np.sign)
def test_has_p(self): c1 = Chebfun.from_function(tools.f, N=10) self.assertTrue(hasattr(c1, 'p')) c2 = Chebfun.from_function(tools.f, ) self.assertTrue(hasattr(c2, 'p'))
def test_init_from_segment(self): c = Chebfun.from_function(segment)
def test_root(self): r = np.random.rand() p = Chebfun.from_function(lambda x: np.sin(x - r)) roots = p.roots() npt.assert_allclose(roots, r)
def test_equal(self): """ Chebfun(f) is equal to itself. """ tools.assert_close(self.p, Chebfun.from_function(self.p))
def test_init_from_circle(self): c = Chebfun.from_function(tools.circle)
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)
def setUp(self): self.p1 = Chebfun.from_function(tools.f) self.p2 = Chebfun.from_function(runge)
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))
def test_underflow(self): self.skipTest('mysterious underflow error') p = Chebfun.from_function(piecewise_continuous, N=pow(2,10)-1)
def test_from_chebfun(self): ce = Chebfun.from_function(tools.f) cr = chebfun(ce) tools.assert_close(cr, ce)
def setUp(self): # Construct the O(dx^-16) "spectrally accurate" chebfun p self.p = Chebfun.from_function(tools.f)
def test_norm(self): """ Norm of x**2 is sqrt(2/5) """ p = Chebfun.from_function(Quad) self.assertAlmostEqual(p.norm(), np.sqrt(2 / 5))
def test_mx(self): c = Chebfun.from_function(lambda x: -x) tools.assert_close(c, lambda x: -x)
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()
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)
def test_underflow(self): self.skipTest('mysterious underflow error') p = Chebfun.from_function(piecewise_continuous, N=pow(2, 10) - 1)
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))
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