Home | History | Annotate | Download | only in tensors
      1 //=====================================================
      2 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog (at) gmail.com>
      3 //=====================================================
      4 //
      5 // This Source Code Form is subject to the terms of the Mozilla
      6 // Public License v. 2.0. If a copy of the MPL was not distributed
      7 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      8 //
      9 #ifndef TENSOR_INTERFACE_HH
     10 #define TENSOR_INTERFACE_HH
     11 
     12 #include <unsupported/Eigen/CXX11/Tensor>
     13 #include <vector>
     14 #include "btl.hh"
     15 
     16 using namespace Eigen;
     17 
     18 template<class real>
     19 class tensor_interface
     20 {
     21 public :
     22   typedef real real_type;
     23   typedef typename Eigen::Tensor<real,2>::Index Index;
     24 
     25   typedef std::vector<real> stl_vector;
     26   typedef std::vector<stl_vector> stl_matrix;
     27 
     28   typedef Eigen::Tensor<real,2> gene_matrix;
     29   typedef Eigen::Tensor<real,1> gene_vector;
     30 
     31 
     32   static inline std::string name( void )
     33   {
     34     return EIGEN_MAKESTRING(BTL_PREFIX);
     35   }
     36 
     37   static void free_matrix(gene_matrix & /*A*/, int /*N*/) {}
     38 
     39   static void free_vector(gene_vector & /*B*/) {}
     40 
     41   static BTL_DONT_INLINE void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
     42     A.resize(Eigen::array<Index,2>(A_stl[0].size(), A_stl.size()));
     43 
     44     for (unsigned int j=0; j<A_stl.size() ; j++){
     45       for (unsigned int i=0; i<A_stl[j].size() ; i++){
     46         A.coeffRef(Eigen::array<Index,2>(i,j)) = A_stl[j][i];
     47       }
     48     }
     49   }
     50 
     51   static BTL_DONT_INLINE  void vector_from_stl(gene_vector & B, stl_vector & B_stl){
     52     B.resize(B_stl.size());
     53 
     54     for (unsigned int i=0; i<B_stl.size() ; i++){
     55       B.coeffRef(i) = B_stl[i];
     56     }
     57   }
     58 
     59   static BTL_DONT_INLINE  void vector_to_stl(gene_vector & B, stl_vector & B_stl){
     60     for (unsigned int i=0; i<B_stl.size() ; i++){
     61       B_stl[i] = B.coeff(i);
     62     }
     63   }
     64 
     65   static BTL_DONT_INLINE  void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
     66     int  N=A_stl.size();
     67 
     68     for (int j=0;j<N;j++){
     69       A_stl[j].resize(N);
     70       for (int i=0;i<N;i++){
     71         A_stl[j][i] = A.coeff(Eigen::array<Index,2>(i,j));
     72       }
     73     }
     74   }
     75 
     76   static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int  /*N*/){
     77     typedef typename Eigen::Tensor<real_type, 1>::DimensionPair DimPair;
     78     const Eigen::array<DimPair, 1> dims(DimPair(1, 0));
     79     X/*.noalias()*/ = A.contract(B, dims);
     80   }
     81 
     82   static inline void matrix_vector_product(const gene_matrix & A, const gene_vector & B, gene_vector & X, int  /*N*/){
     83     typedef typename Eigen::Tensor<real_type, 1>::DimensionPair DimPair;
     84     const Eigen::array<DimPair, 1> dims(DimPair(1, 0));
     85     X/*.noalias()*/ = A.contract(B, dims);
     86   }
     87 
     88   static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int  /*N*/){
     89     Y += X.constant(coef) * X;
     90   }
     91 
     92   static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int  /*N*/){
     93     Y = X.constant(a)*X + Y.constant(b)*Y;
     94   }
     95 
     96   static EIGEN_DONT_INLINE void copy_matrix(const gene_matrix & source, gene_matrix & cible, int  /*N*/){
     97     cible = source;
     98   }
     99 
    100   static EIGEN_DONT_INLINE void copy_vector(const gene_vector & source, gene_vector & cible, int  /*N*/){
    101     cible = source;
    102   }
    103 };
    104 
    105 #endif
    106