STL_interface.hh
Go to the documentation of this file.
00001 //=====================================================
00002 // File   :  STL_interface.hh
00003 // Author :  L. Plagne <laurent.plagne@edf.fr)>
00004 // Copyright (C) EDF R&D,  lun sep 30 14:23:24 CEST 2002
00005 //=====================================================
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 //
00020 #ifndef STL_INTERFACE_HH
00021 #define STL_INTERFACE_HH
00022 #include <string>
00023 #include <vector>
00024 #include "utilities.h"
00025 
00026 using namespace std;
00027 
00028 template<class real>
00029 class STL_interface{
00030 
00031 public :
00032 
00033   typedef real real_type ;
00034 
00035   typedef std::vector<real>  stl_vector;
00036   typedef std::vector<stl_vector > stl_matrix;
00037 
00038   typedef stl_matrix gene_matrix;
00039 
00040   typedef stl_vector gene_vector;
00041 
00042   static inline std::string name( void )
00043   {
00044     return "STL";
00045   }
00046 
00047   static void free_matrix(gene_matrix & A, int N){}
00048 
00049   static void free_vector(gene_vector & B){}
00050 
00051   static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
00052     A = A_stl;
00053   }
00054 
00055   static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){
00056     B = B_stl;
00057   }
00058 
00059   static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){
00060     B_stl = B ;
00061   }
00062 
00063 
00064   static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
00065     A_stl = A ;
00066   }
00067 
00068   static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
00069     for (int i=0;i<N;i++){
00070       cible[i]=source[i];
00071     }
00072   }
00073 
00074 
00075   static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
00076     for (int i=0;i<N;i++)
00077       for (int j=0;j<N;j++)
00078         cible[i][j]=source[i][j];
00079   }
00080 
00081   static inline void ata_product(const gene_matrix & A, gene_matrix & X, int N)
00082   {
00083     real somme;
00084     for (int j=0;j<N;j++){
00085       for (int i=0;i<N;i++){
00086         somme=0.0;
00087         for (int k=0;k<N;k++)
00088           somme += A[i][k]*A[j][k];
00089         X[j][i]=somme;
00090       }
00091     }
00092   }
00093 
00094   static inline void aat_product(const gene_matrix & A, gene_matrix & X, int N)
00095   {
00096     real somme;
00097     for (int j=0;j<N;j++){
00098       for (int i=0;i<N;i++){
00099         somme=0.0;
00100         for (int k=0;k<N;k++){
00101           somme+=A[k][i]*A[k][j];
00102         }
00103         X[j][i]=somme;
00104       }
00105     }
00106   }
00107 
00108 
00109   static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int N)
00110   {
00111     real somme;
00112     for (int j=0;j<N;j++){
00113       for (int i=0;i<N;i++){
00114         somme=0.0;
00115         for (int k=0;k<N;k++)
00116           somme+=A[k][i]*B[j][k];
00117         X[j][i]=somme;
00118       }
00119     }
00120   }
00121 
00122   static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
00123   {
00124     real somme;
00125     for (int i=0;i<N;i++){
00126       somme=0.0;
00127       for (int j=0;j<N;j++)
00128         somme+=A[j][i]*B[j];
00129       X[i]=somme;
00130     }
00131   }
00132 
00133   static inline void symv(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
00134   {
00135     for (int j=0; j<N; ++j)
00136       X[j] = 0;
00137     for (int j=0; j<N; ++j)
00138     {
00139       real t1 = B[j];
00140       real t2 = 0;
00141       X[j] += t1 * A[j][j];
00142       for (int i=j+1; i<N; ++i) {
00143         X[i] += t1 * A[j][i];
00144         t2 += A[j][i] * B[i];
00145       }
00146       X[j] += t2;
00147     }
00148   }
00149   
00150   static inline void syr2(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
00151   {
00152     for (int j=0; j<N; ++j)
00153     {
00154       for (int i=j; i<N; ++i)
00155         A[j][i] += B[i]*X[j] + B[j]*X[i];
00156     }
00157   }
00158 
00159   static inline void ger(gene_matrix & A, gene_vector & X, gene_vector & Y, int N)
00160   {
00161     for (int j=0; j<N; ++j)
00162     {
00163       for (int i=j; i<N; ++i)
00164         A[j][i] += X[i]*Y[j];
00165     }
00166   }
00167 
00168   static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N)
00169   {
00170     real somme;
00171     for (int i=0;i<N;i++){
00172       somme = 0.0;
00173       for (int j=0;j<N;j++)
00174         somme += A[i][j]*B[j];
00175       X[i] = somme;
00176     }
00177   }
00178 
00179   static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int N){
00180     for (int i=0;i<N;i++)
00181       Y[i]+=coef*X[i];
00182   }
00183 
00184   static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int N){
00185     for (int i=0;i<N;i++)
00186       Y[i] = a*X[i] + b*Y[i];
00187   }
00188 
00189   static inline void trisolve_lower(const gene_matrix & L, const gene_vector & B, gene_vector & X, int N){
00190     copy_vector(B,X,N);
00191     for(int i=0; i<N; ++i)
00192     {
00193       X[i] /= L[i][i];
00194       real tmp = X[i];
00195       for (int j=i+1; j<N; ++j)
00196         X[j] -= tmp * L[i][j];
00197     }
00198   }
00199 
00200   static inline real norm_diff(const stl_vector & A, const stl_vector & B)
00201   {
00202     int N=A.size();
00203     real somme=0.0;
00204     real somme2=0.0;
00205 
00206     for (int i=0;i<N;i++){
00207       real diff=A[i]-B[i];
00208       somme+=diff*diff;
00209       somme2+=A[i]*A[i];
00210     }
00211     return somme/somme2;
00212   }
00213 
00214   static inline real norm_diff(const stl_matrix & A, const stl_matrix & B)
00215   {
00216     int N=A[0].size();
00217     real somme=0.0;
00218     real somme2=0.0;
00219 
00220     for (int i=0;i<N;i++){
00221       for (int j=0;j<N;j++){
00222         real diff=A[i][j] - B[i][j];
00223         somme += diff*diff;
00224         somme2 += A[i][j]*A[i][j];
00225       }
00226     }
00227 
00228     return somme/somme2;
00229   }
00230 
00231   static inline void display_vector(const stl_vector & A)
00232   {
00233     int N=A.size();
00234     for (int i=0;i<N;i++){
00235       INFOS("A["<<i<<"]="<<A[i]<<endl);
00236     }
00237   }
00238 
00239 };
00240 
00241 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:00