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