Ejemplo n.º 1
0
 def test_dtype(self):
     y = linspace(0, 6, dtype='float32')
     assert_equal(y.dtype, dtype('float32'))
     y = linspace(0, 6, dtype='float64')
     assert_equal(y.dtype, dtype('float64'))
     y = linspace(0, 6, dtype='int32')
     assert_equal(y.dtype, dtype('int32'))
Ejemplo n.º 2
0
 def test_complex(self):
     lim1 = linspace(1 + 2j, 3 + 4j, 5)
     t1 = array([1.0 + 2.j, 1.5 + 2.5j, 2.0 + 3j, 2.5 + 3.5j, 3.0 + 4j])
     lim2 = linspace(1j, 10, 5)
     t2 = array(
         [0.0 + 1.j, 2.5 + 0.75j, 5.0 + 0.5j, 7.5 + 0.25j, 10.0 + 0j])
     assert_equal(lim1, t1)
     assert_equal(lim2, t2)
Ejemplo n.º 3
0
 def test_corner(self):
     y = list(linspace(0, 1, 1))
     assert_(y == [0.0], y)
     with suppress_warnings() as sup:
         sup.filter(DeprecationWarning,
                    ".*safely interpreted as an integer")
         y = list(linspace(0, 1, 2.5))
         assert_(y == [0.0, 1.0])
Ejemplo n.º 4
0
 def test_basic(self):
     y = linspace(0, 10)
     assert_(len(y) == 50)
     y = linspace(2, 10, num=100)
     assert_(y[-1] == 10)
     y = linspace(2, 10, endpoint=0)
     assert_(y[-1] < 10)
     assert_raises(ValueError, linspace, 0, 10, num=-1)
Ejemplo n.º 5
0
 def test_retstep(self):
     y = linspace(0, 1, 2, retstep=True)
     assert_(isinstance(y, tuple) and len(y) == 2)
     for num in (0, 1):
         for ept in (False, True):
             y = linspace(0, 1, num, endpoint=ept, retstep=True)
             assert_(
                 isinstance(y, tuple) and len(y) == 2 and len(y[0]) == num
                 and isnan(y[1]), 'num={0}, endpoint={1}'.format(num, ept))
Ejemplo n.º 6
0
 def test_subclass(self):
     a = array(0).view(PhysicalQuantity2)
     b = array(1).view(PhysicalQuantity2)
     ls = linspace(a, b)
     assert type(ls) is PhysicalQuantity2
     assert_equal(ls, linspace(0.0, 1.0))
     ls = linspace(a, b, 1)
     assert type(ls) is PhysicalQuantity2
     assert_equal(ls, linspace(0.0, 1.0, 1))
Ejemplo n.º 7
0
 def test_asym(self):
     x = array([1, 1, 2, 3, 4, 4, 4, 5])
     y = array([1, 3, 2, 0, 1, 2, 3, 4])
     H, xed, yed = histogram2d(x,
                               y, (6, 5),
                               range=[[0, 6], [0, 5]],
                               density=True)
     answer = array([[0., 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0],
                     [1, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 1]])
     assert_array_almost_equal(H, answer / 8., 3)
     assert_array_equal(xed, np.linspace(0, 6, 7))
     assert_array_equal(yed, np.linspace(0, 5, 6))
Ejemplo n.º 8
0
    def test_simple(self):
        """
        Straightforward testing with a mixture of linspace data (for
        consistency). All test values have been precomputed and the values
        shouldn't change
        """
        # Some basic sanity checking, with some fixed data.
        # Checking for the correct number of bins
        basic_test = {
            50: {
                'fd': 4,
                'scott': 4,
                'rice': 8,
                'sturges': 7,
                'doane': 8,
                'sqrt': 8,
                'auto': 7
            },
            500: {
                'fd': 8,
                'scott': 8,
                'rice': 16,
                'sturges': 10,
                'doane': 12,
                'sqrt': 23,
                'auto': 10
            },
            5000: {
                'fd': 17,
                'scott': 17,
                'rice': 35,
                'sturges': 14,
                'doane': 17,
                'sqrt': 71,
                'auto': 17
            }
        }

        for testlen, expectedResults in basic_test.items():
            # Create some sort of non uniform data to test with
            # (2 peak uniform mixture)
            x1 = np.linspace(-10, -1, testlen // 5 * 2)
            x2 = np.linspace(1, 10, testlen // 5 * 3)
            x = np.concatenate((x1, x2))
            for estimator, numbins in expectedResults.items():
                a, b = np.histogram(x, estimator)
                assert_equal(len(a),
                             numbins,
                             err_msg="For the {0} estimator "
                             "with datasize of {1}".format(estimator, testlen))
Ejemplo n.º 9
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.º 10
0
    def linspace(self, n=100, domain=None):
        """Return x, y values at equally spaced points in domain.

        Returns the x, y values at `n` linearly spaced points across the
        domain.  Here y is the value of the polynomial at the points x. By
        default the domain is the same as that of the series instance.
        This method is intended mostly as a plotting aid.

        .. versionadded:: 1.5.0

        Parameters
        ----------
        n : int, optional
            Number of point pairs to return. The default value is 100.
        domain : {None, array_like}, optional
            If not None, the specified domain is used instead of that of
            the calling instance. It should be of the form ``[beg,end]``.
            The default is None which case the class domain is used.

        Returns
        -------
        x, y : ndarray
            x is equal to linspace(self.domain[0], self.domain[1], n) and
            y is the series evaluated at element of x.

        """
        if domain is None:
            domain = self.domain
        x = np.linspace(domain[0], domain[1], n)
        y = self(x)
        return x, y
Ejemplo n.º 11
0
    def test_complex64_fail(self):
        nulp = 5
        x = np.linspace(-20, 20, 50, dtype=np.float32)
        x = 10**x
        x = np.r_[-x, x]
        xi = x + x * 1j

        eps = np.finfo(x.dtype).eps
        y = x + x * eps * nulp * 2.
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      x + y * 1j, nulp)
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + x * 1j, nulp)
        y = x + x * eps * nulp
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + y * 1j, nulp)

        epsneg = np.finfo(x.dtype).epsneg
        y = x - x * epsneg * nulp * 2.
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      x + y * 1j, nulp)
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + x * 1j, nulp)
        y = x - x * epsneg * nulp
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + y * 1j, nulp)
Ejemplo n.º 12
0
 def test_polyroots(self):
     assert_almost_equal(poly.polyroots([1]), [])
     assert_almost_equal(poly.polyroots([1, 2]), [-.5])
     for i in range(2, 5):
         tgt = np.linspace(-1, 1, i)
         res = poly.polyroots(poly.polyfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 13
0
 def test_lagroots(self):
     assert_almost_equal(lag.lagroots([1]), [])
     assert_almost_equal(lag.lagroots([0, 1]), [1])
     for i in range(2, 5):
         tgt = np.linspace(0, 3, i)
         res = lag.lagroots(lag.lagfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 14
0
 def test_chebroots(self):
     assert_almost_equal(cheb.chebroots([1]), [])
     assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
     for i in range(2, 5):
         tgt = np.linspace(-1, 1, i)
         res = cheb.chebroots(cheb.chebfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 15
0
    def test_lagfit(self):
        def f(x):
            return x * (x - 1) * (x - 2)

        # Test exceptions
        assert_raises(ValueError, lag.lagfit, [1], [1], -1)
        assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
        assert_raises(TypeError, lag.lagfit, [], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
        assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
        assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
        assert_raises(ValueError, lag.lagfit, [1], [1], [
            -1,
        ])
        assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
        assert_raises(TypeError, lag.lagfit, [1], [1], [])

        # Test fit
        x = np.linspace(0, 2)
        y = f(x)
        #
        coef3 = lag.lagfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
        assert_equal(len(coef3), 4)
        assert_almost_equal(lag.lagval(x, coef3), y)
        #
        coef4 = lag.lagfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
        assert_equal(len(coef4), 5)
        assert_almost_equal(lag.lagval(x, coef4), y)
        #
        coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = lag.lagfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        # test scaling with complex values x points whose square
        # is zero when summed.
        x = [1, 1j, -1, -1j]
        assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
        assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
Ejemplo n.º 16
0
 def test_legroots(self):
     assert_almost_equal(leg.legroots([1]), [])
     assert_almost_equal(leg.legroots([1, 2]), [-.5])
     for i in range(2, 5):
         tgt = np.linspace(-1, 1, i)
         res = leg.legroots(leg.legfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 17
0
 def test_hermeroots(self):
     assert_almost_equal(herme.hermeroots([1]), [])
     assert_almost_equal(herme.hermeroots([1, 1]), [-1])
     for i in range(2, 5):
         tgt = np.linspace(-1, 1, i)
         res = herme.hermeroots(herme.hermefromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Ejemplo n.º 18
0
    def test_complex128_fail(self):
        nulp = 5
        x = np.linspace(-20, 20, 50, dtype=np.float64)
        x = 10**x
        x = np.r_[-x, x]
        xi = x + x * 1j

        eps = np.finfo(x.dtype).eps
        y = x + x * eps * nulp * 2.
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      x + y * 1j, nulp)
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + x * 1j, nulp)
        # The test condition needs to be at least a factor of sqrt(2) smaller
        # because the real and imaginary parts both change
        y = x + x * eps * nulp
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + y * 1j, nulp)

        epsneg = np.finfo(x.dtype).epsneg
        y = x - x * epsneg * nulp * 2.
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      x + y * 1j, nulp)
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + x * 1j, nulp)
        y = x - x * epsneg * nulp
        assert_raises(AssertionError, assert_array_almost_equal_nulp, xi,
                      y + y * 1j, nulp)
Ejemplo n.º 19
0
def test_linspace(Poly):
    d = Poly.domain + random((2, )) * .25
    w = Poly.window + random((2, )) * .25
    p = Poly([1, 2, 3], domain=d, window=w)
    # check default domain
    xtgt = np.linspace(d[0], d[1], 20)
    ytgt = p(xtgt)
    xres, yres = p.linspace(20)
    assert_almost_equal(xres, xtgt)
    assert_almost_equal(yres, ytgt)
    # check specified domain
    xtgt = np.linspace(0, 2, 20)
    ytgt = p(xtgt)
    xres, yres = p.linspace(20, domain=[0, 2])
    assert_almost_equal(xres, xtgt)
    assert_almost_equal(yres, ytgt)
Ejemplo n.º 20
0
    def test_limited_variance(self):
        """
        Check when IQR is 0, but variance exists, we return the sturges value
        and not the fd value.
        """
        lim_var_data = np.ones(1000)
        lim_var_data[:3] = 0
        lim_var_data[-4:] = 100

        edges_auto = histogram_bin_edges(lim_var_data, 'auto')
        assert_equal(edges_auto, np.linspace(0, 100, 12))

        edges_fd = histogram_bin_edges(lim_var_data, 'fd')
        assert_equal(edges_fd, np.array([0, 100]))

        edges_sturges = histogram_bin_edges(lim_var_data, 'sturges')
        assert_equal(edges_sturges, np.linspace(0, 100, 12))
Ejemplo n.º 21
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.º 22
0
    def test_data_attr_assignment(self):
        a = np.arange(10)
        b = np.linspace(0, 1, 10)

        self.message = ("Assigning the 'data' attribute is an "
                        "inherently unsafe operation and will "
                        "be removed in the future.")
        self.assert_deprecated(a.__setattr__, args=('data', b.data))
Ejemplo n.º 23
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.º 24
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.º 25
0
    def test_simple_range(self):
        """
        Straightforward testing with a mixture of linspace data (for
        consistency). Adding in a 3rd mixture that will then be
        completely ignored. All test values have been precomputed and
        the shouldn't change.
        """
        # some basic sanity checking, with some fixed data.
        # Checking for the correct number of bins
        basic_test = {
            50: {
                'fd': 8,
                'scott': 8,
                'rice': 15,
                'sturges': 14,
                'auto': 14
            },
            500: {
                'fd': 15,
                'scott': 16,
                'rice': 32,
                'sturges': 20,
                'auto': 20
            },
            5000: {
                'fd': 33,
                'scott': 33,
                'rice': 69,
                'sturges': 27,
                'auto': 33
            }
        }

        for testlen, expectedResults in basic_test.items():
            # create some sort of non uniform data to test with
            # (3 peak uniform mixture)
            x1 = np.linspace(-10, -1, testlen // 5 * 2)
            x2 = np.linspace(1, 10, testlen // 5 * 3)
            x3 = np.linspace(-100, -50, testlen)
            x = np.hstack((x1, x2, x3))
            for estimator, numbins in expectedResults.items():
                a, b = np.histogram(x, estimator, range=(-20, 20))
                msg = "For the {0} estimator".format(estimator)
                msg += " with datasize of {0}".format(testlen)
                assert_equal(len(a), numbins, err_msg=msg)
Ejemplo n.º 26
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.º 27
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.º 28
0
class TestArithmetic(object):
    x = np.linspace(-1, 1, 100)

    def test_legadd(self):
        for i in range(5):
            for j in range(5):
                msg = "At i=%d, j=%d" % (i, j)
                tgt = np.zeros(max(i, j) + 1)
                tgt[i] += 1
                tgt[j] += 1
                res = leg.legadd([0] * i + [1], [0] * j + [1])
                assert_equal(trim(res), trim(tgt), err_msg=msg)

    def test_legsub(self):
        for i in range(5):
            for j in range(5):
                msg = "At i=%d, j=%d" % (i, j)
                tgt = np.zeros(max(i, j) + 1)
                tgt[i] += 1
                tgt[j] -= 1
                res = leg.legsub([0] * i + [1], [0] * j + [1])
                assert_equal(trim(res), trim(tgt), err_msg=msg)

    def test_legmulx(self):
        assert_equal(leg.legmulx([0]), [0])
        assert_equal(leg.legmulx([1]), [0, 1])
        for i in range(1, 5):
            tmp = 2 * i + 1
            ser = [0] * i + [1]
            tgt = [0] * (i - 1) + [i / tmp, 0, (i + 1) / tmp]
            assert_equal(leg.legmulx(ser), tgt)

    def test_legmul(self):
        # check values of result
        for i in range(5):
            pol1 = [0] * i + [1]
            val1 = leg.legval(self.x, pol1)
            for j in range(5):
                msg = "At i=%d, j=%d" % (i, j)
                pol2 = [0] * j + [1]
                val2 = leg.legval(self.x, pol2)
                pol3 = leg.legmul(pol1, pol2)
                val3 = leg.legval(self.x, pol3)
                assert_(len(pol3) == i + j + 1, msg)
                assert_almost_equal(val3, val1 * val2, err_msg=msg)

    def test_legdiv(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 = leg.legadd(ci, cj)
                quo, rem = leg.legdiv(tgt, ci)
                res = leg.legadd(leg.legmul(quo, ci), rem)
                assert_equal(trim(res), trim(tgt), err_msg=msg)
Ejemplo n.º 29
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.º 30
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)