1 | #ifndef _thep_gsl_api_matrix_ |
---|
2 | #define _thep_gsl_api_matrix_ |
---|
3 | |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | #include <iomanip> |
---|
7 | #include <cmath> |
---|
8 | #include <cstdlib> |
---|
9 | #include <cassert> |
---|
10 | |
---|
11 | #include <gsl/gsl_math.h> |
---|
12 | #include <gsl/gsl_matrix.h> |
---|
13 | #include <gsl/gsl_linalg.h> |
---|
14 | #include <gsl/gsl_blas.h> |
---|
15 | |
---|
16 | #include "vector.h" |
---|
17 | |
---|
18 | namespace thep_gsl_api |
---|
19 | { |
---|
20 | |
---|
21 | /** |
---|
22 | This is a matrix interface to GSL for c++. |
---|
23 | Note that the namespace is "thep_gsl_api". |
---|
24 | */ |
---|
25 | |
---|
26 | class matrix |
---|
27 | { |
---|
28 | public: |
---|
29 | typedef double Type; |
---|
30 | |
---|
31 | /** |
---|
32 | Default constructor |
---|
33 | Does not allocate any memory |
---|
34 | */ |
---|
35 | matrix(); |
---|
36 | |
---|
37 | /** |
---|
38 | Constructor taking three argumens: |
---|
39 | N = Number of rows |
---|
40 | M = Number of columns |
---|
41 | init_to_zero, if true matrix will be initialized with zeros. |
---|
42 | */ |
---|
43 | matrix( const size_t& rows, const size_t& cols, |
---|
44 | bool init_to_zero = true ); |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | Copy constructor |
---|
52 | */ |
---|
53 | matrix( const matrix& other ); |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | Constructor taking as only argument a matrix of gsl type. |
---|
58 | Be causious here! Do not delete this matrix outside of |
---|
59 | class: NO COPY IS MADE!!! |
---|
60 | */ |
---|
61 | matrix( gsl_matrix* ); |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | Constructor taking as only argument a an istream that should |
---|
66 | contain the matrix where columns are separated with whitespace |
---|
67 | (not '\\n') and rows by '\\n'. |
---|
68 | */ |
---|
69 | matrix(std::istream &); |
---|
70 | |
---|
71 | /** |
---|
72 | Destructor will clear matrix's allocated memory |
---|
73 | */ |
---|
74 | ~matrix(); |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | The most important function when adding new classes to |
---|
79 | c++_tools. This function will return a pointer to the |
---|
80 | inner data of the class. Should be forbidden in a strictly |
---|
81 | object-orientated environment. |
---|
82 | Motivation: Effeciency! ( CAN IT BE MADE CONST? ) |
---|
83 | To be used when writting new classes using gsl-functions. |
---|
84 | */ |
---|
85 | inline gsl_matrix* get_gsl_matrix() const { return m_; } |
---|
86 | |
---|
87 | |
---|
88 | /** |
---|
89 | Returns the number of rows in the matrix. |
---|
90 | */ |
---|
91 | size_t rows() const { return m_->size1; } |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | Returns the number of rows in the matrix. |
---|
96 | */ |
---|
97 | size_t cols() const { return m_->size2; } |
---|
98 | |
---|
99 | /** |
---|
100 | get( i, j ) will return the Aij element in matrix A |
---|
101 | where i = row number and j = column number |
---|
102 | */ |
---|
103 | inline Type get( const size_t& i, const size_t& j ) const |
---|
104 | { return gsl_matrix_get(m_,i_row,j_col); } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | set( i, j, val ) will make Aij = val in matrix A |
---|
109 | where i = row number and j = column number |
---|
110 | */ |
---|
111 | inline void set( const size_t& i, const size_t& j, const Type& val ) |
---|
112 | { gsl_matrix_set( m_, i_row, j_col, val ); } |
---|
113 | |
---|
114 | /** |
---|
115 | set_all( val ) will make Aij = val in matrix A |
---|
116 | for all "i" |
---|
117 | */ |
---|
118 | inline void set_all( const Type& val ) { gsl_matrix_set_all(m_,val); } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | Assignment-operator. No requirements on dimensions, |
---|
124 | i.e. if A = B, then A will be a copy of B with B:s |
---|
125 | dimensions. |
---|
126 | */ |
---|
127 | matrix& operator=( const matrix& other ); |
---|
128 | |
---|
129 | /** |
---|
130 | operators for comparison are: |
---|
131 | == equal |
---|
132 | != not equal |
---|
133 | */ |
---|
134 | bool operator==( const matrix &other ) const; |
---|
135 | bool operator!=( const matrix &other ) const; |
---|
136 | |
---|
137 | /** |
---|
138 | operators for basic arithmetics are: \n |
---|
139 | \f$+\f$ matrix-matrix addition \n |
---|
140 | \f$-\f$ matrix-matrix subtraction \n |
---|
141 | \f$*\f$ matrix-matrix multiplication |
---|
142 | */ |
---|
143 | matrix operator+( const matrix &other ) const; |
---|
144 | matrix operator-( const matrix &other ) const; |
---|
145 | matrix operator*( const matrix &other ) const; |
---|
146 | vector operator*( const vector &other ) const; |
---|
147 | |
---|
148 | /** |
---|
149 | multiplying each element of two matrices |
---|
150 | dimension MUST AGREE |
---|
151 | */ |
---|
152 | matrix mul_elements( const matrix& a, const matrix& b ); |
---|
153 | |
---|
154 | /** |
---|
155 | Method for transposing a matrix |
---|
156 | */ |
---|
157 | matrix transpose() const; |
---|
158 | |
---|
159 | /** |
---|
160 | Method for calculating the n:th sqrt of the sum of |
---|
161 | the n:th power of all elements |
---|
162 | */ |
---|
163 | double norm( double n ) const; |
---|
164 | |
---|
165 | /** |
---|
166 | Calculates sum of all elements in matrix |
---|
167 | */ |
---|
168 | double matrix::sum() const; |
---|
169 | |
---|
170 | /** |
---|
171 | Exchange row i with row j (vice versa) |
---|
172 | */ |
---|
173 | void swap_rows( const size_t& i, const size_t& j ); |
---|
174 | |
---|
175 | /** |
---|
176 | Exchange column i with column j (vice versa) |
---|
177 | */ |
---|
178 | void swap_cols( const size_t& i, const size_t& j ); |
---|
179 | |
---|
180 | /** |
---|
181 | Returns row i in matrix |
---|
182 | */ |
---|
183 | matrix row( const size_t& i ) const; |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | Returns row i in matrix (vector form) |
---|
188 | */ |
---|
189 | vector row_vector( const size_t& i ) const; |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | Returns col i in matrix |
---|
194 | */ |
---|
195 | matrix col( const size_t& i ) const; |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | Returns col i in matrix (vector form) |
---|
200 | */ |
---|
201 | vector col_vector( const size_t& i ) const; |
---|
202 | |
---|
203 | |
---|
204 | /** |
---|
205 | Method for calculating the row sum |
---|
206 | */ |
---|
207 | vector row_sum() const; |
---|
208 | |
---|
209 | /** |
---|
210 | Method for calculating the mean row sum |
---|
211 | */ |
---|
212 | vector mean_row_sum() const; |
---|
213 | |
---|
214 | /** |
---|
215 | standard output operator is defined |
---|
216 | */ |
---|
217 | friend std::ostream& operator<< ( std::ostream& s_out, const matrix& ); |
---|
218 | |
---|
219 | /** |
---|
220 | Getting vector operator |
---|
221 | */ |
---|
222 | vector operator[]( const size_t& i ) const; |
---|
223 | |
---|
224 | private: |
---|
225 | gsl_matrix* new_copy( const gsl_matrix* ); |
---|
226 | |
---|
227 | gsl_matrix* m_; |
---|
228 | }; |
---|
229 | |
---|
230 | |
---|
231 | }; |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | #endif |
---|