source: trunk/src/matrix.h @ 294

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