Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // See docs in ../ops/linalg_ops.cc.
     17 
     18 #include "third_party/eigen3/Eigen/Core"
     19 #include "third_party/eigen3/unsupported/Eigen/MatrixFunctions"
     20 #include "tensorflow/core/framework/kernel_def_builder.h"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/framework/tensor_shape.h"
     23 #include "tensorflow/core/kernels/linalg_ops_common.h"
     24 #include "tensorflow/core/lib/core/errors.h"
     25 #include "tensorflow/core/platform/logging.h"
     26 #include "tensorflow/core/platform/macros.h"
     27 #include "tensorflow/core/platform/types.h"
     28 
     29 namespace tensorflow {
     30 
     31 template <class Scalar>
     32 class MatrixLogarithmOp : public LinearAlgebraOp<Scalar> {
     33  public:
     34   INHERIT_LINALG_TYPEDEFS(Scalar);
     35 
     36   explicit MatrixLogarithmOp(OpKernelConstruction* context) : Base(context) {}
     37 
     38   void ComputeMatrix(OpKernelContext* context, const ConstMatrixMaps& inputs,
     39                      MatrixMaps* outputs) final {
     40     const ConstMatrixMap& input = inputs[0];
     41     if (input.rows() == 0) return;
     42     using Matrix =
     43         Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
     44     Matrix tmp = input;
     45     outputs->at(0) = tmp.log();
     46   }
     47 
     48  private:
     49   TF_DISALLOW_COPY_AND_ASSIGN(MatrixLogarithmOp);
     50 };
     51 
     52 // For real-valued matrices, this Op would return the real part of the matrix
     53 // logarithm. If all eigenvalues are positive, then this returns the correct
     54 // logarithm, however checking for positive definiteness adds significant
     55 // overhead. Therefore at present we only register this Op for complex types.
     56 REGISTER_LINALG_OP("MatrixLogarithm", (MatrixLogarithmOp<complex64>),
     57                    complex64);
     58 REGISTER_LINALG_OP("MatrixLogarithm", (MatrixLogarithmOp<complex128>),
     59                    complex128);
     60 
     61 }  // namespace tensorflow
     62