1 //===================================================== 2 // Copyright (C) 2008 Gael Guennebaud <[email protected]> 3 //===================================================== 4 // 5 // This program is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License 7 // as published by the Free Software Foundation; either version 2 8 // of the License, or (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // You should have received a copy of the GNU General Public License 15 // along with this program; if not, write to the Free Software 16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 // 18 #ifndef BLAZE_INTERFACE_HH 19 #define BLAZE_INTERFACE_HH 20 21 #include <blaze/Math.h> 22 #include <blaze/Blaze.h> 23 #include <Eigen/Core> 24 // using namespace blaze; 25 26 #include <vector> 27 28 template<class real> 29 class blaze_interface { 30 31 public : 32 33 typedef real real_type ; 34 35 typedef std::vector<real> stl_vector; 36 typedef std::vector<stl_vector > stl_matrix; 37 38 typedef blaze::DynamicMatrix<real,blaze::columnMajor> gene_matrix; 39 typedef blaze::DynamicVector<real> gene_vector; 40 name()41 static inline std::string name() { return "blaze"; } 42 free_matrix(gene_matrix & A,int N)43 static void free_matrix(gene_matrix & A, int N){ 44 return ; 45 } 46 free_vector(gene_vector & B)47 static void free_vector(gene_vector & B){ 48 return ; 49 } 50 matrix_from_stl(gene_matrix & A,stl_matrix & A_stl)51 static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){ 52 A.resize(A_stl[0].size(), A_stl.size()); 53 54 for (int j=0; j<A_stl.size() ; j++){ 55 for (int i=0; i<A_stl[j].size() ; i++){ 56 A(i,j) = A_stl[j][i]; 57 } 58 } 59 } 60 vector_from_stl(gene_vector & B,stl_vector & B_stl)61 static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){ 62 B.resize(B_stl.size()); 63 for (int i=0; i<B_stl.size() ; i++){ 64 B[i] = B_stl[i]; 65 } 66 } 67 vector_to_stl(gene_vector & B,stl_vector & B_stl)68 static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){ 69 for (int i=0; i<B_stl.size() ; i++){ 70 B_stl[i] = B[i]; 71 } 72 } 73 matrix_to_stl(gene_matrix & A,stl_matrix & A_stl)74 static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){ 75 int N=A_stl.size(); 76 for (int j=0;j<N;j++){ 77 A_stl[j].resize(N); 78 for (int i=0;i<N;i++){ 79 A_stl[j][i] = A(i,j); 80 } 81 } 82 } 83 matrix_matrix_product(const gene_matrix & A,const gene_matrix & B,gene_matrix & X,int N)84 static EIGEN_DONT_INLINE void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){ 85 X = (A*B); 86 } 87 transposed_matrix_matrix_product(const gene_matrix & A,const gene_matrix & B,gene_matrix & X,int N)88 static EIGEN_DONT_INLINE void transposed_matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){ 89 X = (trans(A)*trans(B)); 90 } 91 ata_product(const gene_matrix & A,gene_matrix & X,int N)92 static EIGEN_DONT_INLINE void ata_product(const gene_matrix & A, gene_matrix & X, int N){ 93 X = (trans(A)*A); 94 } 95 aat_product(const gene_matrix & A,gene_matrix & X,int N)96 static EIGEN_DONT_INLINE void aat_product(const gene_matrix & A, gene_matrix & X, int N){ 97 X = (A*trans(A)); 98 } 99 matrix_vector_product(gene_matrix & A,gene_vector & B,gene_vector & X,int N)100 static EIGEN_DONT_INLINE void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 101 X = (A*B); 102 } 103 atv_product(gene_matrix & A,gene_vector & B,gene_vector & X,int N)104 static EIGEN_DONT_INLINE void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 105 X = (trans(A)*B); 106 } 107 axpy(const real coef,const gene_vector & X,gene_vector & Y,int N)108 static EIGEN_DONT_INLINE void axpy(const real coef, const gene_vector & X, gene_vector & Y, int N){ 109 Y += coef * X; 110 } 111 axpby(real a,const gene_vector & X,real b,gene_vector & Y,int N)112 static EIGEN_DONT_INLINE void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){ 113 Y = a*X + b*Y; 114 } 115 116 // static inline void cholesky(const gene_matrix & X, gene_matrix & C, int N){ 117 // C = X; 118 // recursive_cholesky(C); 119 // } 120 121 // static inline void lu_decomp(const gene_matrix & X, gene_matrix & R, int N){ 122 // R = X; 123 // std::vector<int> ipvt(N); 124 // lu_factor(R, ipvt); 125 // } 126 127 // static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector & X, int N){ 128 // X = lower_trisolve(L, B); 129 // } 130 copy_matrix(const gene_matrix & source,gene_matrix & cible,int N)131 static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){ 132 cible = source; 133 } 134 copy_vector(const gene_vector & source,gene_vector & cible,int N)135 static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){ 136 cible = source; 137 } 138 139 }; 140 141 #endif 142