1 | // $Id: matrix.h 390 2005-09-13 09:28:13Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_gslapi_matrix_ |
---|
4 | #define _theplu_gslapi_matrix_ |
---|
5 | |
---|
6 | #include <c++_tools/gslapi/vector.h> |
---|
7 | |
---|
8 | #include <gsl/gsl_matrix.h> |
---|
9 | #include <iostream> |
---|
10 | |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace gslapi { |
---|
14 | |
---|
15 | class vector; |
---|
16 | |
---|
17 | /// |
---|
18 | /// This is the C++ tools interface to GSL matrix. 'double' is the |
---|
19 | /// only type supported, maybe we should add a 'complex' type as |
---|
20 | /// well in the future. |
---|
21 | /// |
---|
22 | class matrix |
---|
23 | { |
---|
24 | public: |
---|
25 | |
---|
26 | /// |
---|
27 | /// The default constructor. |
---|
28 | /// |
---|
29 | matrix(void); |
---|
30 | |
---|
31 | /// |
---|
32 | /// Constructor. Allocates memory space for \a r times \a c |
---|
33 | /// elements, and sets all elements to \a init_value. |
---|
34 | /// |
---|
35 | matrix(const size_t& r, const size_t& c, const double init_value=0); |
---|
36 | |
---|
37 | /// |
---|
38 | /// The copy constructor. |
---|
39 | /// |
---|
40 | matrix(const matrix&); |
---|
41 | |
---|
42 | /// |
---|
43 | /// The istream constructor. |
---|
44 | /// |
---|
45 | /// Matrix rows are sepearated with the new line character, and |
---|
46 | /// column elements in a row must be separated with white space |
---|
47 | /// characters except the new line (\\n) character. Rows, and |
---|
48 | /// columns, are read consequently and only rectangular matrices |
---|
49 | /// are supported. Empty lines are ignored. End of input is at end |
---|
50 | /// of file marker. |
---|
51 | /// |
---|
52 | explicit matrix(std::istream &); |
---|
53 | |
---|
54 | /// |
---|
55 | /// The destructor. |
---|
56 | /// |
---|
57 | ~matrix(void); |
---|
58 | |
---|
59 | /// |
---|
60 | /// @return The number of columns in the matrix. |
---|
61 | /// |
---|
62 | inline size_t columns(void) const { return m_->size2; } |
---|
63 | |
---|
64 | /// |
---|
65 | /// Swap column \a i with \a j. |
---|
66 | /// |
---|
67 | inline void column_swap(const size_t& i,const size_t& j) |
---|
68 | { gsl_matrix_swap_columns(m_,i,j); } |
---|
69 | |
---|
70 | /// |
---|
71 | /// @return true if each element deviates less or equal than \a |
---|
72 | /// d. If any matrix contain a nan false is always returned. |
---|
73 | /// |
---|
74 | bool equal(const matrix&, const double d=0) const; |
---|
75 | |
---|
76 | /// |
---|
77 | /// Create a new copy of the internal GSL matrix. |
---|
78 | /// |
---|
79 | /// Necessary memory for the new GSL matrix is allocated and the |
---|
80 | /// caller is responsible for freeing the allocated memory. |
---|
81 | /// |
---|
82 | /// @return A pointer to a copy of the internal GSL matrix. |
---|
83 | /// |
---|
84 | gsl_matrix* gsl_matrix_copy(void) const; |
---|
85 | |
---|
86 | /// |
---|
87 | /// @return A const pointer to the internal GSL matrix. |
---|
88 | /// |
---|
89 | inline const gsl_matrix* gsl_matrix_pointer(void) const { return m_; } |
---|
90 | |
---|
91 | /// |
---|
92 | /// @return A pointer to the internal GSL matrix. |
---|
93 | /// |
---|
94 | inline gsl_matrix* gsl_matrix_pointer(void) { return m_; }; |
---|
95 | |
---|
96 | /// |
---|
97 | /// This function multiplies all elements between two matrices and |
---|
98 | /// the result is stored back into the calling object, \f$a_{ij} = |
---|
99 | /// a_{ij} * b_{ij} \; \forall i,j\f$. |
---|
100 | /// |
---|
101 | inline void mul_elements(const matrix& b) |
---|
102 | { gsl_matrix_mul_elements(m_,b.m_); } |
---|
103 | |
---|
104 | /// |
---|
105 | /// Swap row \a i with \a j. |
---|
106 | /// |
---|
107 | inline void row_swap( const size_t& i, const size_t& j) |
---|
108 | { gsl_matrix_swap_rows(m_,i,j); } |
---|
109 | |
---|
110 | /// |
---|
111 | /// @return The number of rows in the matrix. |
---|
112 | /// |
---|
113 | inline size_t rows(void) const { return m_->size1; } |
---|
114 | |
---|
115 | /// |
---|
116 | /// Set all elements to \a value. |
---|
117 | /// |
---|
118 | inline void set_all(const double value) { gsl_matrix_set_all(m_,value); } |
---|
119 | |
---|
120 | /// |
---|
121 | /// Set column \a i to \a vec. |
---|
122 | /// |
---|
123 | inline void set_column(const size_t i, const vector vec) |
---|
124 | {gsl_matrix_set_col(m_, i, vec.gsl_vector_pointer());} |
---|
125 | |
---|
126 | /// |
---|
127 | /// Set row \a i to \a vec. |
---|
128 | /// |
---|
129 | inline void set_row(const size_t i, const vector vec) |
---|
130 | {gsl_matrix_set_row(m_, i, vec.gsl_vector_pointer());} |
---|
131 | |
---|
132 | /// |
---|
133 | /// Set column \a i to \a vec |
---|
134 | /// |
---|
135 | //void matrix set_column(const size_t i, const vector vec) const; |
---|
136 | |
---|
137 | /// |
---|
138 | /// Transpose the matrix. |
---|
139 | /// |
---|
140 | /// @return Transpose of the matrix. |
---|
141 | /// |
---|
142 | matrix transpose(void) const; |
---|
143 | |
---|
144 | /// |
---|
145 | /// @return Reference to the element position (\a row, \a column). |
---|
146 | /// |
---|
147 | inline double& operator()(size_t row,size_t column) |
---|
148 | { return (*gsl_matrix_ptr(m_,row,column)); } |
---|
149 | |
---|
150 | /// |
---|
151 | /// @return Const reference to the element position (\a row, \a |
---|
152 | /// column). |
---|
153 | /// |
---|
154 | inline const double& operator()(size_t row,size_t column) const |
---|
155 | { return (*gsl_matrix_const_ptr(m_,row,column)); } |
---|
156 | |
---|
157 | /// |
---|
158 | /// @todo to be properly implemented. Should return a reference to a vector |
---|
159 | /// object. Underlying GSL supports only GSL_vector views, thus |
---|
160 | /// some redesign of the vector class is needed. |
---|
161 | /// |
---|
162 | /// inline vector& operator[](size_t i); |
---|
163 | /// So for now, stupid copying is done, and actually a matrix |
---|
164 | /// row is returned using this function. |
---|
165 | vector operator[](size_t row) const; |
---|
166 | |
---|
167 | /// |
---|
168 | /// @todo to be properly implemented. Should return a reference to |
---|
169 | /// a vector object (matrix row). Underlying GSL supports only GSL_vector |
---|
170 | /// views, thus some redesign of the vector class is needed. |
---|
171 | /// |
---|
172 | /// inline const vector& operator[](size_t i) const; |
---|
173 | /// |
---|
174 | /// So for now, stupid copying is done, and actually a matrix |
---|
175 | /// column is returned using this function. |
---|
176 | vector TEMP_col_return(size_t column) const; |
---|
177 | |
---|
178 | /// |
---|
179 | /// Matrix multiplication. |
---|
180 | /// |
---|
181 | /// @return The resulting matrix. |
---|
182 | /// |
---|
183 | matrix operator*(const matrix&) const; |
---|
184 | |
---|
185 | /// |
---|
186 | /// Matrix-vector multiplication. |
---|
187 | /// |
---|
188 | /// @return The resulting vector. |
---|
189 | /// |
---|
190 | vector operator*(const vector&) const; |
---|
191 | |
---|
192 | /// |
---|
193 | /// Matrix addition. |
---|
194 | /// |
---|
195 | /// @return The resulting matrix. |
---|
196 | /// |
---|
197 | matrix operator+(const matrix&) const; |
---|
198 | |
---|
199 | /// |
---|
200 | /// @todo Matrix addition. |
---|
201 | /// |
---|
202 | /// @return The resulting matrix. |
---|
203 | /// |
---|
204 | matrix operator+=(const matrix&); |
---|
205 | |
---|
206 | /// |
---|
207 | /// Matrix subtraction. |
---|
208 | /// |
---|
209 | /// @return The resulting matrix. |
---|
210 | /// |
---|
211 | matrix operator-(const matrix&) const; |
---|
212 | |
---|
213 | /// |
---|
214 | /// @todo Matrix subtraction. |
---|
215 | /// |
---|
216 | /// @return The resulting matrix. |
---|
217 | /// |
---|
218 | matrix operator-=(const matrix&) const; |
---|
219 | |
---|
220 | /// |
---|
221 | /// Comparison operator. |
---|
222 | /// |
---|
223 | /// @return True if all elements are equal otherwise False. |
---|
224 | /// |
---|
225 | /// @see equal |
---|
226 | /// |
---|
227 | inline bool operator==( const matrix& other ) const { return equal(other);} |
---|
228 | |
---|
229 | /// |
---|
230 | /// Comparison operator. |
---|
231 | /// |
---|
232 | /// @return False if all elements are equal otherwise True. |
---|
233 | /// |
---|
234 | /// @see equal |
---|
235 | /// |
---|
236 | inline bool operator!=( const matrix& other ) const {return !equal(other);} |
---|
237 | |
---|
238 | /// |
---|
239 | /// The assignment operator. There is no requirements on |
---|
240 | /// dimensions. |
---|
241 | /// |
---|
242 | matrix& operator=(const matrix& other); |
---|
243 | |
---|
244 | /// |
---|
245 | /// Subtract and assign operator. |
---|
246 | /// |
---|
247 | inline matrix& operator-=(const matrix& m) |
---|
248 | { gsl_matrix_sub(m_,m.m_); return *this; } |
---|
249 | |
---|
250 | /// |
---|
251 | /// Multiply and assign operator. |
---|
252 | /// |
---|
253 | inline matrix& operator*=(const double d) |
---|
254 | { gsl_matrix_scale(m_,d); return *this; } |
---|
255 | |
---|
256 | |
---|
257 | private: |
---|
258 | |
---|
259 | gsl_matrix* m_; |
---|
260 | |
---|
261 | }; |
---|
262 | |
---|
263 | /// |
---|
264 | /// The output operator for the matrix class. |
---|
265 | /// |
---|
266 | std::ostream& operator<< (std::ostream& s, const matrix&); |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | }} // of namespace gslapi and namespace theplu |
---|
271 | |
---|
272 | #endif |
---|