MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
dgeequtest.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/dgeequtest.cc,v 1.17 2017/01/12 14:43:53 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <iostream>
35 #include <iomanip>
36 #include <typeinfo>
37 
38 #include "dgeequ.h"
39 
40 #include "spmapmh.h"
41 #include "ccmh.h"
42 #include "dirccmh.h"
43 #include "naivemh.h"
44 
45 static doublereal mat[5][5] = {
46  { 11., 0., 13., 0., 15. },
47  { 0., 22., 0., 24., 0. },
48  { 31., 0., 33., 0., 35. },
49  { 0., 42., 0., 44., 0. },
50  { 51., 0., 53., 0., 55. }
51 };
52 
53 void ReportMatScale(const char* title, bool fOK, const MatrixScaleBase& matScale, const MatrixHandler& mh, const double cond[2]);
54 
55 template<typename T>
56 void ScaleMatrix(const char* title, MatrixScale<T>& matScale, T& mh)
57 {
58  doublereal cond[2];
59 
60  //std::cout << "matrix before scaling:" << std::endl << mh << std::endl;
61 
62  cond[0] = mh.ConditionNumber();
63 
64  const bool fOK = matScale.ComputeScaleFactors(mh);
65 
66  matScale.ScaleMatrix(mh);
67 
68  cond[1] = mh.ConditionNumber();
69 
70  ReportMatScale(title, fOK, matScale, mh, cond);
71 }
72 
73 int
74 main(int argc, char* argv[])
75 {
78  scale.iMaxIter = argc >= 2 ? atoi(argv[1]) : 100;
79  scale.dTol = argc >= 3 ? atof(argv[2]) : sqrt(std::numeric_limits<doublereal>::epsilon());
80 
81  struct {
90  } matScale[] = {
147  };
148 
149  const int N = sizeof(matScale)/sizeof(matScale[0]);
150 
151  for (int iMatScale = 0; iMatScale < N; ++iMatScale) {
152  std::vector<integer> perm(5), invperm(5);
153  perm[0] = 4;
154  perm[1] = 3;
155  perm[2] = 2;
156  perm[3] = 1;
157  perm[4] = 0;
158  for (int i = 0; i < 5; i++) {
159  invperm[perm[i]] = i;
160  }
161 
162  NaiveMatrixHandler nm(5);
163  NaivePermMatrixHandler npm(5, perm, invperm);
164  FullMatrixHandler fm(5);
165  SpMapMatrixHandler spm(5, 5);
166 
167  nm.Reset();
168  npm.Reset();
169  fm.Reset();
170  spm.Reset();
171  for (unsigned ir = 0; ir < 5; ir++) {
172  for (unsigned ic = 0; ic < 5; ic++) {
173  if (mat[ir][ic] != 0.) {
174  nm(ir + 1, ic + 1) = mat[ir][ic];
175  npm(ir + 1, ic + 1) = mat[ir][ic];
176  fm(ir + 1, ic + 1) = mat[ir][ic];
177  spm(ir + 1, ic + 1) = mat[ir][ic];
178  }
179  }
180  }
181 
182  std::vector<doublereal> Ax0, Ax1, Axd0, Axd1;
183  std::vector<integer> Ai0, Ai1, Ap0, Ap1, Aid0, Apd0, Aid1, Apd1;
184 
185  spm.MakeCompressedColumnForm(Ax0, Ai0, Ap0, 0);
186  spm.MakeCompressedColumnForm(Ax1, Ai1, Ap1, 1);
187  spm.MakeCompressedColumnForm(Axd0, Aid0, Apd0, 0);
188  spm.MakeCompressedColumnForm(Axd1, Aid1, Apd1, 1);
189 
190  CColMatrixHandler<0> ccm0(Ax0, Ai0, Ap0);
191  CColMatrixHandler<1> ccm1(Ax1, Ai1, Ap1);
192  DirCColMatrixHandler<0> dirccm0(Axd0, Aid0, Apd0);
193  DirCColMatrixHandler<1> dirccm1(Axd1, Aid1, Apd1);
194 
195  ScaleMatrix("Naive", *matScale[iMatScale].pNaive, nm);
196  ScaleMatrix("NaivePerm", *matScale[iMatScale].pNaivePerm, npm);
197  ScaleMatrix("Full", *matScale[iMatScale].pFull, fm);
198  ScaleMatrix("Dir0", *matScale[iMatScale].pDirCCol0, dirccm0);
199  ScaleMatrix("Dir1", *matScale[iMatScale].pDirCCol1, dirccm1);
200  ScaleMatrix("CC0", *matScale[iMatScale].pCCol0, ccm0);
201  ScaleMatrix("CC1", *matScale[iMatScale].pCCol1, ccm1);
202  ScaleMatrix("Map", *matScale[iMatScale].pMap, spm);
203  }
204 
205  for (unsigned i = 0; i < sizeof(matScale)/sizeof(matScale[0]); ++i) {
206  delete matScale[i].pNaive;
207  delete matScale[i].pNaivePerm;
208  delete matScale[i].pFull;
209  delete matScale[i].pCCol0;
210  delete matScale[i].pCCol1;
211  delete matScale[i].pDirCCol0;
212  delete matScale[i].pDirCCol1;
213  delete matScale[i].pMap;
214  }
215 
216  return 0;
217 }
218 
219 void ReportMatScale(const char* title, bool fOK, const MatrixScaleBase& matScale, const MatrixHandler& mh, const double cond[2]) {
220  std::cout << "------------------------------------------------------------" << std::endl;
221  std::cout << title << ": " << (fOK ? "OK" : "NOK") << " : " << typeid(matScale).name() << std::endl;
222 
223  std::cout << "condition number before scaling:" << cond[0] << std::endl;
224  std::cout << "condition number after scaling:" << cond[1] << std::endl;
225 
226  matScale.Report(std::cout);
227  const std::vector<doublereal>& r = matScale.GetRowScale();
228  const std::vector<doublereal>& c = matScale.GetColScale();
229 
230  const int N = std::min(mh.iGetNumRows(), mh.iGetNumCols());
231 
232  for (int i = 0; i < N; ++i) {
233  std::cout
234  << " r[" << i << "]=" << std::setw(12) << (r.empty() ? 1. : r[i])
235  << " c[" << i << "]=" << std::setw(12) << (c.empty() ? 1. : c[i])
236  << std::endl;
237  }
238 
239  std::cout << "matrix after scaling:" << std::endl << mh << std::endl;
240  std::cout << "------------------------------------------------------------" << std::endl;
241 }
242 
243 template
245 
246 template
248 
249 template
251 
252 template
254 
255 template
257 
258 template
260 
261 template
263 
264 template
266 
267 template
269 
270 template
272 
273 template
275 
276 template
278 
279 template
281 
282 template
284 
285 template
287 
288 template
290 
291 template
293 
294 template
296 
297 template
299 
300 template
302 
303 template
305 
306 template
308 
309 template
311 
312 template
314 
315 template
317 
318 template
320 
321 template
323 
324 template
326 
327 template
329 
330 template
332 
333 template
335 
336 template
338 
339 template
341 
342 template
344 
345 template
347 
348 template
350 
351 template
353 
354 template
356 
357 template
359 
360 template
362 
363 template
365 
366 template
void Reset(void)
Definition: naivemh.cc:151
virtual integer iGetNumCols(void) const =0
int main(int argc, char *argv[])
Definition: dgeequtest.cc:74
integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const
Definition: spmapmh.cc:80
bool ComputeScaleFactors(const T &mh)
Definition: dgeequ.h:305
const std::vector< doublereal > & GetRowScale() const
Definition: dgeequ.h:57
std::ostream & Report(std::ostream &os) const
Definition: dgeequ.cc:52
void Reset(void)
Definition: spmapmh.cc:161
const std::vector< doublereal > & GetColScale() const
Definition: dgeequ.h:58
void ScaleMatrix(const char *title, MatrixScale< T > &matScale, T &mh)
Definition: dgeequtest.cc:56
static doublereal mat[5][5]
Definition: dgeequtest.cc:45
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
Definition: gradient.h:2974
void ReportMatScale(const char *title, bool fOK, const MatrixScaleBase &matScale, const MatrixHandler &mh, const double cond[2])
Definition: dgeequtest.cc:219
static std::stack< cleanup * > c
Definition: cleanup.cc:59
double doublereal
Definition: colamd.c:52
virtual integer iGetNumRows(void) const =0
void Reset(void)
Definition: fullmh.cc:44
T & ScaleMatrix(T &mh) const
Definition: dgeequ.h:274