MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
itertest.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/itertest.cc,v 1.15 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 
37 #include "fullmh.h"
38 #include "spmapmh.h"
39 #include "ccmh.h"
40 #include "dirccmh.h"
41 #include "naivemh.h"
42 
43 static doublereal mat[5][5] = {
44  { 11., 0., 13., 0., 15. },
45  { 0., 22., 0., 24., 0. },
46  { 31., 0., 33., 0., 35. },
47  { 0., 42., 0., 44., 0. },
48  { 51., 0., 53., 0., 55. }
49 };
50 
51 int
52 main(void)
53 {
54  std::vector<integer> perm(5), invperm(5);
55  perm[0] = 4;
56  perm[1] = 3;
57  perm[2] = 2;
58  perm[3] = 1;
59  perm[4] = 0;
60  for (int i = 0; i < 5; i++) {
61  invperm[perm[i]] = i;
62  }
63 
64  FullMatrixHandler fm(5);
65  SpMapMatrixHandler spm(5, 5);
66  NaiveMatrixHandler nm(5);
67  NaivePermMatrixHandler npm(5, perm, invperm);
68 
69  for (int r = 0; r < 5; r++) {
70  for (int c = 0; c < 5; c++) {
71  if (mat[r][c] != 0.) {
72  fm(r + 1, c + 1) = mat[r][c];
73  spm(r + 1, c + 1) = mat[r][c];
74  nm(r + 1, c + 1) = mat[r][c];
75  npm(r + 1, c + 1) = mat[r][c];
76  }
77  }
78  }
79 
80  std::cout << "matrix in full form: " << std::endl
81  << fm << std::endl;
82 
83  std::cout << "matrix in sparse form: " << std::endl
84  << spm << std::endl;
85 
86  std::cout << "matrix in naive form: " << std::endl
87  << nm << std::endl;
88 
89  std::cout << "matrix in naive permuted form: " << std::endl
90  << npm << std::endl;
91 
92  std::vector<doublereal> Ax0;
93  std::vector<integer> Ai0, Ap0;
94  spm.MakeCompressedColumnForm(Ax0, Ai0, Ap0, 0);
95 
96  CColMatrixHandler<0> ccm0(Ax0, Ai0, Ap0);
97 
98  std::cout << "matrix in cc<0> form: " << std::endl
99  << ccm0 << std::endl;
100 
101  std::cout << "matrix in cc<0> form again: " << std::endl;
102  const CColMatrixHandler<0>& const_ccm0 = ccm0;
103  for (int ir = 1; ir <= 5; ir++) {
104  for (int ic = 1; ic <= 5; ic++) {
105  std::cout << std::setw(16) << const_ccm0(ir, ic);
106  }
107  std::cout << std::endl;
108  }
109 
110  std::vector<doublereal> Ax1;
111  std::vector<integer> Ai1, Ap1;
112  spm.MakeCompressedColumnForm(Ax1, Ai1, Ap1, 1);
113 
114  CColMatrixHandler<1> ccm1(Ax1, Ai1, Ap1);
115 
116  std::cout << "matrix in cc<1> form: " << std::endl
117  << ccm1 << std::endl;
118 
119  std::cout << "matrix in cc<1> form again: " << std::endl;
120  const CColMatrixHandler<1>& const_ccm1 = ccm1;
121  for (int ir = 1; ir <= 5; ir++) {
122  for (int ic = 1; ic <= 5; ic++) {
123  std::cout << std::setw(16) << const_ccm1(ir, ic);
124  }
125  std::cout << std::endl;
126  }
127 
128  DirCColMatrixHandler<0> dirm0(Ax0, Ai0, Ap0);
129  std::cout << "matrix in dir<0> form: " << std::endl
130  << dirm0 << std::endl;
131 
132  std::cout << "matrix in dir<0> form again: " << std::endl;
133  const DirCColMatrixHandler<0>& const_dirm0 = dirm0;
134  for (int ir = 1; ir <= 5; ir++) {
135  for (int ic = 1; ic <= 5; ic++) {
136  std::cout << std::setw(16) << const_dirm0(ir, ic);
137  }
138  std::cout << std::endl;
139  }
140 
141  DirCColMatrixHandler<1> dirm1(Ax1, Ai1, Ap1);
142  std::cout << "matrix in dir<1> form: " << std::endl
143  << dirm1 << std::endl;
144 
145  std::cout << "matrix in dir<1> form again: " << std::endl;
146  const DirCColMatrixHandler<1>& const_dirm1 = dirm1;
147  for (int ir = 1; ir <= 5; ir++) {
148  for (int ic = 1; ic <= 5; ic++) {
149  std::cout << std::setw(16) << const_dirm1(ir, ic);
150  }
151  std::cout << std::endl;
152  }
153 
154  std::cout << "***************************" << std::endl
155  << "full matrix handler:" << std::endl;
156 
158  i != fm.end(); ++i)
159  {
160  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
161  if (mat[i->iRow][i->iCol] != i->dCoef) {
162  std::cout << "==> failed!" << std::endl;
163  }
164  }
165 
166  std::cout << "***************************" << std::endl
167  << "sparse map matrix handler:" << std::endl;
168 
170  i != spm.end(); ++i)
171  {
172  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
173  if (mat[i->iRow][i->iCol] != i->dCoef) {
174  std::cout << "==> failed!" << std::endl;
175  }
176  }
177 
178  std::cout << "***************************" << std::endl
179  << "naive sparse matrix handler:" << std::endl;
181  i != nm.end(); ++i)
182  {
183  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
184  if (mat[i->iRow][i->iCol] != i->dCoef) {
185  std::cout << "==> failed!" << std::endl;
186  }
187  }
188 
189  std::cout << "***************************" << std::endl
190  << "naive permuted sparse matrix handler:" << std::endl;
192  i != npm.end(); ++i)
193  {
194  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
195  if (mat[i->iRow][i->iCol] != i->dCoef) {
196  std::cout << "==> failed!" << std::endl;
197  }
198  }
199 
200  std::cout << "***************************" << std::endl
201  << "column compressed <0> sparse matrix handler:" << std::endl;
203  i != ccm0.end(); ++i)
204  {
205  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
206  if (mat[i->iRow][i->iCol] != i->dCoef) {
207  std::cout << "==> failed!" << std::endl;
208  }
209  }
210 
211  std::cout << "***************************" << std::endl
212  << "column compressed <1> sparse matrix handler:" << std::endl;
214  i != ccm1.end(); ++i)
215  {
216  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
217  if (mat[i->iRow][i->iCol] != i->dCoef) {
218  std::cout << "==> failed!" << std::endl;
219  }
220  }
221 
222  std::cout << "***************************" << std::endl
223  << "dir column compressed <0> sparse matrix handler:" << std::endl;
225  i != dirm0.end(); ++i)
226  {
227  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
228  if (mat[i->iRow][i->iCol] != i->dCoef) {
229  std::cout << "==> failed!" << std::endl;
230  }
231  }
232 
233  std::cout << "***************************" << std::endl
234  << "dir column compressed <1> sparse matrix handler:" << std::endl;
236  i != dirm1.end(); ++i)
237  {
238  std::cout << "(" << i->iRow << ", " << i->iCol << ", " << i->dCoef << ")" << std::endl;
239  if (mat[i->iRow][i->iCol] != i->dCoef) {
240  std::cout << "==> failed!" << std::endl;
241  }
242  }
243 
244  return 0;
245 }
246 
NaiveMatrixHandler::const_iterator begin(void) const
Definition: naivemh.h:97
integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const
Definition: spmapmh.cc:80
const NaiveMatrixHandler::const_iterator & end(void) const
Definition: naivemh.h:101
const FullMatrixHandler::const_iterator & end(void) const
Definition: fullmh.h:106
FullMatrixHandler::const_iterator begin(void) const
Definition: fullmh.h:102
CompactSparseMatrixHandler_tpl< off >::const_iterator begin(void) const
Definition: spmh.h:262
const SpMapMatrixHandler::const_iterator & end(void) const
Definition: spmapmh.h:120
int main(void)
Definition: itertest.cc:52
const NaivePermMatrixHandler::const_iterator & end(void) const
Definition: naivemh.h:253
const CompactSparseMatrixHandler_tpl< off >::const_iterator & end(void) const
Definition: spmh.h:266
SpMapMatrixHandler::const_iterator begin(void) const
Definition: spmapmh.h:116
static std::stack< cleanup * > c
Definition: cleanup.cc:59
static doublereal mat[5][5]
Definition: itertest.cc:43
double doublereal
Definition: colamd.c:52
NaivePermMatrixHandler::const_iterator begin(void) const
Definition: naivemh.h:249