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.

141 lines
4.2 KiB

  1. //=====================================================
  2. // File : ublas_interface.hh
  3. // Author : L. Plagne <laurent.plagne@edf.fr)>
  4. // Copyright (C) EDF R&D, lun sep 30 14:23:27 CEST 2002
  5. //=====================================================
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. #ifndef UBLAS_INTERFACE_HH
  21. #define UBLAS_INTERFACE_HH
  22. #include <boost/numeric/ublas/vector.hpp>
  23. #include <boost/numeric/ublas/matrix.hpp>
  24. #include <boost/numeric/ublas/io.hpp>
  25. #include <boost/numeric/ublas/triangular.hpp>
  26. using namespace boost::numeric;
  27. template <class real>
  28. class ublas_interface{
  29. public :
  30. typedef real real_type ;
  31. typedef std::vector<real> stl_vector;
  32. typedef std::vector<stl_vector> stl_matrix;
  33. typedef typename boost::numeric::ublas::matrix<real,boost::numeric::ublas::column_major> gene_matrix;
  34. typedef typename boost::numeric::ublas::vector<real> gene_vector;
  35. static inline std::string name( void ) { return "ublas"; }
  36. static void free_matrix(gene_matrix & A, int N) {}
  37. static void free_vector(gene_vector & B) {}
  38. static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
  39. A.resize(A_stl.size(),A_stl[0].size());
  40. for (int j=0; j<A_stl.size() ; j++)
  41. for (int i=0; i<A_stl[j].size() ; i++)
  42. A(i,j)=A_stl[j][i];
  43. }
  44. static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){
  45. B.resize(B_stl.size());
  46. for (int i=0; i<B_stl.size() ; i++)
  47. B(i)=B_stl[i];
  48. }
  49. static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){
  50. for (int i=0; i<B_stl.size() ; i++)
  51. B_stl[i]=B(i);
  52. }
  53. static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
  54. int N=A_stl.size();
  55. for (int j=0;j<N;j++)
  56. {
  57. A_stl[j].resize(N);
  58. for (int i=0;i<N;i++)
  59. A_stl[j][i]=A(i,j);
  60. }
  61. }
  62. static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
  63. for (int i=0;i<N;i++){
  64. cible(i) = source(i);
  65. }
  66. }
  67. static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
  68. for (int i=0;i<N;i++){
  69. for (int j=0;j<N;j++){
  70. cible(i,j) = source(i,j);
  71. }
  72. }
  73. }
  74. static inline void matrix_vector_product_slow(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  75. X = prod(A,B);
  76. }
  77. static inline void matrix_matrix_product_slow(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){
  78. X = prod(A,B);
  79. }
  80. static inline void axpy_slow(const real coef, const gene_vector & X, gene_vector & Y, int N){
  81. Y+=coef*X;
  82. }
  83. // alias free assignements
  84. static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  85. X.assign(prod(A,B));
  86. }
  87. static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
  88. X.assign(prod(trans(A),B));
  89. }
  90. static inline void matrix_matrix_product(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){
  91. X.assign(prod(A,B));
  92. }
  93. static inline void axpy(const real coef, const gene_vector & X, gene_vector & Y, int N){
  94. Y.plus_assign(coef*X);
  95. }
  96. static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){
  97. Y = a*X + b*Y;
  98. }
  99. static inline void ata_product(gene_matrix & A, gene_matrix & X, int N){
  100. // X = prod(trans(A),A);
  101. X.assign(prod(trans(A),A));
  102. }
  103. static inline void aat_product(gene_matrix & A, gene_matrix & X, int N){
  104. // X = prod(A,trans(A));
  105. X.assign(prod(A,trans(A)));
  106. }
  107. static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector & X, int N){
  108. X = solve(L, B, ublas::lower_tag ());
  109. }
  110. };
  111. #endif