Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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 MatrixExponentialOp : public LinearAlgebraOp<Scalar> {
     33  public:
     34   INHERIT_LINALG_TYPEDEFS(Scalar);
     35 
     36   explicit MatrixExponentialOp(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.exp();
     46   }
     47 
     48  private:
     49   TF_DISALLOW_COPY_AND_ASSIGN(MatrixExponentialOp);
     50 };
     51 
     52 REGISTER_LINALG_OP("MatrixExponential", (MatrixExponentialOp<float>), float);
     53 REGISTER_LINALG_OP("MatrixExponential", (MatrixExponentialOp<double>), double);
     54 REGISTER_LINALG_OP("MatrixExponential", (MatrixExponentialOp<complex64>),
     55                    complex64);
     56 REGISTER_LINALG_OP("MatrixExponential", (MatrixExponentialOp<complex128>),
     57                    complex128);
     58 
     59 }  // namespace tensorflow
     60