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)
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_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 )
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)
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_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_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_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_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)
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_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_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)
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