예제 #1
0
    def test_dot_mm_partial(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)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            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_bm = bm.dot_mm_partial(l, u, a_bm, b_bm, diag=diag)
            c_full = fl.band_ec(
                l, u,
                np.dot(np.dot(a_full, np.diag(diag_value)), b_full)
            )
            assert c_bm.l == l
            assert c_bm.u == u
            assert c_bm.size == size
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)
            assert not np.may_share_memory(c_bm.data, b_bm.data)
            if diag is not None:
                assert not np.may_share_memory(c_bm.data, diag)
예제 #2
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
예제 #3
0
    def test_trace_dot(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)
            a_full = a_bm.full()
            b_full = b_bm.full()

            c = bm.trace_dot(a_bm, b_bm)
            c_good = np.trace(np.dot(a_full.T, b_full))
            assert_allclose(c, c_good)
예제 #4
0
    def test_trace_dot(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)
            a_full = a_bm.full()
            b_full = b_bm.full()

            c = bm.trace_dot(a_bm, b_bm)
            c_good = np.trace(np.dot(a_full.T, b_full))
            assert_allclose(c, c_good)
예제 #5
0
    def test_extract_overlapping_m_chunked(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
            chunk_size = random.choice([1, randint(1, 10), randint(1, 10)])

            mat_bm = gen_BandMat(mat_size, l=depth, u=depth)

            indices_remaining = set(range(num_subs))
            submats_all = np.empty((num_subs, width, width))
            for start, end, submats in bmo.extract_overlapping_m_chunked(
                    mat_bm, chunk_size, step=step):
                assert end >= start + 1
                for index in range(start, end):
                    assert index in indices_remaining
                    indices_remaining.remove(index)
                submats_all[start:end] = submats

            submats_good = bmo.extract_overlapping_m(mat_bm, step=step)
            assert_allclose(submats_all, submats_good)
예제 #6
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())
예제 #7
0
    def test_extract_overlapping_m_chunked(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
            chunk_size = random.choice([1, randint(1, 10), randint(1, 10)])

            mat_bm = gen_BandMat(mat_size, l=depth, u=depth)

            indices_remaining = set(range(num_subs))
            submats_all = np.empty((num_subs, width, width))
            for start, end, submats in bmo.extract_overlapping_m_chunked(
                mat_bm, chunk_size, step=step
            ):
                assert end >= start + 1
                for index in range(start, end):
                    assert index in indices_remaining
                    indices_remaining.remove(index)
                submats_all[start:end] = submats

            submats_good = bmo.extract_overlapping_m(mat_bm, step=step)
            assert_allclose(submats_all, submats_good)
예제 #8
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()
                )
예제 #9
0
def gen_symmetric_BandMat(size, depth=None, transposed=None):
    if depth is None:
        depth = random.choice([0, 1, randint(0, 10)])
    if transposed is None:
        transposed = rand_bool()
    a_bm = gen_BandMat(size, l=depth, u=depth, transposed=transposed)
    b_bm = a_bm + a_bm.T
    randomize_extra_entries_bm(b_bm)
    return b_bm
예제 #10
0
def gen_symmetric_BandMat(size, depth=None, transposed=None):
    if depth is None:
        depth = random.choice([0, 1, randint(0, 10)])
    if transposed is None:
        transposed = rand_bool()
    a_bm = gen_BandMat(size, l=depth, u=depth, transposed=transposed)
    b_bm = a_bm + a_bm.T
    randomize_extra_entries_bm(b_bm)
    return b_bm
예제 #11
0
    def test_dot_mm(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)
            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_bm = bm.dot_mm(a_bm, b_bm, diag=diag)
            c_full = np.dot(np.dot(a_full, np.diag(diag_value)), b_full)
            assert c_bm.l == a_bm.l + b_bm.l
            assert c_bm.u == a_bm.u + b_bm.u
            assert c_bm.size == size
            assert_allclose(c_bm.full(), c_full)
            assert not np.may_share_memory(c_bm.data, a_bm.data)
            assert not np.may_share_memory(c_bm.data, b_bm.data)
            if diag is not None:
                assert not np.may_share_memory(c_bm.data, diag)
예제 #12
0
    def test_dot_mmm_partial(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)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            a_full = a_bm.full()
            b_full = b_bm.full()
            c_full = c_bm.full()

            d_bm = bm.dot_mmm_partial(l, u, a_bm, b_bm, c_bm)
            d_full = fl.band_ec(l, u, np.dot(a_full, np.dot(b_full, c_full)))
            assert d_bm.l == l
            assert d_bm.u == u
            assert d_bm.size == size
            assert_allclose(d_bm.full(), d_full)
            assert not np.may_share_memory(d_bm.data, a_bm.data)
            assert not np.may_share_memory(d_bm.data, b_bm.data)
            assert not np.may_share_memory(d_bm.data, c_bm.data)
예제 #13
0
    def test_dot_mv(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)
            a_full = a_bm.full()

            c = bm.dot_mv(a_bm, b)
            c_good = np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert not np.may_share_memory(c, a_bm.data)
            assert not np.may_share_memory(c, b)
예제 #14
0
    def test_dot_mmm_partial(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)
            l = random.choice([0, 1, randint(0, 10)])
            u = random.choice([0, 1, randint(0, 10)])
            a_full = a_bm.full()
            b_full = b_bm.full()
            c_full = c_bm.full()

            d_bm = bm.dot_mmm_partial(l, u, a_bm, b_bm, c_bm)
            d_full = fl.band_ec(l, u, np.dot(a_full, np.dot(b_full, c_full)))
            assert d_bm.l == l
            assert d_bm.u == u
            assert d_bm.size == size
            assert_allclose(d_bm.full(), d_full)
            assert not np.may_share_memory(d_bm.data, a_bm.data)
            assert not np.may_share_memory(d_bm.data, b_bm.data)
            assert not np.may_share_memory(d_bm.data, c_bm.data)
예제 #15
0
    def test_dot_mv(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)
            a_full = a_bm.full()

            c = bm.dot_mv(a_bm, b)
            c_good = np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert not np.may_share_memory(c, a_bm.data)
            assert not np.may_share_memory(c, b)
예제 #16
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
예제 #17
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
예제 #18
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
예제 #19
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
예제 #20
0
def gen_pos_def_BandMat(size, depth=None, contrib_rank=2, transposed=None):
    """Generates a random positive definite BandMat."""
    assert contrib_rank >= 0
    if depth is None:
        depth = random.choice([0, 1, randint(0, 10)])
    if transposed is None:
        transposed = rand_bool()
    mat_bm = bm.zeros(depth, depth, size)
    for _ in range(contrib_rank):
        diff = randint(0, depth + 1)
        chol_bm = gen_BandMat(size, l=depth - diff, u=diff)
        bm.dot_mm_plus_equals(chol_bm, chol_bm.T, mat_bm)
    if transposed:
        mat_bm = mat_bm.T
    randomize_extra_entries_bm(mat_bm)
    return mat_bm
예제 #21
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
예제 #22
0
def gen_pos_def_BandMat(size, depth=None, contrib_rank=2, transposed=None):
    """Generates a random positive definite BandMat."""
    assert contrib_rank >= 0
    if depth is None:
        depth = random.choice([0, 1, randint(0, 10)])
    if transposed is None:
        transposed = rand_bool()
    mat_bm = bm.zeros(depth, depth, size)
    for _ in range(contrib_rank):
        diff = randint(0, depth + 1)
        chol_bm = gen_BandMat(size, l=depth - diff, u=diff)
        bm.dot_mm_plus_equals(chol_bm, chol_bm.T, mat_bm)
    if transposed:
        mat_bm = mat_bm.T
    randomize_extra_entries_bm(mat_bm)
    return mat_bm
예제 #23
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))
예제 #24
0
    def test_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            # the below tries to ensure the matrix is well-conditioned
            a_bm = gen_BandMat(size) + bm.diag(np.ones((size,)) * 10.0)
            a_full = a_bm.full()

            x = bla.solve(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)
예제 #25
0
    def test_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            # the below tries to ensure the matrix is well-conditioned
            a_bm = gen_BandMat(size) + bm.diag(np.ones((size,)) * 10.0)
            a_full = a_bm.full()

            x = bla.solve(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)
예제 #26
0
    def test_sum_overlapping_m_chunked(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)
            contribs_chunks = chunk_randomly(contribs)
            target_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target_bm_orig = target_bm.copy()

            bmo.sum_overlapping_m_chunked(
                contribs_chunks, target_bm, step=step
            )
            mat_bm_good = bmo.sum_overlapping_m(contribs, step=step)
            assert_allclose(*cc(target_bm, target_bm_orig + mat_bm_good))
예제 #27
0
    def test_sum_overlapping_m_chunked(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)
            contribs_chunks = chunk_randomly(contribs)
            target_bm = gen_BandMat(mat_size, l=depth, u=depth)
            target_bm_orig = target_bm.copy()

            bmo.sum_overlapping_m_chunked(contribs_chunks,
                                          target_bm,
                                          step=step)
            mat_bm_good = bmo.sum_overlapping_m(contribs, step=step)
            assert_allclose(*cc(target_bm, target_bm_orig + mat_bm_good))
예제 #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))