RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matrix_t.cpp
Go to the documentation of this file.
1 //
2 // File = matrix_t.cpp
3 //
4 
5 #include "matrix_t.h"
6 #include <error.h>
7 
8 #ifdef _DEBUG
9 #include <fstream>
10 extern std::ofstream DebugFile;
11 //#define _MTX_DEBUG 1
12 #endif
13 
14 //---------------------------------------------
15 // constructor
16 template <class T>
17 matrix<T>::matrix(int row_orig, int nrows, int col_orig, int ncols)
18 {
19  if ((nrows <= 0) || (ncols <= 0))
20  std::cout << "illegal matrix dimension" << std::endl;
21  _p = new mrep;
22 #ifdef _MTX_DEBUG
23  DebugFile << "\nctor for matrix at " << this << " (mrep = " << (void*)_p
24  << ")" << std::endl;
25 #endif
26  _p->length = nrows;
27  _p->orig_indx = row_orig;
28  _p->max_indx = row_orig + nrows - 1;
29  _p->f = new rowvec<T>*[nrows];
30 #ifdef _MTX_DEBUG
31  DebugFile << "v::v(i,i): ptr array " << nrows << " long alloc at "
32  << (void*)(_p->f) << std::endl;
33 #endif
34  for (int i = 0; i < nrows; i++)
35  _p->f[i] = new rowvec<T>(col_orig, ncols);
36  _p->refcnt = 1;
37  Is_Temp = 0;
38 }
39 
40 //----------------------------------------------
41 template <class T>
43 {
44 // rowvec<T> *row_ptr;
45 
46 #ifdef _MTX_DEBUG
47  DebugFile << "\ndtor for matrix at " << (void*)this << std::endl;
48 #endif
49  if (--_p->refcnt == 0) {
50  int nrows = _p->length;
51  for (int i = 0; i < nrows; i++)
52  delete _p->f[i];
53  delete _p->f;
54 #ifdef _MTX_DEBUG
55  DebugFile << "\nm::~m(): deleting mrep at " << (void*)_p << std::endl;
56 #endif
57  delete _p;
58  }
59 }
60 //----------------------------------------------
61 // row extraction
62 template <class T>
64 {
65  return *(_p->f[(((i >= (_p->orig_indx)) && (i <= _p->max_indx))
66  ? (i - (_p->orig_indx))
67  : 0)]);
68 }
69 
70 //--------------------------------------------------
71 // post-multiply matrix by a column vector
72 template <class T>
74 {
75  // check dimensions
76  int row_orig = _p->orig_indx;
77  int nrows = _p->length;
78  int col_orig = ((_p->f[_p->orig_indx])->p)->orig_indx;
79  int ncols = ((_p->f[_p->orig_indx])->p)->length;
80  int vec_orig = v2.p->orig_indx;
81  int vec_len = v2.p->length;
82 
83  if (ncols != vec_len) {
84  std::cout << "error in matrix method" << std::endl;
85  return (v2);
86  }
87 
88  // allocate new vector for result
89  colvec<T>* v_res = new colvec<T>(row_orig, nrows);
90 #ifdef _MTX_DEBUG
91  DebugFile << "\nm::op*(cv): new colvec at " << (void*)v_res << std::endl;
92 #endif
93  v_res->Is_Temp = 1;
94 
95  // perform multiplication and populate results vector
96  T sum;
97  for (int i = 0; i < nrows; i++) {
98  sum = 0.0;
99  for (int j = 0; j < vec_len; j++) {
100  // sum += ((v2.p->f[j]) * (((_p->f[i-(_p->orig_indx)])->p)->f[j]));
101  sum += ((v2.p->f[j]) * (((_p->f[i])->p)->f[j]));
102  }
103  (v_res->p)->f[i] = sum;
104  }
105  if (v2.Is_Temp) {
106 #ifdef _MTX_DEBUG
107  DebugFile << "\nm::op*(cv): deleting colvec at " << (void*)(&v2)
108  << std::endl;
109 #endif
110  delete (&v2);
111  }
112  if (Is_Temp) {
113 #ifdef _MTX_DEBUG
114  DebugFile << "\nm::op*(cv): deleting matrix at " << (void*)this
115  << std::endl;
116 #endif
117  delete this;
118  }
119  return (*v_res);
120 }
121 //--------------------------------------------------
122 // do element-by-element subtraction
123 template <class T>
124 matrix<T>&
126 {
127  int nrows = _p->length;
128  int ncols = ((_p->f[_p->orig_indx])->p)->length;
129  for (int i = 0; i < nrows; i++) {
130  for (int j = 0; j < ncols; j++) {
131  ((_p->f[i])->p)->f[j] -= (((m2._p)->f[i])->p)->f[j];
132  }
133  }
134  if (m2.Is_Temp) {
135 #ifdef _MTX_DEBUG
136  DebugFile << "\nm::op-=(m): deleting matrix at " << (void*)(&m2)
137  << std::endl;
138 #endif
139  delete (&m2);
140  }
141  return (*this);
142 }
143 template class matrix<double>;
144 template class matrix<complex>;
Definition: matrix_t.h:6
struct vector::vrep * p
colvec< T > & operator*(colvec< T > &v2)
Definition: matrix_t.cpp:73
int Is_Temp
Definition: matrix_t.h:23
struct matrix::mrep * _p
Definition: matrix_t.h:8
matrix(int row_orig, int nrows, int col_orig, int ncols)
Definition: matrix_t.cpp:17
int orig_indx
Definition: vector_t.h:27
int length
Definition: vector_t.h:28
matrix< T > & operator-=(matrix< T > &m2)
Definition: matrix_t.cpp:125
std::ofstream DebugFile
int Is_Temp
Definition: vector_t.h:31
rowvec< T > & operator[](int i)
Definition: matrix_t.cpp:63
~matrix(void)
Definition: matrix_t.cpp:42