source: trunk/src/matrix.h @ 30

Last change on this file since 30 was 30, checked in by Peter, 19 years ago

adding SVM

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
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
18namespace 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; 
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
105
106    /**
107       set( i, j, val ) will make Aij = val in matrix A
108       where i = row number and j = column number
109    */
110    inline void set( const size_t& i, const size_t& j, const Type& val );
111
112    /**
113       set_all( val ) will make Aij = val in matrix A
114       for all "i"
115    */
116    inline void set_all( const Type& val );
117
118
119
120    /**
121       Assignment-operator. No requirements on dimensions,
122       i.e. if A = B, then A will be a copy of B with B:s
123       dimensions.
124     */
125    matrix& operator=( const matrix& other );
126   
127    /**
128       operators for comparison are:
129       == equal
130       != not equal
131    */
132    bool operator==( const matrix &other ) const;
133    bool operator!=( const matrix &other ) const; 
134
135    /**
136       operators for basic arithmetics are: \n
137       \f$+\f$ matrix-matrix addition \n
138       \f$-\f$ matrix-matrix subtraction \n
139       \f$*\f$ matrix-matrix multiplication
140    */
141    matrix operator+( const matrix &other ) const;
142    matrix operator-( const matrix &other ) const;
143    matrix operator*( const matrix &other ) const;
144    vector operator*( const vector &other ) const;
145
146    /**
147  multiplying each element of two matrices
148  dimension MUST AGREE
149    */
150    matrix mul_elements( const matrix& a, const matrix& b );
151
152    /**
153       Method for transposing a matrix
154     */
155    matrix transpose() const;
156   
157    /**
158       Method for calculating the n:th sqrt of the sum of
159       the n:th power of all elements
160    */
161    double norm( double n ) const;
162
163    /**
164       Calculates sum of all elements in matrix
165    */   
166    double matrix::sum() const;
167     
168    /**
169       Exchange row i with row j (vice versa)
170     */
171    void swap_rows( const size_t& i, const size_t& j );
172
173    /**
174       Exchange column i with column j (vice versa)
175     */
176    void swap_cols( const size_t& i, const size_t& j );
177
178    /**
179       Returns row i in matrix
180    */   
181    matrix row( const size_t& i ) const;
182
183
184    /**
185       Returns row i in matrix (vector form)
186    */
187    vector row_vector( const size_t& i ) const;
188
189
190    /**
191       Returns col i in matrix
192    */   
193    matrix col( const size_t& i ) const;
194
195
196    /**
197       Returns col i in matrix (vector form)
198    */
199    vector col_vector( const size_t& i ) const;
200
201
202    /**
203       Method for calculating the row sum
204    */   
205    vector row_sum() const;
206
207    /**
208       Method for calculating the mean row sum
209    */   
210    vector mean_row_sum() const;
211
212    /**
213  standard output operator is defined
214    */
215    friend std::ostream& operator<< ( std::ostream& s_out, const matrix& );
216
217    /**
218       Getting vector operator
219    */
220    vector operator[]( const size_t& i ) const;
221 
222  private:
223    gsl_matrix* new_copy( const gsl_matrix* );
224
225    gsl_matrix* m_;
226  };
227#include "matrix.icc"
228
229
230};
231
232
233
234
235#endif
Note: See TracBrowser for help on using the repository browser.