source: trunk/src/matrix.h @ 131

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

function set_row added

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