def test_non_square_operator_raises(self): operators = [ linalg.LinearOperatorFullMatrix(rng.rand(3, 4), is_square=False), linalg.LinearOperatorFullMatrix(rng.rand(3, 3)) ] with self.assertRaisesRegexp(ValueError, "square matrices"): block_diag.LinearOperatorBlockDiag(operators)
def test_different_dtypes_raises(self): operators = [ linalg.LinearOperatorFullMatrix(rng.rand(2, 3, 3)), linalg.LinearOperatorFullMatrix(rng.rand(2, 3, 3).astype(np.float32)) ] with self.assertRaisesRegexp(TypeError, "same dtype"): block_diag.LinearOperatorBlockDiag(operators)
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) expected_blocks = (build_info.__dict__["blocks"] if "blocks" in build_info.__dict__ else [shape]) diag_matrices = [ linear_operator_test_util.random_uniform(shape=block_shape[:-1], minval=1., maxval=20., dtype=dtype) for block_shape in expected_blocks ] if use_placeholder: diag_matrices_ph = [ array_ops.placeholder(dtype=dtype) for _ in expected_blocks ] diag_matrices = self.evaluate(diag_matrices) # Evaluate here because (i) you cannot feed a tensor, and (ii) # values are random and we want the same value used for both mat and # feed_dict. operator = block_diag.LinearOperatorBlockDiag( [linalg.LinearOperatorDiag(m_ph) for m_ph in diag_matrices_ph]) feed_dict = { m_ph: m for (m_ph, m) in zip(diag_matrices_ph, diag_matrices) } else: operator = block_diag.LinearOperatorBlockDiag( [linalg.LinearOperatorDiag(m) for m in diag_matrices]) feed_dict = None # Should be auto-set. self.assertTrue(operator.is_square) # Broadcast the shapes. expected_shape = list(build_info.shape) matrices = linear_operator_util.broadcast_matrix_batch_dims([ array_ops.matrix_diag(diag_block) for diag_block in diag_matrices ]) block_diag_dense = _block_diag_dense(expected_shape, matrices) if not use_placeholder: block_diag_dense.set_shape( expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]]) return operator, block_diag_dense, feed_dict
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) expected_blocks = (build_info.__dict__["blocks"] if "blocks" in build_info.__dict__ else [shape]) matrices = [ linear_operator_test_util.random_positive_definite_matrix( block_shape, dtype, force_well_conditioned=True) for block_shape in expected_blocks ] if use_placeholder: matrices_ph = [ array_ops.placeholder(dtype=dtype) for _ in expected_blocks ] # Evaluate here because (i) you cannot feed a tensor, and (ii) # values are random and we want the same value used for both mat and # feed_dict. matrices = self.evaluate(matrices) operator = block_diag.LinearOperatorBlockDiag([ linalg.LinearOperatorFullMatrix(m_ph, is_square=True) for m_ph in matrices_ph ], is_square=True) feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)} else: operator = block_diag.LinearOperatorBlockDiag([ linalg.LinearOperatorFullMatrix(m, is_square=True) for m in matrices ]) feed_dict = None # Should be auto-set. self.assertTrue(operator.is_square) # Broadcast the shapes. expected_shape = list(build_info.shape) matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices) block_diag_dense = _block_diag_dense(expected_shape, matrices) if not use_placeholder: block_diag_dense.set_shape( expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]]) return operator, block_diag_dense, feed_dict
def test_name(self): matrix = [[11., 0.], [1., 8.]] operator_1 = linalg.LinearOperatorFullMatrix(matrix, name="left") operator_2 = linalg.LinearOperatorFullMatrix(matrix, name="right") operator = block_diag.LinearOperatorBlockDiag([operator_1, operator_2]) self.assertEqual("left_ds_right", operator.name)
def test_is_non_singular_auto_set(self): # Matrix with two positive eigenvalues, 11 and 8. # The matrix values do not effect auto-setting of the flags. matrix = [[11., 0.], [1., 8.]] operator_1 = linalg.LinearOperatorFullMatrix(matrix, is_non_singular=True) operator_2 = linalg.LinearOperatorFullMatrix(matrix, is_non_singular=True) operator = block_diag.LinearOperatorBlockDiag( [operator_1, operator_2], is_positive_definite=False, # No reason it HAS to be False... is_non_singular=None) self.assertFalse(operator.is_positive_definite) self.assertTrue(operator.is_non_singular) with self.assertRaisesRegexp(ValueError, "always non-singular"): block_diag.LinearOperatorBlockDiag( [operator_1, operator_2], is_non_singular=False)
def test_is_x_flags(self): # Matrix with two positive eigenvalues, 1, and 1. # The matrix values do not effect auto-setting of the flags. matrix = [[1., 0.], [1., 1.]] operator = block_diag.LinearOperatorBlockDiag( [linalg.LinearOperatorFullMatrix(matrix)], is_positive_definite=True, is_non_singular=True, is_self_adjoint=False) self.assertTrue(operator.is_positive_definite) self.assertTrue(operator.is_non_singular) self.assertFalse(operator.is_self_adjoint)
def test_empty_operators_raises(self): with self.assertRaisesRegexp(ValueError, "non-empty"): block_diag.LinearOperatorBlockDiag([])