def test_solve_adjoint_complex_operator(self): matrix1 = self.evaluate(linear_operator_test_util.random_tril_matrix( [4, 4], dtype=dtypes.complex128, force_well_conditioned=True) + 1j * linear_operator_test_util.random_tril_matrix( [4, 4], dtype=dtypes.complex128, force_well_conditioned=True)) matrix2 = np.random.randn(4, 4) + 1j * np.random.randn(4, 4) full_matrix1 = linalg.LinearOperatorLowerTriangular( matrix1, is_non_singular=True) full_matrix2 = linalg.LinearOperatorFullMatrix(matrix2) self.assertAllClose( self.evaluate(linalg.triangular_solve(matrix1, matrix2.conj().T)), self.evaluate( full_matrix1.solve(full_matrix2, adjoint_arg=True).to_dense())) self.assertAllClose( self.evaluate( linalg.triangular_solve( matrix1.conj().T, matrix2, lower=False)), self.evaluate( full_matrix1.solve(full_matrix2, adjoint=True).to_dense())) self.assertAllClose( self.evaluate( linalg.triangular_solve( matrix1.conj().T, matrix2.conj().T, lower=False)), self.evaluate( full_matrix1.solve( full_matrix2, adjoint=True, adjoint_arg=True).to_dense()))
def test_solve_adjoint_complex_operator(self): matrix1 = self.evaluate( linear_operator_test_util.random_tril_matrix( [4, 4], dtype=dtypes.complex128, force_well_conditioned=True) + 1j * linear_operator_test_util.random_tril_matrix( [4, 4], dtype=dtypes.complex128, force_well_conditioned=True)) matrix2 = np.random.randn(4, 4) + 1j * np.random.randn(4, 4) full_matrix1 = linalg.LinearOperatorLowerTriangular( matrix1, is_non_singular=True) full_matrix2 = linalg.LinearOperatorFullMatrix(matrix2) self.assertAllClose( self.evaluate(linalg.triangular_solve(matrix1, matrix2.conj().T)), self.evaluate( full_matrix1.solve(full_matrix2, adjoint_arg=True).to_dense())) self.assertAllClose( self.evaluate( linalg.triangular_solve(matrix1.conj().T, matrix2, lower=False)), self.evaluate( full_matrix1.solve(full_matrix2, adjoint=True).to_dense())) self.assertAllClose( self.evaluate( linalg.triangular_solve(matrix1.conj().T, matrix2.conj().T, lower=False)), self.evaluate( full_matrix1.solve(full_matrix2, adjoint=True, adjoint_arg=True).to_dense()))
def operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) # Upper triangle will be nonzero, but ignored. # Use a diagonal that ensures this matrix is well conditioned. tril = linear_operator_test_util.random_tril_matrix( shape, dtype=dtype, force_well_conditioned=True, remove_upper=False) if ensure_self_adjoint_and_pd: # Get the diagonal and make the matrix out of it. tril = array_ops.matrix_diag_part(tril) tril = math_ops.abs(tril) + 1e-1 tril = array_ops.matrix_diag(tril) lin_op_tril = tril if use_placeholder: lin_op_tril = array_ops.placeholder_with_default(lin_op_tril, shape=None) operator = linalg.LinearOperatorLowerTriangular( lin_op_tril, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) matrix = array_ops.matrix_band_part(tril, -1, 0) return operator, matrix
def _operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) if ensure_self_adjoint_and_pd: matrix = linear_operator_test_util.random_positive_definite_matrix( shape, dtype, force_well_conditioned=True) else: matrix = linear_operator_test_util.random_tril_matrix( shape, dtype, force_well_conditioned=True, remove_upper=True) lin_op_matrix = matrix if use_placeholder: lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None) if ensure_self_adjoint_and_pd: operator = LinearOperatorInversion( linalg.LinearOperatorFullMatrix(lin_op_matrix, is_positive_definite=True, is_self_adjoint=True)) else: operator = LinearOperatorInversion( linalg.LinearOperatorLowerTriangular(lin_op_matrix)) return operator, linalg.inv(matrix)
def test_block_diag_solve_type(self): matrices1 = [] matrices2 = [] for i in range(1, 5): matrices1.append( linalg.LinearOperatorFullMatrix( linear_operator_test_util.random_tril_matrix( [i, i], dtype=dtypes.float32, force_well_conditioned=True))) matrices2.append( linalg.LinearOperatorFullMatrix( linear_operator_test_util.random_normal( [i, 3], dtype=dtypes.float32))) operator1 = block_diag.LinearOperatorBlockDiag(matrices1) operator2 = block_diag.LinearOperatorBlockDiag(matrices2, is_square=False) expected_matrix = linalg.solve(operator1.to_dense(), operator2.to_dense()) actual_operator = operator1.solve(operator2) self.assertIsInstance(actual_operator, block_diag.LinearOperatorBlockDiag) actual_, expected_ = self.evaluate( [actual_operator.to_dense(), expected_matrix]) self.assertAllClose(actual_, expected_)
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) # Upper triangle will be nonzero, but ignored. # Use a diagonal that ensures this matrix is well conditioned. tril = linear_operator_test_util.random_tril_matrix( shape, dtype=dtype, force_well_conditioned=True, remove_upper=False) if use_placeholder: tril_ph = array_ops.placeholder(dtype=dtype) # Evaluate the tril here because (i) you cannot feed a tensor, and (ii) # tril is random and we want the same value used for both mat and # feed_dict. tril = tril.eval() operator = linalg.LinearOperatorLowerTriangular(tril_ph) feed_dict = {tril_ph: tril} else: operator = linalg.LinearOperatorLowerTriangular(tril) feed_dict = None mat = array_ops.matrix_band_part(tril, -1, 0) return operator, mat, feed_dict
def operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) if ensure_self_adjoint_and_pd: matrix = linear_operator_test_util.random_positive_definite_matrix( shape, dtype, force_well_conditioned=True) else: matrix = linear_operator_test_util.random_tril_matrix( shape, dtype, force_well_conditioned=True, remove_upper=True) lin_op_matrix = matrix if use_placeholder: lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None) if ensure_self_adjoint_and_pd: operator = LinearOperatorAdjoint( linalg.LinearOperatorFullMatrix( lin_op_matrix, is_positive_definite=True, is_self_adjoint=True)) else: operator = LinearOperatorAdjoint( linalg.LinearOperatorLowerTriangular(lin_op_matrix)) return operator, linalg.adjoint(matrix)
def test_assert_non_singular_raises_if_cond_too_big_but_finite(self): with self.test_session(): tril = linear_operator_test_util.random_tril_matrix( shape=(50, 50), dtype=np.float32) diag = np.logspace(-2, 2, 50).astype(np.float32) tril = array_ops.matrix_set_diag(tril, diag) matrix = math_ops.matmul(tril, tril, transpose_b=True).eval() operator = linalg.LinearOperatorFullMatrix(matrix) with self.assertRaisesOpError("Singular matrix"): # Ensure that we have finite condition number...just HUGE. cond = np.linalg.cond(matrix) self.assertTrue(np.isfinite(cond)) self.assertGreater(cond, 1e12) operator.assert_non_singular().run()
def test_assert_non_singular_raises_if_cond_too_big_but_finite(self): with self.cached_session(): tril = linear_operator_test_util.random_tril_matrix( shape=(50, 50), dtype=np.float32) diag = np.logspace(-2, 2, 50).astype(np.float32) tril = array_ops.matrix_set_diag(tril, diag) matrix = math_ops.matmul(tril, tril, transpose_b=True).eval() operator = linalg.LinearOperatorFullMatrix(matrix) with self.assertRaisesOpError("Singular matrix"): # Ensure that we have finite condition number...just HUGE. cond = np.linalg.cond(matrix) self.assertTrue(np.isfinite(cond)) self.assertGreater(cond, 1e12) operator.assert_non_singular().run()
def operator_and_matrix(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) # Upper triangle will be nonzero, but ignored. # Use a diagonal that ensures this matrix is well conditioned. tril = linear_operator_test_util.random_tril_matrix( shape, dtype=dtype, force_well_conditioned=True, remove_upper=False) lin_op_tril = tril if use_placeholder: lin_op_tril = array_ops.placeholder_with_default(lin_op_tril, shape=None) operator = linalg.LinearOperatorLowerTriangular(lin_op_tril) matrix = array_ops.matrix_band_part(tril, -1, 0) return operator, matrix
def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) # Upper triangle will be nonzero, but ignored. # Use a diagonal that ensures this matrix is well conditioned. tril = linear_operator_test_util.random_tril_matrix( shape, dtype=dtype, force_well_conditioned=True, remove_upper=False) lin_op_tril = tril if use_placeholder: lin_op_tril = array_ops.placeholder_with_default(lin_op_tril, shape=None) operator = linalg.LinearOperatorLowerTriangular(lin_op_tril) matrix = array_ops.matrix_band_part(tril, -1, 0) return operator, matrix
def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder): # Upper triangle will be nonzero, but ignored. # Use a diagonal that ensures this matrix is well conditioned. tril = linear_operator_test_util.random_tril_matrix( shape, dtype=dtype, force_well_conditioned=True, remove_upper=False) if use_placeholder: tril_ph = array_ops.placeholder(dtype=dtype) # Evaluate the tril here because (i) you cannot feed a tensor, and (ii) # tril is random and we want the same value used for both mat and # feed_dict. tril = tril.eval() operator = linalg.LinearOperatorLowerTriangular(tril_ph) feed_dict = {tril_ph: tril} else: operator = linalg.LinearOperatorLowerTriangular(tril) feed_dict = None mat = array_ops.matrix_band_part(tril, -1, 0) return operator, mat, feed_dict