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