source: trunk/src/matrix.h @ 12

Last change on this file since 12 was 12, checked in by daniel, 19 years ago

Matrix and vector APIs for GSL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 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  Copy constructor
50    */
51    matrix( const matrix& other );
52
53
54    /**
55       Constructor taking as only argument a matrix of gsl type.
56       Be causious here! Do not delete this matrix outside of
57       class: NO COPY IS MADE!!!
58    */
59    matrix( gsl_matrix* );
60   
61    /**
62       Destructor will clear matrix's allocated memory
63    */
64    ~matrix();
65
66
67    /**
68       The most important function when adding new classes to
69       c++_tools. This function will return a pointer to the
70       inner data of the class. Should be forbidden in a strictly
71       object-orientated environment.
72       Motivation: Effeciency! ( CAN IT BE MADE CONST? )
73       To be used when writting new classes using gsl-functions.
74    */
75    inline gsl_matrix* get_gsl_matrix() const; 
76
77
78    /**
79       Returns the number of rows in the matrix.
80    */
81    size_t rows() const { return m_->size1; }
82
83
84    /**
85       Returns the number of rows in the matrix.
86    */
87    size_t cols() const { return m_->size2; }
88
89    /**
90       get( i, j ) will return the Aij element in matrix A
91       where i = row number and j = column number
92    */
93    inline Type get( const size_t& i, const size_t& j ) const;
94
95
96    /**
97       set( i, j, val ) will make Aij = val in matrix A
98       where i = row number and j = column number
99    */
100    inline void set( const size_t& i, const size_t& j, const Type& val );
101
102    /**
103       set_all( val ) will make Aij = val in matrix A
104       for all "i"
105    */
106    inline void set_all( const Type& val );
107
108
109
110    /**
111       Assignment-operator. No requirements on dimensions,
112       i.e. if A = B, then A will be a copy of B with B:s
113       dimensions.
114     */
115    matrix& operator=( const matrix& other );
116   
117    /**
118       operators for comparison are:
119       == equal
120       != not equal
121    */
122    bool operator==( const matrix &other ) const;
123    bool operator!=( const matrix &other ) const; 
124
125    /**
126       operators for basic arithmetics are: \n
127       \f$+\f$ matrix-matrix addition \n
128       \f$-\f$ matrix-matrix subtraction \n
129       \f$*\f$ matrix-matrix multiplication
130    */
131    matrix operator+( const matrix &other ) const;
132    matrix operator-( const matrix &other ) const;
133    matrix operator*( const matrix &other ) const;
134    vector operator*( const vector &other ) const;
135
136
137    /**
138       Method for transposing a matrix
139     */
140    matrix transpose() const;
141   
142    /**
143       Method for calculating the n:th sqrt of the sum of
144       the n:th power of all elements
145    */
146    double norm( double n ) const;
147
148    /**
149       Calculates sum of all elements in matrix
150    */   
151    double matrix::sum() const;
152     
153    /**
154       Exchange row i with row j (vice versa)
155     */
156    void swap_rows( const size_t& i, const size_t& j );
157
158    /**
159       Exchange column i with column j (vice versa)
160     */
161    void swap_cols( const size_t& i, const size_t& j );
162
163    /**
164       Returns row i in matrix
165    */   
166    matrix row( const size_t& i ) const;
167
168
169    /**
170       Returns row i in matrix
171    */   
172    matrix col( const size_t& i ) const;
173
174
175    /**
176       Method for calculating the row sum
177    */   
178    vector row_sum() const;
179
180    /**
181       Method for calculating the mean row sum
182    */   
183    vector mean_row_sum() const;
184
185    /**
186  standard output operator is defined
187    */
188    friend std::ostream& operator<< ( std::ostream& s_out, const matrix& );
189
190
191
192
193  private:
194    gsl_matrix* new_copy( const gsl_matrix* );
195
196    gsl_matrix* m_;
197  };
198#include "matrix.icc"
199
200
201};
202
203
204
205
206#endif
207
208
Note: See TracBrowser for help on using the repository browser.