tensor_interface.hh
Go to the documentation of this file.
1 //=====================================================
2 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@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>
20 {
21 public :
22  typedef real real_type;
24 
25  typedef std::vector<real> stl_vector;
26  typedef std::vector<stl_vector> stl_matrix;
27 
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*/){
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*/){
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
#define EIGEN_MAKESTRING(a)
Definition: Macros.h:485
float real
Definition: datatypes.h:10
std::vector< real > stl_vector
static BTL_DONT_INLINE void vector_from_stl(gene_vector &B, stl_vector &B_stl)
Scalar * b
Definition: benchVecAdd.cpp:17
#define BTL_DONT_INLINE
Definition: btl.hh:38
static void free_vector(gene_vector &)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
Definition: Tensor.h:164
static std::string name(void)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
#define N
Definition: gksort.c:12
static BTL_DONT_INLINE void matrix_from_stl(gene_matrix &A, stl_matrix &A_stl)
Eigen::Tensor< real, 2 >::Index Index
Eigen::Tensor< real, 1 > gene_vector
#define EIGEN_DONT_INLINE
Definition: Macros.h:517
Array33i a
static void matrix_matrix_product(const gene_matrix &A, const gene_matrix &B, gene_matrix &X, int)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Tensor< float, 1 >::DimensionPair DimPair
static EIGEN_DONT_INLINE void copy_matrix(const gene_matrix &source, gene_matrix &cible, int)
internal::traits< Self >::Index Index
Definition: Tensor.h:70
static EIGEN_DONT_INLINE void copy_vector(const gene_vector &source, gene_vector &cible, int)
Eigen::Tensor< real, 2 > gene_matrix
static void axpy(real coef, const gene_vector &X, gene_vector &Y, int)
EIGEN_DEVICE_FUNC void resize(const array< Index, NumIndices > &dimensions)
Definition: Tensor.h:432
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(const array< Index, NumIndices > &indices) const
Definition: Tensor.h:124
static void axpby(real a, const gene_vector &X, real b, gene_vector &Y, int)
static void matrix_vector_product(const gene_matrix &A, const gene_vector &B, gene_vector &X, int)
static BTL_DONT_INLINE void vector_to_stl(gene_vector &B, stl_vector &B_stl)
static void free_matrix(gene_matrix &, int)
static BTL_DONT_INLINE void matrix_to_stl(gene_matrix &A, stl_matrix &A_stl)
#define X
Definition: icosphere.cpp:20
std::vector< stl_vector > stl_matrix
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:45:09