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