1 | // $Id: vector.h 40 2004-02-20 14:03:25Z jari $ |
---|
2 | |
---|
3 | #ifndef _thep_gsl_api_vector_ |
---|
4 | #define _thep_gsl_api_vector_ |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <fstream> |
---|
8 | #include <iomanip> |
---|
9 | #include <cmath> |
---|
10 | #include <cstdlib> |
---|
11 | #include <cassert> |
---|
12 | |
---|
13 | #include <gsl/gsl_math.h> |
---|
14 | #include <gsl/gsl_vector.h> |
---|
15 | #include <gsl/gsl_linalg.h> |
---|
16 | #include <gsl/gsl_blas.h> |
---|
17 | |
---|
18 | |
---|
19 | namespace thep_gsl_api |
---|
20 | { |
---|
21 | |
---|
22 | /** |
---|
23 | This is a vector interface to GSL for c++. |
---|
24 | Important: vector has two dimenstions depending on |
---|
25 | whether it's a "row" vector or a "column" vector. |
---|
26 | A column vector is created by default. |
---|
27 | Note that the namespace is "thep_gsl_api". |
---|
28 | */ |
---|
29 | |
---|
30 | class vector |
---|
31 | { |
---|
32 | public: |
---|
33 | typedef double Type; |
---|
34 | |
---|
35 | /** |
---|
36 | Default constructor |
---|
37 | Does not allocate any memory |
---|
38 | */ |
---|
39 | vector(); |
---|
40 | |
---|
41 | /** |
---|
42 | Constructor taking three argumens: |
---|
43 | N = Number of elements |
---|
44 | init_to_zero, if true vector will be initialized with zeros. |
---|
45 | */ |
---|
46 | vector( const size_t&, bool is_col_vector = true, |
---|
47 | bool init_to_zero = true ); |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | Copy constructor |
---|
53 | */ |
---|
54 | vector( const vector& other ); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | Constructor taking as only argument a matrix of gsl type. |
---|
59 | Be causious here! Do not delete this matrix outside of |
---|
60 | class: NO COPY IS MADE!!! |
---|
61 | */ |
---|
62 | vector( gsl_vector*, bool is_col = true ); |
---|
63 | |
---|
64 | /** |
---|
65 | Constructor taking as only argument a an istream that should |
---|
66 | contain the vector. Column vector should be sepated with '\\n' |
---|
67 | and row vector should be separeted with whitespace (not '\\n'). |
---|
68 | |
---|
69 | */ |
---|
70 | vector(std::istream &); |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | Destructor will clear matrix's allocated memory |
---|
75 | */ |
---|
76 | ~vector(); |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | The most important function when adding new classes to |
---|
81 | c++_tools. This function will return a pointer to the |
---|
82 | inner data of the class. Should be forbidden in a strictly |
---|
83 | object-orientated environment. |
---|
84 | Motivation: Effeciency! ( CAN IT BE MADE CONST? ) |
---|
85 | To be used when writting new classes using gsl-functions. |
---|
86 | */ |
---|
87 | inline gsl_vector* get_gsl_vector() const { return v_; } |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | Returns the number of rows in the vector. |
---|
92 | */ |
---|
93 | size_t size() const { return v_->size; } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | Tells whether a vector is column or not |
---|
98 | */ |
---|
99 | bool is_column() const { return is_col_; } |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | get( i ) will return the vi element in vector v |
---|
104 | where i = column (if row vector) or row (if column vector) |
---|
105 | */ |
---|
106 | inline Type get( const size_t& i ) const { return gsl_vector_get(v_,i); } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | set( i, val ) will make vi = val in vector v |
---|
111 | where i = column (if row vector) or row (if column vector) |
---|
112 | */ |
---|
113 | inline void set( const size_t& i, const Type& val ) |
---|
114 | { gsl_vector_set(v_,i,val); } |
---|
115 | |
---|
116 | /** |
---|
117 | set_all( val ) will make vi = val in vector v |
---|
118 | for all "i" |
---|
119 | */ |
---|
120 | inline void set_all( const Type& val ) { gsl_vector_set_all(v_,val); } |
---|
121 | |
---|
122 | |
---|
123 | /** |
---|
124 | Assignment-operator. No requirements on dimensions, |
---|
125 | i.e. if a = b, then a will be a copy of b with b:s |
---|
126 | dimensions. |
---|
127 | */ |
---|
128 | vector& operator=( const vector& other ); |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | Access element-operator |
---|
133 | */ |
---|
134 | inline double &operator[](size_t i) { return *gsl_vector_ptr( v_, i ); } |
---|
135 | const double &operator[](size_t i) const { return *gsl_vector_ptr( v_, i ); } |
---|
136 | |
---|
137 | |
---|
138 | /** |
---|
139 | operators for comparison are: \n |
---|
140 | == equal \n |
---|
141 | != not equal |
---|
142 | */ |
---|
143 | bool operator==( const vector &other ) const; |
---|
144 | bool operator!=( const vector &other ) const; |
---|
145 | |
---|
146 | /** |
---|
147 | operators for basic arithmetics are: \n |
---|
148 | \f$+\f$ vector-vector addition \n |
---|
149 | \f$-\f$ vector-vector subtraction \n |
---|
150 | \f$/=\f$ divide all elements with a constant |
---|
151 | */ |
---|
152 | vector operator+( const vector& other ) const; |
---|
153 | vector operator-( const vector& other ) const; |
---|
154 | int operator/=( const double& c ); |
---|
155 | int operator/=( const vector& other ) const; |
---|
156 | vector operator-() const; |
---|
157 | /** |
---|
158 | This operator is implemented as dot-product. |
---|
159 | I case of discussions later on; Jari decided |
---|
160 | this! |
---|
161 | */ |
---|
162 | double operator*( const vector &other ) const; |
---|
163 | |
---|
164 | /** |
---|
165 | This function multiplies all elements in two vectors |
---|
166 | and returns a third vector containing the result. |
---|
167 | res = a*b; where size(a) = size(b) = size(res) |
---|
168 | */ |
---|
169 | vector vector::mul_elements( const vector& other ) const; |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | Calculates sum of all elements in vector |
---|
174 | */ |
---|
175 | double vector::sum() const; |
---|
176 | |
---|
177 | /** |
---|
178 | standard output operator is defined |
---|
179 | */ |
---|
180 | friend std::ostream& operator<< ( std::ostream&, const vector& ); |
---|
181 | |
---|
182 | |
---|
183 | private: |
---|
184 | gsl_vector* new_copy( const gsl_vector* ); |
---|
185 | |
---|
186 | gsl_vector* v_; |
---|
187 | bool is_col_; |
---|
188 | }; |
---|
189 | |
---|
190 | |
---|
191 | }; |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | #endif |
---|