def test_real_spectrum_gives_self_adjoint_operator(self): with self.cached_session(): # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant3D(spectrum) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), operator.shape) self.assertEqual( operator.parameters, { "input_output_dtype": dtypes.complex64, "is_non_singular": None, "is_positive_definite": None, "is_self_adjoint": None, "is_square": True, "name": "LinearOperatorCirculant3D", "spectrum": spectrum, }) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, dtypes.complex64) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), matrix.shape) self.assertAllClose(matrix, matrix_h)
def test_compare_to_numpy(self): for dtype in np.float64, np.float64, np.complex64, np.complex128: matrix_np = np.array([[1 + 1j, 2 + 2j, 3 + 3j], [4 + 4j, 5 + 5j, 6 + 6j]]).astype(dtype) expected_transposed = np.conj(matrix_np.T) with self.session(): matrix = ops.convert_to_tensor(matrix_np) transposed = linalg.adjoint(matrix) self.assertEqual((3, 2), transposed.get_shape()) self.assertAllEqual(expected_transposed, self.evaluate(transposed))
def test_simple_positive_real_spectrum_gives_self_adjoint_pos_def_oper(self): with self.cached_session() as sess: spectrum = math_ops.cast([6., 4, 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) matrix, matrix_h = sess.run( [operator.to_dense(), linalg.adjoint(operator.to_dense())]) self.assertAllClose(matrix, matrix_h) operator.assert_positive_definite().run() # Should not fail operator.assert_self_adjoint().run() # Should not fail
def test_simple_positive_real_spectrum_gives_self_adjoint_pos_def_oper(self): with self.cached_session() as sess: spectrum = math_ops.cast([6., 4, 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) matrix, matrix_h = sess.run( [operator.to_dense(), linalg.adjoint(operator.to_dense())]) self.assertAllClose(matrix, matrix_h) operator.assert_positive_definite().run() # Should not fail operator.assert_self_adjoint().run() # Should not fail
def test_real_spectrum_gives_self_adjoint_operator(self): with self.cached_session(): # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(3, 3), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant2D(spectrum) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, dtypes.complex64) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllClose(matrix, matrix_h, atol=0)
def test_real_spectrum_gives_self_adjoint_operator(self): with self.cached_session(): # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(3, 3), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant2D(spectrum) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, dtypes.complex64) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllClose(matrix, matrix_h, atol=0)
def test_real_spectrum_gives_self_adjoint_operator(self): with self.test_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(3, 3), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant2D(spectrum) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, linear_operator_circulant._DTYPE_COMPLEX) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = sess.run([matrix_tensor, matrix_h]) self.assertAllClose(matrix, matrix_h, atol=0)
def test_real_spectrum_gives_self_adjoint_operator(self): with self.test_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(3, 3), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant2D(spectrum) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, linear_operator_circulant._DTYPE_COMPLEX) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = sess.run([matrix_tensor, matrix_h]) self.assertAllClose(matrix, matrix_h, atol=0)
def test_real_spectrum_gives_self_adjoint_operator(self): with self.cached_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant3D(spectrum) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), operator.shape) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, linear_operator_circulant._DTYPE_COMPLEX) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), matrix.shape) self.assertAllClose(matrix, matrix_h)
def test_real_spectrum_gives_self_adjoint_operator(self): with self.cached_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant3D(spectrum) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), operator.shape) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, linear_operator_circulant._DTYPE_COMPLEX) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), matrix.shape) self.assertAllClose(matrix, matrix_h)
def TriAngSolveCompositeGrad(l, grad): # Gradient is l^{-H} @ ((l^{H} @ grad) * (tril(ones)-1/2*eye)) @ l^{-1} # Compute ((l^{H} @ grad) * (tril(ones)-1/2*eye)) = middle middle = math_ops.matmul(l, grad, adjoint_a=True) middle = array_ops.matrix_set_diag(middle, 0.5 * array_ops.matrix_diag_part(middle)) middle = array_ops.matrix_band_part(middle, -1, 0) # Compute l^{-H} @ middle = z l_inverse_middle = linalg_ops.matrix_triangular_solve(l, middle, adjoint=True) # We need to compute z @ l^{-1}. With matrix_triangular_solve we # actually compute l^{-H} @ z^{H} = grad. Since we later add grad^{H} # we can ommit the conjugate transpose here. z_h = math_ops.conj(array_ops.matrix_transpose(l_inverse_middle)) grad_a = linalg_ops.matrix_triangular_solve(l, z_h, adjoint=True) grad_a += linalg.adjoint(grad_a) return grad_a * 0.5
def test_real_spectrum_gives_self_adjoint_operator(self): if test.is_built_with_rocm(): # ROCm does not yet support BLAS operations with complext types self.skipTest("ROCm does not support BLAS operations for complex types") with self.cached_session(): # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) operator = linalg.LinearOperatorCirculant3D(spectrum) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), operator.shape) matrix_tensor = operator.to_dense() self.assertEqual(matrix_tensor.dtype, dtypes.complex64) matrix_h = linalg.adjoint(matrix_tensor) matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h]) self.assertAllEqual((2, 2 * 3 * 5, 2 * 3 * 5), matrix.shape) self.assertAllClose(matrix, matrix_h)