MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
stlvh.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/stlvh.cc,v 1.11 2017/01/12 14:43:54 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 <cmath>
35 #include <algorithm>
36 
37 #include "stlvh.h"
38 #include "matvec3.h"
39 
40 /* per il debugging */
41 #include "myassert.h"
42 #include "mynewmem.h"
43 #include "except.h"
44 
45 /* STLVectorHandler - begin */
46 
48 : std::vector<doublereal>(iSize)
49 {
50  ASSERT(iSize >= 0);
51 }
52 
54 {
55  NO_OP;
56 }
57 
58 #ifdef DEBUG
59 /* Usata per il debug */
60 void
61 STLVectorHandler::IsValid(void) const
62 {
63  ASSERT(!empty());
64 }
65 #endif // DEBUG
66 
67 doublereal *
69 {
70 #ifdef DEBUG
71  IsValid();
72 #endif // DEBUG
73 
74  return &((*const_cast<STLVectorHandler *>(this))[0]);
75 }
76 
77 integer
79 {
80  return size();
81 }
82 
83 void
85 {
86 #ifdef DEBUG
87  IsValid();
88 #endif // DEBUG
89 
90  std::fill(begin(), end(), 0.);
91 }
92 
93 void
95 {
96  ASSERT(iNewSize >= 0);
97 
98  resize(iNewSize);
99 }
100 
101 void
103 {
104  ASSERT(iRow > 0);
105  ASSERT(unsigned(iRow) <= size());
106 
107  (*this)[--iRow] = dCoef;
108 }
109 
110 void
112 {
113  ASSERT(iRow > 0);
114  ASSERT(unsigned(iRow) <= size());
115 
116  (*this)[--iRow] += dCoef;
117 }
118 
119 void
121 {
122  ASSERT(iRow > 0);
123  ASSERT(unsigned(iRow) <= size());
124 
125  (*this)[--iRow] -= dCoef;
126 }
127 
128 const doublereal&
130 {
131  ASSERT(iRow > 0);
132  ASSERT(unsigned(iRow) <= size());
133 
134  return (*this)[--iRow];
135 }
136 
137 const doublereal&
139 {
140  ASSERT(iRow > 0);
141  ASSERT(unsigned(iRow) <= size());
142 
143  return (*this)[--iRow];
144 }
145 
146 doublereal&
148 {
149  ASSERT(iRow > 0);
150  ASSERT(unsigned(iRow) <= size());
151 
152  return (*this)[--iRow];
153 }
154 
155 /* Somma un Vec3 nella posizione desiderata */
156 void
158 {
159  ASSERT(iRow > 0);
160  ASSERT(unsigned(iRow) + 2 <= size());
161 
162  (*this)[--iRow] += v(1);
163  (*this)[++iRow] += v(2);
164  (*this)[++iRow] += v(3);
165 }
166 
167 /* Sottrae un Vec3 nella posizione desiderata */
168 void
170 {
171  ASSERT(iRow > 0);
172  ASSERT(unsigned(iRow) + 2 <= size());
173 
174  (*this)[--iRow] -= v(1);
175  (*this)[++iRow] -= v(2);
176  (*this)[++iRow] -= v(3);
177 }
178 
179 /* Scrive un Vec3 nella posizione desiderata */
180 void
182 {
183  ASSERT(iRow > 0);
184  ASSERT(unsigned(iRow) + 2 <= size());
185 
186  (*this)[--iRow] = v(1);
187  (*this)[++iRow] = v(2);
188  (*this)[++iRow] = v(3);
189 }
190 
191 /* Somma e moltiplica per uno scalare */
194 {
195  ASSERT(VH.iGetSize() > 0);
196  ASSERT(unsigned(VH.iGetSize()) == size());
197 
198  for (integer i = iGetSize(); i > 0;) {
199  doublereal dd = d*VH(i);
200  i--;
201  (*this)[i] = dd;
202  }
203 
204  return *this;
205 }
206 
207 /* Somma e moltiplica per uno scalare v = VH + d * VH1 */
210  const VectorHandler& VH1,
211  const doublereal& d)
212 {
213  ASSERT(VH.iGetSize() > 0);
214  ASSERT(unsigned(VH.iGetSize()) == size());
215  ASSERT(VH1.iGetSize() > 0);
216  ASSERT(unsigned(VH1.iGetSize()) == size());
217 
218  for (integer i = iGetSize(); i > 0;) {
219  doublereal dd = VH(i) + d*VH1(i);
220  i--;
221  (*this)[i] = dd;
222  }
223 
224  return *this;
225 }
226 
227 /* Moltiplica per uno scalare */
230 {
231  ASSERT(VH.iGetSize() > 0);
232  ASSERT(unsigned(VH.iGetSize()) == size());
233 
234  for (integer i = iGetSize(); i > 0; ) {
235  doublereal dd = d*VH(i);
236  i--;
237  (*this)[i] = dd;
238  }
239 
240  return *this;
241 }
242 
243 /* Overload di += usato per la correzione della soluzione */
246 {
247  ASSERT(VH.iGetSize() > 0);
248  ASSERT(unsigned(VH.iGetSize()) == size());
249 
250  for (integer i = iGetSize(); i > 0;) {
251  doublereal dd = VH(i);
252  i--;
253  (*this)[i] += dd;
254  }
255 
256  return *this;
257 }
258 
259 /* Overload di -= */
262 {
263  ASSERT(VH.iGetSize() > 0);
264  ASSERT(unsigned(VH.iGetSize()) == size());
265 
266  for (integer i = iGetSize(); i > 0;) {
267  doublereal dd = VH(i);
268  i--;
269  (*this)[i] -= dd;
270  }
271 
272  return *this;
273 }
274 
275 /* Overload di *= */
278 {
279  for (std::vector<doublereal>::iterator i = begin(); i != end(); ++i) {
280  *i *= d;
281  }
282 
283  return *this;
284 }
285 
286 /* Assegnazione che copia il contenuto della memoria di due handlers */
289 {
290  ASSERT(VH.iGetSize() > 0);
291  ASSERT(unsigned(VH.iGetSize()) == size());
292 
293  for (integer i = iGetSize(); i > 0;) {
294  doublereal dd = VH(i);
295  i--;
296  (*this)[i] = dd;
297  }
298 
299  return *this;
300 }
301 
302 /* Norma 2 del vettore */
305 {
306 #ifdef DEBUG
307  IsValid();
308 #endif // DEBUG
309 
310  doublereal d2 = 0.;
311 
312  for (std::vector<doublereal>::const_iterator i = begin();
313  i != end(); ++i)
314  {
315  doublereal d = *i;
316  d2 += d*d;
317  }
318 
319  return d2;
320 }
321 
322 
323 /* Prodotto Scalare di due vettori */
326 {
327  ASSERT(VH.iGetSize() > 0);
328  ASSERT(unsigned(VH.iGetSize()) == size());
329 
330  doublereal d = 0.;
331 
332  for (integer i = iGetSize(); i > 0;) {
333  doublereal dd = VH(i);
334  i--;
335  d += (*this)[i]*dd;
336  }
337 
338  return d;
339 }
340 
341 /* STLVectorHandler - end */
342 
Definition: matrix.h:68
Definition: matvec3.h:98
virtual void IncCoef(integer iRow, const doublereal &dCoef)
Definition: stlvh.cc:111
virtual VectorHandler & operator*=(const doublereal &d)
Definition: stlvh.cc:277
virtual integer iGetSize(void) const
Definition: stlvh.cc:78
virtual VectorHandler & operator+=(const VectorHandler &VH)
Definition: stlvh.cc:245
virtual doublereal * pdGetVec(void) const
Definition: stlvh.cc:68
virtual void Reset(void)
Definition: stlvh.cc:84
#define NO_OP
Definition: myassert.h:74
virtual integer iGetSize(void) const =0
virtual const doublereal & dGetCoef(integer iRow) const
Definition: stlvh.cc:129
virtual VectorHandler & operator-=(const VectorHandler &VH)
Definition: stlvh.cc:261
STLVectorHandler(integer iSize=0)
Definition: stlvh.cc:47
virtual void Sub(integer iRow, const Vec3 &v)
Definition: stlvh.cc:169
virtual VectorHandler & ScalarMul(const VectorHandler &VH, const doublereal &d)
Definition: stlvh.cc:229
virtual VectorHandler & operator=(const VectorHandler &VH)
Definition: stlvh.cc:288
virtual void Put(integer iRow, const Vec3 &v)
Definition: stlvh.cc:181
virtual const doublereal & operator()(integer iRow) const
Definition: stlvh.cc:138
virtual void DecCoef(integer iRow, const doublereal &dCoef)
Definition: stlvh.cc:120
virtual ~STLVectorHandler(void)
Definition: stlvh.cc:53
virtual void Resize(integer iNewSize)
Definition: stlvh.cc:94
virtual void Add(integer iRow, const Vec3 &v)
Definition: stlvh.cc:157
#define ASSERT(expression)
Definition: colamd.c:977
virtual VectorHandler & ScalarAddMul(const VectorHandler &VH, const doublereal &d)
Definition: stlvh.cc:193
virtual void PutCoef(integer iRow, const doublereal &dCoef)
Definition: stlvh.cc:102
virtual doublereal InnerProd(const VectorHandler &VH) const
Definition: stlvh.cc:325
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
virtual doublereal Dot(void) const
Definition: stlvh.cc:304