source: trunk/src/matrix.h @ 240

Last change on this file since 240 was 240, checked in by Jari Häkkinen, 17 years ago

Changed mul_elements interface, and fixed serious bug in it.

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