Example #1
0
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
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
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
Example #5
0
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
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 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)
Example #9
0
 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.)
Example #10
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.)
Example #11
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
Example #12
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
Example #13
0
def adaptiveTester(fun, funlen):
    ff = Chebtech2.initfun_adaptive(fun)

    def tester(self):
        self.assertEquals(ff.size, funlen)

    return tester
Example #14
0
def fixedlenTester(fun, n):
    ff = Chebtech2.initfun_fixedlen(fun, n)

    def tester(self):
        self.assertEquals(ff.size, n)

    return tester
Example #15
0
 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)
Example #16
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
Example #17
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
Example #18
0
 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)
Example #19
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)
Example #20
0
def rootsTester(f, roots, tol):
    ff = Chebtech2.initfun_adaptive(f)
    rts = ff.roots()

    def tester(self):
        self.assertLessEqual(infnorm(rts - roots), tol)

    return tester
Example #21
0
 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)
Example #22
0
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
Example #23
0
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
Example #24
0
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
Example #25
0
 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))
Example #26
0
 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)
Example #27
0
 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)
Example #28
0
 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)
Example #29
0
 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)
Example #30
0
 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)
Example #31
0
 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)
Example #32
0
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)
Example #33
0
 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)
Example #34
0
 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)
Example #35
0
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)
Example #36
0
 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)
Example #37
0
 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)
Example #38
0
 def test_initidentity(self):
     x = Chebtech2.initidentity()
     s = -1 + 2*np.random.rand(10000)
     self.assertEqual(infnorm(s-x(s)), 0.)
Example #39
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.])
Example #40
0
 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.])
Example #41
0
 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.])
Example #42
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)
Example #43
0
def rootsTester(f, roots, tol):
    ff = Chebtech2.initfun_adaptive(f)
    rts = ff.roots()
    def tester(self):
        self.assertLessEqual(infnorm(rts-roots), tol)
    return tester
Example #44
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.])
Example #45
0
def adaptiveTester(fun, funlen):
    ff = Chebtech2.initfun_adaptive(fun)
    def tester(self):
        self.assertEquals(ff.size, funlen)
    return tester
Example #46
0
 def test_const(self):
     ff = Chebtech2.initconst(0.)
     gg = Chebtech2.initconst(2.)
     self.assertEquals(ff.roots().size, 0)
     self.assertEquals(gg.roots().size, 0)
Example #47
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)
Example #48
0
 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))
Example #49
0
 def test_chebpts_0(self):
     self.assertEquals(Chebtech2._chebpts(0).size, 0)
Example #50
0
 def setUp(self):
     self.xx = -1 + 2 * rand(1000)
     self.emptyfun = Chebtech2.initempty()
Example #51
0
 def test_const(self):
     ff = Chebtech2.initconst(0.)
     gg = Chebtech2.initconst(2.)
     self.assertEquals(ff.roots().size, 0)
     self.assertEquals(gg.roots().size, 0)
Example #52
0
 def test_chebpts_0(self):
     self.assertEquals(Chebtech2._chebpts(0).size, 0)
Example #53
0
 def setUp(self):
     self.emptyfun = Bndfun(Chebtech2.initempty(), Interval())
     self.yy = np.linspace(-1,1,2000)
Example #54
0
def fixedlenTester(fun, n):
    ff = Chebtech2.initfun_fixedlen(fun, n)
    def tester(self):
        self.assertEquals(ff.size, n)
    return tester
Example #55
0
 def setUp(self):
     self.xx = -1 + 2 * np.random.rand(1000)
     self.emptyfun = Chebtech2.initempty()
Example #56
0
 def test_empty(self):
     ff = Chebtech2.initempty()
     self.assertEquals(ff.roots().size, 0)
Example #57
0
 def test_empty(self):
     ff = Chebtech2.initempty()
     self.assertEquals(ff.roots().size, 0)
Example #58
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)
Example #59
0
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