Ejemplo n.º 1
0
    def test_extract_overlapping_m(self, its=50):
        for it in range(its):
            num_subs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(1, 10)])
            width = step + random.choice([0, 1, randint(10)])
            depth = width - 1
            assert depth >= 0
            overlap = width - step
            mat_size = num_subs * step + overlap

            mat_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target = None if rand_bool() else randn(num_subs, width, width)

            if target is None:
                submats = bmo.extract_overlapping_m(mat_bm, step=step)
                assert submats.shape == (num_subs, width, width)
            else:
                bmo.extract_overlapping_m(mat_bm, step=step, target=target)
                submats = target

            for index in range(num_subs):
                assert_allequal(
                    submats[index],
                    mat_bm.sub_matrix_view(index * step,
                                           index * step + width).full())
Ejemplo n.º 2
0
    def test_extract_overlapping_m(self, its=50):
        for it in range(its):
            num_subs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(1, 10)])
            width = step + random.choice([0, 1, randint(10)])
            depth = width - 1
            assert depth >= 0
            overlap = width - step
            mat_size = num_subs * step + overlap

            mat_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target = None if rand_bool() else randn(num_subs, width, width)

            if target is None:
                submats = bmo.extract_overlapping_m(mat_bm, step=step)
                assert submats.shape == (num_subs, width, width)
            else:
                bmo.extract_overlapping_m(mat_bm, step=step, target=target)
                submats = target

            for index in range(num_subs):
                assert_allequal(
                    submats[index],
                    mat_bm.sub_matrix_view(
                        index * step, index * step + width
                    ).full()
                )
Ejemplo n.º 3
0
    def test_randomize_extra_entries_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat_simple(size)

            mat_full = mat_bm.full()
            th.randomize_extra_entries_bm(mat_bm)
            th.assert_allequal(mat_bm.full(), mat_full)
Ejemplo n.º 4
0
    def test_BandMat_T(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)

            assert_allequal(a_bm.T.full(), a_bm.full().T)

            assert a_bm.T.data is a_bm.data
            if size > 0:
                assert np.may_share_memory(a_bm.T.data, a_bm.data)
Ejemplo n.º 5
0
    def test_zeros(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            mat_bm = bm.zeros(l, u, size)
            assert mat_bm.l == l
            assert mat_bm.u == u
            assert_allequal(mat_bm.full(), np.zeros((size, size)))
Ejemplo n.º 6
0
    def test_zeros(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            mat_bm = bm.zeros(l, u, size)
            assert mat_bm.l == l
            assert mat_bm.u == u
            assert_allequal(mat_bm.full(), np.zeros((size, size)))
Ejemplo n.º 7
0
    def test_BandMat_T(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)

            assert_allequal(a_bm.T.full(), a_bm.full().T)

            assert a_bm.T.data is a_bm.data
            if size > 0:
                assert np.may_share_memory(a_bm.T.data, a_bm.data)
Ejemplo n.º 8
0
 def test_assert_allequal(self):
     a0 = np.array([2.0, 3.0, 4.0])
     a1 = np.array([2.0, 3.0, 4.0])
     a2 = np.array([2.0, 3.0])
     a3 = np.array([2.0, 3.0, 5.0])
     a4 = np.array([[2.0, 3.0, 4.0]])
     th.assert_allequal(a0, a0)
     th.assert_allequal(a0, a1)
     self.assertRaises(AssertionError, th.assert_allequal, a0, a2)
     self.assertRaises(AssertionError, th.assert_allequal, a0, a3)
     self.assertRaises(AssertionError, th.assert_allequal, a0, a4)
Ejemplo n.º 9
0
    def test_band_ec_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            b_bm = bm.band_ec_bm(l, u, a_bm)

            b_full_good = fl.band_ec(l, u, a_bm.full())
            assert_allequal(b_bm.full(), b_full_good)
            assert not np.may_share_memory(b_bm.data, a_bm.data)
Ejemplo n.º 10
0
    def test_band_e_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            mat_rect = bm.band_e_bm(l, u, mat_bm)

            mat_rect_good = fl.band_e(l, u, mat_bm.full())
            assert_allequal(mat_rect, mat_rect_good)
            assert not np.may_share_memory(mat_rect, mat_bm.data)
Ejemplo n.º 11
0
    def test_band_c_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            mat_rect = randn(l + u + 1, size)

            mat_bm = bm.band_c_bm(l, u, mat_rect)

            mat_full_good = fl.band_c(l, u, mat_rect)
            assert_allequal(mat_bm.full(), mat_full_good)
            assert not np.may_share_memory(mat_bm.data, mat_rect)
Ejemplo n.º 12
0
    def test_band_c_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            mat_rect = randn(l + u + 1, size)

            mat_bm = bm.band_c_bm(l, u, mat_rect)

            mat_full_good = fl.band_c(l, u, mat_rect)
            assert_allequal(mat_bm.full(), mat_full_good)
            assert not np.may_share_memory(mat_bm.data, mat_rect)
Ejemplo n.º 13
0
    def test_band_e_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            mat_rect = bm.band_e_bm(l, u, mat_bm)

            mat_rect_good = fl.band_e(l, u, mat_bm.full())
            assert_allequal(mat_rect, mat_rect_good)
            assert not np.may_share_memory(mat_rect, mat_bm.data)
Ejemplo n.º 14
0
    def test_band_ec_bm(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])

            b_bm = bm.band_ec_bm(l, u, a_bm)

            b_full_good = fl.band_ec(l, u, a_bm.full())
            assert_allequal(b_bm.full(), b_full_good)
            assert not np.may_share_memory(b_bm.data, a_bm.data)
Ejemplo n.º 15
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
Ejemplo n.º 16
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
Ejemplo n.º 17
0
    def test_band_ec(self, its=100):
        """Checks band_ec 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_full = randn(size, size)

            mat_full_new = fl.band_ec(l, u, mat_full)
            mat_full_new_good = fl.band_c(l, u, fl.band_e(l, u, mat_full))
            assert_allequal(mat_full_new, mat_full_new_good)
            assert not np.may_share_memory(mat_full_new, mat_full)

            # check idempotent
            assert_allequal(fl.band_ec(l, u, mat_full_new), mat_full_new)
Ejemplo n.º 18
0
    def test_from_full(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            mat_full = gen_BandMat(size).full()
            zero_outside_band = np.all(fl.band_ec(l, u, mat_full) == mat_full)

            if zero_outside_band:
                mat_bm = bm.from_full(l, u, mat_full)
                assert mat_bm.l == l
                assert mat_bm.u == u
                assert_allequal(mat_bm.full(), mat_full)
                assert not np.may_share_memory(mat_bm.data, mat_full)
            else:
                self.assertRaises(AssertionError, bm.from_full, l, u, mat_full)
Ejemplo n.º 19
0
    def test_from_full(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            mat_full = gen_BandMat(size).full()
            zero_outside_band = np.all(fl.band_ec(l, u, mat_full) == mat_full)

            if zero_outside_band:
                mat_bm = bm.from_full(l, u, mat_full)
                assert mat_bm.l == l
                assert mat_bm.u == u
                assert_allequal(mat_bm.full(), mat_full)
                assert not np.may_share_memory(mat_bm.data, mat_full)
            else:
                self.assertRaises(AssertionError, bm.from_full, l, u, mat_full)
Ejemplo n.º 20
0
    def test_sum_overlapping_m(self, its=50):
        for it in range(its):
            num_contribs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(2), randint(10)])
            width = max(step, 1) + random.choice([0, 1, randint(10)])
            depth = width - 1
            assert depth >= 0
            overlap = width - step
            mat_size = num_contribs * step + overlap

            contribs = randn(num_contribs, width, width)
            target_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target_bm_orig = target_bm.copy()

            mat_bm = bmo.sum_overlapping_m(contribs, step=step)
            assert mat_bm.size == mat_size
            assert mat_bm.l == mat_bm.u == depth

            # check target-based version adds to target_bm correctly
            bmo.sum_overlapping_m(contribs, step=step, target_bm=target_bm)
            assert_allclose(*cc(target_bm, target_bm_orig + mat_bm))

            if num_contribs == 0:
                # check action for no contributions
                assert_allequal(mat_bm.full(), np.zeros((overlap, overlap)))
            elif num_contribs == 1:
                # check action for a single contribution
                assert_allequal(mat_bm.full(), contribs[0])
            else:
                # check action under splitting list of contributions in two
                split_pos = randint(num_contribs + 1)
                mat_bm_again = bm.zeros(depth, depth, mat_size)
                bmo.sum_overlapping_m(
                    contribs[:split_pos],
                    step=step,
                    target_bm=mat_bm_again.sub_matrix_view(
                        0, split_pos * step + overlap
                    )
                )
                bmo.sum_overlapping_m(
                    contribs[split_pos:],
                    step=step,
                    target_bm=mat_bm_again.sub_matrix_view(
                        split_pos * step, mat_size
                    )
                )
                assert_allclose(*cc(mat_bm, mat_bm_again))
Ejemplo n.º 21
0
    def test_diag(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])

            vec = randn(size)
            mat_bm = bm.diag(vec)
            assert isinstance(mat_bm, bm.BandMat)
            assert_allequal(mat_bm.full(), np.diag(vec))
            assert mat_bm.data.base is vec
            if size > 0:
                assert np.may_share_memory(mat_bm.data, vec)

            mat_bm = gen_BandMat(size)
            vec = bm.diag(mat_bm)
            assert_allequal(vec, np.diag(mat_bm.full()))
            assert vec.base is mat_bm.data
            if size > 0:
                assert np.may_share_memory(vec, mat_bm.data)
Ejemplo n.º 22
0
    def test_BandMat_full(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            l, u = a_bm.l, a_bm.u

            # N.B. these tests are not really testing much of anything (they
            #   are virtually identical to the implementation of BandMat.full),
            #   but this is not that surprising since the lines below are kind
            #   of the definition of the representation used by BandMat in the
            #   two cases (transposed True and transposed False).
            if a_bm.transposed:
                assert_allequal(a_full.T, fl.band_c(u, l, a_bm.data))
            else:
                assert_allequal(a_full, fl.band_c(l, u, a_bm.data))

            assert not np.may_share_memory(a_full, a_bm.data)
Ejemplo n.º 23
0
    def test_diag(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])

            vec = randn(size)
            mat_bm = bm.diag(vec)
            assert isinstance(mat_bm, bm.BandMat)
            assert_allequal(mat_bm.full(), np.diag(vec))
            assert mat_bm.data.base is vec
            if size > 0:
                assert np.may_share_memory(mat_bm.data, vec)

            mat_bm = gen_BandMat(size)
            vec = bm.diag(mat_bm)
            assert_allequal(vec, np.diag(mat_bm.full()))
            assert vec.base is mat_bm.data
            if size > 0:
                assert np.may_share_memory(vec, mat_bm.data)
Ejemplo n.º 24
0
    def test_BandMat_full(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            a_full = a_bm.full()
            l, u = a_bm.l, a_bm.u

            # N.B. these tests are not really testing much of anything (they
            #   are virtually identical to the implementation of BandMat.full),
            #   but this is not that surprising since the lines below are kind
            #   of the definition of the representation used by BandMat in the
            #   two cases (transposed True and transposed False).
            if a_bm.transposed:
                assert_allequal(a_full.T, fl.band_c(u, l, a_bm.data))
            else:
                assert_allequal(a_full, fl.band_c(l, u, a_bm.data))

            assert not np.may_share_memory(a_full, a_bm.data)
Ejemplo n.º 25
0
    def test_band_ec(self, its=100):
        """Checks band_ec 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_full = randn(size, size)

            mat_full_new = fl.band_ec(l, u, mat_full)
            mat_full_new_good = fl.band_c(l, u, fl.band_e(l, u, mat_full))
            assert_allequal(mat_full_new, mat_full_new_good)
            assert not np.may_share_memory(mat_full_new, mat_full)

            # check idempotent
            assert_allequal(
                fl.band_ec(l, u, mat_full_new),
                mat_full_new
            )
Ejemplo n.º 26
0
    def test_BandMat_copy(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            mat_full_orig = mat_bm.full().copy()

            mat_bm_new = mat_bm.copy()
            assert mat_bm_new.l == mat_bm.l
            assert mat_bm_new.u == mat_bm.u
            assert not mat_bm_new.transposed

            # check that copy represents the same matrix
            assert_allequal(mat_bm_new.full(), mat_full_orig)

            # check that copy does not share memory with original
            assert not np.may_share_memory(mat_bm_new.data, mat_bm.data)

            # check that mutating the copy does not change the original
            mat_bm_new.data += 1.0
            assert_allequal(mat_bm.full(), mat_full_orig)
Ejemplo n.º 27
0
    def test_BandMat_copy_exact(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            mat_full_orig = mat_bm.full().copy()

            mat_bm_new = mat_bm.copy_exact()
            assert mat_bm_new.l == mat_bm.l
            assert mat_bm_new.u == mat_bm.u
            assert mat_bm_new.transposed == mat_bm.transposed

            # check that copy represents the same matrix
            assert_allequal(mat_bm_new.full(), mat_full_orig)

            # check that copy does not share memory with original
            assert not np.may_share_memory(mat_bm_new.data, mat_bm.data)

            # check that mutating the copy does not change the original
            mat_bm_new.data += 1.0
            assert_allequal(mat_bm.full(), mat_full_orig)
Ejemplo n.º 28
0
    def test_sum_overlapping_m(self, its=50):
        for it in range(its):
            num_contribs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(2), randint(10)])
            width = max(step, 1) + random.choice([0, 1, randint(10)])
            depth = width - 1
            assert depth >= 0
            overlap = width - step
            mat_size = num_contribs * step + overlap

            contribs = randn(num_contribs, width, width)
            target_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target_bm_orig = target_bm.copy()

            mat_bm = bmo.sum_overlapping_m(contribs, step=step)
            assert mat_bm.size == mat_size
            assert mat_bm.l == mat_bm.u == depth

            # check target-based version adds to target_bm correctly
            bmo.sum_overlapping_m(contribs, step=step, target_bm=target_bm)
            assert_allclose(*cc(target_bm, target_bm_orig + mat_bm))

            if num_contribs == 0:
                # check action for no contributions
                assert_allequal(mat_bm.full(), np.zeros((overlap, overlap)))
            elif num_contribs == 1:
                # check action for a single contribution
                assert_allequal(mat_bm.full(), contribs[0])
            else:
                # check action under splitting list of contributions in two
                split_pos = randint(num_contribs + 1)
                mat_bm_again = bm.zeros(depth, depth, mat_size)
                bmo.sum_overlapping_m(contribs[:split_pos],
                                      step=step,
                                      target_bm=mat_bm_again.sub_matrix_view(
                                          0, split_pos * step + overlap))
                bmo.sum_overlapping_m(contribs[split_pos:],
                                      step=step,
                                      target_bm=mat_bm_again.sub_matrix_view(
                                          split_pos * step, mat_size))
                assert_allclose(*cc(mat_bm, mat_bm_again))
Ejemplo n.º 29
0
    def test_extract_overlapping_v(self, its=50):
        for it in range(its):
            num_subs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(1, 10)])
            width = step + random.choice([0, 1, randint(10)])
            overlap = width - step
            vec_size = num_subs * step + overlap

            vec = randn(vec_size)
            target = None if rand_bool() else randn(num_subs, width)

            if target is None:
                subvectors = bmo.extract_overlapping_v(vec, width, step=step)
                assert subvectors.shape == (num_subs, width)
            else:
                bmo.extract_overlapping_v(vec, width, step=step, target=target)
                subvectors = target

            for index in range(num_subs):
                assert_allequal(subvectors[index],
                                vec[(index * step):(index * step + width)])
Ejemplo n.º 30
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
Ejemplo n.º 31
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
Ejemplo n.º 32
0
    def test_sum_overlapping_v(self, its=50):
        for it in range(its):
            num_contribs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(2), randint(10)])
            width = step + random.choice([0, 1, randint(10)])
            overlap = width - step
            vec_size = num_contribs * step + overlap

            contribs = randn(num_contribs, width)
            target = randn(vec_size)
            target_orig = target.copy()

            vec = bmo.sum_overlapping_v(contribs, step=step)
            assert vec.shape == (vec_size,)

            # check target-based version adds to target correctly
            bmo.sum_overlapping_v(contribs, step=step, target=target)
            assert_allclose(target, target_orig + vec)

            if num_contribs == 0:
                # check action for no contributions
                assert_allequal(vec, np.zeros((overlap,)))
            elif num_contribs == 1:
                # check action for a single contribution
                assert_allequal(vec, contribs[0])
            else:
                # check action under splitting list of contributions in two
                split_pos = randint(num_contribs + 1)
                vec_again = np.zeros((vec_size,))
                bmo.sum_overlapping_v(
                    contribs[:split_pos],
                    step=step,
                    target=vec_again[0:(split_pos * step + overlap)]
                )
                bmo.sum_overlapping_v(
                    contribs[split_pos:],
                    step=step,
                    target=vec_again[(split_pos * step):vec_size]
                )
                assert_allclose(vec, vec_again)
Ejemplo n.º 33
0
    def test_band_e_basis(self, its=100):
        """Checks band_e behaves correctly on canonical basis matrices."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            # size >= 1 (there are no canonical basis matrices if size == 0)
            size = random.choice([1, 2, randint(1, 10), randint(1, 100)])

            # pick a random canonical basis matrix
            k = randint(size)
            j = randint(size)
            mat_full = np.zeros((size, size))
            mat_full[k, j] = 1.0

            mat_rect = fl.band_e(l, u, mat_full)

            i = k - j
            mat_rect_good = np.zeros((l + u + 1, size))
            if -u <= i <= l:
                mat_rect_good[u + i, j] = 1.0

            assert_allequal(mat_rect, mat_rect_good)
Ejemplo n.º 34
0
    def test_band_c_basis(self, its=100):
        """Checks band_c behaves correctly on canonical basis matrices."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            # size >= 1 (there are no canonical basis matrices if size == 0)
            size = random.choice([1, 2, randint(1, 10), randint(1, 100)])

            # pick a random canonical basis matrix
            i = randint(-u, l + 1)
            j = randint(size)
            mat_rect = np.zeros((l + u + 1, size))
            mat_rect[u + i, j] = 1.0

            mat_full = fl.band_c(l, u, mat_rect)

            k = i + j
            mat_full_good = np.zeros((size, size))
            if 0 <= k < size:
                mat_full_good[k, j] = 1.0

            assert_allequal(mat_full, mat_full_good)
Ejemplo n.º 35
0
    def test_band_c_basis(self, its=100):
        """Checks band_c behaves correctly on canonical basis matrices."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            # size >= 1 (there are no canonical basis matrices if size == 0)
            size = random.choice([1, 2, randint(1, 10), randint(1, 100)])

            # pick a random canonical basis matrix
            i = randint(-u, l + 1)
            j = randint(size)
            mat_rect = np.zeros((l + u + 1, size))
            mat_rect[u + i, j] = 1.0

            mat_full = fl.band_c(l, u, mat_rect)

            k = i + j
            mat_full_good = np.zeros((size, size))
            if 0 <= k < size:
                mat_full_good[k, j] = 1.0

            assert_allequal(mat_full, mat_full_good)
Ejemplo n.º 36
0
    def test_band_e_basis(self, its=100):
        """Checks band_e behaves correctly on canonical basis matrices."""
        for it in range(its):
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            # size >= 1 (there are no canonical basis matrices if size == 0)
            size = random.choice([1, 2, randint(1, 10), randint(1, 100)])

            # pick a random canonical basis matrix
            k = randint(size)
            j = randint(size)
            mat_full = np.zeros((size, size))
            mat_full[k, j] = 1.0

            mat_rect = fl.band_e(l, u, mat_full)

            i = k - j
            mat_rect_good = np.zeros((l + u + 1, size))
            if -u <= i <= l:
                mat_rect_good[u + i, j] = 1.0

            assert_allequal(mat_rect, mat_rect_good)
Ejemplo n.º 37
0
    def test_sum_overlapping_v(self, its=50):
        for it in range(its):
            num_contribs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(2), randint(10)])
            width = step + random.choice([0, 1, randint(10)])
            overlap = width - step
            vec_size = num_contribs * step + overlap

            contribs = randn(num_contribs, width)
            target = randn(vec_size)
            target_orig = target.copy()

            vec = bmo.sum_overlapping_v(contribs, step=step)
            assert vec.shape == (vec_size, )

            # check target-based version adds to target correctly
            bmo.sum_overlapping_v(contribs, step=step, target=target)
            assert_allclose(target, target_orig + vec)

            if num_contribs == 0:
                # check action for no contributions
                assert_allequal(vec, np.zeros((overlap, )))
            elif num_contribs == 1:
                # check action for a single contribution
                assert_allequal(vec, contribs[0])
            else:
                # check action under splitting list of contributions in two
                split_pos = randint(num_contribs + 1)
                vec_again = np.zeros((vec_size, ))
                bmo.sum_overlapping_v(contribs[:split_pos],
                                      step=step,
                                      target=vec_again[0:(split_pos * step +
                                                          overlap)])
                bmo.sum_overlapping_v(contribs[split_pos:],
                                      step=step,
                                      target=vec_again[(split_pos *
                                                        step):vec_size])
                assert_allclose(vec, vec_again)
Ejemplo n.º 38
0
    def test_BandMat_equiv(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            l_new = random.choice([None, 0, 1, randint(0, 10)])
            u_new = random.choice([None, 0, 1, randint(0, 10)])
            transposed_new = random.choice([None, True, False])
            zero_extra = rand_bool()

            l_new_value = mat_bm.l if l_new is None else l_new
            u_new_value = mat_bm.u if u_new is None else u_new
            transposed_new_value = (mat_bm.transposed if transposed_new is None
                                    else transposed_new)

            if l_new_value < mat_bm.l or u_new_value < mat_bm.u:
                self.assertRaises(AssertionError,
                                  mat_bm.equiv,
                                  l_new=l_new,
                                  u_new=u_new,
                                  transposed_new=transposed_new,
                                  zero_extra=zero_extra)
            else:
                mat_bm_new = mat_bm.equiv(l_new=l_new,
                                          u_new=u_new,
                                          transposed_new=transposed_new,
                                          zero_extra=zero_extra)
                assert mat_bm_new.l == l_new_value
                assert mat_bm_new.u == u_new_value
                assert mat_bm_new.transposed == transposed_new_value
                assert_allequal(mat_bm_new.full(), mat_bm.full())
                assert not np.may_share_memory(mat_bm_new.data, mat_bm.data)

                if zero_extra:
                    mat_new_data_good = (fl.band_e(
                        u_new_value, l_new_value,
                        mat_bm.full().T)) if mat_bm_new.transposed else (
                            fl.band_e(l_new_value, u_new_value, mat_bm.full()))
                    assert_allequal(mat_bm_new.data, mat_new_data_good)
Ejemplo n.º 39
0
    def test_extract_overlapping_v(self, its=50):
        for it in range(its):
            num_subs = random.choice([0, 1, randint(10), randint(100)])
            step = random.choice([1, randint(1, 10)])
            width = step + random.choice([0, 1, randint(10)])
            overlap = width - step
            vec_size = num_subs * step + overlap

            vec = randn(vec_size)
            target = None if rand_bool() else randn(num_subs, width)

            if target is None:
                subvectors = bmo.extract_overlapping_v(vec, width, step=step)
                assert subvectors.shape == (num_subs, width)
            else:
                bmo.extract_overlapping_v(vec, width, step=step, target=target)
                subvectors = target

            for index in range(num_subs):
                assert_allequal(
                    subvectors[index],
                    vec[(index * step):(index * step + width)]
                )
Ejemplo n.º 40
0
    def test_BandMat_equiv(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            mat_bm = gen_BandMat(size)
            l_new = random.choice([None, 0, 1, randint(0, 10)])
            u_new = random.choice([None, 0, 1, randint(0, 10)])
            transposed_new = random.choice([None, True, False])
            zero_extra = rand_bool()

            l_new_value = mat_bm.l if l_new is None else l_new
            u_new_value = mat_bm.u if u_new is None else u_new
            transposed_new_value = (mat_bm.transposed if transposed_new is None
                                    else transposed_new)

            if l_new_value < mat_bm.l or u_new_value < mat_bm.u:
                self.assertRaises(AssertionError,
                                  mat_bm.equiv,
                                  l_new=l_new, u_new=u_new,
                                  transposed_new=transposed_new,
                                  zero_extra=zero_extra)
            else:
                mat_bm_new = mat_bm.equiv(l_new=l_new, u_new=u_new,
                                          transposed_new=transposed_new,
                                          zero_extra=zero_extra)
                assert mat_bm_new.l == l_new_value
                assert mat_bm_new.u == u_new_value
                assert mat_bm_new.transposed == transposed_new_value
                assert_allequal(mat_bm_new.full(), mat_bm.full())
                assert not np.may_share_memory(mat_bm_new.data, mat_bm.data)

                if zero_extra:
                    mat_new_data_good = (
                        fl.band_e(u_new_value, l_new_value, mat_bm.full().T)
                    ) if mat_bm_new.transposed else (
                        fl.band_e(l_new_value, u_new_value, mat_bm.full())
                    )
                    assert_allequal(mat_bm_new.data, mat_new_data_good)