def setUp(self): f = lambda x: sin(1 * x) + 5e-1 * cos(10 * x) + 5e-3 * sin(100 * x) u = lambda x: np.exp(2 * np.pi * 1j * x) subinterval = Interval(-6, 10) self.f0 = Bndfun.initfun_fixedlen(f, subinterval, 1000) self.f1 = Bndfun.initfun_adaptive(f, subinterval) self.f2 = Bndfun.initfun_adaptive(u, Interval(-1, 1))
def binaryOpTester(f, g, subinterval, binop): ff = Bndfun.initfun_adaptive(f, subinterval) gg = Bndfun.initfun_adaptive(g, subinterval) 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]) xx = subinterval(self.yy) self.assertLessEqual(infnorm(fg(xx)-FG(xx)), 2*vscl*lscl*eps) return tester
def setUp(self): f = lambda x: np.exp(x) self.fun0 = Bndfun.initfun_adaptive(f, Interval(-1, 0)) self.fun1 = Bndfun.initfun_adaptive(f, Interval(0, 1)) self.fun2 = Bndfun.initfun_adaptive(f, Interval(-.5, 0.5)) self.fun3 = Bndfun.initfun_adaptive(f, Interval(2, 2.5)) self.fun4 = Bndfun.initfun_adaptive(f, Interval(-3, -2)) self.funs_a = np.array([self.fun1, self.fun0, self.fun2]) self.funs_b = np.array([self.fun1, self.fun2]) self.funs_c = np.array([self.fun0, self.fun3]) self.funs_d = np.array([self.fun1, self.fun4])
def binaryOpTester(f, g, subinterval, binop): ff = Bndfun.initfun_adaptive(f, subinterval) gg = Bndfun.initfun_adaptive(g, subinterval) 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]) xx = subinterval(self.yy) self.assertLessEqual(infnorm(fg(xx)-FG(xx)), 6*vscl*lscl*eps) return tester
def setUp(self): f = lambda x: np.exp(x) self.fun0 = Bndfun.initfun_adaptive(f, Interval(-1,0)) self.fun1 = Bndfun.initfun_adaptive(f, Interval(0,1)) self.fun2 = Bndfun.initfun_adaptive(f, Interval(-.5,0.5)) self.fun3 = Bndfun.initfun_adaptive(f, Interval(2,2.5)) self.fun4 = Bndfun.initfun_adaptive(f, Interval(-3,-2)) self.funs_a = np.array([self.fun1, self.fun0, self.fun2]) self.funs_b = np.array([self.fun1, self.fun2]) self.funs_c = np.array([self.fun0, self.fun3]) self.funs_d = np.array([self.fun1, self.fun4])
def definiteIntegralTester(fun, interval, integral, tol): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(fun, subinterval) def tester(self): absdiff = abs(ff.sum()-integral) self.assertLessEqual(absdiff, tol) return tester
def definiteIntegralTester(fun, interval, vscale): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(fun, subinterval) def tester(self): absdiff = abs(ff.vscale-vscale) self.assertLessEqual(absdiff, .1*vscale) return tester
def adaptiveTester(fun, subinterval, funlen): ff = Bndfun.initfun_adaptive(fun, subinterval) def tester(self): self.assertEquals(ff.size, funlen) return tester
def rootsTester(f, interval, roots, tol): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(f, subinterval) rts = ff.roots() def tester(self): self.assertLessEqual(infnorm(rts-roots), 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 derivativeTester(fun, ifn, interval, tol): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(fun, subinterval) gg = Bndfun.initfun_fixedlen(ifn, subinterval, max(ff.size-1,1)) def tester(self): absdiff = infnorm(ff.diff().coeffs - gg.coeffs) self.assertLessEqual(absdiff, tol) return tester
def unaryOpTester(unaryop, f, subinterval): ff = Bndfun.initfun_adaptive(f, subinterval) gg = lambda x: unaryop(f(x)) GG = unaryop(ff) def tester(self): xx = subinterval(self.yy) self.assertLessEqual(infnorm(gg(xx)-GG(xx)), 4e1*eps) 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 test_pow_const(self): subinterval = Interval(-0.5, 0.9) xx = subinterval(self.yy) for func in (np.sin, np.exp, np.cos): for c in (1, 2): f = lambda x: func(x)**c ff = Bndfun.initfun_adaptive(func, subinterval)**c tol = 2e1 * eps * abs(c) self.assertLessEqual(infnorm(f(xx) - ff(xx)), tol)
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_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_truediv_empty(self): subinterval = Interval(-2,3) for (fun, _, _) in testfunctions: bndfun = Bndfun.initfun_adaptive(fun, subinterval) self.assertTrue(operator.truediv(self.emptyfun, bndfun).isempty) self.assertTrue(operator.truediv(self.emptyfun, bndfun).isempty) # __truediv__ self.assertTrue((self.emptyfun/bndfun).isempty) self.assertTrue((bndfun/self.emptyfun).isempty)
def test_truediv_empty(self): subinterval = Interval(-2, 3) for (fun, _, _) in testfunctions: bndfun = Bndfun.initfun_adaptive(fun, subinterval) self.assertTrue(operator.truediv(self.emptyfun, bndfun).isempty) self.assertTrue(operator.truediv(self.emptyfun, bndfun).isempty) # __truediv__ self.assertTrue((self.emptyfun / bndfun).isempty) self.assertTrue((bndfun / self.emptyfun).isempty)
def indefiniteIntegralTester(fun, ifn, interval, tol): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(fun, subinterval) gg = Bndfun.initfun_fixedlen(ifn, subinterval, ff.size+1) coeffs = gg.coeffs coeffs[0] = coeffs[0] - ifn(array([interval[0]])) def tester(self): absdiff = infnorm(ff.cumsum().coeffs - coeffs) self.assertLessEqual(absdiff, tol) return tester
def ufuncTester(ufunc, f, interval, tol): ff = Bndfun.initfun_adaptive(f, interval) gg = lambda x: ufunc(f(x)) GG = ufunc(ff) def tester(self): xx = interval(self.yy) vscl = GG.vscale lscl = GG.size self.assertLessEqual(infnorm(gg(xx)-GG(xx)), vscl*lscl*tol) return tester
def indefiniteIntegralTester(fun, ifn, interval, tol): subinterval = Interval(*interval) ff = Bndfun.initfun_adaptive(fun, subinterval) gg = Bndfun.initfun_fixedlen(ifn, subinterval, ff.size+1) coeffs = gg.coeffs coeffs[0] = coeffs[0] - ifn(np.array([interval[0]])) def tester(self): absdiff = infnorm(ff.cumsum().coeffs - coeffs) self.assertLessEqual(absdiff, tol) return tester
def ufuncTester(ufunc, f, interval, tol): ff = Bndfun.initfun_adaptive(f, interval) gg = lambda x: ufunc(f(x)) GG = getattr(ff, ufunc.__name__)() def tester(self): xx = interval(self.yy) vscl = GG.vscale lscl = GG.size self.assertLessEqual(infnorm(gg(xx)-GG(xx)), vscl*lscl*tol) return tester
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__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): 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__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): 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_truediv_constant(self): subinterval = Interval(-.5,.9) xx = subinterval(self.yy) for (fun, funlen, 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 setUp(self): f = lambda x: np.exp(x) self.fun0 = Bndfun.initfun_adaptive(f, Interval(-1, 0)) self.fun1 = Bndfun.initfun_adaptive(f, Interval(0, 1))
def test__mul__rmul__empty(self): subinterval = Interval(-2, 3) for (fun, _, _) in testfunctions: chebtech = Bndfun.initfun_adaptive(fun, subinterval) self.assertTrue((self.emptyfun * chebtech).isempty) self.assertTrue((chebtech * self.emptyfun).isempty)
def setUp(self): self.z = Bndfun.initfun_adaptive(lambda x: np.exp(np.pi * 1j * x), Interval(-1, 1))
def setUp(self): f = lambda x: sin(1*x) + 5e-1*cos(10*x) + 5e-3*sin(100*x) subinterval = Interval(-6, 10) self.f0 = Bndfun.initfun_fixedlen(f, subinterval, 1000) self.f1 = Bndfun.initfun_adaptive(f, subinterval)
def setUp(self): f = lambda x: sin(1 * x) + 5e-1 * cos(10 * x) + 5e-3 * sin(100 * x) subinterval = Interval(-6, 10) self.f0 = Bndfun.initfun_fixedlen(f, subinterval, 1000) self.f1 = Bndfun.initfun_adaptive(f, subinterval)
def setUp(self): f = lambda x: np.exp(x) self.fun0 = Bndfun.initfun_adaptive(f, Interval(-1,0) ) self.fun1 = Bndfun.initfun_adaptive(f, Interval(0,1) )
def test__mul__rmul__empty(self): subinterval = Interval(-2,3) for (fun, _, _) in testfunctions: chebtech = Bndfun.initfun_adaptive(fun, subinterval) self.assertTrue((self.emptyfun*chebtech).isempty) self.assertTrue((chebtech*self.emptyfun).isempty)