RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
vector_t.cpp
Go to the documentation of this file.
1 //
2 // File = vector_t.cpp
3 //
4 
5 #include "vector_t.h"
6 #include "complex.h"
7 #include "matrix_t.h"
8 
9 #ifdef _DEBUG
10 #include <fstream>
11 //#define _VEC_DEBUG 1
12 extern std::ofstream DebugFile;
13 #endif
14 
15 template <class T>
16 class vector;
17 //------------------------------------------
18 // constructor for row vector
19 template <class T>
21  : vector<T>()
22 {
23 #ifdef _VEC_DEBUG
24  DebugFile << "\nshallow ctor for rowvec at " << (void*)this << std::endl;
25 #endif
26  // p->is_row_vec = 0;
27 }
28 
29 //------------------------------------------
30 template <class T>
31 rowvec<T>::rowvec(int origin, int size)
32  : vector<T>(origin, size)
33 {
34 #ifdef _VEC_DEBUG
35  DebugFile << "\nctor for rowvec at " << (void*)this
36  << " (vrep = " << (void*)p << ")" << std::endl;
37 #endif
38 }
39 //------------------------------------------------
40 // transpose operator
41 template <class T>
43 {
44  // rowvec<T> *rv = new rowvec<T>(p->orig_indx,p->length);
45  rowvec<T>* rv = new rowvec<T>();
46 #ifdef _VEC_DEBUG
47  DebugFile << "\ncv::op!(): new rowvec at " << (void*)rv << std::endl;
48  DebugFile << "\ncv::op!(): hook vrep " << (void*)p << " to rowvec "
49  << (void*)rv << std::endl;
50 #endif
51  rv->p = this->p;
52  *(rv->p) = *(this->p);
53  ((rv->p)->refcnt)++;
54  rv->Is_Temp = 1;
55  if (this->Is_Temp) {
56 #ifdef _VEC_DEBUG
57  DebugFile << "\ncv::op!(): deleting colvec at " << (void*)this << std::endl;
58 #endif
59  delete this;
60  }
61  return (*rv);
62 }
63 
64 //------------------------------------------------
65 // transpose operator
66 template <class T>
68 {
69  colvec<T>* cv = new colvec<T>(this->p->orig_indx, this->p->length);
70 #ifdef _VEC_DEBUG
71  DebugFile << "\nnew colvec at " << (void*)cv << std::endl;
72  DebugFile << "\nrv::op!(): hook vrep " << (void*)p << " to colvec "
73  << (void*)cv << std::endl;
74 #endif
75  cv->p = this->p;
76  *(cv->p) = *(this->p);
77  cv->Is_Temp = 1;
78  if (this->Is_Temp) {
79 #ifdef _VEC_DEBUG
80  DebugFile << "\nrv::op!(): deleting rowvec at " << (void*)this << std::endl;
81 #endif
82  delete this;
83  }
84  return (*cv);
85 }
86 
87 //---------------------------------------------------
88 // row vector times column vector
89 template <class T>
91 {
92  // get origin and length of row vector
93  int v1_orig = this->p->orig_indx;
94  int v1_len = this->p->length;
95 
96  // get origin and length of column vector
97  int v2_orig = v2.p->orig_indx;
98  int v2_len = v2.p->length;
99 
100  // alocate scalar for result
101  // T *result = new T;
102  // #ifdef _VEC_DEBUG
103  // DebugFile << "rv::op*(cv): new scalar alloc at "
104  // << (void*)result << std::endl;
105  // #endif
106  T sum;
107  sum = 0;
108  for (int idx = 0; idx < v1_len; idx++) {
109  // sum += ((p->f[idx+v1_orig]) * (v2.p->f[idx+v2_orig]));
110  sum += ((this->p->f[idx]) * (v2.p->f[idx]));
111  }
112  //*result = sum;
113  if (v2.Is_Temp) {
114 #ifdef _VEC_DEBUG
115  DebugFile << "\nrv::op*(cv): deleting colvec at " << (void*)(&v2)
116  << std::endl;
117 #endif
118  delete (&v2);
119  }
120  if (this->Is_Temp) {
121 #ifdef _VEC_DEBUG
122  DebugFile << "\nrv::op*(cv): deleting rowvec at " << (void*)this
123  << std::endl;
124 #endif
125  delete this;
126  }
127  // return(*result);
128  return (sum);
129 }
130 //---------------------------------------------------
131 // method to multiply row vector times matrix
132 template <class T>
134 {
135  // check dimensions
136  int vec_orig = this->p->orig_indx;
137  int vec_len = this->p->length;
138  int row_orig = m2._p->orig_indx;
139  int nrows = m2._p->length;
140  int col_orig = ((m2._p->f[row_orig])->p)->orig_indx;
141  int ncols = ((m2._p->f[row_orig])->p)->length;
142 
143  if (nrows != vec_len) {
144 #ifdef _DEBUG
145  DebugFile << "error in vector method" << std::endl;
146 #endif
147  return (*this);
148  }
149  // allocate new vector for result
150  rowvec<T>* v_res = new rowvec<T>(col_orig, ncols);
151  v_res->Is_Temp = 1;
152 #ifdef _VEC_DEBUG
153  DebugFile << "rv::op*(m): new rowvec at " << (void*)v_res << std::endl;
154 #endif
155 
156  // perform multiplication and populate results vector
157  T sum;
158  for (int j = 0; j < ncols; j++) {
159  sum = 0.0;
160  for (int i = 0; i < nrows; i++) {
161  sum += ((this->p->f[i]) * (((m2._p->f[i - (m2._p->orig_indx)])->p)->f[j]));
162  }
163  (v_res->p)->f[j] = sum;
164  }
165  if (m2.Is_Temp) {
166 #ifdef _VEC_DEBUG
167  DebugFile << "\nrv::op*(m): deleting matrix at " << (void*)(&m2)
168  << std::endl;
169 #endif
170  delete (&m2);
171  }
172  if (this->Is_Temp) {
173 #ifdef _VEC_DEBUG
174  DebugFile << "\nrv::op*(m): deleting rowvec at " << (void*)this
175  << std::endl;
176 #endif
177  delete this;
178  }
179  return (*v_res);
180 }
181 //------------------------------------------
182 // constructor for column vector
183 template <class T>
185  : vector<T>()
186 {
187 #ifdef _VEC_DEBUG
188  DebugFile << "\nshallow ctor for colvec at " << (void*)this << std::endl;
189 #endif
190  // p->is_row_vec = 0;
191 }
192 
193 //------------------------------------------
194 // constructor for column vector
195 template <class T>
196 colvec<T>::colvec(int origin, int size)
197  : vector<T>(origin, size)
198 {
199 #ifdef _VEC_DEBUG
200  DebugFile << "\nctor for colvec at " << (void*)this
201  << " (vrep = " << (void*)p << ")" << std::endl;
202 #endif
203 }
204 
205 //---------------------------------------------------
206 // method to multiply column vector times row vector
207 template <class T>
209 {
210  // get origin and length of column vector
211  int v1_orig = this->p->orig_indx;
212  int v1_len = this->p->length;
213 
214  // get origin and length of row vector
215  int v2_orig = v2.p->orig_indx;
216  int v2_len = v2.p->length;
217 
218  // allocate matrix for result
219  matrix<T>* m_res = new matrix<T>(v1_orig, v1_len, v2_orig, v2_len);
220  m_res->Is_Temp = 1;
221 #ifdef _VEC_DEBUG
222  DebugFile << "\ncv::op*(rv): new matrix at " << (void*)m_res << std::endl;
223 #endif
224  for (int row = 0; row < v1_len; row++) {
225  for (int col = 0; col < v2_len; col++) {
226  ((((m_res->_p)->f[row])->p)->f[col]) = (this->p->f[row]) * (v2.p->f[col]);
227  }
228  }
229  if (v2.Is_Temp) {
230 #ifdef _VEC_DEBUG
231  DebugFile << "\ncv::op*(rv): deleting rowvec at " << (void*)(&v2)
232  << std::endl;
233 #endif
234  delete (&v2);
235  }
236  if (this->Is_Temp) {
237 #ifdef _VEC_DEBUG
238  DebugFile << "\ncv::op*(rv): deleting colvec at " << (void*)this
239  << std::endl;
240 #endif
241  delete this;
242  }
243  return (*m_res);
244 };
245 //------------------------------------------
246 template <class T>
248 {
249 #ifdef _VEC_DEBUG
250  DebugFile << "\nctor for vector(void) at " << (void*)this << std::endl;
251 #endif
252  p = new vrep;
253  p->refcnt = 1;
254  Is_Temp = 0;
255 };
256 
257 //------------------------------------------
258 template <class T>
259 void
261 {
262 #ifdef _VEC_DEBUG
263  DebugFile << "\nv::PurgeData(): deleting elem array at " << (void*)(p->f)
264  << std::endl;
265 #endif
266  if (p != NULL) {
267  delete[] p->f;
268 #ifdef _VEC_DEBUG
269  DebugFile << "\nv::PurgeData(): deleting vrep at " << (void*)p << std::endl;
270 #endif
271  delete p;
272  p = NULL;
273  }
274 };
275 //------------------------------------------
276 template <class T>
277 vector<T>::vector(int origin, int size)
278 {
279 #ifdef _VEC_DEBUG
280  DebugFile << "\nctor for vector at " << (void*)this << std::endl;
281 #endif
282  if (size <= 0)
283  std::cout << "illegal vector dimension" << std::endl;
284  p = new vrep;
285  p->orig_indx = origin;
286  p->length = size;
287  p->max_indx = origin + size - 1;
288  p->f = new T[size];
289 #ifdef _VEC_DEBUG
290  DebugFile << "v::v(i,i): array " << size << " long alloc at " << (void*)(p->f)
291  << std::endl;
292 #endif
293  for (int i = 0; i < size; i++)
294  p->f[i] = T(0.0);
295  p->refcnt = 1;
296  Is_Temp = 0;
297 };
298 
299 //-----------------------------------------
300 // destructor
301 template <class T>
303 {
304 #ifdef _VEC_DEBUG
305  DebugFile << "\ndtor for rowvec at " << (void*)this << std::endl;
306 #endif
307  //~vector();
308 };
309 //-----------------------------------------
310 // destructor
311 template <class T>
313 {
314 #ifdef _VEC_DEBUG
315  DebugFile << "\ndtor for colvec at " << (void*)this << std::endl;
316 #endif
317  //~vector();
318 };
319 //-----------------------------------------
320 // destructor
321 template <class T>
323 {
324 #ifdef _VEC_DEBUG
325  DebugFile << "\ndtor for vector at " << (void*)this << std::endl;
326 #endif
327  if (p != NULL) {
328 #ifdef _VEC_DEBUG
329  DebugFile << "refcnt = " << (p->refcnt) << std::endl;
330 #endif
331  if (--p->refcnt == 0) {
332 #ifdef _VEC_DEBUG
333  DebugFile << "\nv::~v(): deleting elem array at " << (void*)(p->f)
334  << std::endl;
335 #endif
336  delete[] p->f;
337 #ifdef _VEC_DEBUG
338  DebugFile << "\nv::~v(): deleting vrep at " << (void*)p << std::endl;
339 #endif
340  delete p;
341  p = NULL;
342  }
343  }
344 };
345 //-----------------------------------------------
346 // convert vector into regular array
347 template <class T>
348 T*
350 {
351  T* result;
352  int len;
353  len = p->length;
354  result = new T[len];
355 #ifdef _VEC_DEBUG
356  DebugFile << "\nv::array(): alloc new array of T at " << (void*)result
357  << std::endl;
358 #endif
359  for (int i = 0; i < len; i++) {
360  result[i] = p->f[i];
361  }
362  if (Is_Temp) {
363 #ifdef _VEC_DEBUG
364  DebugFile << "\nv::array(): deleting vector at " << (void*)this
365  << std::endl;
366 #endif
367  delete this;
368  }
369  return (result);
370 }
371 //------------------------------------------------
372 // "copy" constructor
373 template <class T>
375 {
376 #ifdef _VEC_DEBUG
377  DebugFile << "in copy constructor v::v(v)" << std::endl;
378 #endif
379  x.p->refcnt++;
380  p = x.p;
381 }
382 
383 //----------------------------------------------
384 // divide vector elements by a scalar
385 template <class T>
386 vector<T>&
388 {
389  vector<T>* res_vec = new vector<T>(p->orig_indx, p->length);
390 #ifdef _VEC_DEBUG
391  DebugFile << "\nv::op/(T): new vector at " << (void*)res_vec
392  << " (vrep = " << (void*)(res_vec->p) << ")" << std::endl;
393 #endif
394 
395  (res_vec->p)->refcnt = p->refcnt;
396  (res_vec->p)->orig_indx = p->orig_indx;
397  (res_vec->p)->length = p->length;
398  (res_vec->p)->max_indx = p->max_indx;
399  res_vec->Is_Temp = 1;
400  for (int i = 0; i < (p->length); i++) {
401  // std::cout << "p->f[" << i << "] = " << (p->f[i]) << std::endl;
402  res_vec->p->f[i] = ((p->f[i]) / denom);
403  }
404  if (Is_Temp) {
405 #ifdef _VEC_DEBUG
406  DebugFile << "\nv::op/(T): deleting vector at " << (void*)this << std::endl;
407 #endif
408  delete this;
409  }
410  return (*res_vec);
411 }
412 //----------------------------------------------
413 // assignment x = y
414 template <class T>
415 vector<T>&
417 {
418  vec.p->refcnt++;
419  if (--p->refcnt == 0) {
420 #ifdef _VEC_DEBUG
421  DebugFile << "\nv::op=(v): deleting vrep at " << (void*)p << std::endl;
422 #endif
423  delete[] p->f;
424  delete p;
425  p = NULL;
426  }
427 #ifdef _VEC_DEBUG
428  DebugFile << "\nv::op=(v): hook vrep " << (void*)(vec.p) << " to vector "
429  << (void*)this << std::endl;
430 #endif
431  p = vec.p;
432  if (vec.Is_Temp) {
433 #ifdef _VEC_DEBUG
434  DebugFile << "\nv::op=(v): deleting vector at " << (void*)(&vec)
435  << std::endl;
436 #endif
437  delete &vec;
438  }
439  return *this;
440 }
441 
442 //----------------------------------------------
443 // assignment x = y
444 template <class T>
445 rowvec<T>&
447 {
448  vec.p->refcnt++;
449  if (--(this->p->refcnt) == 0) {
450 #ifdef _VEC_DEBUG
451  DebugFile << "\nrv::op=(v): deleting vrep at " << (void*)p << std::endl;
452 #endif
453  delete[] this->p->f;
454  delete this->p;
455  this->p = NULL;
456  }
457 #ifdef _VEC_DEBUG
458  DebugFile << "\nrv::op=(v): hook vrep " << (void*)(vec.p) << " to vector "
459  << (void*)this << std::endl;
460 #endif
461  this->p = vec.p;
462  if (vec.Is_Temp) {
463 #ifdef _VEC_DEBUG
464  DebugFile << "\nrv::op=(v): deleting vector at " << (void*)(&vec)
465  << std::endl;
466 #endif
467  delete &vec;
468  }
469  return *this;
470 }
471 
472 //----------------------------------------------
473 // assignment x = y
474 template <class T>
475 colvec<T>&
477 {
478  vec.p->refcnt++;
479  if (--(this->p->refcnt) == 0) {
480 #ifdef _VEC_DEBUG
481  DebugFile << "\ncv::op=(v): deleting vrep at " << (void*)p << std::endl;
482 #endif
483  delete[] this->p->f;
484  delete this->p;
485  }
486 #ifdef _VEC_DEBUG
487  DebugFile << "\ncv::op=(v): hook vrep " << (void*)(vec.p) << " to vector "
488  << (void*)this << std::endl;
489 #endif
490  this->p = vec.p;
491  if (vec.Is_Temp) {
492 #ifdef _VEC_DEBUG
493  DebugFile << "\ncv::op=(v): deleting vector at " << (void*)(&vec)
494  << std::endl;
495 #endif
496  delete &vec;
497  }
498  return *this;
499 }
500 
501 //-------------------------------------------------
502 // assignment x = complex(2.0, 3.0)
503 
504 template <class T>
505 vector<T>&
507 {
508  int lng;
509  lng = p->length;
510  if (p->refcnt > 1) {
511  p->refcnt--;
512  p = new vrep;
513  p->length = lng;
514  p->refcnt = 1;
515  p->f = new T[lng];
516 #ifdef _VEC_DEBUG
517  DebugFile << "v::op=(T): array " << lng << " long alloc at "
518  << (void*)(p->f) << std::endl;
519 #endif
520  }
521  T* f = p->f;
522  for (int i = 0; i < lng; i++)
523  *f++ = x;
524  return *this;
525 }
526 
527 template <class T>
529 {
530  return p->f[(
531  ((i >= (p->orig_indx)) && (i <= p->max_indx)) ? (i - (p->orig_indx)) : 0)];
532 }
533 //-----------------------------------------------------
534 // pre-multiply matrix by a row vector
535 template <class T>
537 {
538  // check dimensions
539  int vec_orig = p->orig_indx;
540  int vec_len = p->length;
541  int row_orig = m2._p->orig_indx;
542  int nrows = m2._p->length;
543  int col_orig = ((m2._p->f[row_orig])->p)->orig_indx;
544  int ncols = ((m2._p->f[row_orig])->p)->length;
545 
546  if (nrows != vec_len) {
547 #ifdef _VEC_DEBUG
548  DebugFile << "error in vector method" << std::endl;
549 #endif
550  return (*this);
551  }
552  // allocate new vector for result
553  vector<T>* v_res = new vector<T>(col_orig, ncols);
554  v_res->Is_Temp = 1;
555 #ifdef _VEC_DEBUG
556  DebugFile << "\nv op*(m): new rowvec at " << (void*)v_res << std::endl;
557 #endif
558 
559  // perform multiplication and populate results vector
560  T sum;
561  for (int j = 0; j < ncols; j++) {
562  sum = 0.0;
563  for (int i = 0; i < nrows; i++) {
564  sum += ((p->f[i]) * (((m2._p->f[i - (m2._p->orig_indx)])->p)->f[j]));
565  }
566  (v_res->p)->f[j] = sum;
567  }
568  if (m2.Is_Temp) {
569 #ifdef _VEC_DEBUG
570  DebugFile << "\nv op*(m): deleting matrix at " << (void*)(&m2) << std::endl;
571 #endif
572  delete (&m2);
573  }
574  if (Is_Temp) {
575 #ifdef _VEC_DEBUG
576  DebugFile << "\nv op*(m): deleting rowvec at " << (void*)this << std::endl;
577 #endif
578  delete this;
579  }
580  return (*v_res);
581 }
582 //---------------------------------
583 // force desired instantiations
584 template class vector<double>;
585 template class rowvec<double>;
586 template class colvec<double>;
587 template class vector<complex>;
588 template class rowvec<complex>;
589 template class colvec<complex>;
Definition: matrix_t.h:6
void PurgeData(void)
Definition: vector_t.cpp:260
rowvec(void)
Definition: vector_t.cpp:20
struct vector::vrep * p
vector< T > & operator=(vector< T > &vec)
Definition: vector_t.cpp:416
~rowvec(void)
Definition: vector_t.cpp:302
colvec< T > & operator!()
Definition: vector_t.cpp:67
int Is_Temp
Definition: matrix_t.h:23
struct matrix::mrep * _p
Definition: matrix_t.h:8
int orig_indx
Definition: vector_t.h:27
int refcnt
Definition: vector_t.h:26
int length
Definition: vector_t.h:28
rowvec< T > & operator!()
Definition: vector_t.cpp:42
matrix< T > & operator*(rowvec< T > &v2)
Definition: vector_t.cpp:208
int max_indx
Definition: vector_t.h:29
T & operator[](int)
Definition: vector_t.cpp:528
rowvec< T > & operator=(vector< T > &vec)
Definition: vector_t.cpp:446
rowvec< T > ** f
Definition: matrix_t.h:17
int orig_indx
Definition: matrix_t.h:19
std::ofstream DebugFile
rowvec< T > & operator*(matrix< T > &m2)
Definition: vector_t.cpp:133
vector< T > & operator*(matrix< T > &m2)
Definition: vector_t.cpp:536
int length
Definition: matrix_t.h:20
vector< T > & operator/(T x)
Definition: vector_t.cpp:387
~vector(void)
Definition: vector_t.cpp:322
int Is_Temp
Definition: vector_t.h:31
~colvec(void)
Definition: vector_t.cpp:312
rowvec< double > * transpose(colvec< double > *)
colvec(void)
Definition: vector_t.cpp:184
T * array(void)
Definition: vector_t.cpp:349
vector(void)
Definition: vector_t.cpp:247
colvec< T > & operator=(vector< T > &vec)
Definition: vector_t.cpp:476