def test_compute_breakdata_2(self): funs = np.array([self.fun0, self.fun1]) breaks = compute_breakdata(funs) x, y = list(breaks.keys()), list(breaks.values()) self.assertLessEqual(infnorm(x-np.array([-1,0,1])), eps) self.assertLessEqual(infnorm(y-np.array([np.exp(-1),np.exp(0), np.exp(1)])), 2*eps)
def test__add__radd__constant(self): xx = self.xx for (fun, funlen, _) in testfunctions: for const in (-1, 1, 10, -1e5): f = lambda x: const + fun(x) techfun = Chebtech2.initfun_fixedlen(fun, funlen) f1 = const + techfun f2 = techfun + const tol = 5e1 * eps * abs(const) self.assertLessEqual(infnorm(f(xx)-f1(xx)), tol) self.assertLessEqual(infnorm(f(xx)-f2(xx)), tol)
def test__add__radd__constant(self): subinterval = Interval(-.5,.9) xx = subinterval(self.yy) for (fun, _, _) in testfunctions: for const in (-1, 1, 10, -1e5): f = lambda x: const + fun(x) bndfun = Bndfun.initfun_adaptive(fun, subinterval) f1 = const + bndfun f2 = bndfun + const tol = 4e1 * eps * abs(const) self.assertLessEqual(infnorm(f(xx)-f1(xx)), tol) self.assertLessEqual(infnorm(f(xx)-f2(xx)), tol)
def test__mul__rmul__constant(self): xx = self.xx for (fun, funlen, _) in testfunctions: for const in (-1, 1, 10, -1e5): techfun = Chebtech2.initfun_fixedlen(fun, funlen) f = lambda x: const * fun(x) g = lambda x: fun(x) * const ff = const * techfun gg = techfun * const tol = 5e1 * eps * abs(const) self.assertLessEqual(infnorm(f(xx)-ff(xx)), tol) self.assertLessEqual(infnorm(g(xx)-gg(xx)), tol)
def test__mul__rmul__constant(self): subinterval = Interval(-.5,.9) xx = subinterval(self.yy) for (fun, _, _) in testfunctions: for const in (-1, 1, 10, -1e5): bndfun = Bndfun.initfun_adaptive(fun, subinterval) f = lambda x: const * fun(x) g = lambda x: fun(x) * const ff = const * bndfun gg = bndfun * const tol = 4e1 * eps * abs(const) self.assertLessEqual(infnorm(f(xx)-ff(xx)), tol) self.assertLessEqual(infnorm(g(xx)-gg(xx)), tol)
def test_truediv_constant(self): xx = self.xx for (fun, funlen, hasRoots) in testfunctions: for const in (-1, 1, 10, -1e5): tol = eps*abs(const) techfun = Chebtech2.initfun_fixedlen(fun, funlen) g = lambda x: fun(x) / const gg = techfun / const self.assertLessEqual(infnorm(g(xx)-gg(xx)), 2*gg.size*tol) # don't do the following test for functions with roots if not hasRoots: f = lambda x: const / fun(x) ff = const / techfun self.assertLessEqual(infnorm(f(xx)-ff(xx)), 3*ff.size*tol)
def test__call__general_evaluation(self): f = lambda x: sin(4*x) + exp(cos(14*x)) - 1.4 npts = 50000 dom1 = [-1,1] dom2 = [-1,0,1] dom3 = [-2,-0.3,1.2] ff1 = Chebfun.initfun_adaptive(f, dom1) ff2 = Chebfun.initfun_adaptive(f, dom2) ff3 = Chebfun.initfun_adaptive(f, dom3) x1 = np.linspace(dom1[0], dom1[-1], npts) x2 = np.linspace(dom2[0], dom2[-1], npts) x3 = np.linspace(dom3[0], dom3[-1], npts) self.assertLessEqual(infnorm(f(x1)-ff1(x1)), 5e1*eps) self.assertLessEqual(infnorm(f(x2)-ff2(x2)), 2e1*eps) self.assertLessEqual(infnorm(f(x3)-ff3(x3)), 5e1*eps)
def test_truediv_constant(self): subinterval = Interval(-.5,.9) xx = subinterval(self.yy) for (fun, _, hasRoots) in testfunctions: for const in (-1, 1, 10, -1e5): hscl = abs(subinterval).max() tol = hscl * eps * abs(const) bndfun = Bndfun.initfun_adaptive(fun, subinterval) g = lambda x: fun(x) / const gg = bndfun / const self.assertLessEqual(infnorm(g(xx)-gg(xx)), 3*gg.size*tol) # don't do the following test for functions with roots if not hasRoots: f = lambda x: const / fun(x) ff = const / bndfun self.assertLessEqual(infnorm(f(xx)-ff(xx)), 2*ff.size*tol)
def test__mul__rmul__constant(self): for (f, _, _) in testfunctions: for c in (-1, 1, 10, -1e5): for dom, _ in chebfun_testdomains: a,b = dom xx = np.linspace(a, b, 1001) ff = Chebfun.initfun_adaptive(f, np.linspace(a, b, 11)) g = lambda x: c * f(x) gg1 = c * ff gg2 = ff * c vscl = ff.vscale hscl = ff.hscale lscl = max([fun.size for fun in ff]) tol = 2*abs(c)*vscl*hscl*lscl*eps self.assertLessEqual(infnorm(g(xx)-gg1(xx)), tol) self.assertLessEqual(infnorm(g(xx)-gg2(xx)), tol)
def test_cumsum(self): xx = np.linspace(-5,5,10000) for f in [self.f1, self.f2, self.f3, self.f4]: a, b = f.support x = xx[(xx>a)&(xx<b)] fa = self.If(a) self.assertLessEqual(infnorm(f.cumsum()(x)-self.If(x)+fa), 3*eps)
def test_onefun_construction(self): coeffs = np.random.rand(10) subinterval = Interval() onefun = Chebtech2(coeffs) f = Bndfun(onefun, subinterval) self.assertIsInstance(f, Bndfun) self.assertLess(infnorm(f.coeffs-coeffs), eps)
def test_minimum_multipiece(self): x = chebfun('x', np.linspace(-2,3,11)) y = chebfun(2, x.domain) g = (x**y).minimum(1.5) t = np.linspace(-2,3,2001) f = lambda x: np.minimum(x**2,1.5) self.assertLessEqual(infnorm(f(t)-g(t)), 1e1*eps)
def test_copy(self): ff = self.ff gg = self.ff.copy() self.assertEquals(ff, ff) self.assertEquals(gg, gg) self.assertNotEquals(ff, gg) self.assertEquals(infnorm(ff.coeffs-gg.coeffs), 0)
def test_simplify(self): interval = Interval(-2,1) ff = Bndfun.initfun_fixedlen(self.f, interval, 1000) gg = ff.simplify() self.assertEqual(gg.size, standard_chop(ff.onefun.coeffs)) self.assertEqual(infnorm(ff.coeffs[:gg.size]-gg.coeffs), 0) self.assertEqual(ff.interval, gg.interval)
def tester(self): vscl = max([ff.vscale, gg.vscale]) lscl = max([ff.size, gg.size]) self.assertLessEqual(infnorm(fg(self.xx)-FG(self.xx)), 3*vscl*lscl*eps) if binop is operator.mul: # check simplify is not being called in __mul__ self.assertEqual(fg.size, ff.size+gg.size-1)
def tester(self): vscl = max([ff.vscale, gg.vscale]) hscl = max([ff.hscale, gg.hscale]) lscl = max([fun.size for fun in np.append(ff.funs, gg.funs)]) self.assertEqual(ff.funs.size, n) self.assertEqual(gg.funs.size, m) self.assertEqual(fg.funs.size, n+m-1) self.assertLessEqual(infnorm(fg(xx)-FG(xx)), vscl*hscl*lscl*tol)
def test_identity_construction(self): for (a,b) in [(-1,1), (-10,-2), (-2.3, 1.24), (20,2000)]: itvl = Interval(a,b) ff = Bndfun.initidentity(itvl) self.assertEquals(ff.size, 2) xx = np.linspace(a,b,1001) tol = eps * abs(itvl).max() self.assertLessEqual(infnorm(ff(xx)-xx), tol)
def test_rpow_const(self): xx = self.xx for (fun, funlen) in [(np.sin, 15), (np.exp,15)]: for c in (1, 2): techfun = Chebtech2.initfun_fixedlen(fun, funlen) g = lambda x: c ** fun(x) gg = c ** techfun tol = 2e1 * eps * abs(c) self.assertLessEqual(infnorm(g(xx)-gg(xx)), tol)
def test_pow_const(self): xx = self.xx for (fun, funlen) in [(np.sin, 15), (np.exp,15)]: for c in (1, 2): techfun = Chebtech2.initfun_fixedlen(fun, funlen) f = lambda x: fun(x) ** c ff = techfun ** c tol = 2e1 * eps * abs(c) self.assertLessEqual(infnorm(f(xx)-ff(xx)), tol)
def test__break_2(self): altdom = Domain([-2,3]) newdom = self.f1.domain.union(altdom) f1_new = self.f1._break(newdom) self.assertEqual(f1_new.domain, newdom) self.assertNotEqual(f1_new.domain, altdom) xx = np.linspace(-2,3,1000) error = infnorm(self.f1(xx)-f1_new(xx)) self.assertLessEqual(error, 3*eps)
def test_rpow_const(self): subinterval = Interval(-.5,.9) xx = subinterval(self.yy) for func in (np.sin, np.exp, np.cos): for c in (1, 2): f = lambda x: c ** func(x) ff = c ** Bndfun.initfun_adaptive(func, subinterval) tol = 1e1 * eps * abs(c) self.assertLessEqual(infnorm(f(xx)-ff(xx)), tol)
def test__break_3(self): altdom = Domain(np.linspace(-2,3,1000)) newdom = self.f2.domain.union(altdom) f2_new = self.f2._break(newdom) self.assertEqual(f2_new.domain, newdom) self.assertNotEqual(f2_new.domain, altdom) self.assertNotEqual(f2_new.domain, self.f2.domain) xx = np.linspace(-2,3,1000) error = infnorm(self.f2(xx)-f2_new(xx)) self.assertLessEqual(error, 3*eps)
def test_coeffmult(self): f, g = self.f, self.g fn, gn = self.fn, self.gn hn = fn + gn - 1 h = lambda x: self.f(x) * self.g(x) fc = Chebtech2.initfun(f, fn).prolong(hn).coeffs gc = Chebtech2.initfun(g, gn).prolong(hn).coeffs hc = coeffmult(fc, gc) HC = Chebtech2.initfun(h, hn).coeffs self.assertLessEqual( infnorm(hc-HC), 2e1*eps)
def test_initidentity(self): _doms = ( np.linspace(-1,1,2), np.linspace(-1,1,11), np.linspace(-10,17,351), np.linspace(-9.3,-3.2,22), np.linspace(2.5,144.3,2112), ) for _dom in _doms: ff = Chebfun.initidentity(_dom) a, b = ff.support xx = np.linspace(a, b, 1001) tol = eps * ff.hscale self.assertLessEqual(infnorm(ff(xx)-xx), tol) # test the default case ff = Chebfun.initidentity() a, b = ff.support xx = np.linspace(a, b, 1001) tol = eps * ff.hscale self.assertLessEqual(infnorm(ff(xx)-xx), tol)
def test_initvalues(self): # test n = 0 case separately vals = np.random.rand(0) fun = Chebtech2.initvalues(vals) cfs = Chebtech2._vals2coeffs(vals) self.assertTrue(fun.coeffs.size==cfs.size==0) # now test the other cases for n in range(1,10): vals = np.random.rand(n) fun = Chebtech2.initvalues(vals) cfs = Chebtech2._vals2coeffs(vals) self.assertEqual(infnorm(fun.coeffs-cfs), 0.)
def test_simplify(self): gg = self.ff.simplify() # check that simplify is calling standard_chop underneath self.assertEqual(gg.size, standard_chop(self.ff.coeffs)) self.assertEqual(infnorm(self.ff.coeffs[:gg.size]-gg.coeffs), 0) # check we are returned a copy of self's coeffcients by changing # one entry of gg fcfs = self.ff.coeffs gcfs = gg.coeffs self.assertEqual((fcfs[:gg.size]-gcfs).sum(),0) gg.coeffs[0] = 1 self.assertNotEqual((fcfs[:gg.size]-gcfs).sum(),0)
def test_x_property(self): _doms = ( np.linspace(-1,1,2), np.linspace(-1,1,11), np.linspace(-9.3,-3.2,22), ) for _dom in _doms: f = Chebfun.initfun_fixedlen(sin, 1000, _dom) x = f.x a, b = x.support pts = np.linspace(a, b, 1001) tol = eps * f.hscale self.assertLessEqual(infnorm(x(pts)-pts), tol)
def test_truediv_constant(self): for (f, _, hasRoots) in testfunctions: for c in (-1, 1, 10, -1e5): for dom, _ in chebfun_testdomains: a,b = dom xx = np.linspace(a, b, 1001) ff = Chebfun.initfun_adaptive(f, np.linspace(a, b, 11)) g = lambda x: f(x) / c gg = ff / c vscl = gg.vscale hscl = gg.hscale lscl = max([fun.size for fun in gg]) tol = 2*abs(c)*vscl*hscl*lscl*eps self.assertLessEqual(infnorm(g(xx)-gg(xx)), tol) # don't do the following test for functions with roots if not hasRoots: h = lambda x: c / f(x) hh = c / ff vscl = hh.vscale hscl = hh.hscale lscl = max([fun.size for fun in hh]) tol = 2*abs(c)*vscl*hscl*lscl*eps self.assertLessEqual(infnorm(h(xx)-hh(xx)), tol)
def test_rpow_constant(self): for ((_,_), (f, _)) in powtestfuns: for c in (1, 2, 3): for dom, _ in powtestdomains: a,b = dom xx = np.linspace(a, b, 1001) ff = Chebfun.initfun_adaptive(f, np.linspace(a, b, 11)) g = lambda x: c ** f(x) gg = c ** ff vscl = gg.vscale hscl = gg.hscale lscl = max([fun.size for fun in gg]) tol = 2*abs(c)*vscl*hscl*lscl*eps self.assertLessEqual(infnorm(g(xx)-gg(xx)), tol)
def test_restrict_(self): # test a variety of domains with breaks doms = [(-4,4), (-4,0,4), (-2,-1, 0.3, 1, 2.5)] for dom in doms: ff = Chebfun.initfun_fixedlen(cos, 25, domain=dom) # define some arbitrary subdomains yy = np.linspace(dom[0], dom[-1], 11) subdoms = [yy, yy[2:7], yy[::2]] for subdom in subdoms: xx = np.linspace(subdom[0], subdom[-1], 1001) gg = ff._restrict(subdom) vscl = ff.vscale hscl = ff.hscale lscl = max([fun.size for fun in ff]) tol = vscl*hscl*lscl*eps # sample the restricted function and comapre with original self.assertLessEqual(infnorm(ff(xx)-gg(xx)), tol) # check there are at least as many funs as subdom elements self.assertGreaterEqual(len(gg.funs), len(subdom)-1) for fun in gg: # chec each fun has length 25 self.assertEqual(fun.size, 25)
def test_coeffs2vals_size1(self): for k in np.arange(10): ak = np.array([k]) self.assertLessEqual(infnorm(_coeffs2vals(ak)-ak), eps)
def tester(self): vscl = ff.vscale hscl = ff.hscale lscl = max([fun.size for fun in ff]) self.assertEqual(ff.funs.size, gg.funs.size) self.assertLessEqual(infnorm(gg(xx)-GG(xx)), vscl*hscl*lscl*tol)
def test_vals2coeffs_size1(self): for k in np.arange(10): fk = np.array([k]) self.assertLessEqual(infnorm(_vals2coeffs(fk)-fk), eps)
def test_breakpoint_roots_1(self): rts = self.f2.roots() self.assertEqual(rts.size, 5) self.assertLessEqual(infnorm(rts - self.f2.breakpoints), eps)
def test_multiple_pieces(self): rts = self.f1.roots() self.assertEqual(rts.size, 80) self.assertLessEqual(infnorm(rts - np.arange(-9.875, 10, .25)), 10 * eps)
def test_restrict(self): i1 = Interval(-1, 1) gg = self.ff.restrict(i1) yy = np.linspace(-1, 1, 1000) self.assertLessEqual(infnorm(self.ff(yy) - gg(yy)), 1e2 * eps)
def tester(self): self.assertLessEqual(infnorm(gg(self.xx)-GG(self.xx)), 4e1*eps)
def test_maps(self): yy = -1 + 2 * np.random.rand(1000) interval = Interval(-2,3) vals = interval.invmap(interval(yy)) - yy self.assertLessEqual(infnorm(vals), eps)
def test_breakpoint_roots_2(self): rts = self.f3.roots() self.assertEqual(rts.size, 81) self.assertLessEqual(infnorm(rts-arange(-10,10.25,.25)), 1e1*eps)
def tester(self): absdiff = infnorm(ff.diff().coeffs - gg.coeffs) self.assertLessEqual(absdiff, tol)
def tester(self): absdiff = infnorm(ff.cumsum().coeffs - coeffs) self.assertLessEqual(absdiff, tol)
def test_coeff_construction(self): coeffs = np.random.rand(10) f = Chebtech2(coeffs) self.assertIsInstance(f, Chebtech2) self.assertLess(infnorm(f.coeffs-coeffs), eps)
def test_call_bary_vs_clenshaw(self): b = self.ff(self.xx, "clenshaw") c = self.ff(self.xx, "bary") self.assertLessEqual(infnorm(b - c), 2e2 * eps)
def tester(self): vscl = max([ff.vscale, gg.vscale]) lscl = max([ff.size, gg.size]) xx = subinterval(self.yy) self.assertLessEqual(infnorm(fg(xx) - FG(xx)), 6 * vscl * lscl * eps)
def tester(self): vscl = max([ff.vscale, gg.vscale]) hscl = max([ff.hscale, gg.hscale]) lscl = max([fun.size for fun in append(ff.funs, gg.funs)]) self.assertLessEqual(infnorm(fg(xx)-ffgg), vscl*hscl*lscl*tol)
def test_initidentity(self): x = Chebtech2.initidentity() s = -1 + 2*np.random.rand(10000) self.assertEqual(infnorm(s-x(s)), 0.)
def tester(self): xx = subinterval(self.yy) self.assertLessEqual(infnorm(gg(xx) - GG(xx)), 4e1 * eps)
def tester(self): xx = interval(self.yy) vscl = GG.vscale lscl = GG.size self.assertLessEqual(infnorm(gg(xx) - GG(xx)), vscl * lscl * tol)
def tester(self): self.assertLessEqual(infnorm(rts - roots), tol)
def tester(self): xx = interval(self.yy) vscl = GG.vscale lscl = sum([fun.size for fun in GG]) self.assertLessEqual(infnorm(gg(xx)-GG(xx)), vscl*lscl*tol)
def asserter(self): coeffs = np.random.rand(n) values = _coeffs2vals(coeffs) _coeffs_ = _vals2coeffs(values) self.assertLessEqual( infnorm(coeffs-_coeffs_), scaled_tol(n) )
def test_diff(self): xx = linspace(-5,5,10000) for f in [self.f1, self.f2, self.f3, self.f4]: a, b = f.support x = xx[(xx>a)&(xx<b)] self.assertLessEqual(infnorm(f.diff()(x)-self.df(x)), 2e3*eps)
def test_compute_breakdata_1(self): funs = array([self.fun0]) breaks = compute_breakdata(funs) x, y = list(breaks.keys()), list(breaks.values()) self.assertLessEqual(infnorm(x - array([-1, 0])), eps) self.assertLessEqual(infnorm(y - array([exp(-1), exp(0)])), 2 * eps)