CPPLapack
 All Classes Files Functions Variables Friends Pages
dgematrix-misc.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! clear all the matrix data and set the sizes 0 */
3 inline void dgematrix::clear()
4 {CPPL_VERBOSE_REPORT;
5  m =0;
6  n =0;
7  delete [] array;
8  delete [] darray;
9  array =NULL;
10  darray =NULL;
11 }
12 
13 //=============================================================================
14 /*! change the matrix into a zero matrix */
16 {CPPL_VERBOSE_REPORT;
17  for(CPPL_INT i=0; i<m*n; i++){
18  array[i] =0.0;
19  }
20  return *this;
21 }
22 
23 //=============================================================================
24 /*! change the matrix into an identity matrix */
26 {CPPL_VERBOSE_REPORT;
27 #ifdef CPPL_DEBUG
28  if(m!=n){
29  ERROR_REPORT;
30  std::cerr << "Only square matrix can be a identity matrix." << std::endl
31  << "The matrix size was " << m << "x" << n << "." << std::endl;
32  exit(1);
33  }
34 #endif//CPPL_DEBUG
35 
36  for(CPPL_INT i=0; i<m*n; i++){
37  array[i] =0.0;
38  }
39  for(CPPL_INT i=0; i<m; i++){
40  operator()(i,i) =1.0;
41  }
42  return *this;
43 }
44 
45 //=============================================================================
46 /*! change sign(+/-) of the matrix */
47 inline void dgematrix::chsign()
48 {CPPL_VERBOSE_REPORT;
49  for(CPPL_INT i=0; i<m*n; i++){
50  array[i] =-array[i];
51  }
52 }
53 
54 //=============================================================================
55 /*! make a deep copy of the matrix */
56 inline void dgematrix::copy(const dgematrix& mat)
57 {CPPL_VERBOSE_REPORT;
58  m =mat.m;
59  n =mat.n;
60  delete [] array;
61  array =new double[mat.m*mat.n];
62  delete [] darray;
63  darray =new double*[n];
64  for(int i=0; i<n; i++){
65  darray[i] =&array[i*m];
66  }
67 
68  CPPL_INT mn =mat.m*mat.n;
69  CPPL_INT inc =1;
70  dcopy_(&mn, mat.array, &inc, array, &inc);
71 }
72 
73 //=============================================================================
74 /*! make a shallow copy of the matrix\n
75  This function is not designed to be used in project codes. */
76 inline void dgematrix::shallow_copy(const _dgematrix& mat)
77 {CPPL_VERBOSE_REPORT;
78  m =mat.m;
79  n =mat.n;
80  delete [] array;
81  array =mat.array;
82  delete [] darray;
83  darray =mat.darray;
84 
85  mat.nullify();
86 }
87 
88 //=============================================================================
89 /*! resize the matrix */
90 inline dgematrix& dgematrix::resize(const CPPL_INT& _m, const CPPL_INT& _n)
91 {CPPL_VERBOSE_REPORT;
92 #ifdef CPPL_DEBUG
93  if( _m<0 || _n<0 ){
94  ERROR_REPORT;
95  std::cerr << "Matrix sizes must be positive integers." << std::endl
96  << "Your input was (" << _m << "," << _n << ")." << std::endl;
97  exit(1);
98  }
99 #endif//CPPL_DEBUG
100 
101  m =_m;
102  n =_n;
103  delete [] array;
104  array =new double[m*n];
105  delete [] darray;
106  darray =new double*[n];
107  for(int i=0; i<n; i++){
108  darray[i] =&array[i*m];
109  }
110 
111  return *this;
112 }
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 ///////////////////////////////////////////////////////////////////////////////
116 ///////////////////////////////////////////////////////////////////////////////
117 
118 //=============================================================================
119 /*! get row of the matrix */
120 inline _drovector dgematrix::row(const CPPL_INT& _m) const
121 {CPPL_VERBOSE_REPORT;
122 #ifdef CPPL_DEBUG
123  if( _m<0 || _m>m ){
124  ERROR_REPORT;
125  std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
126  << "Your input was " << _m << "." << std::endl;
127  exit(1);
128  }
129 #endif//CPPL_DEBUG
130 
131  drovector v(n);
132  for(CPPL_INT j=0; j<n; j++){
133  v(j)=(*this)(_m,j);
134  }
135  return _(v);
136 }
137 
138 //=============================================================================
139 /*! get column of the matrix */
140 inline _dcovector dgematrix::col(const CPPL_INT& _n) const
141 {CPPL_VERBOSE_REPORT;
142 #ifdef CPPL_DEBUG
143  if( _n<0 || _n>n ){
144  ERROR_REPORT;
145  std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
146  << "Your input was " << _n << "." << std::endl;
147  exit(1);
148  }
149 #endif//CPPL_DEBUG
150 
151  dcovector v(m);
152  for(CPPL_INT i=0; i<m; i++){
153  v(i)=(*this)(i,_n);
154  }
155  return _(v);
156 }
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////////
160 ///////////////////////////////////////////////////////////////////////////////
161 
162 //=============================================================================
163 /*! swap two matrices */
164 inline void swap(dgematrix& A, dgematrix& B)
165 {CPPL_VERBOSE_REPORT;
166  CPPL_INT A_m =A.m, A_n =A.n;
167  double* A_array =A.array;
168  double** A_darray=A.darray;
169  A.m=B.m; A.n=B.n; A.array=B.array; A.darray=B.darray;
170  B.m=A_m; B.n=A_n; B.array=A_array; B.darray=A_darray;
171 }
172 
173 //=============================================================================
174 /*! convert user object to smart-temporary object */
175 inline _dgematrix _(dgematrix& mat)
176 {CPPL_VERBOSE_REPORT;
177  _dgematrix newmat;
178 
179  //////// shallow copy ////////
180  newmat.m =mat.m;
181  newmat.n =mat.n;
182  newmat.array =mat.array;
183  newmat.darray =mat.darray;
184 
185  //////// nullify ////////
186  mat.m =0;
187  mat.n =0;
188  mat.array =NULL;
189  mat.darray =NULL;
190 
191  return newmat;
192 }
void clear()
dgematrix & resize(const CPPL_INT &, const CPPL_INT &)
dgematrix & identity()
_dgematrix _(dgematrix &mat)
void swap(dgematrix &A, dgematrix &B)
CPPL_INT m
matrix row size
Definition: dgematrix.hpp:9
double ** darray
array of pointers of column head addresses
Definition: dgematrix.hpp:12
double * array
1D array to store matrix data
Definition: dgematrix.hpp:11
double ** darray
array of pointers of column head addresses
Definition: _dgematrix.hpp:12
dgematrix & zero()
double & operator()(const CPPL_INT &, const CPPL_INT &)
Definition: dgematrix-io.hpp:3
CPPL_INT n
matrix column size
Definition: dgematrix.hpp:10
Real Double-precision General Dence Matrix Class.
Definition: dgematrix.hpp:3
Real Double-precision Row Vector Class.
Definition: drovector.hpp:3
CPPL_INT m
matrix row size
Definition: _dgematrix.hpp:9
double * array
1D array to store matrix data
Definition: _dgematrix.hpp:11
void nullify() const
(DO NOT USE) Smart-temporary Real Double-precision Row Vector Class
Definition: _drovector.hpp:3
(DO NOT USE) Smart-temporary Real Double-precision General Dence Matrix Class
Definition: _dgematrix.hpp:3
void copy(const dgematrix &)
void shallow_copy(const _dgematrix &)
CPPL_INT n
matrix column size
Definition: _dgematrix.hpp:10
_drovector row(const CPPL_INT &) const
_dcovector col(const CPPL_INT &) const
Real Double-precision Column Vector Class.
Definition: dcovector.hpp:3
friend _dgematrix i(const dgematrix &)
friend _dgematrix _(dgematrix &)
(DO NOT USE) Smart-temporary Real Double-precision Column Vector Class
Definition: _dcovector.hpp:3