Esempio n. 1
0
    def test_dot_mm_plus_equals(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            c_bm = gen_BandMat(size)
            diag = None if rand_bool() else randn(size)
            diag_value = np.ones((size,)) if diag is None else diag
            a_full = a_bm.full()
            b_full = b_bm.full()
            c_full = c_bm.full()
            l = c_bm.l
            u = c_bm.u
            array_mem = get_array_mem(a_bm.data, b_bm.data, c_bm.data)
            if diag is not None:
                diag_mem = get_array_mem(diag)

            bm.dot_mm_plus_equals(a_bm, b_bm, c_bm, diag=diag)
            c_full += fl.band_ec(
                l, u,
                np.dot(np.dot(a_full, np.diag(diag_value)), b_full)
            )
            assert_allclose(c_bm.full(), c_full)
            assert get_array_mem(a_bm.data, b_bm.data, c_bm.data) == array_mem
            if diag is not None:
                assert get_array_mem(diag) == diag_mem
Esempio n. 2
0
    def test_band_cTe(self, its=100):
        """Checks band_cTe against its definition and required properties."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_rect = randn(l + u + 1, size)

            mat_rect_new = fl.band_cTe(l, u, mat_rect)
            mat_rect_new_good = fl.band_e(u, l, fl.band_c(l, u, mat_rect).T)
            assert_allequal(mat_rect_new, mat_rect_new_good)
            assert not np.may_share_memory(mat_rect_new, mat_rect)

            # check a property to do with doing band_cTe twice
            assert_allequal(
                fl.band_cTe(u, l, mat_rect_new),
                fl.band_ce(l, u, mat_rect)
            )

            # check version that uses pre-specified target
            mat_rect_new2 = np.empty((l + u + 1, size))
            array_mem = get_array_mem(mat_rect, mat_rect_new2)
            ret = fl.band_cTe(l, u, mat_rect, target_rect=mat_rect_new2)
            self.assertIsNone(ret)
            assert_allequal(mat_rect_new2, mat_rect_new)
            assert get_array_mem(mat_rect, mat_rect_new2) == array_mem
Esempio n. 3
0
    def test_band_cTe(self, its=100):
        """Checks band_cTe against its definition and required properties."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_rect = randn(l + u + 1, size)

            mat_rect_new = fl.band_cTe(l, u, mat_rect)
            mat_rect_new_good = fl.band_e(u, l, fl.band_c(l, u, mat_rect).T)
            assert_allequal(mat_rect_new, mat_rect_new_good)
            assert not np.may_share_memory(mat_rect_new, mat_rect)

            # check a property to do with doing band_cTe twice
            assert_allequal(
                fl.band_cTe(u, l, mat_rect_new),
                fl.band_ce(l, u, mat_rect)
            )

            # check version that uses pre-specified target
            mat_rect_new2 = np.empty((l + u + 1, size))
            array_mem = get_array_mem(mat_rect, mat_rect_new2)
            ret = fl.band_cTe(l, u, mat_rect, target_rect=mat_rect_new2)
            self.assertIsNone(ret)
            assert_allequal(mat_rect_new2, mat_rect_new)
            assert get_array_mem(mat_rect, mat_rect_new2) == array_mem
Esempio n. 4
0
    def test_BandMat_isub(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            a_full = a_bm.full()
            b_full = b_bm.full()
            array_mem = get_array_mem(a_bm.data, b_bm.data)

            if a_bm.l >= b_bm.l and a_bm.u >= b_bm.u:
                a_bm -= b_bm
                a_full -= b_full
                assert_allclose(a_bm.full(), a_full)
                assert get_array_mem(a_bm.data, b_bm.data) == array_mem
            else:
                with self.assertRaises(AssertionError):
                    a_bm -= b_bm

            a_bm -= 0
            a_full -= 0
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data, b_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm -= 1.0
Esempio n. 5
0
    def test_get_array_mem(self, its=50):
        # (FIXME : these are not great tests, since not familiar enough with
        #   numpy internals to know what sorts of changes in memory layout are
        #   possible and how they might arise in a realistic program)
        for it in range(its):
            x = gen_array()
            array_mem = th.get_array_mem(x)
            x *= 2.0
            assert th.get_array_mem(x) == array_mem

            x = gen_array()
            array_mem = th.get_array_mem(x)
            x.shape = x.shape + (1,)
            assert th.get_array_mem(x) != array_mem

            x = gen_array(ranks=[1, 2, 3])
            array_mem = th.get_array_mem(x)
            shape = x.shape
            strides = x.strides
            shape_new = x.T.shape
            strides_new = x.T.strides
            if np.prod(shape_new) != 0:
                x.shape = shape_new
                x.strides = strides_new
                if shape_new != shape or strides_new != strides:
                    # FIXME : re-enable once I understand better when this may
                    #   fail (i.e. when memory may be unexpectedly shared).
                    #assert th.get_array_mem(x) != array_mem
                    pass
Esempio n. 6
0
    def test_BandMat_isub(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            a_full = a_bm.full()
            b_full = b_bm.full()
            array_mem = get_array_mem(a_bm.data, b_bm.data)

            if a_bm.l >= b_bm.l and a_bm.u >= b_bm.u:
                a_bm -= b_bm
                a_full -= b_full
                assert_allclose(a_bm.full(), a_full)
                assert get_array_mem(a_bm.data, b_bm.data) == array_mem
            else:
                with self.assertRaises(AssertionError):
                    a_bm -= b_bm

            a_bm -= 0
            a_full -= 0
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data, b_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm -= 1.0
Esempio n. 7
0
    def test_BandMat_various_idivs(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm //= mult
            a_full //= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm /= mult
            a_full /= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm.__ifloordiv__(mult)
            a_full.__ifloordiv__(mult)
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            # __idiv__ does not exist in python3
            if sys.version_info[0] < 3:
                a_bm = gen_BandMat(size)
                a_full = a_bm.full()
                array_mem = get_array_mem(a_bm.data)
                a_bm.__idiv__(mult)
                a_full.__idiv__(mult)
                assert_allclose(a_bm.full(), a_full)
                assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm.__itruediv__(mult)
            a_full.__itruediv__(mult)
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm //= a_bm
            with self.assertRaises(TypeError):
                a_bm /= a_bm
Esempio n. 8
0
    def test_BandMat_various_idivs(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm //= mult
            a_full //= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm /= mult
            a_full /= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm.__ifloordiv__(mult)
            a_full.__ifloordiv__(mult)
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            # __idiv__ does not exist in python3
            if sys.version_info[0] < 3:
                a_bm = gen_BandMat(size)
                a_full = a_bm.full()
                array_mem = get_array_mem(a_bm.data)
                a_bm.__idiv__(mult)
                a_full.__idiv__(mult)
                assert_allclose(a_bm.full(), a_full)
                assert get_array_mem(a_bm.data) == array_mem

            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)
            a_bm.__itruediv__(mult)
            a_full.__itruediv__(mult)
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm //= a_bm
            with self.assertRaises(TypeError):
                a_bm /= a_bm
Esempio n. 9
0
    def test_dot_mv_plus_equals(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b = randn(size)
            c = randn(size)
            a_full = a_bm.full()
            c_good = c.copy()
            array_mem = get_array_mem(a_bm.data, b, c)

            bm.dot_mv_plus_equals(a_bm, b, c)
            c_good += np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert get_array_mem(a_bm.data, b, c) == array_mem
Esempio n. 10
0
    def test_zero_extra_entries(self, its=100):
        """Checks zero_extra_entries against its equivalent definition."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_rect = randn(l + u + 1, size)
            mat_rect_good = mat_rect.copy()
            array_mem = get_array_mem(mat_rect)

            fl.zero_extra_entries(l, u, mat_rect)
            mat_rect_good[:] = fl.band_e(l, u, fl.band_c(l, u, mat_rect_good))
            assert_allequal(mat_rect, mat_rect_good)
            assert get_array_mem(mat_rect) == array_mem
Esempio n. 11
0
    def test_zero_extra_entries(self, its=100):
        """Checks zero_extra_entries against its equivalent definition."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_rect = randn(l + u + 1, size)
            mat_rect_good = mat_rect.copy()
            array_mem = get_array_mem(mat_rect)

            fl.zero_extra_entries(l, u, mat_rect)
            mat_rect_good[:] = fl.band_e(l, u, fl.band_c(l, u, mat_rect_good))
            assert_allequal(mat_rect, mat_rect_good)
            assert get_array_mem(mat_rect) == array_mem
Esempio n. 12
0
    def test_dot_mv_plus_equals(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b = randn(size)
            c = randn(size)
            a_full = a_bm.full()
            c_good = c.copy()
            array_mem = get_array_mem(a_bm.data, b, c)

            bm.dot_mv_plus_equals(a_bm, b, c)
            c_good += np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert get_array_mem(a_bm.data, b, c) == array_mem
Esempio n. 13
0
    def test_BandMat_plus_equals_band_of(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()
            target_bm = gen_BandMat(size)
            mat_bm = gen_BandMat(size)
            target_full = target_bm.full()
            mat_full = mat_bm.full()
            array_mem = get_array_mem(target_bm.data, mat_bm.data)

            target_bm.plus_equals_band_of(mat_bm, mult)
            target_full += (fl.band_ec(target_bm.l, target_bm.u, mat_full) *
                            mult)
            assert_allclose(target_bm.full(), target_full)
            assert get_array_mem(target_bm.data, mat_bm.data) == array_mem
Esempio n. 14
0
    def test_BandMat_imul(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()
            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)

            a_bm *= mult
            a_full *= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm *= a_bm
Esempio n. 15
0
    def test_BandMat_imul(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()
            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            array_mem = get_array_mem(a_bm.data)

            a_bm *= mult
            a_full *= mult
            assert_allclose(a_bm.full(), a_full)
            assert get_array_mem(a_bm.data) == array_mem

            with self.assertRaises(TypeError):
                a_bm *= a_bm
Esempio n. 16
0
    def test_BandMat_plus_equals_band_of(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mult = randn()
            target_bm = gen_BandMat(size)
            mat_bm = gen_BandMat(size)
            target_full = target_bm.full()
            mat_full = mat_bm.full()
            array_mem = get_array_mem(target_bm.data, mat_bm.data)

            target_bm.plus_equals_band_of(mat_bm, mult)
            target_full += (
                fl.band_ec(target_bm.l, target_bm.u, mat_full) * mult
            )
            assert_allclose(target_bm.full(), target_full)
            assert get_array_mem(target_bm.data, mat_bm.data) == array_mem
Esempio n. 17
0
    def test_band_of_outer_plus_equals(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_vec = randn(size)
            b_vec = randn(size)
            mult = randn()
            mat_bm = gen_BandMat(size)
            mat_full = mat_bm.full()
            l = mat_bm.l
            u = mat_bm.u
            array_mem = get_array_mem(a_vec, b_vec, mat_bm.data)

            bm.band_of_outer_plus_equals(a_vec, b_vec, mat_bm, mult=mult)
            mat_full += fl.band_ec(l, u, np.outer(a_vec, b_vec) * mult)
            assert_allclose(mat_bm.full(), mat_full)
            assert get_array_mem(a_vec, b_vec, mat_bm.data) == array_mem
Esempio n. 18
0
    def test_band_of_outer_plus_equals(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_vec = randn(size)
            b_vec = randn(size)
            mult = randn()
            mat_bm = gen_BandMat(size)
            mat_full = mat_bm.full()
            l = mat_bm.l
            u = mat_bm.u
            array_mem = get_array_mem(a_vec, b_vec, mat_bm.data)

            bm.band_of_outer_plus_equals(a_vec, b_vec, mat_bm, mult=mult)
            mat_full += fl.band_ec(l, u, np.outer(a_vec, b_vec) * mult)
            assert_allclose(mat_bm.full(), mat_full)
            assert get_array_mem(a_vec, b_vec, mat_bm.data) == array_mem
Esempio n. 19
0
    def test_fancy_plus_equals(self, its=100):
        for it in range(its):
            source_size = random.choice([0, 1, randint(10), randint(100)])
            target_size = random.choice([1, randint(1, 10), randint(1, 100)])
            source = randn(source_size)
            target = randn(target_size)
            target_index_seq = randint(target_size, size=source_size)
            array_mem = get_array_mem(target_index_seq, source, target)

            target_good = target.copy()
            for source_index, target_index in enumerate(target_index_seq):
                target_good[target_index] += source[source_index]

            fancy_plus_equals(target_index_seq, source, target)

            assert_allclose(target, target_good)
            assert get_array_mem(target_index_seq, source, target) == array_mem
Esempio n. 20
0
    def test_fancy_plus_equals(self, its=100):
        for it in range(its):
            source_size = random.choice([0, 1, randint(10), randint(100)])
            target_size = random.choice([1, randint(1, 10), randint(1, 100)])
            source = randn(source_size)
            target = randn(target_size)
            target_index_seq = randint(target_size, size=source_size)
            array_mem = get_array_mem(target_index_seq, source, target)

            target_good = target.copy()
            for source_index, target_index in enumerate(target_index_seq):
                target_good[target_index] += source[source_index]

            fancy_plus_equals(target_index_seq, source, target)

            assert_allclose(target, target_good)
            assert get_array_mem(target_index_seq, source, target) == array_mem
Esempio n. 21
0
    def test_dot_mm_plus_equals(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b_bm = gen_BandMat(size)
            c_bm = gen_BandMat(size)
            diag = None if rand_bool() else randn(size)
            diag_value = np.ones((size, )) if diag is None else diag
            a_full = a_bm.full()
            b_full = b_bm.full()
            c_full = c_bm.full()
            l = c_bm.l
            u = c_bm.u
            array_mem = get_array_mem(a_bm.data, b_bm.data, c_bm.data)
            if diag is not None:
                diag_mem = get_array_mem(diag)

            bm.dot_mm_plus_equals(a_bm, b_bm, c_bm, diag=diag)
            c_full += fl.band_ec(
                l, u, np.dot(np.dot(a_full, np.diag(diag_value)), b_full))
            assert_allclose(c_bm.full(), c_full)
            assert get_array_mem(a_bm.data, b_bm.data, c_bm.data) == array_mem
            if diag is not None:
                assert get_array_mem(diag) == diag_mem