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 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 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 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 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 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 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 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 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 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 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 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_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__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 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_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__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 setUp(self): f = lambda x: sin(3*x) + 5e-1*cos(30*x) self.f0 = Chebtech2.initfun_fixedlen(f, 100) self.f1 = Chebtech2.initfun_adaptive(f)
def setUp(self): f = lambda x: sin(3 * x) + 5e-1 * cos(30 * x) self.f0 = Chebtech2.initfun_fixedlen(f, 100) self.f1 = Chebtech2.initfun_adaptive(f)
def setUp(self): self.ff = Chebtech2.initfun_fixedlen(lambda x: sin(30 * x), 100) self.xx = -1 + 2 * rand(100)
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 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 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 setUp(self): f = lambda x: sin(3 * x) + 5e-1 * cos(30 * x) u = lambda x: np.exp(2 * np.pi * 1j * x) self.f0 = Chebtech2.initfun_fixedlen(f, 100) self.f1 = Chebtech2.initfun_adaptive(f) self.f2 = Chebtech2.initfun_adaptive(u)
def setUp(self): self.ff = Chebtech2.initfun_fixedlen(lambda x: np.sin(30*x), 100) self.xx = -1 + 2*np.random.rand(100)
def fixedlenTester(fun, n): ff = Chebtech2.initfun_fixedlen(fun, n) def tester(self): self.assertEquals(ff.size, n) return tester