Ejemplo n.º 1
0
    def test_density(self):
        # Check that the integral of the density equals 1.
        n = 100
        v = np.random.rand(n)
        a, b = histogram(v, density=True)
        area = np.sum(a * np.diff(b))
        assert_almost_equal(area, 1)

        # Check with non-constant bin widths
        v = np.arange(10)
        bins = [0, 1, 3, 6, 10]
        a, b = histogram(v, bins, density=True)
        assert_array_equal(a, .1)
        assert_equal(np.sum(a * np.diff(b)), 1)

        # Test that passing False works too
        a, b = histogram(v, bins, density=False)
        assert_array_equal(a, [1, 2, 3, 4])

        # Variale bin widths are especially useful to deal with
        # infinities.
        v = np.arange(10)
        bins = [0, 1, 3, 6, np.inf]
        a, b = histogram(v, bins, density=True)
        assert_array_equal(a, [.1, .1, .1, 0.])

        # Taken from a bug report from N. Becker on the numpy-discussion
        # mailing list Aug. 6, 2010.
        counts, dmy = np.histogram([1, 2, 3, 4], [0.5, 1.5, np.inf],
                                   density=True)
        assert_equal(counts, [.25, 0])
Ejemplo n.º 2
0
 def test_lagmulx(self):
     assert_equal(lag.lagmulx([0]), [0])
     assert_equal(lag.lagmulx([1]), [1, -1])
     for i in range(1, 5):
         ser = [0] * i + [1]
         tgt = [0] * (i - 1) + [-i, 2 * i + 1, -(i + 1)]
         assert_almost_equal(lag.lagmulx(ser), tgt)
Ejemplo n.º 3
0
    def test_hermeder(self):
        # check exceptions
        assert_raises(ValueError, herme.hermeder, [0], .5)
        assert_raises(ValueError, herme.hermeder, [0], -1)

        # check that zeroth derivative does nothing
        for i in range(5):
            tgt = [0] * i + [1]
            res = herme.hermeder(tgt, m=0)
            assert_equal(trim(res), trim(tgt))

        # check that derivation is the inverse of integration
        for i in range(5):
            for j in range(2, 5):
                tgt = [0] * i + [1]
                res = herme.hermeder(herme.hermeint(tgt, m=j), m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check derivation with scaling
        for i in range(5):
            for j in range(2, 5):
                tgt = [0] * i + [1]
                res = herme.hermeder(herme.hermeint(tgt, m=j, scl=2),
                                     m=j,
                                     scl=.5)
                assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 4
0
    def test_weights(self):
        v = np.random.rand(100)
        w = np.ones(100) * 5
        a, b = histogram(v)
        na, nb = histogram(v, density=True)
        wa, wb = histogram(v, weights=w)
        nwa, nwb = histogram(v, weights=w, density=True)
        assert_array_almost_equal(a * 5, wa)
        assert_array_almost_equal(na, nwa)

        # Check weights are properly applied.
        v = np.linspace(0, 10, 10)
        w = np.concatenate((np.zeros(5), np.ones(5)))
        wa, wb = histogram(v, bins=np.arange(11), weights=w)
        assert_array_almost_equal(wa, w)

        # Check with integer weights
        wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1])
        assert_array_equal(wa, [4, 5, 0, 1])
        wa, wb = histogram([1, 2, 2, 4],
                           bins=4,
                           weights=[4, 3, 2, 1],
                           density=True)
        assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4)

        # Check weights with non-uniform bin widths
        a, b = histogram(np.arange(9), [0, 1, 3, 6, 10],
                         weights=[2, 1, 1, 1, 1, 1, 1, 1, 1],
                         density=True)
        assert_almost_equal(a, [.2, .1, .1, .075])
Ejemplo n.º 5
0
 def test_result_values(self):
     tgt = [np.percentile(d, 28) for d in _rdat]
     res = np.nanpercentile(_ndat, 28, axis=1)
     assert_almost_equal(res, tgt)
     # Transpose the array to fit the output convention of numpy1.percentile
     tgt = np.transpose([np.percentile(d, (28, 98)) for d in _rdat])
     res = np.nanpercentile(_ndat, (28, 98), axis=1)
     assert_almost_equal(res, tgt)
Ejemplo n.º 6
0
 def test_out(self):
     mat = np.eye(3)
     for nf, rf in zip(self.nanfuncs, self.stdfuncs):
         resout = np.zeros(3)
         tgt = rf(mat, axis=1)
         res = nf(mat, axis=1, out=resout)
         assert_almost_equal(res, resout)
         assert_almost_equal(res, tgt)
Ejemplo n.º 7
0
 def test_ddof(self):
     nanfuncs = [np.nanvar, np.nanstd]
     stdfuncs = [np.var, np.std]
     for nf, rf in zip(nanfuncs, stdfuncs):
         for ddof in [0, 1]:
             tgt = [rf(d, ddof=ddof) for d in _rdat]
             res = nf(_ndat, axis=1, ddof=ddof)
             assert_almost_equal(res, tgt)
Ejemplo n.º 8
0
def assert_poly_almost_equal(p1, p2, msg=""):
    try:
        assert_(np.all(p1.domain == p2.domain))
        assert_(np.all(p1.window == p2.window))
        assert_almost_equal(p1.coef, p2.coef)
    except AssertionError:
        msg = "Result: %s\nTarget: %s", (p1, p2)
        raise AssertionError(msg)
Ejemplo n.º 9
0
 def test_result_values(self):
     for axis in (-2, -1, 0, 1, None):
         tgt = np.cumprod(_ndat_ones, axis=axis)
         res = np.nancumprod(_ndat, axis=axis)
         assert_almost_equal(res, tgt)
         tgt = np.cumsum(_ndat_zeros,axis=axis)
         res = np.nancumsum(_ndat, axis=axis)
         assert_almost_equal(res, tgt)
Ejemplo n.º 10
0
 def test_polyfromroots(self):
     res = poly.polyfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5):
         roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
         tgt = Tlist[i]
         res = poly.polyfromroots(roots) * 2**(i - 1)
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 11
0
def check_complex_value(f, x1, y1, x2, y2, exact=True):
    z1 = np.array([complex(x1, y1)])
    z2 = complex(x2, y2)
    with np.errstate(invalid='ignore'):
        if exact:
            assert_equal(f(z1), z2)
        else:
            assert_almost_equal(f(z1), z2)
Ejemplo n.º 12
0
 def test_chebfromroots(self):
     res = cheb.chebfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5):
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         tgt = [0]*i + [1]
         res = cheb.chebfromroots(roots)*2**(i-1)
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 13
0
def test_identity(Poly):
    d = Poly.domain + random((2, )) * .25
    w = Poly.window + random((2, )) * .25
    x = np.linspace(d[0], d[1], 11)
    p = Poly.identity(domain=d, window=w)
    assert_equal(p.domain, d)
    assert_equal(p.window, w)
    assert_almost_equal(p(x), x)
Ejemplo n.º 14
0
    def test_approximation(self):
        def powx(x, p):
            return x**p

        x = np.linspace(0, 2, 10)
        for deg in range(0, 10):
            for t in range(0, deg + 1):
                p = Chebyshev.interpolate(powx, deg, domain=[0, 2], args=(t, ))
                assert_almost_equal(p(x), powx(x, t), decimal=12)
Ejemplo n.º 15
0
 def test_out(self):
     mat = np.eye(3)
     for nf, rf in zip(self.nanfuncs, self.stdfuncs):
         resout = np.eye(3)
         for axis in (-2, -1, 0, 1):
             tgt = rf(mat, axis=axis)
             res = nf(mat, axis=axis, out=resout)
             assert_almost_equal(res, resout)
             assert_almost_equal(res, tgt)
Ejemplo n.º 16
0
def test_roots(Poly):
    d = Poly.domain * 1.25 + .25
    w = Poly.window
    tgt = np.linspace(d[0], d[1], 5)
    res = np.sort(Poly.fromroots(tgt, domain=d, window=w).roots())
    assert_almost_equal(res, tgt)
    # default domain and window
    res = np.sort(Poly.fromroots(tgt).roots())
    assert_almost_equal(res, tgt)
Ejemplo n.º 17
0
 def test_large_types(self):
     for t in [np.int32, np.int64, np.float32, np.float64, np.longdouble]:
         a = t(51)
         b = a**4
         msg = "error with %r: got %r" % (t, b)
         if np.issubdtype(t, np.integer):
             assert_(b == 6765201, msg)
         else:
             assert_almost_equal(b, 6765201, err_msg=msg)
Ejemplo n.º 18
0
def test_mapparms(Poly):
    # check with defaults. Should be identity.
    d = Poly.domain
    w = Poly.window
    p = Poly([1], domain=d, window=w)
    assert_almost_equal([0, 1], p.mapparms())
    #
    w = 2 * d + 1
    p = Poly([1], domain=d, window=w)
    assert_almost_equal([1, 2], p.mapparms())
Ejemplo n.º 19
0
def test_call(Poly):
    P = Polynomial
    d = Poly.domain
    x = np.linspace(d[0], d[1], 11)

    # Check defaults
    p = Poly.cast(P([1, 2, 3]))
    tgt = 1 + x * (2 + 3 * x)
    res = p(x)
    assert_almost_equal(res, tgt)
Ejemplo n.º 20
0
 def test_lagdiv(self):
     for i in range(5):
         for j in range(5):
             msg = "At i=%d, j=%d" % (i, j)
             ci = [0] * i + [1]
             cj = [0] * j + [1]
             tgt = lag.lagadd(ci, cj)
             quo, rem = lag.lagdiv(tgt, ci)
             res = lag.lagadd(lag.lagmul(quo, ci), rem)
             assert_almost_equal(trim(res), trim(tgt), err_msg=msg)
Ejemplo n.º 21
0
def test_trapz_matrix():
    # Test to make sure matrices give the same answer as ndarrays
    # 2018-04-29: moved here from core.tests.test_function_base.
    x = np.linspace(0, 5)
    y = x * x
    r = np.trapz(y, x)
    mx = np.matrix(x)
    my = np.matrix(y)
    mr = np.trapz(my, mx)
    assert_almost_equal(mr, r)
Ejemplo n.º 22
0
    def test_approximation(self):

        def powx(x, p):
            return x**p

        x = np.linspace(-1, 1, 10)
        for deg in range(0, 10):
            for p in range(0, deg + 1):
                c = cheb.chebinterpolate(powx, deg, (p,))
                assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12)
Ejemplo n.º 23
0
 def test_lower_align(self):
     # check data that is not aligned to element size
     # i.e doubles are aligned to 4 bytes on i386
     d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
     o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
     assert_almost_equal(d + d, d * 2)
     np.add(d, d, out=o)
     np.add(np.ones_like(d), d, out=o)
     np.add(d, np.ones_like(d), out=o)
     np.add(np.ones_like(d), d)
     np.add(d, np.ones_like(d))
Ejemplo n.º 24
0
    def test_lagder_axis(self):
        # check that axis keyword works
        c2d = np.random.random((3, 4))

        tgt = np.vstack([lag.lagder(c) for c in c2d.T]).T
        res = lag.lagder(c2d, axis=0)
        assert_almost_equal(res, tgt)

        tgt = np.vstack([lag.lagder(c) for c in c2d])
        res = lag.lagder(c2d, axis=1)
        assert_almost_equal(res, tgt)
Ejemplo n.º 25
0
    def test_getdomain(self):
        # test for real values
        x = [1, 10, 3, -1]
        tgt = [-1, 10]
        res = pu.getdomain(x)
        assert_almost_equal(res, tgt)

        # test for complex values
        x = [1 + 1j, 1 - 1j, 0, 2]
        tgt = [-1j, 2 + 1j]
        res = pu.getdomain(x)
        assert_almost_equal(res, tgt)
Ejemplo n.º 26
0
    def test_lagvander3d(self):
        # also tests lagval3d for non-square coefficient array
        x1, x2, x3 = self.x
        c = np.random.random((2, 3, 4))
        van = lag.lagvander3d(x1, x2, x3, [1, 2, 3])
        tgt = lag.lagval3d(x1, x2, x3, c)
        res = np.dot(van, c.flat)
        assert_almost_equal(res, tgt)

        # check shape
        van = lag.lagvander3d([x1], [x2], [x3], [1, 2, 3])
        assert_(van.shape == (1, 5, 24))
Ejemplo n.º 27
0
 def test_array(self):
     x = np.array([1, 1j,         2,  2.5+.37j, np.inf, np.nan])
     y = np.array([1, 1j, -0.5+1.5j, -0.5+1.5j,      2,      3])
     lx = list(range(len(x)))
     # Compute the values for complex type in python
     p_r = [complex(x[i]) ** complex(y[i]) for i in lx]
     # Substitute a result allowed by C99 standard
     p_r[4] = complex(np.inf, np.nan)
     # Do the same with numpy1 arrays
     n_r = x ** y
     for i in lx:
         assert_almost_equal(n_r[i], p_r[i], err_msg='Loop %d\n' % i)
Ejemplo n.º 28
0
    def test_chebvander2d(self):
        # also tests chebval2d for non-square coefficient array
        x1, x2, x3 = self.x
        c = np.random.random((2, 3))
        van = cheb.chebvander2d(x1, x2, [1, 2])
        tgt = cheb.chebval2d(x1, x2, c)
        res = np.dot(van, c.flat)
        assert_almost_equal(res, tgt)

        # check shape
        van = cheb.chebvander2d([x1], [x2], [1, 2])
        assert_(van.shape == (1, 5, 6))
Ejemplo n.º 29
0
 def test_lagmul(self):
     # check values of result
     for i in range(5):
         pol1 = [0] * i + [1]
         val1 = lag.lagval(self.x, pol1)
         for j in range(5):
             msg = "At i=%d, j=%d" % (i, j)
             pol2 = [0] * j + [1]
             val2 = lag.lagval(self.x, pol2)
             pol3 = lag.lagmul(pol1, pol2)
             val3 = lag.lagval(self.x, pol3)
             assert_(len(pol3) == i + j + 1, msg)
             assert_almost_equal(val3, val1 * val2, err_msg=msg)
Ejemplo n.º 30
0
    def test_laggrid3d(self):
        x1, x2, x3 = self.x
        y1, y2, y3 = self.y

        #test values
        tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
        res = lag.laggrid3d(x1, x2, x3, self.c3d)
        assert_almost_equal(res, tgt)

        #test shape
        z = np.ones((2, 3))
        res = lag.laggrid3d(z, z, z, self.c3d)
        assert_(res.shape == (2, 3) * 3)