Exemplo n.º 1
0
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

import numpy

import nlcpy
from nlcpy import testing


@testing.parameterize(*(
    testing.product({
        'shape': [(2,), (2, 3), (2, 3, 4)],
    })
))
class TestSumprod1(unittest.TestCase):

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose()
    def test_sum_all(self, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        return xp.sum(a)

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose()
    def test_external_sum_all(self, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        return xp.sum(a)
Exemplo n.º 2
0
    integer(kind=8) :: ipvt(n), test_dbgmsm, ierr
    test_dbgmsm = -1
    call dbgmsm(ab, lna, n, m, ipvt, ierr)
    test_dbgmsm = ierr
end
'''


def _callback(err):
    if err != 0:
        raise RuntimeError


@testing.parameterize(*testing.product({
    'openmp': [True, False],
    'sync': [True, False],
    'callback': [_callback, None],
}))
class TestAslNative(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        nlcpy.jit.unload_library(self.lib)

    def _helper(self, code, compiler, name, args_type, ret_type):
        self.lib = nlcpy.jit.CustomVELibrary(
            code=code,
            compiler=compiler,
            cflags=nlcpy.jit.get_default_cflags(openmp=self.openmp),
Exemplo n.º 3
0
#     all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shape': [(3, 4), (0, 4, 2), (3, 6, 5, 4), (6, 3, 4, 2, 5)],
    'axis': [None, 0, 1, -2, (0, 1)],
    'keepdims': [True, False]
})))
class TestCount(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.for_orders('CF')
    @testing.numpy_nlcpy_array_equal()
    def test_count_nonzero(self, xp, dtype, order):
        a = xp.ones(self.shape, dtype=dtype, order=order)
        mask = testing.shaped_random(self.shape, xp, dtype='?')
        return xp.count_nonzero(a * mask, axis=self.axis)


class TestCountFailure(unittest.TestCase):
    @testing.numpy_nlcpy_raises
    def test_count_nonzero_axis_error1(self, xp):
Exemplo n.º 4
0
        else:
            cls.lib = nlcpy.jit.CustomVELibrary(
                path=os.path.join(tmpdir, base_name + '.so'))
    cls.kern = cls.lib.get_function(name,
                                    args_type=args_type,
                                    ret_type=ret_type)


@testing.parameterize(*testing.product({
    'dtype': [
        'i4',
        'i8',
        'u4',
        'u8',
        'f4',
        'f8',
        'c8',
        'c16',
    ],
    'openmp': [True, False],
    'opt_level': [2, 3],
    'sync': [True, False],
    'callback': [_callback, None],
}))
class TestCustomLibraryFromStr(unittest.TestCase):
    def setUp(self):
        self.lib = None

    def tearDown(self):
        if self.lib:
            nlcpy.jit.unload_library(self.lib)
Exemplo n.º 5
0
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'params': [
        ([10], None),
        ([4, 5], None),
        ([3, 5, 4, 2, 6], None),
        ([8], 0),
        ([5, 1, 2, 5, 4], 0),
        ([5, 1, 2, 5, 4], 1),
        ([5, 1, 2, 5, 4], 4),
        ([5, 1, 2, 5, 4], -2),
        ([3, 4, 5, 6, 7], (1, 3, 2)),
        ([4, 2, 1, 4, 5], (-1, 2, 3)),
        ([3, 4, 5, 6, 7], [1, 3, 2]),
        ([4, 2, 1, 4, 5], [-1, 2, 3]),
    ],
})))
class TestFlip(unittest.TestCase):
    @testing.for_all_dtypes(name='dtype_m')
    @testing.numpy_nlcpy_array_equal()
    def test_flip(self, xp, dtype_m):
        m = testing.shaped_arange(self.params[0], xp, dtype_m)
        axis = self.params[1]
        return xp.flip(m=m, axis=axis)
Exemplo n.º 6
0
import functools  # NOQA
import unittest

import numpy as np  # NOQA

import nlcpy  # NOQA
from nlcpy import testing


@testing.parameterize(*testing.product({
    'shape': [(2, 3, 4), (2, 3, 4, 5), (6, 5, 4, 3, 2)],
    'move_1': [(0, 1), (1, 2), (-2, -1), (-2, -3)],
    'move_2': [(0, 1), (1, 2), (-2, -1), (-2, -3)],
}))
@testing.with_requires('numpy>=1.10.0')
class TestFftMoveAxis(unittest.TestCase):

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose(rtol=1e-4, atol=1e-7,
                                  contiguous_check=False)
    def test_fft_move(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        ma = xp.moveaxis(a, self.move_1, self.move_2)
        out = xp.fft.fft(ma)

        if xp == np and dtype is np.float32 or dtype is np.complex64:
            out = out.astype(np.complex64)
        return out

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose(rtol=1e-4, atol=1e-7,
Exemplo n.º 7
0
    return True


def execute_ufunc(op, xp, in1, out=None, dtype=None,
                  axis=0, initial=numpy._NoValue, where=True, keepdims=False):
    dtype_out = None if out is None else out.dtype
    if not is_executable(op, initial, where, in1.dtype, dtype, dtype_out):
        return 0
    initial = convert_initial(initial, op, in1.dtype, dtype, dtype_out)
    return getattr(xp, op).reduce(
        in1, out=out, dtype=dtype, axis=axis,
        initial=initial, where=where, keepdims=keepdims)


@testing.parameterize(*testing.product({
    'initial': (numpy._NoValue, None, 0, 2, -2.63, -1.2 + 0.3j),
}))
@pytest.mark.full
class TestReduce(unittest.TestCase):

    shapes = ((1, 3, 2), (3, 2, 5), (5, 4, 2, 3))
    axes = (0, 1, -2, (1, 2), None)

    @testing.numpy_nlcpy_check_for_unary_ufunc(
        ops[2:], shapes, dtype_x=all_types, ufunc_name='reduce', axes=axes, seed=0
    )
    def test_reduce(self, xp, op, in1, axis):
        return execute_ufunc(
            op, xp, in1, axis=axis, initial=self.initial)

    @testing.numpy_nlcpy_check_for_unary_ufunc(
Exemplo n.º 8
0
#     all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shape_a': ((4, 4), (4, 4, 4), (10, 100), (100, 10)),
    'shape_val': ((2, ), (10, ), (4, 4), (0, 4)),
    'wrap': (True, False),
})))
class TestFillDiagonal(unittest.TestCase):
    @testing.for_all_dtypes(name='dtype_a')
    @testing.for_all_dtypes(name='dtype_val')
    @testing.for_orders('CF', name='order_a')
    @testing.for_orders('CF', name='order_val')
    @testing.numpy_nlcpy_array_equal()
    def test_fill_diagonal(self, xp, dtype_a, dtype_val, order_a, order_val):
        a = testing.shaped_arange(self.shape_a,
                                  xp,
                                  dtype=dtype_a,
                                  order=order_a)
        val = xp.asarray(testing.shaped_random(self.shape_val, xp, dtype_val),
                         order=order_val)
Exemplo n.º 9
0
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

import numpy
from numpy.core._exceptions import UFuncTypeError

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shape': [(2, ), (2, 3), (2, 3, 4)],
})))
class TestMisc(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose(atol=1e-5)
    def check_unary(self, name, xp, dtype, no_bool=False):
        if no_bool and numpy.dtype(dtype).char == '?':
            return numpy.int_(0)
        a = testing.shaped_arange(self.shape, xp, dtype)
        return getattr(xp, name)(a)

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose(atol=1e-5)
    def check_binary(self, name, xp, dtype, no_bool=False):
        if no_bool and numpy.dtype(dtype).char == '?':
            return numpy.int_(0)
Exemplo n.º 10
0
    @testing.numpy_nlcpy_allclose()
    def test_linspace_float_overflow(self, xp):
        return xp.linspace(0., sys.float_info.max / 5, 10, dtype=float)

    # @testing.with_requires('numpy>=1.10')
    # @testing.numpy_nlcpy_array_equal()
    # def test_linspace_float_underflow(self, xp):
    #     # find minimum subnormal number
    #     x = sys.float_info.min
    #     while x / 2 > 0:
    #         x /= 2
    #     return xp.linspace(0., x, 10, dtype=float)


@testing.parameterize(*(testing.product({
    'size': [1, 3, 5],
    'num': [1, 2, 3, 5, 7],
})))
class TestMeshgrid(unittest.TestCase):
    @testing.numpy_nlcpy_array_list_equal()
    def test_meshgrid(self, xp):
        arrays = [
            testing.shaped_random([self.size], xp) for _ in range(self.num)
        ]
        return xp.meshgrid(*arrays)


class TestMeshgrid2(unittest.TestCase):
    @testing.numpy_nlcpy_array_list_equal()
    def test_meshgrid_empty(self, xp):
        return xp.meshgrid()
Exemplo n.º 11
0
import functools # NOQA

import unittest
import pytest

import numpy as np

import nlcpy
from nlcpy import testing


@testing.parameterize(*testing.product({
    'n': [None, 0, 5, 10, 15],
    'shape': [(10,), (5, 10), (3, 4, 5)],
    'norm': [None, 'ortho', ''],
}))
@testing.with_requires('numpy>=1.10.0')
class TestRfft(unittest.TestCase):

    @testing.for_all_dtypes(no_complex=True)
    @testing.numpy_nlcpy_allclose(rtol=1e-3, atol=1e-7,
                                  accept_error=(ValueError, TypeError),
                                  contiguous_check=False)
    def test_rfft(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        out = xp.fft.rfft(a, n=self.n, norm=self.norm)

        if xp == np and dtype is np.float32:
            out = out.astype(np.complex64)
        elif xp == np and dtype is not np.float32:
            out = out.astype(np.complex128)
Exemplo n.º 12
0
import unittest

import numpy
import nlcpy

from nlcpy import testing


@testing.parameterize(*(
    testing.product({
        'shape': [
            (3, 3),
            (4, 5, 5),
            (0, 4, 4),
            (3, 4, 6, 6),
            (7, 3, 5, 5),
            (5, 0, 4, 6, 6),
            (2, 5, 3, 4, 4),
            (6, 3, 4, 3, 3),
        ],
    })
))
class TestEig(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.for_orders('CF')
    @testing.numpy_nlcpy_array_equal()
    def test_eig(self, xp, dtype, order):
        a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order)
        args = dict()
        args["a"] = a
        w, v = xp.linalg.eig(**args)
Exemplo n.º 13
0
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

import numpy
import nlcpy
from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shape': [(3, 5), (3, 7, 4), (4, 3, 6, 5), (5, 3, 4, 2, 6),
              (4, 3, 6, 4, 5), (0, 3, 3)],
    'full_matrices': [True, False],
})))
class TestSvd(unittest.TestCase):
    @testing.for_orders('CF')
    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_array_equal()
    def test_svd_general_compute_uv(self, xp, dtype, order):
        a = xp.asarray(testing.shaped_random(self.shape, xp, dtype),
                       order=order)
        args = dict()
        args["a"] = a
        if not self.full_matrices:
            args["full_matrices"] = self.full_matrices
        u, s, vh = xp.linalg.svd(**args)
        ret = [(i.shape, i.dtype, i.flags.c_contiguous, i.flags.f_contiguous)
Exemplo n.º 14
0
#     The above copyright notice and this permission notice shall be included in
#     all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shape': [(2, ), (2, 3), (2, 3, 4)],
})))
class TestTrigonometric(unittest.TestCase):
    @testing.for_all_dtypes(no_bool=True)
    @testing.numpy_nlcpy_allclose(atol=1e-5)
    def check_unary(self, name, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        return getattr(xp, name)(a)

    @testing.for_all_dtypes(no_complex=True, no_bool=True)
    @testing.numpy_nlcpy_allclose(atol=1e-5)
    def check_binary(self, name, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        b = testing.shaped_reverse_arange(self.shape, xp, dtype)
        return getattr(xp, name)(a, b)
Exemplo n.º 15
0
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'params': [
        ([
            6,
        ], [2], None),
        ([3, 2], [1], None),
        ([3, 2], [3, 1], 1),
        ([10, 10, 2], [10, 10, 1], 2),
        ([3, 4, 5], [3, 1, 5], 1),
        ([3, 4, 2, 5], [3, 1, 2, 5], 1),
    ],
})))
class TestAppend(unittest.TestCase):
    @testing.for_all_dtypes(name='dtype_a')
    @testing.for_all_dtypes(name='dtype_v')
    @testing.for_orders('CF', name='order_a')
    @testing.for_orders('CF', name='order_v')
    @testing.numpy_nlcpy_array_equal()
    def test_append(self, xp, dtype_a, dtype_v, order_a, order_v):
        arr = testing.shaped_arange(self.params[0], xp, dtype_a, order_a)
        values = testing.shaped_arange(self.params[1],
                                       xp,
Exemplo n.º 16
0
        a = xp.full([2, 2], xp.pi + 1.0j * xp.e)
        c = BytesIO()
        xp.savetxt(c, a, fmt='%.3e')
        c.seek(0)
        return c.readlines()

    @testing.numpy_nlcpy_array_equal()
    def test_custom_writer(self, xp):
        class CustomWriter(list):
            def write(self, text):
                self.extend(text.split(b'\n'))

        w = CustomWriter()
        a = xp.array([(1, 2), (3, 4)])
        xp.savetxt(w, a)
        return xp.loadtxt(w)


@testing.parameterize(*(testing.product({
    'fmt': [u"%f", b"%f"],
    'iotype': [StringIO, BytesIO]
})))
class TestUnicodeAndBytesFmt(unittest.TestCase):
    @testing.numpy_nlcpy_array_equal()
    def test_unicode_and_bytes_fmt(self, xp):
        a = xp.array([1.])
        s = self.iotype()
        xp.savetxt(s, a, fmt=self.fmt)
        s.seek(0)
        return s.read()
Exemplo n.º 17
0
        a = testing.shaped_arange((3, 3, 3), xp, dtype)
        return a.diagonal(0, -1, -3)

    @testing.numpy_nlcpy_raises()
    def test_diagonal_invalid1(self, xp):
        a = testing.shaped_arange((3, 3, 3), xp)
        a.diagonal(0, 1, 3)

    @testing.numpy_nlcpy_raises()
    def test_diagonal_invalid2(self, xp):
        a = testing.shaped_arange((3, 3, 3), xp)
        a.diagonal(0, 2, -4)


@testing.parameterize(*(testing.product({
    'n': [-1, 0, 1, 2, 4, 7, 1.5],
    'ndim': [-1, 0, 1, 2, 3, 4]
})))
class TestDiagIndices(unittest.TestCase):
    @testing.numpy_nlcpy_array_equal()
    def test_diag_indices(self, xp):
        return xp.diag_indices(self.n, self.ndim)


class TestSelect(unittest.TestCase):
    @testing.for_all_dtypes(no_bool=True)
    @testing.numpy_nlcpy_array_equal()
    def test_select(self, xp, dtype):
        a = xp.arange(10, dtype=dtype)
        condlist = [a > 3, a < 5]
        choicelist = [a, a + 100]
        return xp.select(condlist, choicelist)
Exemplo n.º 18
0
#     all copies or substantial portions of the Software.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(
    testing.product({
        'shape': [(2,), (2, 3), (2, 3, 4)],
    })
))
class TestHyperbolic(unittest.TestCase):

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_allclose(atol=1e-5)
    def check_unary(self, name, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        return getattr(xp, name)(a)

    def test_sinh(self):
        self.check_unary('sinh')

    def test_cosh(self):
        self.check_unary('cosh')
Exemplo n.º 19
0
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

import numpy
from nlcpy import testing


@testing.parameterize(*(testing.product({
    'pat': [
        ((10, ), None),
        ((3, 3), 1),
        ((3, 4, 4), 0),
        ((5, 6, 6), -1),
        ((3, 6, 4, 5, 5), 2),
        ((4, 2, 6, 7, 5), 4),
    ],
    'ord': [None, numpy.inf, -numpy.inf, 0, 1, 2, -1, -2, 5, -4],
    'keepdims': [True, False],
})))
class TestNormVector(unittest.TestCase):
    @testing.for_dtypes("?ilILdD")
    @testing.for_orders("CF")
    @testing.numpy_nlcpy_allclose(atol=1e-12, rtol=1e-12)
    def test_norm_vector(self, xp, dtype, order):
        shape, axis = self.pat
        x = xp.asarray(testing.shaped_random(shape, xp, dtype), order=order)
        args = dict()
        args["x"] = x
        if self.ord is not None:
Exemplo n.º 20
0

import unittest

import numpy
import nlcpy
from nlcpy import testing


class DummyError(Exception):
    pass


@testing.parameterize(*(
    testing.product({
        'val': [0, 3, -5.2, complex(1.2, -3.4)],
        'casting': ['no', 'equiv', 'safe', 'same_kind', 'unsafe'],
    })
))
class TestCopyScalar(unittest.TestCase):
    @testing.with_requires('numpy>=1.10')
    @testing.for_all_dtypes(name='dst_dtype')
    @testing.numpy_nlcpy_array_equal()
    def test_copyto_scalar(self, xp, dst_dtype):
        if numpy.can_cast(self.val, dst_dtype, casting=self.casting):
            dst = xp.asanyarray(-999, dtype=dst_dtype)  # make some 0-dim array
            src = self.val
            xp.copyto(dst, src, casting=self.casting)
            return dst
        else:
            return -1
Exemplo n.º 21
0
@testing.parameterize(*testing.product({
    'shape_pair': [
        # dot test
        ((3, 2), (2, 4)),
        ((3, 0), (0, 4)),
        ((0, 2), (2, 4)),
        ((3, 2), (2, 0)),
        ((2, ), (2, 4)),
        ((0, ), (0, 4)),
        ((3, 2), (2, )),
        ((3, 0), (0, )),
        ((2, ), (2, )),
        ((0, ), (0, )),
        # matmul test
        ((5, 3, 2), (5, 2, 4)),
        ((0, 3, 2), (0, 2, 4)),
        ((5, 3, 2), (2, 4)),
        ((0, 3, 2), (2, 4)),
        ((3, 2), (5, 2, 4)),
        ((3, 2), (0, 2, 4)),
        ((5, 3, 2), (1, 2, 4)),
        ((0, 3, 2), (1, 2, 4)),
        ((1, 3, 2), (5, 2, 4)),
        ((1, 3, 2), (0, 2, 4)),
        ((5, 3, 2), (2, )),
        ((5, 3, 0), (0, )),
        ((2, ), (5, 2, 4)),
        ((0, ), (5, 0, 4)),
        ((2, 2, 3, 2), (2, 2, 2, 4)),
        ((5, 0, 3, 2), (5, 0, 2, 4)),
        ((6, 5, 3, 2), (2, 4)),
        ((5, 0, 3, 2), (2, 4)),
        ((3, 2), (6, 5, 2, 4)),
        ((3, 2), (5, 0, 2, 4)),
        ((1, 5, 3, 2), (6, 1, 2, 4)),
        ((1, 0, 3, 2), (6, 1, 2, 4)),
        ((6, 1, 3, 2), (1, 5, 2, 4)),
        ((6, 1, 3, 2), (1, 0, 2, 4)),
        ((6, 5, 3, 2), (2, )),
        ((6, 5, 3, 0), (0, )),
        ((2, ), (6, 5, 2, 4)),
        ((0, ), (6, 5, 0, 4)),
        ((1, 3, 3), (10, 1, 3, 1)),
    ],
}))
Exemplo n.º 22
0
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_array_equal()
    def test_ones_like(self, xp, dtype, order):
        a = xp.ndarray((2, 3, 4), dtype=dtype)
        return xp.ones_like(a, order=order)

    @testing.numpy_nlcpy_raises()
    def test_ones_like_subok(self):
        a = nlcpy.ndarray((2, 3, 4))
        return nlcpy.ones_like(a, subok=True)


@testing.parameterize(*testing.product({
    'shape': [4, (4, ), (4, 2), (4, 2, 3), (5, 4, 2, 3), (5, 4, 2, 3, 2)],
}))
class TestBasicReshape(unittest.TestCase):
    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('CFAK')
    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_array_equal()
    def test_empty_like_reshape(self, xp, dtype, order):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = xp.empty_like(a, order=order, shape=self.shape)
        b.fill(0)
        return b

    @testing.for_CF_orders()
    @testing.for_all_dtypes()
    def test_empty_like_reshape_nlcpy_only(self, dtype, order):
Exemplo n.º 23
0
    def test_ravel3(self, xp, order):
        a = testing.shaped_arange((2, 3, 4), xp)
        a = xp.array(a, order='f')
        return a.ravel(order)

    @testing.numpy_nlcpy_array_equal()
    def test_external_ravel(self, xp):
        a = testing.shaped_arange((2, 3, 4), xp)
        a = a.transpose(2, 0, 1)
        return xp.ravel(a)


@testing.parameterize(*testing.product({
    'order_init': ['C', 'F'],
    'order_reshape': ['C', 'F', 'A', 'c', 'f', 'a'],
    'shape_in_out': [((2, 3), (1, 6, 1)),  # (shape_init, shape_final)
                     ((6,), (2, 3)),
                     ((3, 3, 3), (9, 3))],
}))
class TestReshapeOrder(unittest.TestCase):

    @testing.with_requires('numpy>=1.12')
    def test_reshape_contiguity(self):
        shape_init, shape_final = self.shape_in_out

        a_nlcpy = testing.shaped_arange(shape_init, xp=nlcpy)
        a_nlcpy = nlcpy.asarray(a_nlcpy, order=self.order_init)
        b_nlcpy = a_nlcpy.reshape(shape_final, order=self.order_reshape)

        a_numpy = testing.shaped_arange(shape_init, xp=numpy)
        a_numpy = numpy.asarray(a_numpy, order=self.order_init)
Exemplo n.º 24
0
        a = testing.shaped_random([10], xp, dtype)
        a[2:4] = a[2:4] + 1j * xp.nan
        return getattr(xp, self.func)(a)

    @testing.for_dtypes('FD')
    @testing.numpy_nlcpy_array_equal()
    def test_nanargfunc_with_nan_real_imag(self, xp, dtype):
        a = testing.shaped_random([10], xp, dtype)
        a[2:4] = xp.nan + 1j * xp.nan
        return getattr(xp, self.func)(a)


@testing.parameterize(*(
    testing.product({
        'shape': ((3, 3), (10, 10), (3, 4, 5), (6, 4, 5), (3, 0, 2)),
        'axis': (None, -1, 0, 1),
        'func': ('nanargmin', 'nanargmax'),
    }))
)
class TestNanArgFuncWithoutNan(unittest.TestCase):

    @testing.for_all_dtypes()
    @testing.numpy_nlcpy_array_equal()
    def test_nanargfunc(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        if self.axis is None and a.size == 0 or \
           self.axis is not None and a.shape[self.axis] == 0:
            return 0
        return getattr(xp, self.func)(a, axis=self.axis)

Exemplo n.º 25
0
nan_dtypes = (
    numpy.float32,
    numpy.float64,
    numpy.complex64,
    numpy.complex128,
)

shapes = (
    (4, ),
    (3, 4),
    (2, 3, 4),
)


@testing.parameterize(*(testing.product({
    'shape': shapes,
})))
class TestQuantile(unittest.TestCase):
    @testing.for_dtypes(['i', 'q', 'f', 'd', 'F', 'D'])
    @testing.numpy_nlcpy_array_equal()
    def test_case_00(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        a = xp.asarray(a)
        return xp.quantile(a, 0.5)

    @testing.for_dtypes(['i', 'q', 'f', 'd', 'F', 'D'])
    @testing.numpy_nlcpy_array_equal()
    def test_case_01(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        a = xp.asarray(a)
        return xp.quantile(a, 0.5, axis=0)
Exemplo n.º 26
0
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

import unittest

from nlcpy import testing


@testing.parameterize(*(testing.product({
    'shapes': ((2, 3, 4), (3, 1, 2), (1, 0, 4), (
        4,
        2,
        3,
        5,
    )),
    'k': (-1, 0, 1, 2),
})))
class TestDiagFlat(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.for_orders('CF')
    @testing.numpy_nlcpy_array_equal()
    def test_diagflat(self, xp, dtype, order):
        a = testing.shaped_arange(self.shapes, xp, dtype, order)
        return xp.diagflat(a, self.k)


@testing.parameterize(*(testing.product({
    'shape': ((2, None), (3, 3), (4, 3), (-2, -1)),
Exemplo n.º 27
0
def get_axis_numbers_from_strtype(t):
    if t == 'xyzw':
        return [-1, -2, -3, -4]
    else:
        raise TypeError


# --------------------------------------------------
# Testing routines for full tests
# --------------------------------------------------


@testing.parameterize(*(testing.product({
    'type': types,
    'shape': shapes,
    'dtype': float_types,
    'stencil_scale': stencil_scales_full,
    'is_out': [True, False],
    'optimize': [True, False],
})))
@pytest.mark.full
class Test4dHypervolumetricFull(unittest.TestCase):
    def test_4d_hypervolumetric(self):
        nlcpy.random.seed(0)
        xin = testing.shaped_random(self.shape, nlcpy).astype(self.dtype)
        rtol = TOL_SINGLE if self.dtype == numpy.float32 else TOL_DOUBLE
        sca_res, sca_out = compute_with_sca(
            self.type,
            xin,
            self.stencil_scale,
            is_out=self.is_out,
            optimize=self.optimize,
Exemplo n.º 28
0
    def test_owndata(self):
        self.assertEqual(3, self.flags['OWNDATA'])

    def test_key_error(self):
        with self.assertRaises(KeyError):
            self.flags['unknown key']

    def test_repr(self):
        self.assertEqual('''  C_CONTIGUOUS : 1
  F_CONTIGUOUS : 2
  OWNDATA : 3''', repr(self.flags))


@testing.parameterize(
    *testing.product({
        'order': ['C', 'F', 'non-contiguous'],
        'shape': [(8, ), (4, 8)],
    })
)
class TestContiguityFlags(unittest.TestCase):

    def setUp(self):
        self.flags = None

    def init_flags(self, xp):
        if self.order == 'non-contiguous':
            a = xp.empty(self.shape)[::2]
        else:
            a = xp.empty(self.shape, order=self.order)
        self.flags = a.flags

    @testing.numpy_nlcpy_equal()
Exemplo n.º 29
0
    @testing.numpy_nlcpy_array_equal()
    def test_external_transpose(self, xp):
        a = testing.shaped_arange((2, 3, 4), xp)
        return xp.transpose(a, (-1, 0, 1))

    @testing.numpy_nlcpy_array_equal()
    def test_external_transpose_all(self, xp):
        a = testing.shaped_arange((2, 3, 4), xp)
        return xp.transpose(a)


@testing.parameterize(*(testing.product({
    'shapes': ((2, 3, 4), (3, 1, 2), (
        4,
        2,
        3,
        5,
    )),
    'axes': ((0, 2), (2, 0), (0, -1), (1, 1), (-3, 1), (2, -3)),
})))
class TestSwapAxes(unittest.TestCase):
    @testing.for_orders('CF')
    @testing.numpy_nlcpy_array_equal()
    def test_swapaxes(self, xp, order):
        a = testing.shaped_arange(self.shapes, xp, order=order)
        return xp.swapaxes(a, self.axes[0], self.axes[1])


@testing.parameterize(*(testing.product({
    'axes': ((3, 0), (0, 3), (-4, 1), (1, -4)),
})))
Exemplo n.º 30
0
def get_n_stencil_elem(N):
    return 2 * N + 1


# --------------------------------------------------
# Testing routines
# --------------------------------------------------

@testing.parameterize(*(
    testing.product({
        'type': types,
        'shape': shapes,
        'dtype': float_types,
        'stencil_scale': stencil_scales,
        'is_out': [True, False],
        'prefix': [True, False],
        'optimize': [True, False],
        'change_coef': [True, False],
        'div': [True, False],
        'sub': [True, False],
    })
))
class Test1dAxialFactorCoefDivSub(unittest.TestCase):

    def test_1d_axial_with_factor_and_coef_div_sub(self):
        if self.prefix and self.div:
            return True
        if self.change_coef and self.div:
            return True
        xin = testing.shaped_arange(self.shape, nlcpy).astype(self.dtype) * 0.1
        n_elem = get_n_stencil_elem(self.stencil_scale)