示例#1
0
    def testBasics(self):

        self.test('clips', clips('test/testBasics.py', limit=12),
                  'test/t....ics.py')
        self.test('halfs2', halfs2('test/testBasics.py'),
                  "('test/test', 'Basics.py')")

        for f, x, y, s in (
            (0, True, False, True),
            (0.0, True, False, True),
            (1, True, False, True),
            (1.0, True, False, True),
            (1e300, True, True, True),
            (-1e300, True, True, True),
            (1e1234, False, False, True),  # == INF
            (INF, False, False, True),
            (NAN, False, False, True),
            (NEG0, True, False, True)):
            t = str(f)
            self.test('isfinite(%s)' % (t, ), isfinite(f), x)
            self.test('isint(%s)' % (t, ), isint(f, both=True), x)
            self.test('isint(%s+0.5)' % (t, ), isint(f + 0.5, both=True), y)
            self.test('isscalar(%s)' % (t, ), isscalar(f), s)

        self.test('isneg0(NEG0)', isneg0(NEG0), True)
        self.test('isneg0(0.0)', isneg0(0.0), False)
        self.test('isneg0(INF)', isneg0(INF), False)
        self.test('isneg0(NAN)', isneg0(NAN), False)

        self.test('type(%s)' % ('C.r_o', ),
                  type(C.r_o).__name__, property_RO.__name__)
        c = C()
        self.test('type(%s)' % ('c.r_o', ), type(c.r_o), bool)
        self.test('c.r_o', c.r_o, True)
        try:
            c.r_o = False
            self.test('c.r_o = False', c.r_o, AttributeError.__name__)
        except AttributeError as x:
            self.test('c.r_o = False', str(x), str(x))
        except Exception as x:
            self.test('c.r_o = False', repr(x), AttributeError.__name__)

        a, b = splice(range(10))  # PYCHOK False
        self.test('splice', (a, b),
                  map1(type(a), (0, 2, 4, 6, 8), (1, 3, 5, 7, 9)))
        a, b, c = splice(range(10), n=3)  # PYCHOK False
        self.test('splice', (a, b, c),
                  map1(type(a), (0, 3, 6, 9), (1, 4, 7), (2, 5, 8)))
        a, b, c = splice(range(10), n=3, fill=-1)  # PYCHOK False
        self.test('splice', (a, b, c),
                  map1(type(a), (0, 3, 6, 9), (1, 4, 7, -1), (2, 5, 8, -1)))
        t = tuple(splice(range(12), n=5))  # PYCHOK False
        self.test(
            'splice', t,
            map1(type(t[0]), (0, 5, 10), (1, 6, 11), (2, 7), (3, 8), (4, 9)))
示例#2
0
    def testFmath(self):

        # see function _p2 in ellipsoidalVincenty.py
        for i in range(-16, 17):
            x = pow(1.0, i)

            p = fpolynomial(x, 16384, 4096, -768, 320, -175) / 16384.0
            a = x / 16384.0 * (4096 + x * (-768 + x * (320 - 175 * x))) + 1
            self.test('fpolynomialA', p, a)
            h = fhorner(x, 16384, 4096, -768, 320, -175) / 16384.0
            self.test('fhornerA', h, p)

            p = fpolynomial(x, 0, 256, -128, 74, -47) / 1024.0
            b = x / 1024.0 * (256 + x * (-128 + x * (74 - 47 * x)))
            self.test('fpolynomialB', p, b)
            h = fhorner(x, 0, 256, -128, 74, -47) / 1024.0
            self.test('fhornerB', h, p)

        # U{Neumaier<http://WikiPedia.org/wiki/Kahan_summation_algorithm>}
        t = 1, 1e101, 1, -1e101
        for _ in range(10):
            s = float(len(t) / 2)  # number of ones
            self.test('sum', sum(t), s, known=True)
            self.test('fsum', fsum(t), s)
            self.test('Fsum', Fsum().fsum(t), s)
            t += t

        # <http://code.ActiveState.com/recipes/393090>
        t = 1.00, 0.00500, 0.00000000000100
        s = 1.00500000000100
        self.test('sum', sum(t), s, known=True)
        self.test('fsum', fsum(t), s)
        self.test('Fsum', Fsum().fsum(t), s)

        # <http://GitHub.com/python/cpython/blob/master/Modules/mathmodule.c>
        t = 1e-16, 1, 1e16
        s = '1.0000000000000002e+16'  # half-even rounded
        self.test('fsum', fsum(t), s, fmt='%.16e')
        self.test('Fsum', Fsum().fsum(t), s, fmt='%.16e')

        # <http://GitHub.com/ActiveState/code/blob/master/recipes/Python/
        #       393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>
        for _ in range(100):
            t = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
            s = 0
            for _ in range(20):
                v = gauss(0, random())**7 - s
                s += v
                t.append(v)
            shuffle(t)
            s = fsum(t)
            self.test('sum', sum(t), s, known=True)
            self.test('fsum', s, s)
            n = len(t) // 2
            f = Fsum()
            f.fsum(t[:n])  # test ps
            self.test('Fsum', f.fsum(t[n:]), s)
            f = Fsum()
            f.fsum(t[n:])  # test ps
            self.test('Fsum', f.fsum(t[:n]), s)

        p = fpowers(2, 10)  # PYCHOK false!
        self.test('fpowers', len(p), 10)
        self.test('fpowers', p[0], 2)
        self.test('fpowers', p[9], 2**10)
        p = fpowers(2, 10, 4)  # PYCHOK false!
        self.test('fpowers', len(p), 4)
        self.test('fpowers', p[0], 2**4)
        self.test('fpowers', p[3], 2**10)
        p = fpowers(2, 10, 3)  # PYCHOK false!
        self.test('fpowers', len(p), 4)
        self.test('fpowers', p[0], 2**3)
        self.test('fpowers', p[3], 2**9)

        self.test('isfinite(0)', isfinite(0), 'True')
        self.test('isfinite(1e300)', isfinite(1e300), 'True')
        self.test('isfinite(-1e300)', isfinite(-1e300), 'True')
        self.test('isfinite(1e1234)', isfinite(1e1234), 'False')
        self.test('isfinite(INF)', isfinite(INF), 'False')
        self.test('isfinite(NAN)', isfinite(NAN), 'False')
        self.test('isfinite(NEG0)', isfinite(NEG0), 'True')

        self.test('isneg0(NEG0)', isneg0(NEG0), True)
        self.test('isneg0(0.0)', isneg0(0.0), False)
        self.test('isneg0(NAN)', isneg0(NAN), False)

        for _, E in sorted(Ellipsoids.items()):
            Ah = E.a / (1 + E.n) * fhorner(E.n**2, 1., 1. / 4, 1. / 64,
                                           1. / 256, 25. / 16384)
            self.test(E.name, Ah, E.A, fmt='%.8f')
            Ah = E.a / (1 + E.n) * (fhorner(E.n**2, 16384, 4096, 256, 64, 25) /
                                    16384)
            self.test(E.name, Ah, E.A, fmt='%.8f')

            Ah = E.a / (1 + E.n) * fhorner(E.n**2, 1., 1. / 4, 1. / 64,
                                           1. / 256, 25. / 16384, 49. / 65536)
            self.test(E.name, Ah, E.A, fmt='%.10f')
            Ah = E.a / (1 + E.n) * (
                fhorner(E.n**2, 65536, 16384, 1024, 256, 100, 49) / 65536)
            self.test(E.name, Ah, E.A, fmt='%.10f')

        t = 1, 1e101, 1, -1e101
        for _ in range(10):
            a = Fsum(*t)
            b = a.fcopy()
            c = a + b
            self.test('FSum+', c.fsum(), a.fsum() + b.fsum())
            c -= a
            self.test('FSum-', c.fsum(), b.fsum())
            c -= b
            self.test('FSum-', c.fsum(), 0.0)
            b = a * 2
            a += a
            self.test('FSum*', a.fsum(), b.fsum())
            t += t