source: Sophya/trunk/SophyaProg/Tests/lpk.cc@ 2609

Last change on this file since 2609 was 2322, checked in by cmv, 23 years ago
  • passage xxstream.h en xxstream
  • compile avec gcc_3.2, gcc_2.96 et cxx En 3.2 le seek from ::end semble marcher (voir Eval/COS/pbseekios.cc)

rz+cmv 11/2/2003

File size: 7.7 KB
RevLine 
[778]1#include "machdefs.h"
2
3#include <math.h>
[2322]4#include <iostream>
[778]5
[857]6#include "srandgen.h"
[778]7#include "tarrinit.h"
[857]8#include "array.h"
[778]9#include "timing.h"
10#include "intflapack.h"
11
12
[1343]13int lpk_rzt_arr(int n);
14int lpk_linsolve_mtx(int n);
15int lpk_svd_mtx(int l, int c, int wsf, bool covu=true);
[1567]16int lpk_leastsquare_mtx(int l, int c);
[812]17
[1248]18static double TOLERANCE = 1.e-6;
[1100]19
[778]20int main(int narg, char* arg[])
21{
22
23 SophyaInit();
24 InitTim(); // Initializing the CPU timer
25
26
[812]27
28 if (narg < 2) {
[1343]29 cout << " lpk - LinAlg/LapackServer test \n"
30 << " Usage: lpk linsolve/svd/rzt [sizeL,C=5,5] [prtlev=0] \n"
31 << " [nprtmax=100] [WorkSpaceSizeFactor=2] \n"
32 << " linsolve: lpk_linsolve_mtx() LapackServer::LinSolve with TMatrix<r_8> \n"
33 << " svd: lpk_svd_mtx() LapackServer::SVD(a,s,u,vt) with TMatrix<r_8> \n"
[1567]34 << " svds: lpk_svd_mtx() LapackServer::SVD(a,s); with TMatrix<r_8> \n"
35 << " lss: lpk_leastsquare_mtx() LapackServer::LeastSquareSolve() r_8 \n"
[1343]36 << " rzt: lpk_rzt_tarr() rztest_lapack with TArray<r_4> \n" << endl;
[812]37 exit(0);
38 }
[1343]39 int l,c;
40 l = c = 5;
41 int wsf = 2;
42 string opt = arg[1];
43 if (narg > 2) sscanf(arg[2], "%d,%d", &l, &c);
[812]44 int nprt = 100;
45 int prtlev = 1;
46 if (narg > 3) prtlev = atoi(arg[3]);
47 if (narg > 4) nprt = atoi(arg[4]);
[1343]48 if (narg > 5) wsf = atoi(arg[5]);
[812]49
[1248]50 int rc = 0;
[812]51 BaseArray::SetMaxPrint(nprt, prtlev);
52 try {
[1343]53 if (opt == "linsolve") rc = lpk_linsolve_mtx(l);
54 else if (opt == "svd") rc = lpk_svd_mtx(l,c,wsf,true);
55 else if (opt == "svds") rc = lpk_svd_mtx(l,c,wsf,false);
[1567]56 else if (opt == "lss") rc = lpk_leastsquare_mtx(l,c);
[1343]57 else if (opt == "rzt") rc = lpk_rzt_arr(l);
58 else { cout << " Unknown option " << opt << " ! " << endl; rc = 66; }
[812]59 }
60 catch (PThrowable exc) {
61 cerr << " catched Exception (lpk.cc) " << exc.Msg() << endl;
[1248]62 rc = 77;
[812]63 }
64 catch (...) {
65 cerr << " catched unknown (...) exception (lpk.cc) " << endl;
[1248]66 rc = 78;
[812]67 }
68
69 PrtTim(" End of lpk LinAlg/Lapack test ");
[1343]70 cout << " --------------- END of Programme -------- (Rc= "
71 << rc << ") --- " << endl;
[1248]72 return(rc);
[812]73}
74
[1343]75// -----------------------------------------------------------------------
76/* Nouvelle-Fonction */
77int lpk_linsolve_mtx(int n)
[812]78{
[1343]79 int i,j;
[812]80 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
[1343]81 cout << " lpk_linsolve_mtx() - Test of LapackServer::LinSolve() " << endl;
[812]82 Matrix a(n,n);
83 for(i=0; i<n; i++)
84 for(j=0; j<n; j++) a(j,i) = GauRnd(0., 1.);
85
86 Vector x(n), b;
87 // Matrix x(n,1), b;
88 cout << " ------------ Vector X = \n " << x << "\n" << endl;
89 for(i=0; i<n; i++) x(i) = GauRnd(2., 1.5);
90 b = a*x;
[778]91
[1343]92 cout << " ---- lpk_tmtx() LapackServer::LinSolve Test Using TMatrix<r_8> ----- " << endl;
[812]93 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
94 cout << " ------------ Matrix X = \n " << x << "\n" << endl;
95 cout << " ------------ Matrix B = \n " << b << "\n" << endl;
96
97 cout << "\n Calling LapackLinSolve(a,b) .... " << endl;
[1100]98 PrtTim(" Calling LapackLinSolve(a,b) ");
[812]99 LapackLinSolve(a,b);
[1100]100 PrtTim(" End LapackLinSolve(a,b) ");
[812]101
102 cout << " ------------ Result B(=X ?) = \n " << b << "\n" << endl;
103 Vector diff = b-x;
[1343]104 PrtTim(" End of Compute(diff)");
[812]105 cout << " ------------ Vector diff B-X = \n " << diff << "\n" << endl;
[1248]106 double min,max;
107 diff.MinMax(min, max);
108 cout << " Min/Max difference Vector (?=0) , Min= " << min
109 << " Max= " << max << endl;
110 if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) {
111 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
112 << endl;
113 return(99);
114 }
115 return(0);
[812]116
117}
118
[1343]119// -----------------------------------------------------------------------
120/* Nouvelle-Fonction */
121int lpk_svd_mtx(int m, int n, int wsf, bool covu)
[812]122{
[1343]123 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
124 cout << " lpk_svd_mtx() - Test of LapackServer::SVD " << endl;
125
126 Matrix a(m , n), aa;
127 a = RandomSequence(RandomSequence::Gaussian, 0., 4.);
128 aa = a;
129 Vector s;
130 Matrix u, vt;
131 cout << " ---- lpk_svd_tmtx() LapackServer::SVD Test Using TMatrix<r_8> ---- " << endl;
132 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
133
134 cout << "\n Calling LapackSVD(a,s,u,vt) .... " << endl;
135 PrtTim(" Calling LapackSVD() ");
136 LapackServer<r_8> lpks;
137 lpks.SetWorkSpaceSizeFactor(wsf);
138 if (covu) lpks.SVD(aa, s, u, vt);
139 else lpks.SVD(aa, s);
140 PrtTim(" End LapackSVD() ");
141
142 cout << " ------------ Result S = \n " << s << "\n" << endl;
143 if (!covu) return(0);
144 cout << " ------------ Result U = \n " << u << "\n" << endl;
145 cout << " ------------ Result VT = \n " << vt << "\n" << endl;
146 Matrix sm(m,n);
147 int minmn = (m<n) ? m : n ;
148 for(int k=0; k< minmn ; k++) sm(k,k) = s(k);
149 Matrix diff = u*(sm*vt) - a;
150 PrtTim(" End of Compute(diff)");
151 cout << " ------------ Matrix diff U*S*Vt - A = \n " << diff << "\n" << endl;
152 double min,max;
153 diff.MinMax(min, max);
154 cout << " Min/Max difference Matrix (?=0) , Min= " << min
155 << " Max= " << max << endl;
156 if ((fabs(min) > TOLERANCE) || (fabs(max) > TOLERANCE)) {
157 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
158 << endl;
159 return(99);
160 }
161 return(0);
162
163}
164
[1567]165// -----------------------------------------------------------------------
166/* Nouvelle-Fonction */
167int lpk_leastsquare_mtx(int m, int n)
168{
169 BaseArray::SetDefaultMemoryMapping(BaseArray::FortranMemoryMapping);
170 cout << " lpk_leastsquare_mtx() - Test of LapackLeastSquareSolve " << endl;
171
172 Matrix a(m , n), aa;
173 a = RandomSequence(RandomSequence::Gaussian, 0., 4.);
174 aa = a;
175 Vector x(n),noise(m),b,bx;
176 x = RandomSequence(RandomSequence::Gaussian, 2., 1.);
177 double signoise = 0.1;
178 noise = RandomSequence(RandomSequence::Gaussian, 0., 0.1);
179 b = a*x;
180 bx = b+noise;
181
182 cout << " ---- lpk_leastsquare_tmtx() LapackLeastSquareSolve<r_8> ---- " << endl;
183 cout << " ------------ Matrix A = \n " << a << "\n" << endl;
184 cout << " ------------ Vector B = \n " << bx << "\n" << endl;
185 cout << " ------------ Vector X = \n " << x << "\n" << endl;
186
187 cout << "\n Calling LapackLeastSquareSolve(a,bx) .... " << endl;
188 PrtTim(" Calling LapackLeastSquareSolve() ");
189 LapackLeastSquareSolve(aa, bx);
190 PrtTim(" End LapackLeastSquareSolve() ");
191 bx.Share(bx(Range(0,0,n)));
192 cout << " ------------ Result X = \n " << bx << "\n" << endl;
193 cout << " ------------ X-X_Real = \n " << bx-x << "\n" << endl;
194 Vector diff = b-a*bx;
195 PrtTim(" End of Compute(diff)");
196 cout << " ------------ Matrix diff b-a*x = \n " << diff << "\n" << endl;
197 double mean,sigma;
198 MeanSigma(diff, mean, sigma);
199 cout << " MeanSigma(diff) , Mean= " << mean << " Sigma=" << sigma
200 << " SigNoise= " << signoise << endl;
201 if ((fabs(mean) > signoise) || (fabs(sigma-signoise) > signoise)) {
202 cout << " !!! Difference exceeding tolerance (=" << TOLERANCE << ") !!!"
203 << endl;
204 return(99);
205 }
206 return(0);
207
208}
209
210
[1425]211void rztest_lapack(TArray<r_4>& aa, TArray<r_4>& bb);
212
[1343]213int lpk_rzt_arr(int n)
214{
215 int i,j;
[778]216 TArray<r_4> a(n,n);
217 for(i=0; i<n; i++)
218 for(j=0; j<n; j++) a(i,j,0) = GauRnd(0., 1.);
219
[789]220 TArray<r_4> x(n,1), b(n,1);
[778]221 r_4 sum ;
[789]222 for(i=0; i<n; i++) x(i,0,0) = GauRnd(2., 1.5);
[778]223 for(i=0; i<n; i++) {
224 sum = 0.;
[789]225 for(j=0; j<n; j++) sum += a(i,j,0)*x(j,0,0);
226 b(i,0,0) = sum;
[778]227 }
228
[812]229 // cout << ":::::::: rztest_lapack - Size=" << n << " ::::::::: " << endl;
230 cout << " ------- lpk_tarr() LapaackTest Using TArray<r_4> -------- " << endl;
[807]231 cout << " ------------ Array A = \n " << a << "\n" << endl;
232 cout << " ------------ Array X = \n " << x << "\n" << endl;
233 cout << " ------------ Array B = \n " << b << "\n" << endl;
[778]234
235 cout << "\n Calling rztest_lapack ... " << endl;
236
237 rztest_lapack(a, b);
238
239 cout << " ------------ Result B(=X ?) = \n " << b << "\n" << endl;
[1248]240 return(0);
[778]241}
Note: See TracBrowser for help on using the repository browser.