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.

244 lines
5.7 KiB

  1. //=====================================================
  2. // File : STL_interface.hh
  3. // Author : L. Plagne <laurent.plagne@edf.fr)>
  4. // Copyright (C) EDF R&D, lun sep 30 14:23:24 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 STL_INTERFACE_HH
  21. #define STL_INTERFACE_HH
  22. #include <string>
  23. #include <vector>
  24. #include "utilities.h"
  25. using namespace std;
  26. template<class real>
  27. class STL_interface{
  28. public :
  29. typedef real real_type ;
  30. typedef std::vector<real> stl_vector;
  31. typedef std::vector<stl_vector > stl_matrix;
  32. typedef stl_matrix gene_matrix;
  33. typedef stl_vector gene_vector;
  34. static inline std::string name( void )
  35. {
  36. return "STL";
  37. }
  38. static void free_matrix(gene_matrix & A, int N){}
  39. static void free_vector(gene_vector & B){}
  40. static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
  41. A = A_stl;
  42. }
  43. static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){
  44. B = B_stl;
  45. }
  46. static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){
  47. B_stl = B ;
  48. }
  49. static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
  50. A_stl = A ;
  51. }
  52. static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
  53. for (int i=0;i<N;i++){
  54. cible[i]=source[i];
  55. }
  56. }
  57. static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
  58. for (int i=0;i<N;i++)
  59. for (int j=0;j<N;j++)
  60. cible[i][j]=source[i][j];
  61. }
  62. // static inline void ata_product(const gene_matrix & A, gene_matrix & X, int N)
  63. // {
  64. // real somme;
  65. // for (int j=0;j<N;j++){
  66. // for (int i=0;i<N;i++){
  67. // somme=0.0;
  68. // for (int k=0;k<N;k++)
  69. // somme += A[i][k]*A[j][k];
  70. // X[j][i]=somme;
  71. // }
  72. // }
  73. // }
  74. static inline void aat_product(const gene_matrix & A, gene_matrix & X, int N)
  75. {
  76. real somme;
  77. for (int j=0;j<N;j++){
  78. for (int i=0;i<N;i++){
  79. somme=0.0;
  80. if(i>=j)
  81. {
  82. for (int k=0;k<N;k++){
  83. somme+=A[k][i]*A[k][j];
  84. }
  85. X[j][i]=somme;
  86. }
  87. }
  88. }
  89. }
  90. static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N)
  91. {
  92. real somme;
  93. for (int j=0;j<N;j++){
  94. for (int i=0;i<N;i++){
  95. somme=0.0;
  96. for (int k=0;k<N;k++)
  97. somme+=A[k][i]*B[j][k];
  98. X[j][i]=somme;
  99. }
  100. }
  101. }
  102. static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
  103. {
  104. real somme;
  105. for (int i=0;i<N;i++){
  106. somme=0.0;
  107. for (int j=0;j<N;j++)
  108. somme+=A[j][i]*B[j];
  109. X[i]=somme;
  110. }
  111. }
  112. static inline void symv(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
  113. {
  114. for (int j=0; j<N; ++j)
  115. X[j] = 0;
  116. for (int j=0; j<N; ++j)
  117. {
  118. real t1 = B[j];
  119. real t2 = 0;
  120. X[j] += t1 * A[j][j];
  121. for (int i=j+1; i<N; ++i) {
  122. X[i] += t1 * A[j][i];
  123. t2 += A[j][i] * B[i];
  124. }
  125. X[j] += t2;
  126. }
  127. }
  128. static inline void syr2(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
  129. {
  130. for (int j=0; j<N; ++j)
  131. {
  132. for (int i=j; i<N; ++i)
  133. A[j][i] += B[i]*X[j] + B[j]*X[i];
  134. }
  135. }
  136. static inline void ger(gene_matrix & A, gene_vector & X, gene_vector & Y, int N)
  137. {
  138. for (int j=0; j<N; ++j)
  139. {
  140. for (int i=j; i<N; ++i)
  141. A[j][i] += X[i]*Y[j];
  142. }
  143. }
  144. static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
  145. {
  146. real somme;
  147. for (int i=0;i<N;i++){
  148. somme = 0.0;
  149. for (int j=0;j<N;j++)
  150. somme += A[i][j]*B[j];
  151. X[i] = somme;
  152. }
  153. }
  154. static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int N){
  155. for (int i=0;i<N;i++)
  156. Y[i]+=coef*X[i];
  157. }
  158. static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){
  159. for (int i=0;i<N;i++)
  160. Y[i] = a*X[i] + b*Y[i];
  161. }
  162. static inline void trisolve_lower(const gene_matrix & L, const gene_vector & B, gene_vector & X, int N){
  163. copy_vector(B,X,N);
  164. for(int i=0; i<N; ++i)
  165. {
  166. X[i] /= L[i][i];
  167. real tmp = X[i];
  168. for (int j=i+1; j<N; ++j)
  169. X[j] -= tmp * L[i][j];
  170. }
  171. }
  172. static inline real norm_diff(const stl_vector & A, const stl_vector & B)
  173. {
  174. int N=A.size();
  175. real somme=0.0;
  176. real somme2=0.0;
  177. for (int i=0;i<N;i++){
  178. real diff=A[i]-B[i];
  179. somme+=diff*diff;
  180. somme2+=A[i]*A[i];
  181. }
  182. return somme/somme2;
  183. }
  184. static inline real norm_diff(const stl_matrix & A, const stl_matrix & B)
  185. {
  186. int N=A[0].size();
  187. real somme=0.0;
  188. real somme2=0.0;
  189. for (int i=0;i<N;i++){
  190. for (int j=0;j<N;j++){
  191. real diff=A[i][j] - B[i][j];
  192. somme += diff*diff;
  193. somme2 += A[i][j]*A[i][j];
  194. }
  195. }
  196. return somme/somme2;
  197. }
  198. static inline void display_vector(const stl_vector & A)
  199. {
  200. int N=A.size();
  201. for (int i=0;i<N;i++){
  202. INFOS("A["<<i<<"]="<<A[i]<<endl);
  203. }
  204. }
  205. };
  206. #endif