def derivativeTester(fun, der, n, tol): ff = Chebtech2.initfun_fixedlen(fun, n) gg = Chebtech2.initfun_fixedlen(der, max(n-1,1)) def tester(self): absdiff = infnorm(ff.diff().coeffs - gg.coeffs) self.assertLessEqual(absdiff, tol) return tester
def setUp(self): f = lambda x: sin(30*x) subinterval = Interval(-2,3) self.f = f self.ff = Bndfun.initfun_adaptive(f, subinterval) self.xx = subinterval(np.linspace(-1,1,100)) self.emptyfun = Bndfun(Chebtech2.initempty(), subinterval) self.constfun = Bndfun(Chebtech2.initconst(1.), subinterval)
def setUp(self): npts = 15 self.xk = Chebtech2._chebpts(npts) self.vk = Chebtech2._barywts(npts) self.fk = np.random.rand(npts) self.ak = np.random.rand(11) self.xx = -1 + 2*np.random.rand(9) self.pts = -1 + 2*np.random.rand(1001)
def indefiniteIntegralTester(fun, dfn, n, tol): ff = Chebtech2.initfun_fixedlen(fun, n) gg = Chebtech2.initfun_fixedlen(dfn, n+1) coeffs = gg.coeffs coeffs[0] = coeffs[0] - dfn(np.array([-1])) def tester(self): absdiff = infnorm(ff.cumsum().coeffs - coeffs) self.assertLessEqual(absdiff, tol) return tester
def derivativeTester(fun, der, n, tol): ff = Chebtech2.initfun_fixedlen(fun, n) gg = Chebtech2.initfun_fixedlen(der, max(n - 1, 1)) def tester(self): absdiff = infnorm(ff.diff().coeffs - gg.coeffs) self.assertLessEqual(absdiff, tol) return tester
def test_size(self): cfs = np.random.rand(10) subinterval = Interval() b0 = Bndfun(Chebtech2(np.array([])), subinterval) b1 = Bndfun(Chebtech2(np.array([1.0])), subinterval) b2 = Bndfun(Chebtech2(cfs), subinterval) self.assertEquals(b0.size, 0) self.assertEquals(b1.size, 1) self.assertEquals(b2.size, cfs.size)
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_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_initvalues(self): # test n = 0 case separately vals = 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 = rand(n) fun = Chebtech2.initvalues(vals) cfs = Chebtech2._vals2coeffs(vals) self.assertEqual(infnorm(fun.coeffs - cfs), 0.)
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 binaryOpTester(f, g, binop, nf, ng): ff = Chebtech2.initfun_fixedlen(f, nf) gg = Chebtech2.initfun_fixedlen(g, ng) FG = lambda x: binop(f(x),g(x)) fg = binop(ff, gg) 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) return tester
def adaptiveTester(fun, funlen): ff = Chebtech2.initfun_adaptive(fun) def tester(self): self.assertEquals(ff.size, funlen) return tester
def fixedlenTester(fun, n): ff = Chebtech2.initfun_fixedlen(fun, n) def tester(self): self.assertEquals(ff.size, n) return tester
def test__add__negself(self): xx = self.xx for (fun, funlen, _) in testfunctions: chebtech = Chebtech2.initfun_fixedlen(fun, funlen) chebzero = chebtech - chebtech self.assertTrue(chebzero.isconst) self.assertEqual(infnorm(chebzero(xx)), 0)
def unaryOpTester(unaryop, f, nf): ff = Chebtech2.initfun_fixedlen(f, nf) gg = lambda x: unaryop(f(x)) GG = unaryop(ff) def tester(self): self.assertLessEqual(infnorm(gg(self.xx)-GG(self.xx)), 4e1*eps) return tester
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_truediv_empty(self): for (fun, funlen, _) in testfunctions: chebtech = Chebtech2.initfun_fixedlen(fun, funlen) self.assertTrue(operator.truediv(self.emptyfun,chebtech).isempty) self.assertTrue(operator.truediv(chebtech,self.emptyfun).isempty) # __truediv__ self.assertTrue((self.emptyfun/chebtech).isempty) self.assertTrue((chebtech/self.emptyfun).isempty)
def rootsTester(f, roots, tol): ff = Chebtech2.initfun_adaptive(f) rts = ff.roots() def tester(self): self.assertLessEqual(infnorm(rts - roots), tol) return tester
def test_truediv_empty(self): for (fun, funlen, _) in testfunctions: chebtech = Chebtech2.initfun_fixedlen(fun, funlen) self.assertTrue(truediv(self.emptyfun, chebtech).isempty) self.assertTrue(truediv(chebtech, self.emptyfun).isempty) # __truediv__ self.assertTrue((self.emptyfun / chebtech).isempty) self.assertTrue((chebtech / self.emptyfun).isempty)
def definiteIntegralTester(fun, n, vscale): ff = Chebtech2.initfun_fixedlen(fun, n) def tester(self): absdiff = abs(ff.vscale - vscale) self.assertLessEqual(absdiff, .1 * vscale) return tester
def definiteIntegralTester(fun, n, integral, tol): ff = Chebtech2.initfun_fixedlen(fun, n) def tester(self): absdiff = abs(ff.sum() - integral) self.assertLessEqual(absdiff, tol) return tester
def adaptiveTester(fun, funlen): ff = Chebtech2.initfun_adaptive(fun) def tester(self): self.assertLessEqual(ff.size - funlen, 2) self.assertGreater(ff.size - funlen, -1) return tester
def test_bary_clenshaw_consistency(self): coeffs = np.random.rand(3) evalpts = (0.5, np.array([]), np.array([.5]), np.array([.5, .6])) for n in range(len(coeffs)): ff = Chebtech2(coeffs[:n]) for xx in evalpts: fb = ff(xx, "bary") fc = ff(xx, "clenshaw") self.assertEquals(type(fb), type(fc))
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_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__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): 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 evalTester(method, fun, evalpts, chebpts): x = evalpts xk = chebpts fvals = fun(xk) if method is bary: vk = Chebtech2._barywts(fvals.size) a = bary(x, fvals, xk, vk) tol_multiplier = 1e0 elif method is clenshaw: ak = Chebtech2._vals2coeffs(fvals) a = clenshaw(x, ak) tol_multiplier = 2e1 b = fun(evalpts) n = evalpts.size tol = tol_multiplier * scaled_tol(n) return infNormLessThanTol(a, b, 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): 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_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_initidentity(self): x = Chebtech2.initidentity() s = -1 + 2*np.random.rand(10000) self.assertEqual(infnorm(s-x(s)), 0.)
def test_empty_construction(self): ff = Chebtech2.initempty() self.assertEquals(ff.size, 0) self.assertFalse(ff.isconst) self.assertTrue(ff.isempty) self.assertRaises(TypeError, Chebtech2.initempty, [1.])
def test_const_construction(self): ff = Chebtech2.initconst(1.) self.assertEquals(ff.size, 1) self.assertTrue(ff.isconst) self.assertFalse(ff.isempty) self.assertRaises(ValueError, Chebtech2.initconst, [1.])
def test__mul__rmul__empty(self): for (fun, funlen, _) in testfunctions: chebtech = Chebtech2.initfun_fixedlen(fun, funlen) self.assertTrue((self.emptyfun * chebtech).isempty) self.assertTrue((chebtech * self.emptyfun).isempty)
def rootsTester(f, roots, tol): ff = Chebtech2.initfun_adaptive(f) rts = ff.roots() def tester(self): self.assertLessEqual(infnorm(rts-roots), tol) return tester
def test_const(self): ff = Chebtech2.initconst(0.) gg = Chebtech2.initconst(2.) self.assertEquals(ff.roots().size, 0) self.assertEquals(gg.roots().size, 0)
def test__mul__rmul__empty(self): for (fun, funlen, _) in testfunctions: chebtech = Chebtech2.initfun_fixedlen(fun, funlen) self.assertTrue((self.emptyfun*chebtech).isempty) self.assertTrue((chebtech*self.emptyfun).isempty)
def test_clenshaw__float_output(self): ff = Chebtech2.initconst(1) gg = Chebtech2.initconst(1.) self.assertTrue(isinstance(ff(0, "clenshaw"), float)) self.assertTrue(isinstance(gg(0, "clenshaw"), float))
def test_chebpts_0(self): self.assertEquals(Chebtech2._chebpts(0).size, 0)
def setUp(self): self.xx = -1 + 2 * rand(1000) self.emptyfun = Chebtech2.initempty()
def setUp(self): self.emptyfun = Bndfun(Chebtech2.initempty(), Interval()) self.yy = np.linspace(-1,1,2000)
def setUp(self): self.xx = -1 + 2 * np.random.rand(1000) self.emptyfun = Chebtech2.initempty()
def test_empty(self): ff = Chebtech2.initempty() self.assertEquals(ff.roots().size, 0)
self.assertTrue(isinstance(gg(0, "clenshaw"), float)) # Check that we get consistent output from bary and clenshaw # TODO: Move these tests elsewhere? def test_bary_clenshaw_consistency(self): coeffs = np.random.rand(3) evalpts = (0.5, np.array([]), np.array([.5]), np.array([.5, .6])) for n in range(len(coeffs)): ff = Chebtech2(coeffs[:n]) for xx in evalpts: fb = ff(xx, "bary") fc = ff(xx, "clenshaw") self.assertEquals(type(fb), type(fc)) evalpts = [np.linspace(-1,1,n) for n in np.array([1e2, 1e3, 1e4, 1e5])] ptsarry = [Chebtech2._chebpts(n) for n in np.array([100, 200])] methods = [bary, clenshaw] def evalTester(method, fun, evalpts, chebpts): x = evalpts xk = chebpts fvals = fun(xk) if method is bary: vk = Chebtech2._barywts(fvals.size) a = bary(x, fvals, xk, vk) tol_multiplier = 1e0 elif method is clenshaw: ak = Chebtech2._vals2coeffs(fvals)
def definiteIntegralTester(fun, n, integral, tol): ff = Chebtech2.initfun_fixedlen(fun, n) def tester(self): absdiff = abs(ff.sum()-integral) self.assertLessEqual(absdiff, tol) return tester