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 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 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 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 test_const_construction(self): subinterval = Interval() ff = Bndfun.initconst(1., subinterval) self.assertEquals(ff.size, 1) self.assertTrue(ff.isconst) self.assertFalse(ff.isempty) self.assertRaises(ValueError, Bndfun.initconst, [1.], subinterval)
def test_const_construction(self): subinterval = Interval() ff = Bndfun.initconst(1.0, subinterval) self.assertEquals(ff.size, 1) self.assertTrue(ff.isconst) self.assertFalse(ff.isempty) self.assertRaises(ValueError, Bndfun.initconst, [1.0], subinterval)
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 fixedlenTester(fun, subinterval, n): ff = Bndfun.initfun_fixedlen(fun, subinterval, n) def tester(self): self.assertEquals(ff.size, n) return tester
def adaptiveTester(fun, subinterval, funlen): ff = Bndfun.initfun_adaptive(fun, subinterval) def tester(self): self.assertEquals(ff.size, funlen) 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 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 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 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 pwc(domain=[-1,0,1], values=[0,1]): """Initialise a piecewise-constant Chebfun""" funs = [] intervals = [x for x in Domain(domain).intervals] for interval, value in zip(intervals, values): funs.append(Bndfun.initconst(value, interval)) return Chebfun(funs)
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 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
class Calculus(unittest.TestCase): """Unit-tests for Bndfun calculus operations""" def setUp(self): self.emptyfun = Bndfun(Chebtech2.initempty(), Interval()) self.yy = np.linspace(-1,1,2000) # self.constfun = Bndfun(Chebtech2.initconst(1.), subinterval) # tests for the correct results in the empty cases def test_sum_empty(self): self.assertEqual(self.emptyfun.sum(), 0) def test_cumsum_empty(self): self.assertTrue(self.emptyfun.cumsum().isempty) def test_diff_empty(self): self.assertTrue(self.emptyfun.diff().isempty)
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_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)
class Calculus(unittest.TestCase): """Unit-tests for Bndfun calculus operations""" def setUp(self): self.emptyfun = Bndfun(Chebtech2.initempty(), Interval()) self.yy = np.linspace(-1, 1, 2000) # self.constfun = Bndfun(Chebtech2.initconst(1.), subinterval) # tests for the correct results in the empty cases def test_sum_empty(self): self.assertEqual(self.emptyfun.sum(), 0) def test_cumsum_empty(self): self.assertTrue(self.emptyfun.cumsum().isempty) def test_diff_empty(self): self.assertTrue(self.emptyfun.diff().isempty)
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_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 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 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 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 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 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 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 test_translate(self): c = -1 shifted_interval = self.ff.interval + c gg = self.ff.translate(c) hh = Bndfun.initfun_fixedlen(lambda x: self.ff(x - c), shifted_interval, gg.size) yk = shifted_interval(np.linspace(-1, 1, 100)) self.assertEqual(gg.interval, hh.interval) self.assertLessEqual(infnorm(gg.coeffs - hh.coeffs), 2e1 * eps) self.assertLessEqual(infnorm(gg(yk) - hh(yk)), 1e2 * eps)
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 initfun_fixedlen(cls, f, n, domain=None): nn = np.array(n) if nn.size < 2: funs = generate_funs(domain, Bndfun.initfun_fixedlen, { 'f': f, 'n': n }) else: domain = Domain(domain if domain is not None else prefs.domain) if not nn.size == domain.size - 1: raise BadFunLengthArgument funs = [] for interval, length in zip(domain.intervals, nn): funs.append(Bndfun.initfun_fixedlen(f, interval, length)) return cls(funs)
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 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 initfun_fixedlen(cls, f, n, domain=DefaultPrefs.domain): domain = np.array(domain) nn = np.array(n) if nn.size == 1: nn = nn * np.ones(domain.size-1) elif nn.size > 1: if nn.size != domain.size - 1: raise BadFunLengthArgument if domain.size < 2: raise BadDomainArgument funs = np.array([]) intervals = zip(domain[:-1], domain[1:]) for interval, length in zip(intervals, nn): interval = Interval(*interval) fun = Bndfun.initfun_fixedlen(f, interval, length) funs = np.append(funs, fun) return cls(funs)
def initfun_fixedlen(cls, f, n, domain=DefaultPrefs.domain): domain = np.array(domain) nn = np.array(n) if nn.size == 1: nn = nn * np.ones(domain.size - 1) elif nn.size > 1: if nn.size != domain.size - 1: raise BadFunLengthArgument if domain.size < 2: raise BadDomainArgument funs = np.array([]) intervals = zip(domain[:-1], domain[1:]) for interval, length in zip(intervals, nn): interval = Interval(*interval) fun = Bndfun.initfun_fixedlen(f, interval, length) funs = np.append(funs, fun) return cls(funs)
def test_empty(self): ff = Bndfun.initempty() self.assertEquals(ff.roots().size, 0)
def setUp(self): self.yy = np.linspace(-1,1,1000) self.emptyfun = Bndfun.initempty()
def setUp(self): self.yy = np.linspace(-1, 1, 1000) self.emptyfun = Bndfun.initempty()
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 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 test_empty_construction(self): ff = Bndfun.initempty() self.assertEquals(ff.size, 0) self.assertFalse(ff.isconst) self.assertTrue(ff.isempty) self.assertRaises(TypeError, Bndfun.initempty, [1.])
def setUp(self): self.emptyfun = Bndfun(Chebtech2.initempty(), Interval()) self.yy = np.linspace(-1,1,2000)
def setUp(self): self.yy = -1 + 2 * rand(1000) self.emptyfun = Bndfun.initempty()
def setUp(self): self.emptyfun = Bndfun(Chebtech2.initempty(), Interval()) self.yy = np.linspace(-1, 1, 2000)
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 test_const(self): ff = Bndfun.initconst(0., Interval(-2, 3)) gg = Bndfun.initconst(2., Interval(-2, 3)) self.assertEquals(ff.roots().size, 0) self.assertEquals(gg.roots().size, 0)
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_const(self): ff = Bndfun.initconst(0., Interval(-2,3)) gg = Bndfun.initconst(2., Interval(-2,3)) self.assertEquals(ff.roots().size, 0) self.assertEquals(gg.roots().size, 0)