You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.0 KiB

  1. //=====================================================
  2. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
  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 EIGEN2_INTERFACE_HH
  19. #define EIGEN2_INTERFACE_HH
  20. // #include <cblas.h>
  21. #include <Eigen/Core>
  22. #include <Eigen/Cholesky>
  23. #include <Eigen/LU>
  24. #include <Eigen/QR>
  25. #include <vector>
  26. #include "btl.hh"
  27. using namespace Eigen;
  28. template<class real, int SIZE=Dynamic>
  29. class eigen2_interface
  30. {
  31. public :
  32. enum {IsFixedSize = (SIZE!=Dynamic)};
  33. typedef real real_type;
  34. typedef std::vector<real> stl_vector;
  35. typedef std::vector<stl_vector> stl_matrix;
  36. typedef Eigen::Matrix<real,SIZE,SIZE> gene_matrix;
  37. typedef Eigen::Matrix<real,SIZE,1> gene_vector;
  38. static inline std::string name( void )
  39. {
  40. #if defined(EIGEN_VECTORIZE_SSE)
  41. if (SIZE==Dynamic) return "eigen2"; else return "tiny_eigen2";
  42. #elif defined(EIGEN_VECTORIZE_ALTIVEC)
  43. if (SIZE==Dynamic) return "eigen2"; else return "tiny_eigen2";
  44. #else
  45. if (SIZE==Dynamic) return "eigen2_novec"; else return "tiny_eigen2_novec";
  46. #endif
  47. }
  48. static void free_matrix(gene_matrix & A, int N) {}
  49. static void free_vector(gene_vector & B) {}
  50. static BTL_DONT_INLINE void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
  51. A.resize(A_stl[0].size(), A_stl.size());
  52. for (int j=0; j<A_stl.size() ; j++){
  53. for (int i=0; i<A_stl[j].size() ; i++){
  54. A.coeffRef(i,j) = A_stl[j][i];
  55. }
  56. }
  57. }
  58. static BTL_DONT_INLINE void vector_from_stl(gene_vector & B, stl_vector & B_stl){
  59. B.resize(B_stl.size(),1);
  60. for (int i=0; i<B_stl.size() ; i++){
  61. B.coeffRef(i) = B_stl[i];
  62. }
  63. }
  64. static BTL_DONT_INLINE void vector_to_stl(gene_vector & B, stl_vector & B_stl){
  65. for (int i=0; i<B_stl.size() ; i++){
  66. B_stl[i] = B.coeff(i);
  67. }
  68. }
  69. static BTL_DONT_INLINE void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
  70. int N=A_stl.size();
  71. for (int j=0;j<N;j++){
  72. A_stl[j].resize(N);
  73. for (int i=0;i<N;i++){
  74. A_stl[j][i] = A.coeff(i,j);
  75. }
  76. }
  77. }
  78. static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){
  79. X = (A*B).lazy();
  80. }
  81. static inline void transposed_matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){
  82. X = (A.transpose()*B.transpose()).lazy();
  83. }
  84. static inline void ata_product(const gene_matrix & A, gene_matrix & X, int N){
  85. X = (A.transpose()*A).lazy();
  86. }
  87. static inline void aat_product(const gene_matrix & A, gene_matrix & X, int N){
  88. X = (A*A.transpose()).lazy();
  89. }
  90. static inline void matrix_vector_product(const gene_matrix & A, const gene_vector & B, gene_vector & X, int N){
  91. X = (A*B)/*.lazy()*/;
  92. }
  93. static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  94. X = (A.transpose()*B)/*.lazy()*/;
  95. }
  96. static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int N){
  97. Y += coef * X;
  98. }
  99. static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){
  100. Y = a*X + b*Y;
  101. }
  102. static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
  103. cible = source;
  104. }
  105. static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
  106. cible = source;
  107. }
  108. static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector& X, int N){
  109. X = L.template marked<LowerTriangular>().solveTriangular(B);
  110. }
  111. static inline void trisolve_lower_matrix(const gene_matrix & L, const gene_matrix& B, gene_matrix& X, int N){
  112. X = L.template marked<LowerTriangular>().solveTriangular(B);
  113. }
  114. static inline void cholesky(const gene_matrix & X, gene_matrix & C, int N){
  115. C = X.llt().matrixL();
  116. // C = X;
  117. // Cholesky<gene_matrix>::computeInPlace(C);
  118. // Cholesky<gene_matrix>::computeInPlaceBlock(C);
  119. }
  120. static inline void lu_decomp(const gene_matrix & X, gene_matrix & C, int N){
  121. C = X.lu().matrixLU();
  122. // C = X.inverse();
  123. }
  124. static inline void tridiagonalization(const gene_matrix & X, gene_matrix & C, int N){
  125. C = Tridiagonalization<gene_matrix>(X).packedMatrix();
  126. }
  127. static inline void hessenberg(const gene_matrix & X, gene_matrix & C, int N){
  128. C = HessenbergDecomposition<gene_matrix>(X).packedMatrix();
  129. }
  130. };
  131. #endif