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.

144 lines
4.1 KiB

  1. //=====================================================
  2. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.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 GMM_INTERFACE_HH
  19. #define GMM_INTERFACE_HH
  20. #include <gmm/gmm.h>
  21. #include <vector>
  22. using namespace gmm;
  23. template<class real>
  24. class gmm_interface {
  25. public :
  26. typedef real real_type ;
  27. typedef std::vector<real> stl_vector;
  28. typedef std::vector<stl_vector > stl_matrix;
  29. typedef gmm::dense_matrix<real> gene_matrix;
  30. typedef stl_vector gene_vector;
  31. static inline std::string name( void )
  32. {
  33. return "gmm";
  34. }
  35. static void free_matrix(gene_matrix & A, int N){
  36. return ;
  37. }
  38. static void free_vector(gene_vector & B){
  39. return ;
  40. }
  41. static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
  42. A.resize(A_stl[0].size(),A_stl.size());
  43. for (int j=0; j<A_stl.size() ; j++){
  44. for (int i=0; i<A_stl[j].size() ; i++){
  45. A(i,j) = A_stl[j][i];
  46. }
  47. }
  48. }
  49. static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){
  50. B = B_stl;
  51. }
  52. static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){
  53. B_stl = B;
  54. }
  55. static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
  56. int N=A_stl.size();
  57. for (int j=0;j<N;j++){
  58. A_stl[j].resize(N);
  59. for (int i=0;i<N;i++){
  60. A_stl[j][i] = A(i,j);
  61. }
  62. }
  63. }
  64. static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){
  65. gmm::mult(A,B, X);
  66. }
  67. static inline void transposed_matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N){
  68. gmm::mult(gmm::transposed(A),gmm::transposed(B), X);
  69. }
  70. static inline void ata_product(const gene_matrix & A, gene_matrix & X, int N){
  71. gmm::mult(gmm::transposed(A),A, X);
  72. }
  73. static inline void aat_product(const gene_matrix & A, gene_matrix & X, int N){
  74. gmm::mult(A,gmm::transposed(A), X);
  75. }
  76. static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  77. gmm::mult(A,B,X);
  78. }
  79. static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  80. gmm::mult(gmm::transposed(A),B,X);
  81. }
  82. static inline void axpy(const real coef, const gene_vector & X, gene_vector & Y, int N){
  83. gmm::add(gmm::scaled(X,coef), Y);
  84. }
  85. static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){
  86. gmm::add(gmm::scaled(X,a), gmm::scaled(Y,b), Y);
  87. }
  88. static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
  89. gmm::copy(source,cible);
  90. }
  91. static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
  92. gmm::copy(source,cible);
  93. }
  94. static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector & X, int N){
  95. gmm::copy(B,X);
  96. gmm::lower_tri_solve(L, X, false);
  97. }
  98. static inline void partial_lu_decomp(const gene_matrix & X, gene_matrix & R, int N){
  99. gmm::copy(X,R);
  100. std::vector<int> ipvt(N);
  101. gmm::lu_factor(R, ipvt);
  102. }
  103. static inline void hessenberg(const gene_matrix & X, gene_matrix & R, int N){
  104. gmm::copy(X,R);
  105. gmm::Hessenberg_reduction(R,X,false);
  106. }
  107. static inline void tridiagonalization(const gene_matrix & X, gene_matrix & R, int N){
  108. gmm::copy(X,R);
  109. gmm::Householder_tridiagonalization(R,X,false);
  110. }
  111. };
  112. #endif