linalg_impl.py revision d835d677ade78a41e0e097f67c87b6ab8588a90a
10cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 20cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# 30cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# Licensed under the Apache License, Version 2.0 (the "License"); 40cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# you may not use this file except in compliance with the License. 50cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# You may obtain a copy of the License at 60cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# 70cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# http://www.apache.org/licenses/LICENSE-2.0 80cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# 90cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# Unless required by applicable law or agreed to in writing, software 100cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# distributed under the License is distributed on an "AS IS" BASIS, 110cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# See the License for the specific language governing permissions and 130cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# limitations under the License. 140cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower# ============================================================================== 150cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower"""Operations for linear algebra.""" 160cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 170cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom __future__ import absolute_import 180cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom __future__ import division 190cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom __future__ import print_function 200cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 210cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom tensorflow.python.framework import ops 220cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom tensorflow.python.ops import array_ops 230cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom tensorflow.python.ops import gen_linalg_ops 240cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerfrom tensorflow.python.ops import math_ops 250cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 260cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 270cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlowerdef logdet(matrix, name=None): 280cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower """Computes log of the determinant of a hermitian positive definite matrix. 290cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 300cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower ```python 310cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower # Compute the determinant of a matrix while reducing the chance of over- or 320cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower underflow: 330cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower A = ... # shape 10 x 10 340cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower det = tf.exp(tf.logdet(A)) # scalar 350cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower ``` 360cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 370cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower Args: 380cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower matrix: A `Tensor`. Must be `float32`, `float64`, `complex64`, or 390cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower `complex128` with shape `[..., M, M]`. 400cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower name: A name to give this `Op`. Defaults to `logdet`. 410cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 420cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower Returns: 430cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower The natural log of the determinant of `matrix`. 440cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower 450cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower @compatibility(numpy) 460cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower Equivalent to numpy.linalg.slogdet, although no sign is returned since only 470cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower hermitian positive definite matrices are supported. 480cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower @end_compatibility 490cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower """ 500cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower # This uses the property that the log det(A) = 2*sum(log(real(diag(C)))) 510cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower # where C is the cholesky decomposition of A. 520cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower with ops.name_scope(name, 'logdet', [matrix]): 530cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower chol = gen_linalg_ops.cholesky(matrix) 540cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower return 2.0 * math_ops.reduce_sum( 550cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower math_ops.log(math_ops.real(array_ops.matrix_diag_part(chol))), 560cff60ebb29f5aba5092988c8b7f13c258115e81A. Unique TensorFlower reduction_indices=[-1]) 57d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 58d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 59d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlowerdef adjoint(matrix, name=None): 60d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower """Conjugates and transposes the last two dimensions of tensor `matrix`. 61d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 62d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower For example: 63d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 64d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower ```python 65d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j], 66d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower [4 + 4j, 5 + 5j, 6 + 6j]]) 67d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower tf.linalg.adjoint(x) # [[1 - 1j, 4 - 4j], 68d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower # [2 - 2j, 5 - 5j], 69d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower # [3 - 3j, 6 - 6j]] 70d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 71d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower Args: 72d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower matrix: A `Tensor`. Must be `float32`, `float64`, `complex64`, or 73d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower `complex128` with shape `[..., M, M]`. 74d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower name: A name to give this `Op` (optional). 75d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower 76d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower Returns: 77d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower The adjoint (a.k.a. Hermitian transpose a.k.a. conjugate transpose) of 78d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower matrix. 79d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower """ 80d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower with ops.name_scope(name, 'adjoint', [matrix]): 81d835d677ade78a41e0e097f67c87b6ab8588a90aA. Unique TensorFlower return array_ops.matrix_transpose(matrix, conjugate=True) 82