def subset_test(lin_op):
""" Test that subsetting a linear operator produces the correct outputs.
:param LinearOperator lin_op: the linear operator
"""
sub_idx = np.random.rand(lin_op.shape[0], 1) > 0.5
# make sure at least one element included
sub_idx[np.random.randint(0, len(sub_idx))] = True
sub_idx = np.flatnonzero(sub_idx)
sub_lin_op = undertest.get_subset_lin_op(lin_op, sub_idx)
# test projection to subset of indices
x = np.random.randn(lin_op.shape[1], np.random.randint(1, 3))
np.testing.assert_array_almost_equal(sub_lin_op * x, (lin_op * x)[sub_idx, :])
# test back projection from subset of indices
y = np.random.randn(len(sub_idx), np.random.randint(1, 3))
z = np.zeros((lin_op.shape[0], y.shape[1]))
z[sub_idx] = y
np.testing.assert_array_almost_equal(sub_lin_op.rmatvec(y), lin_op.rmatvec(z))
评论列表
文章目录