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)
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)
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
def test_band_e_linear(self, its=100): """Checks band_e is linear.""" 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)]) # check additive mat_full1 = randn(size, size) mat_full2 = randn(size, size) assert_allclose( fl.band_e(l, u, mat_full1 + mat_full2), fl.band_e(l, u, mat_full1) + fl.band_e(l, u, mat_full2) ) # check homogeneous mat_full = randn(size, size) mult = random.choice([0.0, randn(), randn(), randn()]) assert_allclose( fl.band_e(l, u, mat_full * mult), fl.band_e(l, u, mat_full) * mult ) # check output is a newly-created array mat_rect = fl.band_e(l, u, mat_full) assert not np.may_share_memory(mat_rect, mat_full)
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
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)
def test_BandMat_neg(self, its=100): 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() b_bm = -a_bm b_full = -a_full assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data)
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)
def test_assert_allclose(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_allclose(a0, a0) th.assert_allclose(a0, a1) self.assertRaises(AssertionError, th.assert_allclose, a0, a2) self.assertRaises(AssertionError, th.assert_allclose, a0, a3) self.assertRaises(AssertionError, th.assert_allclose, a0, a4)
def test_cholesky_banded_upper_scipy_test(self): """Basic test copied from scipy.linalg.tests.test_decomp_cholesky.""" # Symmetric positive definite banded matrix `a` a = np.array([[4.0, 1.0, 0.0, 0.0], [1.0, 4.0, 0.5, 0.0], [0.0, 0.5, 4.0, 0.2], [0.0, 0.0, 0.2, 4.0]]) # Banded storage form of `a`. ab = np.array([[-1.0, 1.0, 0.5, 0.2], [4.0, 4.0, 4.0, 4.0]]) c = bla._cholesky_banded(ab, lower=False) ufac = np.zeros_like(a) ufac[range(4), range(4)] = c[-1] ufac[(0, 1, 2), (1, 2, 3)] = c[0, 1:] assert_allclose(a, np.dot(ufac.T, ufac))
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)
def test_BandMat_reverse_view(self, its=100): 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() b_bm = a_bm.reverse_view() b_full = a_full[::-1, ::-1] assert_allclose(b_bm.full(), b_full) assert b_bm.data.base is a_bm.data if size > 0: assert np.may_share_memory(b_bm.data, a_bm.data)
def test_band_e_bm_common(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) num_bms = randint(5) mat_bms = [ gen_BandMat(size) for _ in range(num_bms) ] if num_bms > 0: l = max([ mat_bm.l for mat_bm in mat_bms ]) u = max([ mat_bm.u for mat_bm in mat_bms ]) mat_rects = bm.band_e_bm_common(*mat_bms) for mat_bm, mat_rect in zip(mat_bms, mat_rects): assert_allclose(mat_rect, bm.band_e_bm(l, u, mat_bm))
def test_cholesky_banded_lower_scipy_test(self): """Basic test copied from scipy.linalg.tests.test_decomp_cholesky.""" # Symmetric positive definite banded matrix `a` a = np.array([[4.0, 1.0, 0.0, 0.0], [1.0, 4.0, 0.5, 0.0], [0.0, 0.5, 4.0, 0.2], [0.0, 0.0, 0.2, 4.0]]) # Banded storage form of `a`. ab = np.array([[4.0, 4.0, 4.0, 4.0], [1.0, 0.5, 0.2, -1.0]]) c = bla._cholesky_banded(ab, lower=True) lfac = np.zeros_like(a) lfac[range(4), range(4)] = c[0] lfac[(1, 2, 3), (0, 1, 2)] = c[1, :3] assert_allclose(a, np.dot(lfac, lfac.T))
def test_band_e_bm_common(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) num_bms = randint(5) mat_bms = [gen_BandMat(size) for _ in range(num_bms)] if num_bms > 0: l = max([mat_bm.l for mat_bm in mat_bms]) u = max([mat_bm.u for mat_bm in mat_bms]) mat_rects = bm.band_e_bm_common(*mat_bms) for mat_bm, mat_rect in zip(mat_bms, mat_rects): assert_allclose(mat_rect, bm.band_e_bm(l, u, mat_bm))
def test_BandMat_various_divs(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() b_bm = a_bm // mult b_full = a_full // mult assert b_bm.l == a_bm.l assert b_bm.u == a_bm.u assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data) b_bm = a_bm / mult b_full = a_full / mult assert b_bm.l == a_bm.l assert b_bm.u == a_bm.u assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data) b_bm = a_bm.__floordiv__(mult) b_full = a_full.__floordiv__(mult) assert b_bm.l == a_bm.l assert b_bm.u == a_bm.u assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data) # __div__ does not exist in python3 if sys.version_info[0] < 3: b_bm = a_bm.__div__(mult) b_full = a_full.__div__(mult) assert b_bm.l == a_bm.l assert b_bm.u == a_bm.u assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data) b_bm = a_bm.__truediv__(mult) b_full = a_full.__truediv__(mult) assert b_bm.l == a_bm.l assert b_bm.u == a_bm.u assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data) with self.assertRaises(TypeError): a_bm // a_bm with self.assertRaises(TypeError): a_bm / a_bm with self.assertRaises(TypeError): 1.0 // a_bm with self.assertRaises(TypeError): 1.0 / a_bm
def test_BandMat_sum(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) num_terms = randint(10) a_bms = [ gen_BandMat(size) for _ in range(num_terms) ] a_fulls = [ a_bm.full() for a_bm in a_bms ] b_bm = sum(a_bms) b_full = sum(a_fulls) if num_terms > 0: assert_allclose(b_bm.full(), b_full) for a_bm in a_bms: assert not np.may_share_memory(b_bm.data, a_bm.data)
def test_BandMat_sum(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) num_terms = randint(10) a_bms = [gen_BandMat(size) for _ in range(num_terms)] a_fulls = [a_bm.full() for a_bm in a_bms] b_bm = sum(a_bms) b_full = sum(a_fulls) if num_terms > 0: assert_allclose(b_bm.full(), b_full) for a_bm in a_bms: assert not np.may_share_memory(b_bm.data, a_bm.data)
def test_band_of_inverse(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) mat_bm = gen_pos_def_BandMat(size) depth = mat_bm.l band_of_inv_bm = bla.band_of_inverse(mat_bm) assert not np.may_share_memory(band_of_inv_bm.data, mat_bm.data) band_of_inv_full_good = fl.band_ec( depth, depth, np.eye(0, 0) if size == 0 else la.inv(mat_bm.full())) assert_allclose(band_of_inv_bm.full(), band_of_inv_full_good)
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
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
def test_band_of_inverse(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) mat_bm = gen_pos_def_BandMat(size) depth = mat_bm.l band_of_inv_bm = bla.band_of_inverse(mat_bm) assert not np.may_share_memory(band_of_inv_bm.data, mat_bm.data) band_of_inv_full_good = fl.band_ec( depth, depth, np.eye(0, 0) if size == 0 else la.inv(mat_bm.full()) ) assert_allclose(band_of_inv_bm.full(), band_of_inv_full_good)
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
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
def test_BandMat_embed_as_sub_matrix(self, its=100): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) start = randint(size + 1) end = randint(size + 1) if start > end: start, end = end, start a_bm = gen_BandMat(end - start) a_full = a_bm.full() b_bm = a_bm.embed_as_sub_matrix(start, size) b_full = np.zeros((size, size)) b_full[start:end, start:end] = a_full assert_allclose(b_bm.full(), b_full) assert not np.may_share_memory(b_bm.data, a_bm.data)
def test_band_of_inverse_from_chol(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) chol_bm = gen_chol_factor_BandMat(size) depth = chol_bm.l + chol_bm.u band_of_inv_bm = bla.band_of_inverse_from_chol(chol_bm) assert not np.may_share_memory(band_of_inv_bm.data, chol_bm.data) mat_bm = (bm.dot_mm(chol_bm, chol_bm.T) if chol_bm.u == 0 else bm.dot_mm(chol_bm.T, chol_bm)) band_of_inv_full_good = fl.band_ec( depth, depth, np.eye(0, 0) if size == 0 else la.inv(mat_bm.full())) assert_allclose(band_of_inv_bm.full(), band_of_inv_full_good)
def test_BandMat_sub_matrix_view(self, its=100): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) start = randint(size + 1) end = randint(size + 1) if start > end: start, end = end, start a_bm = gen_BandMat(size) a_full = a_bm.full() b_bm = a_bm.sub_matrix_view(start, end) b_full = a_full[start:end, start:end] assert_allclose(b_bm.full(), b_full) assert b_bm.data.base is a_bm.data if end > start: assert np.may_share_memory(b_bm.data, a_bm.data)
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
def test_solveh(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) b = randn(size) a_bm = gen_pos_def_BandMat(size) a_full = a_bm.full() x = bla.solveh(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, sym_pos=True) assert_allclose(x, x_good) assert not np.may_share_memory(x, a_bm.data) assert not np.may_share_memory(x, b)
def test_band_of_inverse_from_chol(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) chol_bm = gen_chol_factor_BandMat(size) depth = chol_bm.l + chol_bm.u band_of_inv_bm = bla.band_of_inverse_from_chol(chol_bm) assert not np.may_share_memory(band_of_inv_bm.data, chol_bm.data) mat_bm = (bm.dot_mm(chol_bm, chol_bm.T) if chol_bm.u == 0 else bm.dot_mm(chol_bm.T, chol_bm)) band_of_inv_full_good = fl.band_ec( depth, depth, np.eye(0, 0) if size == 0 else la.inv(mat_bm.full()) ) assert_allclose(band_of_inv_bm.full(), band_of_inv_full_good)
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
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))
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)
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
def test__cholesky_banded(self, its=100): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) if rand_bool(): mat_bm = gen_pos_def_BandMat(size, transposed=False) else: mat_bm = gen_symmetric_BandMat(size, transposed=False) # make it a bit more likely to be pos def bm.diag(mat_bm)[:] = np.abs(bm.diag(mat_bm)) + 0.1 depth = mat_bm.l lower = rand_bool() if lower: mat_half_data = mat_bm.data[depth:] else: mat_half_data = mat_bm.data[:(depth + 1)] overwrite = rand_bool() mat_half_data_arg = mat_half_data.copy() try: chol_data = bla._cholesky_banded( mat_half_data_arg, overwrite_ab=overwrite, lower=lower ) except la.LinAlgError as e: # First part of the message is e.g. "2-th leading minor". msgRe = (r'^' + re.escape(str(e)[:15]) + r'.*not positive definite$') with self.assertRaisesRegexp(la.LinAlgError, msgRe): sla.cholesky(mat_bm.full(), lower=lower) else: assert np.shape(chol_data) == (depth + 1, size) if lower: chol_bm = bm.BandMat(depth, 0, chol_data) mat_bm_again = bm.dot_mm(chol_bm, chol_bm.T) else: chol_bm = bm.BandMat(0, depth, chol_data) mat_bm_again = bm.dot_mm(chol_bm.T, chol_bm) assert_allclose(mat_bm_again.full(), mat_bm.full()) if size > 0: self.assertEqual( np.may_share_memory(chol_data, mat_half_data_arg), overwrite ) if not overwrite: assert np.all(mat_half_data_arg == mat_half_data)
def test__cholesky_banded(self, its=100): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) if rand_bool(): mat_bm = gen_pos_def_BandMat(size, transposed=False) else: mat_bm = gen_symmetric_BandMat(size, transposed=False) # make it a bit more likely to be pos def bm.diag(mat_bm)[:] = np.abs(bm.diag(mat_bm)) + 0.1 depth = mat_bm.l lower = rand_bool() if lower: mat_half_data = mat_bm.data[depth:] else: mat_half_data = mat_bm.data[:(depth + 1)] overwrite = rand_bool() mat_half_data_arg = mat_half_data.copy() try: chol_data = bla._cholesky_banded(mat_half_data_arg, overwrite_ab=overwrite, lower=lower) except la.LinAlgError as e: # First part of the message is e.g. "2-th leading minor". msgRe = (r'^' + re.escape(str(e)[:15]) + r'.*not positive definite$') with self.assertRaisesRegexp(la.LinAlgError, msgRe): sla.cholesky(mat_bm.full(), lower=lower) else: assert np.shape(chol_data) == (depth + 1, size) if lower: chol_bm = bm.BandMat(depth, 0, chol_data) mat_bm_again = bm.dot_mm(chol_bm, chol_bm.T) else: chol_bm = bm.BandMat(0, depth, chol_data) mat_bm_again = bm.dot_mm(chol_bm.T, chol_bm) assert_allclose(mat_bm_again.full(), mat_bm.full()) if size > 0: self.assertEqual( np.may_share_memory(chol_data, mat_half_data_arg), overwrite) if not overwrite: assert np.all(mat_half_data_arg == mat_half_data)
def test_sum_overlapping_v_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 = step + random.choice([0, 1, randint(10)]) overlap = width - step vec_size = num_contribs * step + overlap contribs = randn(num_contribs, width) contribs_chunks = chunk_randomly(contribs) target = randn(vec_size) target_orig = target.copy() bmo.sum_overlapping_v_chunked( contribs_chunks, width, target, step=step ) vec_good = bmo.sum_overlapping_v(contribs, step=step) assert_allclose(target, target_orig + vec_good)
def test_cholesky(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) mat_bm = gen_pos_def_BandMat(size) depth = mat_bm.l lower = rand_bool() alternative = rand_bool() chol_bm = bla.cholesky(mat_bm, lower=lower, alternative=alternative) assert chol_bm.l == (depth if lower else 0) assert chol_bm.u == (0 if lower else depth) assert not np.may_share_memory(chol_bm.data, mat_bm.data) if lower != alternative: mat_bm_again = bm.dot_mm(chol_bm, chol_bm.T) else: mat_bm_again = bm.dot_mm(chol_bm.T, chol_bm) assert_allclose(mat_bm_again.full(), mat_bm.full())
def test_solve_triangular(self, its=50): for it in range(its): size = random.choice([0, 1, randint(0, 10), randint(0, 100)]) b = randn(size) chol_bm = gen_chol_factor_BandMat(size) depth = chol_bm.l + chol_bm.u lower = (chol_bm.u == 0) chol_lower_bm = chol_bm if lower else chol_bm.T chol_full = chol_bm.full() x = bla.solve_triangular(chol_bm, b) assert_allclose(bm.dot_mv(chol_bm, x), b) if size == 0: x_good = np.zeros((size,)) else: x_good = sla.solve_triangular(chol_full, b, lower=lower) assert_allclose(x, x_good) assert not np.may_share_memory(x, chol_bm.data) assert not np.may_share_memory(x, b)
def test_sum_overlapping_v_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 = step + random.choice([0, 1, randint(10)]) overlap = width - step vec_size = num_contribs * step + overlap contribs = randn(num_contribs, width) contribs_chunks = chunk_randomly(contribs) target = randn(vec_size) target_orig = target.copy() bmo.sum_overlapping_v_chunked(contribs_chunks, width, target, step=step) vec_good = bmo.sum_overlapping_v(contribs, step=step) assert_allclose(target, target_orig + vec_good)
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)