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 
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 
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 
60  for (unsigned int i=0; i<B_stl.size() ; i++){
61  B_stl[i] = B.coeff(i);
62  }
63  }
64 
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
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
tensor_interface
Definition: tensor_interface.hh:19
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
Y
const char Y
Definition: test/EulerAngles.cpp:31
btl.hh
Eigen::array
Definition: EmulateArray.h:21
tensor_interface::vector_from_stl
static BTL_DONT_INLINE void vector_from_stl(gene_vector &B, stl_vector &B_stl)
Definition: tensor_interface.hh:51
tensor_interface::stl_vector
std::vector< real > stl_vector
Definition: tensor_interface.hh:25
b
Scalar * b
Definition: benchVecAdd.cpp:17
X
#define X
Definition: icosphere.cpp:20
tensor_interface::free_vector
static void free_vector(gene_vector &)
Definition: tensor_interface.hh:39
tensor_interface::Index
Eigen::Tensor< real, 2 >::Index Index
Definition: tensor_interface.hh:23
tensor_interface::name
static std::string name(void)
Definition: tensor_interface.hh:32
tensor_interface::gene_vector
Eigen::Tensor< real, 1 > gene_vector
Definition: tensor_interface.hh:29
tensor_interface::matrix_matrix_product
static void matrix_matrix_product(const gene_matrix &A, const gene_matrix &B, gene_matrix &X, int)
Definition: tensor_interface.hh:76
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
tensor_interface::matrix_from_stl
static BTL_DONT_INLINE void matrix_from_stl(gene_matrix &A, stl_matrix &A_stl)
Definition: tensor_interface.hh:41
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
tensor_interface::copy_matrix
static EIGEN_DONT_INLINE void copy_matrix(const gene_matrix &source, gene_matrix &cible, int)
Definition: tensor_interface.hh:96
tensor_interface::copy_vector
static EIGEN_DONT_INLINE void copy_vector(const gene_vector &source, gene_vector &cible, int)
Definition: tensor_interface.hh:100
tensor_interface::gene_matrix
Eigen::Tensor< real, 2 > gene_matrix
Definition: tensor_interface.hh:28
tensor_interface::axpy
static void axpy(real coef, const gene_vector &X, gene_vector &Y, int)
Definition: tensor_interface.hh:88
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
EIGEN_MAKESTRING
#define EIGEN_MAKESTRING(a)
Definition: Macros.h:908
tensor_interface::vector_to_stl
static BTL_DONT_INLINE void vector_to_stl(gene_vector &B, stl_vector &B_stl)
Definition: tensor_interface.hh:59
tensor_interface::axpby
static void axpby(real a, const gene_vector &X, real b, gene_vector &Y, int)
Definition: tensor_interface.hh:92
tensor_interface::matrix_vector_product
static void matrix_vector_product(const gene_matrix &A, const gene_vector &B, gene_vector &X, int)
Definition: tensor_interface.hh:82
N
#define N
Definition: igam.h:9
tensor_interface::free_matrix
static void free_matrix(gene_matrix &, int)
Definition: tensor_interface.hh:37
DimPair
Tensor< float, 1 >::DimensionPair DimPair
Definition: cxx11_tensor_contraction.cpp:17
tensor_interface::matrix_to_stl
static BTL_DONT_INLINE void matrix_to_stl(gene_matrix &A, stl_matrix &A_stl)
Definition: tensor_interface.hh:65
BTL_DONT_INLINE
#define BTL_DONT_INLINE
Definition: btl.hh:38
Eigen::Tensor::Index
internal::traits< Self >::Index Index
Definition: Tensor.h:70
real
Definition: main.h:100
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
tensor_interface::stl_matrix
std::vector< stl_vector > stl_matrix
Definition: tensor_interface.hh:26
tensor_interface::real_type
real real_type
Definition: tensor_interface.hh:22
EIGEN_DONT_INLINE
#define EIGEN_DONT_INLINE
Definition: Macros.h:940


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:03:52