Beispiel #1
0
    def test_log1p(self):
        import math
        from numpypy import log1p

        for v in [float('-nan'), float('-inf'), -2, float('nan')]:
            assert math.isnan(log1p(v))
        for v in [-1]:
            assert log1p(v) == float("-inf")
        assert log1p(float('inf')) == float('inf')
        assert (log1p([0, 1e-50, math.e - 1]) == [0, 1e-50, 1]).all()
Beispiel #2
0
 def test_logn(self):
     import math, cmath
     # log and log10 are tested in math (1:1 from rcomplex)
     from numpypy import log2, array, log1p
     inf = float('inf')
     ninf = -float('inf')
     nan = float('nan')
     cmpl = complex
     log_2 = math.log(2)
     a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.),
          cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.),
          cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.),
          cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.),
          cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.),
          cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf),
          cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf),
          cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan),
         ]
     for c,rel_err in (('complex128', 2e-15), ('complex64', 1e-7)):
         b = log2(array(a,dtype=c))
         for i in range(len(a)):
             try:
                 _res = cmath.log(a[i])
                 res = cmpl(_res.real / log_2, _res.imag / log_2)
             except OverflowError:
                 res = cmpl(inf, nan)
             except ValueError:
                 res = cmpl(ninf, math.atan2(a[i].imag, a[i].real) / log_2)
             msg = 'result of log2(%r(%r)) got %r expected %r\n ' % \
                         (c,a[i], b[i], res)
             # cast untranslated boxed results to float,
             # does no harm when translated
             t1 = float(res.real)
             t2 = float(b[i].real)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
             t1 = float(res.imag)
             t2 = float(b[i].imag)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
     for c,rel_err in (('complex128', 2e-15), ('complex64', 1e-7)):
         b = log1p(array(a,dtype=c))
         for i in range(len(a)):
             try:
                 #be careful, normal addition wipes out +-0j
                 res = cmath.log(cmpl(a[i].real+1, a[i].imag))
             except OverflowError:
                 res = cmpl(inf, nan)
             except ValueError:
                 res = cmpl(ninf, 0)
             msg = 'result of log1p(%r(%r)) got %r expected %r\n ' % \
                         (c,a[i], b[i], res)
             # cast untranslated boxed results to float,
             # does no harm when translated
             t1 = float(res.real)
             t2 = float(b[i].real)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
             t1 = float(res.imag)
             t2 = float(b[i].imag)
             self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)