Example #1
0
    def test_convolve_method_large_input(self):
        # This is really a test that convolving two large integers goes to the
        # direct method even if they're in the fft method.
        for n in [10, 20, 50, 51, 52, 53, 54, 60, 62]:
            z = np.array([2**n], dtype=np.int64)
            fft = convolve(z, z, method='fft')
            direct = convolve(z, z, method='direct')

            # this is the case when integer precision gets to us
            # issue #6076 has more detail, hopefully more tests after resolved
            if n < 50:
                assert_equal(fft, direct)
                assert_equal(fft, 2**(2 * n))
                assert_equal(direct, 2**(2 * n))
Example #2
0
 def test_2d_arrays(self):
     a = [[1, 2, 3], [3, 4, 5]]
     b = [[2, 3, 4], [4, 5, 6]]
     c = convolve(a, b)
     d = array([[2, 7, 16, 17, 12], [10, 30, 62, 58, 38],
                [12, 31, 58, 49, 30]])
     assert_array_equal(c, d)
Example #3
0
    def test_valid_mode2(self):
        # See gh-5897
        a = [1, 2, 3, 6, 5, 3]
        b = [2, 3, 4, 5, 3, 4, 2, 2, 1]
        expected = [70, 78, 73, 65]

        out = convolve(a, b, 'valid')
        assert_array_equal(out, expected)

        out = convolve(b, a, 'valid')
        assert_array_equal(out, expected)

        a = [1 + 5j, 2 - 1j, 3 + 0j]
        b = [2 - 3j, 1 + 0j]
        expected = [2 - 3j, 8 - 10j]

        out = convolve(a, b, 'valid')
        assert_array_equal(out, expected)

        out = convolve(b, a, 'valid')
        assert_array_equal(out, expected)
Example #4
0
    def test_convolve_method(self, n=100):
        types = sum([t for _, t in np.sctypes.items()], [])
        types = {np.dtype(t).name for t in types}

        # These types include 'bool' and all precisions (int8, float32, etc)
        # The removed types throw errors in correlate or fftconvolve
        for dtype in [
                'complex256', 'complex192', 'float128', 'float96', 'str',
                'void', 'bytes', 'object', 'unicode', 'string'
        ]:
            if dtype in types:
                types.remove(dtype)

        args = [(t1, t2, mode) for t1 in types for t2 in types
                for mode in ['valid', 'full', 'same']]

        # These are random arrays, which means test is much stronger than
        # convolving testing by convolving two np.ones arrays
        np.random.seed(42)
        array_types = {
            'i': np.random.choice([0, 1], size=n),
            'f': np.random.randn(n)
        }
        array_types['b'] = array_types['u'] = array_types['i']
        array_types['c'] = array_types['f'] + 0.5j * array_types['f']

        for t1, t2, mode in args:
            x1 = array_types[np.dtype(t1).kind].astype(t1)
            x2 = array_types[np.dtype(t2).kind].astype(t2)

            results = {
                key: convolve(x1, x2, method=key, mode=mode)
                for key in ['fft', 'direct']
            }

            assert_equal(results['fft'].dtype, results['direct'].dtype)

            if 'bool' in t1 and 'bool' in t2:
                assert_equal(choose_conv_method(x1, x2), 'direct')
                continue

            # Found by experiment. Found approx smallest value for (rtol, atol)
            # threshold to have tests pass.
            if any([t in {'complex64', 'float32'} for t in [t1, t2]]):
                kwargs = {'rtol': 1.0e-4, 'atol': 1e-6}
            elif 'float16' in [t1, t2]:
                # atol is default for np.allclose
                kwargs = {'rtol': 1e-3, 'atol': 1e-8}
            else:
                # defaults for np.allclose (different from assert_allclose)
                kwargs = {'rtol': 1e-5, 'atol': 1e-8}

            assert_allclose(results['fft'], results['direct'], **kwargs)
Example #5
0
    def test_input_swapping(self):
        small = arange(8).reshape(2, 2, 2)
        big = 1j * arange(27).reshape(3, 3, 3)
        big += arange(27)[::-1].reshape(3, 3, 3)

        out_array = array([[[0 + 0j, 26 + 0j, 25 + 1j, 24 + 2j],
                            [52 + 0j, 151 + 5j, 145 + 11j, 93 + 11j],
                            [46 + 6j, 133 + 23j, 127 + 29j, 81 + 23j],
                            [40 + 12j, 98 + 32j, 93 + 37j, 54 + 24j]],
                           [[104 + 0j, 247 + 13j, 237 + 23j, 135 + 21j],
                            [282 + 30j, 632 + 96j, 604 + 124j, 330 + 86j],
                            [246 + 66j, 548 + 180j, 520 + 208j, 282 + 134j],
                            [142 + 66j, 307 + 161j, 289 + 179j, 153 + 107j]],
                           [[68 + 36j, 157 + 103j, 147 + 113j, 81 + 75j],
                            [174 + 138j, 380 + 348j, 352 + 376j, 186 + 230j],
                            [138 + 174j, 296 + 432j, 268 + 460j, 138 + 278j],
                            [70 + 138j, 145 + 323j, 127 + 341j, 63 + 197j]],
                           [[32 + 72j, 68 + 166j, 59 + 175j, 30 + 100j],
                            [68 + 192j, 139 + 433j, 117 + 455j, 57 + 255j],
                            [38 + 222j, 73 + 499j, 51 + 521j, 21 + 291j],
                            [12 + 144j, 20 + 318j, 7 + 331j, 0 + 182j]]])

        assert_array_equal(convolve(small, big, 'full'), out_array)
        assert_array_equal(convolve(big, small, 'full'), out_array)
        assert_array_equal(convolve(small, big, 'same'), out_array[1:3, 1:3,
                                                                   1:3])
        assert_array_equal(convolve(big, small, 'same'), out_array[0:3, 0:3,
                                                                   0:3])
        assert_array_equal(convolve(small, big, 'valid'), out_array[1:3, 1:3,
                                                                    1:3])
        assert_array_equal(convolve(big, small, 'valid'), out_array[1:3, 1:3,
                                                                    1:3])
Example #6
0
 def test_single_element(self):
     a = array([4967])
     b = array([3920])
     c = convolve(a, b)
     assert_equal(c, a * b)
Example #7
0
 def test_zero_rank(self):
     a = 1289
     b = 4567
     c = convolve(a, b)
     assert_equal(c, a * b)
Example #8
0
 def test_complex(self):
     x = array([1 + 1j, 2 + 1j, 3 + 1j])
     y = array([1 + 1j, 2 + 1j])
     z = convolve(x, y)
     assert_array_equal(z, array([2j, 2 + 6j, 5 + 8j, 5 + 5j]))
Example #9
0
 def test_same_eq(self):
     a = [3, 4, 5]
     b = [1, 2, 3]
     c = convolve(a, b, mode="same")
     assert_array_equal(c, array([10, 22, 22]))
Example #10
0
 def test_basic(self):
     a = [3, 4, 5, 6, 5, 4]
     b = [1, 2, 3]
     c = convolve(a, b)
     assert_array_equal(c, array([3, 10, 22, 28, 32, 32, 23, 12]))
Example #11
0
 def test_same_mode(self):
     a = [1, 2, 3, 3, 1, 2]
     b = [1, 4, 3, 4, 5, 6, 7, 4, 3, 2, 1, 1, 3]
     c = convolve(a, b, 'same')
     d = array([57, 61, 63, 57, 45, 36])
     assert_array_equal(c, d)