source: trunk/src/matrix.h @ 275

Last change on this file since 275 was 275, checked in by Peter, 18 years ago

modified istream constructor for vector class to use the matrix constructor

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1// $Id: matrix.h 275 2005-04-14 17:07:56Z 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 d
71    ///
72    bool equal(const matrix&, const double d=0) const;
73
74    ///
75    /// Create a new copy of the internal GSL matrix.
76    ///
77    /// Necessary memory for the new GSL matrix is allocated and the
78    /// caller is responsible for freeing the allocated memory.
79    ///
80    /// @return A pointer to a copy of the internal GSL matrix.
81    ///
82    gsl_matrix* gsl_matrix_copy(void) const;
83
84    ///
85    /// @return A const pointer to the internal GSL matrix.
86    ///
87    inline const gsl_matrix* gsl_matrix_pointer(void) const { return m_; }
88
89    ///
90    /// @return A pointer to the internal GSL matrix.
91    ///
92    inline gsl_matrix* gsl_matrix_pointer(void) { return m_; };
93
94    ///
95    /// This function multiplies all elements between two matrices and
96    /// the result is stored back into the calling object, \f$a_{ij} =
97    /// a_{ij} * b_{ij} \; \forall i,j\f$.
98    ///
99    inline void mul_elements(const matrix& b)
100      { gsl_matrix_mul_elements(m_,b.m_); }
101
102    ///
103    /// Swap row \a i with \a j.
104    ///
105    inline void row_swap( const size_t& i, const size_t& j)
106      { gsl_matrix_swap_rows(m_,i,j); }
107
108    ///
109    /// @return The number of rows in the matrix.
110    ///
111    inline size_t rows(void) const { return m_->size1; }
112
113    ///
114    /// Set all elements to \a value.
115    ///
116    inline void set_all(const double value) { gsl_matrix_set_all(m_,value); }
117
118    ///
119    /// Set column \a i to \a vec.
120    ///
121    inline void set_column(const size_t i, const vector vec) 
122    {gsl_matrix_set_col(m_, i, vec.gsl_vector_pointer());}
123
124    ///
125    /// Set row \a i to \a vec.
126    ///
127    inline void set_row(const size_t i, const vector vec) 
128    {gsl_matrix_set_row(m_, i, vec.gsl_vector_pointer());}
129
130    ///
131    /// Set column \a i to \a vec
132    ///
133    //void matrix set_column(const size_t i, const vector vec) const;
134
135    ///
136    /// Transpose the matrix.
137    ///
138    /// @return Transpose of the matrix.
139    ///
140    matrix transpose(void) const;
141   
142    ///
143    /// @return Reference to the element position (\a row, \a column).
144    ///
145    inline double& operator()(size_t row,size_t column)
146      { return (*gsl_matrix_ptr(m_,row,column)); }
147
148    ///
149    /// @return Const reference to the element position (\a row, \a
150    /// column).
151    ///
152    inline const double& operator()(size_t row,size_t column) const
153      { return (*gsl_matrix_const_ptr(m_,row,column)); }
154
155    ///
156    /// Jari, To be implemented. Should return a reference to a vector
157    /// object. Underlying GSL supports only GSL_vector views, thus
158    /// some redesign of the vector class is needed.
159    ///
160    /// inline vector& operator[](size_t i);
161    /// So for now, stupid copying is done, and actually a matrix
162    /// row is returned using this function.
163    vector operator[](size_t row) const;
164
165    ///
166    /// Jari, to be properly implemented. Should return a reference to
167    /// a vector object (matrix row). Underlying GSL supports only GSL_vector
168    /// views, thus some redesign of the vector class is needed.
169    ///
170    /// inline const vector& operator[](size_t i) const;
171    ///
172    /// So for now, stupid copying is done, and actually a matrix
173    /// column is returned using this function.
174    vector TEMP_col_return(size_t column) const;
175
176    ///
177    /// Matrix multiplication.
178    ///
179    /// @return The resulting matrix.
180    ///
181    matrix operator*(const matrix&) const;
182
183    ///
184    /// Matrix-vector multiplication.
185    ///
186    /// @return The resulting vector.
187    ///
188    vector operator*(const vector&) const;
189
190    ///
191    /// Matrix addition.
192    ///
193    /// @return The resulting matrix.
194    ///
195    matrix operator+(const matrix&) const;
196
197    ///
198    /// Matrix subtraction.
199    ///
200    /// @return The resulting matrix.
201    ///
202    matrix operator-(const matrix&) const;
203
204    ///
205    /// Comparison operator.
206    ///
207    /// @return True if all elements are equal otherwise False.
208    ///
209    inline bool operator==( const matrix& other ) const { return equal(other);}
210
211    ///
212    /// Comparison operator.
213    ///
214    /// @return False if all elements are equal otherwise True.
215    ///
216    inline bool operator!=( const matrix& other ) const {return !equal(other);}
217
218    ///
219    /// The assignment operator. There is no requirements on
220    /// dimensions.
221    ///
222    matrix& operator=(const matrix& other);
223
224    ///
225    /// Subtract and assign operator.
226    ///
227    inline matrix& operator-=(const matrix& m)
228      { gsl_matrix_sub(m_,m.m_); return *this; }
229
230    ///
231    /// Multiply and assign operator.
232    ///
233    inline matrix& operator*=(const double d)
234      { gsl_matrix_scale(m_,d); return *this; }
235
236
237  private:
238
239    gsl_matrix* m_;
240
241  };
242
243  ///
244  /// The output operator for the matrix class.
245  ///
246  std::ostream& operator<< (std::ostream& s, const matrix&);
247
248
249
250}} // of namespace gslapi and namespace theplu
251
252#endif
Note: See TracBrowser for help on using the repository browser.