def test_fsc3(): mat = FSCMatrix((3, 4), 10) assert_equal(mat.data.shape, (4, 10)) assert_equal(mat.data.index.dtype, int) assert_equal(mat.data.value.dtype, float) mat = FSCMatrix((3, 4), 10, dtype=np.float16, dtype_index=np.int8) assert_equal(mat.data.index.dtype, np.int8) assert_equal(mat.data.value.dtype, np.float16)
def test_fsc_error(): data = np.zeros((3, 4), [('index', int), ('value', float)]) data_wrong1 = np.empty((3, 4), [('index_', int), ('value', float)]) data_wrong2 = np.empty((3, 4), [('value', float), ('index', int)]) assert_raises(TypeError, FSCMatrix, 3) assert_raises(ValueError, FSCMatrix, (2, 3, 4)) assert_raises(ValueError, FSCMatrix, (3, 8), data=data) assert_raises(TypeError, FSCMatrix, (8, 3), data=data_wrong1) assert_raises(TypeError, FSCMatrix, (8, 3), data=data_wrong2) assert_raises(ValueError, FSCMatrix, (8, 3), 5, data=data) FSCMatrix((8, 3), 4, data=data) assert_raises(ValueError, FSCMatrix, (8, 3)) mat = FSCMatrix((8, 3), data=data) mat._matvec(np.ones(3)) assert_raises(ValueError, mat._matvec, np.ones(7)) assert_raises(TypeError, mat._matvec, np.ones(3), out=1) assert_raises(ValueError, mat._matvec, np.ones(3), out=np.zeros(7))
def get_mat(itype, ftype): if np.dtype(itype).kind != 'u': ind = index1, index2 else: ind = index1_u, index2_u dtype = [('index', itype), ('value', ftype)] matrix = np.recarray((6, 2), dtype=dtype) matrix[..., 0].index, matrix[..., 1].index = ind matrix[..., 0].value, matrix[..., 1].value = value1, value2 return FSCMatrix((4, 6), data=matrix)
def func(itype, ftype, vtype, block_size): if np.dtype(itype).kind != 'u': ind = index exp = expected else: ind = index_u exp = expected_u input_ = np.array(input, vtype) if block_size == 2: input_ = np.array([input_, input_]).T.ravel() exp = np.array([exp, exp]).T.ravel() dtype = [('index', itype), ('value', ftype)] matrix = np.recarray((6, 1), dtype=dtype) matrix[..., 0].index = ind matrix[..., 0].value = value op = FSCMatrix((4, 6), data=matrix) out = op * np.array(input_, vtype) assert_same(out, exp) out[...] = 0 op._matvec(np.array(input_, vtype), out=out) assert_same(out, exp)
def func1(itype, ftype, vtype, block_size): input_fsc_ = np.asarray(input_fsc, vtype) input_fsr_ = np.asarray(input_fsr, vtype) if np.dtype(itype).kind != 'u': exp = np.asarray(expected) else: exp = np.asarray(expected_u) if block_size == 2: input_fsc_ = np.array([input_fsc_, input_fsc_]).T.ravel() input_fsr_ = np.array([input_fsr_, input_fsr_]).T.ravel() exp = np.array([exp, exp]).T.ravel() mat = get_mat(itype, ftype) out = mat * input_fsr_ assert_same(out, exp) out[...] = 0 mat._matvec(input_fsr_, out=out) assert_same(out, exp) out = input_fsc_ * mat assert_same(out, FSCMatrix(mat.shape[::-1], data=mat.data) * input_fsc_) out = (3 * mat) * input_fsr_ assert_same(out, 3 * exp) out = (mat * 3) * input_fsr_ assert_same(out, 3 * exp)