Esempio n. 1
0
    def test_diag(self):
        assert_equal(
            eye(4, k=1),
            array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]))

        assert_equal(
            eye(4, k=-1),
            array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]))
Esempio n. 2
0
 def test_order(self):
     mat_c = eye(4, 3, k=-1)
     mat_f = eye(4, 3, k=-1, order='F')
     assert_equal(mat_c, mat_f)
     assert mat_c.flags.c_contiguous
     assert not mat_c.flags.f_contiguous
     assert not mat_f.flags.c_contiguous
     assert mat_f.flags.f_contiguous
Esempio n. 3
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)
Esempio n. 4
0
    def test_basic(self):
        assert_equal(
            eye(4),
            array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))

        assert_equal(
            eye(4, dtype='f'),
            array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
                  'f'))

        assert_equal(eye(3) == 1, eye(3, dtype=bool))
Esempio n. 5
0
 def test_eye_bounds(self):
     assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]])
     assert_equal(eye(2, 2, -1), [[0, 0], [1, 0]])
     assert_equal(eye(2, 2, 2), [[0, 0], [0, 0]])
     assert_equal(eye(2, 2, -2), [[0, 0], [0, 0]])
     assert_equal(eye(3, 2, 2), [[0, 0], [0, 0], [0, 0]])
     assert_equal(eye(3, 2, 1), [[0, 1], [0, 0], [0, 0]])
     assert_equal(eye(3, 2, -1), [[0, 0], [1, 0], [0, 1]])
     assert_equal(eye(3, 2, -2), [[0, 0], [0, 0], [1, 0]])
     assert_equal(eye(3, 2, -3), [[0, 0], [0, 0], [0, 0]])
Esempio n. 6
0
    def test_keepdims(self):
        mat = np.eye(3)
        for axis in [None, 0, 1]:
            tgt = np.percentile(mat, 70, axis=axis, out=None,
                                overwrite_input=False)
            res = np.nanpercentile(mat, 70, axis=axis, out=None,
                                   overwrite_input=False)
            assert_(res.ndim == tgt.ndim)

        d = np.ones((3, 5, 7, 11))
        # Randomly set some elements to NaN:
        w = np.random.random((4, 200)) * np.array(d.shape)[:, None]
        w = w.astype(np.intp)
        d[tuple(w)] = np.nan
        with suppress_warnings() as sup:
            sup.filter(RuntimeWarning)
            res = np.nanpercentile(d, 90, axis=None, keepdims=True)
            assert_equal(res.shape, (1, 1, 1, 1))
            res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True)
            assert_equal(res.shape, (1, 1, 7, 11))
            res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True)
            assert_equal(res.shape, (1, 5, 7, 1))
            res = np.nanpercentile(d, 90, axis=(1,), keepdims=True)
            assert_equal(res.shape, (3, 1, 7, 11))
            res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True)
            assert_equal(res.shape, (1, 1, 1, 1))
            res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True)
            assert_equal(res.shape, (1, 1, 7, 1))
Esempio n. 7
0
 def test_keepdims(self):
     mat = np.eye(3)
     for nf, rf in zip(self.nanfuncs, self.stdfuncs):
         for axis in [None, 0, 1]:
             tgt = rf(mat, axis=axis, keepdims=True)
             res = nf(mat, axis=axis, keepdims=True)
             assert_(res.ndim == tgt.ndim)
Esempio n. 8
0
def test_nanfunctions_matrices_general():
    # Check that it works and that type and
    # shape are preserved
    # 2018-04-29: moved here from core.tests.test_nanfunctions
    mat = np.matrix(np.eye(3))
    for f in (np.nanargmin, np.nanargmax, np.nansum, np.nanprod,
              np.nanmean, np.nanvar, np.nanstd):
        res = f(mat, axis=0)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (1, 3))
        res = f(mat, axis=1)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (3, 1))
        res = f(mat)
        assert_(np.isscalar(res))

    for f in np.nancumsum, np.nancumprod:
        res = f(mat, axis=0)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (3, 3))
        res = f(mat, axis=1)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (3, 3))
        res = f(mat)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (1, 3*3))
Esempio n. 9
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)
Esempio n. 10
0
 def test_dtype_from_input(self):
     codes = 'efdgFDG'
     for nf, rf in zip(self.nanfuncs, self.stdfuncs):
         for c in codes:
             mat = np.eye(3, dtype=c)
             tgt = rf(mat, axis=1).dtype.type
             res = nf(mat, axis=1).dtype.type
             assert_(res is tgt)
             # scalar case
             tgt = rf(mat, axis=None).dtype.type
             res = nf(mat, axis=None).dtype.type
             assert_(res is tgt)
Esempio n. 11
0
 def test_pow(self):
     """Test raising a matrix to an integer power works as expected."""
     m = matrix("1. 2.; 3. 4.")
     m2 = m.copy()
     m2 **= 2
     mi = m.copy()
     mi **= -1
     m4 = m2.copy()
     m4 **= 2
     assert_array_almost_equal(m2, m**2)
     assert_array_almost_equal(m4, np.dot(m2, m2))
     assert_array_almost_equal(np.dot(mi, m), np.eye(2))
Esempio n. 12
0
    def test_append_fields_dtype_list(self):
        # Ticket #1676
        from numpy1.lib.recfunctions import append_fields

        base = np.array([1, 2, 3], dtype=np.int32)
        names = ['a', 'b', 'c']
        data = np.eye(3).astype(np.int32)
        dlist = [np.float64, np.int32, np.int32]
        try:
            append_fields(base, names, data, dlist)
        except Exception:
            raise AssertionError()
Esempio n. 13
0
def test_qr_mode_full_future_warning():
    """Check mode='full' FutureWarning.

    In numpy 1.8 the mode options 'full' and 'economic' in linalg.qr were
    deprecated. The release date will probably be sometime in the summer
    of 2013.

    """
    a = np.eye(2)
    assert_warns(DeprecationWarning, np.linalg.qr, a, mode='full')
    assert_warns(DeprecationWarning, np.linalg.qr, a, mode='f')
    assert_warns(DeprecationWarning, np.linalg.qr, a, mode='economic')
    assert_warns(DeprecationWarning, np.linalg.qr, a, mode='e')
Esempio n. 14
0
    def test_100(self):
        x, w = lag.laggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = lag.lagvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1 / np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 1.0
        assert_almost_equal(w.sum(), tgt)
Esempio n. 15
0
 def test_dtype_from_char(self):
     mat = np.eye(3)
     codes = 'efdgFDG'
     for nf, rf in zip(self.nanfuncs, self.stdfuncs):
         for c in codes:
             with suppress_warnings() as sup:
                 if nf in {np.nanstd, np.nanvar} and c in 'FDG':
                     # Giving the warning is a small bug, see gh-8000
                     sup.filter(np.ComplexWarning)
                 tgt = rf(mat, dtype=c, axis=1).dtype.type
                 res = nf(mat, dtype=c, axis=1).dtype.type
                 assert_(res is tgt)
                 # scalar case
                 tgt = rf(mat, dtype=c, axis=None).dtype.type
                 res = nf(mat, dtype=c, axis=None).dtype.type
                 assert_(res is tgt)
Esempio n. 16
0
    def test_subclass(self):
        class MyNDArray(np.ndarray):
            pass

        # Check that it works and that type and
        # shape are preserved
        mine = np.eye(3).view(MyNDArray)
        for f in self.nanfuncs:
            res = f(mine, axis=0)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine, axis=1)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine)
            assert_(res.shape == ())
Esempio n. 17
0
    def test_keepdims(self):
        for f, g in zip(self.nanfuncs, self.stdfuncs):
            mat = np.eye(3)
            for axis in [None, 0, 1]:
                tgt = f(mat, axis=axis, out=None)
                res = g(mat, axis=axis, out=None)
                assert_(res.ndim == tgt.ndim)

        for f in self.nanfuncs:
            d = np.ones((3, 5, 7, 11))
            # Randomly set some elements to NaN:
            rs = np.random.RandomState(0)
            d[rs.rand(*d.shape) < 0.5] = np.nan
            res = f(d, axis=None)
            assert_equal(res.shape, (1155,))
            for axis in np.arange(4):
                res = f(d, axis=axis)
                assert_equal(res.shape, (3, 5, 7, 11))
Esempio n. 18
0
def eye(n, M=None, k=0, dtype=float, order='C'):
    """
    Return a matrix with ones on the diagonal and zeros elsewhere.

    Parameters
    ----------
    n : int
        Number of rows in the output.
    M : int, optional
        Number of columns in the output, defaults to `n`.
    k : int, optional
        Index of the diagonal: 0 refers to the main diagonal,
        a positive value refers to an upper diagonal,
        and a negative value to a lower diagonal.
    dtype : dtype, optional
        Data-type of the returned matrix.
    order : {'C', 'F'}, optional
        Whether the output should be stored in row-major (C-style) or
        column-major (Fortran-style) order in memory.

        .. versionadded:: 1.14.0

    Returns
    -------
    I : matrix
        A `n` x `M` matrix where all elements are equal to zero,
        except for the `k`-th diagonal, whose values are equal to one.

    See Also
    --------
    numpy.eye : Equivalent array function.
    identity : Square identity matrix.

    Examples
    --------
    >>> import numpy1.matlib
    >>> np.matlib.eye(3, k=1, dtype=float)
    matrix([[ 0.,  1.,  0.],
            [ 0.,  0.,  1.],
            [ 0.,  0.,  0.]])

    """
    return asmatrix(np.eye(n, M=M, k=k, dtype=dtype, order=order))
Esempio n. 19
0
    def test_subclass(self):
        class MyNDArray(np.ndarray):
            pass

        # Check that it works and that type and
        # shape are preserved
        mine = np.eye(3).view(MyNDArray)
        for f in self.nanfuncs:
            res = f(mine, axis=0)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine, axis=1)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == (3,))
            res = f(mine)
            assert_(res.shape == ())

        # check that rows of nan are dealt with for subclasses (#4628)
        mine[1] = np.nan
        for f in self.nanfuncs:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine, axis=0)
                assert_(isinstance(res, MyNDArray))
                assert_(not np.any(np.isnan(res)))
                assert_(len(w) == 0)

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine, axis=1)
                assert_(isinstance(res, MyNDArray))
                assert_(np.isnan(res[1]) and not np.isnan(res[0])
                        and not np.isnan(res[2]))
                assert_(len(w) == 1, 'no warning raised')
                assert_(issubclass(w[0].category, RuntimeWarning))

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                res = f(mine)
                assert_(res.shape == ())
                assert_(res != np.nan)
                assert_(len(w) == 0)
Esempio n. 20
0
    def test_subclass(self):
        class MyNDArray(np.ndarray):
            pass

        # Check that it works and that type and
        # shape are preserved
        array = np.eye(3)
        mine = array.view(MyNDArray)
        for f in self.nanfuncs:
            expected_shape = f(array, axis=0).shape
            res = f(mine, axis=0)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == expected_shape)
            expected_shape = f(array, axis=1).shape
            res = f(mine, axis=1)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == expected_shape)
            expected_shape = f(array).shape
            res = f(mine)
            assert_(isinstance(res, MyNDArray))
            assert_(res.shape == expected_shape)
Esempio n. 21
0
def test_nanfunctions_matrices():
    # Check that it works and that type and
    # shape are preserved
    # 2018-04-29: moved here from core.tests.test_nanfunctions
    mat = np.matrix(np.eye(3))
    for f in [np.nanmin, np.nanmax]:
        res = f(mat, axis=0)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (1, 3))
        res = f(mat, axis=1)
        assert_(isinstance(res, np.matrix))
        assert_(res.shape == (3, 1))
        res = f(mat)
        assert_(np.isscalar(res))
    # check that rows of nan are dealt with for subclasses (#4628)
    mat[1] = np.nan
    for f in [np.nanmin, np.nanmax]:
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat, axis=0)
            assert_(isinstance(res, np.matrix))
            assert_(not np.any(np.isnan(res)))
            assert_(len(w) == 0)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat, axis=1)
            assert_(isinstance(res, np.matrix))
            assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0])
                    and not np.isnan(res[2, 0]))
            assert_(len(w) == 1, 'no warning raised')
            assert_(issubclass(w[0].category, RuntimeWarning))

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            res = f(mat)
            assert_(np.isscalar(res))
            assert_(res != np.nan)
            assert_(len(w) == 0)
Esempio n. 22
0
 def test_simple(self):
     x = array(
         [0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891])
     y = array([0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673])
     xedges = np.linspace(0, 1, 10)
     yedges = np.linspace(0, 1, 10)
     H = histogram2d(x, y, (xedges, yedges))[0]
     answer = array([[0, 0, 0, 1, 0, 0, 0, 0,
                      0], [0, 0, 0, 0, 0, 0, 1, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0,
                      0], [1, 0, 1, 0, 0, 0, 0, 0, 0],
                     [0, 1, 0, 0, 0, 0, 0, 0,
                      0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0,
                      0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0]])
     assert_array_equal(H.T, answer)
     H = histogram2d(x, y, xedges)[0]
     assert_array_equal(H.T, answer)
     H, xedges, yedges = histogram2d(list(range(10)), list(range(10)))
     assert_array_equal(H, eye(10, 10))
     assert_array_equal(xedges, np.linspace(0, 9, 11))
     assert_array_equal(yedges, np.linspace(0, 9, 11))
Esempio n. 23
0
 def test_flat(self):
     # Test that flat can return items even for matrices [#4585, #4615]
     # test simple access
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     assert_equal(test.flat[1], 2)
     assert_equal(test.flat[2], masked)
     assert_(np.all(test.flat[0:2] == test[0, 0:2]))
     # Test flat on masked_matrices
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
     control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
     assert_equal(test, control)
     # Test setting
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     testflat = test.flat
     testflat[:] = testflat[[2, 1, 0]]
     assert_equal(test, control)
     testflat[0] = 9
     # test that matrices keep the correct shape (#4615)
     a = masked_array(np.matrix(np.eye(2)), mask=0)
     b = a.flat
     b01 = b[:2]
     assert_equal(b01.data, np.array([[1., 0.]]))
     assert_equal(b01.mask, np.array([[False, False]]))
Esempio n. 24
0
 def test_no_lists(self):
     assert_equal(np.block(1),         np.array(1))
     assert_equal(np.block(np.eye(3)), np.eye(3))
Esempio n. 25
0
 def test_large_svd_32bit(self):
     # See gh-4442, 64bit would require very large/slow matrices.
     x = np.eye(1000, 66)
     np.linalg.svd(x)
Esempio n. 26
0
 def test_bool(self):
     assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]])
Esempio n. 27
0
 def test_strings(self):
     assert_equal(eye(2, 2, dtype='S3'), [[b'1', b''], [b'', b'1']])
Esempio n. 28
0
    def test_diag2d(self):
        assert_equal(eye(3, 4, k=2),
                     array([[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]))

        assert_equal(eye(4, 3, k=-2),
                     array([[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 1, 0]]))
Esempio n. 29
0
    def test_2d(self):
        assert_equal(eye(4, 3),
                     array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]))

        assert_equal(eye(3, 4),
                     array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]))
Esempio n. 30
0
 def mul():
     np.mat(np.eye(2)) * np.ones(2)