1 | // $Id: matrix.h 295 2005-04-29 09:15:58Z 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 | /// Jari, To be 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 | /// Jari, 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 | /// Matrix subtraction. |
---|
201 | /// |
---|
202 | /// @return The resulting matrix. |
---|
203 | /// |
---|
204 | matrix operator-(const matrix&) const; |
---|
205 | |
---|
206 | /// |
---|
207 | /// Comparison operator. |
---|
208 | /// |
---|
209 | /// @return True if all elements are equal otherwise False. |
---|
210 | /// |
---|
211 | /// @see equal |
---|
212 | /// |
---|
213 | inline bool operator==( const matrix& other ) const { return equal(other);} |
---|
214 | |
---|
215 | /// |
---|
216 | /// Comparison operator. |
---|
217 | /// |
---|
218 | /// @return False if all elements are equal otherwise True. |
---|
219 | /// |
---|
220 | /// @see equal |
---|
221 | /// |
---|
222 | inline bool operator!=( const matrix& other ) const {return !equal(other);} |
---|
223 | |
---|
224 | /// |
---|
225 | /// The assignment operator. There is no requirements on |
---|
226 | /// dimensions. |
---|
227 | /// |
---|
228 | matrix& operator=(const matrix& other); |
---|
229 | |
---|
230 | /// |
---|
231 | /// Subtract and assign operator. |
---|
232 | /// |
---|
233 | inline matrix& operator-=(const matrix& m) |
---|
234 | { gsl_matrix_sub(m_,m.m_); return *this; } |
---|
235 | |
---|
236 | /// |
---|
237 | /// Multiply and assign operator. |
---|
238 | /// |
---|
239 | inline matrix& operator*=(const double d) |
---|
240 | { gsl_matrix_scale(m_,d); return *this; } |
---|
241 | |
---|
242 | |
---|
243 | private: |
---|
244 | |
---|
245 | gsl_matrix* m_; |
---|
246 | |
---|
247 | }; |
---|
248 | |
---|
249 | /// |
---|
250 | /// The output operator for the matrix class. |
---|
251 | /// |
---|
252 | std::ostream& operator<< (std::ostream& s, const matrix&); |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | }} // of namespace gslapi and namespace theplu |
---|
257 | |
---|
258 | #endif |
---|