1 | // $Id: vector.h 62 2004-04-17 18:09:11Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_gslapi_vector_ |
---|
4 | #define _theplu_gslapi_vector_ |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | |
---|
8 | #include <gsl/gsl_vector.h> |
---|
9 | |
---|
10 | // Jari, this should probably go somewhere else for doxygen to process. |
---|
11 | /// |
---|
12 | /// All our C++ projects should ideally be defined within theplu |
---|
13 | /// namespace. |
---|
14 | /// |
---|
15 | namespace theplu { |
---|
16 | // Jari, this should probably go somewhere else for doxygen to process. |
---|
17 | /// |
---|
18 | /// All GSL wrapper functionality is to be defined within gslapi |
---|
19 | /// namespace. |
---|
20 | /// |
---|
21 | /// There are a number of support operators and functions for the |
---|
22 | /// wrapper classes that do not belong to the classes themselves, but |
---|
23 | /// still are defined in this namespace. |
---|
24 | /// |
---|
25 | namespace gslapi { |
---|
26 | |
---|
27 | /// |
---|
28 | /// This is the C++ tools interface to GSL vector. |
---|
29 | /// |
---|
30 | |
---|
31 | class vector |
---|
32 | { |
---|
33 | public: |
---|
34 | |
---|
35 | /// |
---|
36 | /// The default constructor. |
---|
37 | /// |
---|
38 | vector(void); |
---|
39 | |
---|
40 | /// |
---|
41 | /// Constructor. Allocates memory space for \a n elements, and |
---|
42 | /// sets all elements to \a init_value. |
---|
43 | /// |
---|
44 | vector(const size_t n, const double init_value=0); |
---|
45 | |
---|
46 | /// |
---|
47 | /// The copy constructor. |
---|
48 | /// |
---|
49 | vector(const vector&); |
---|
50 | |
---|
51 | /// |
---|
52 | /// Constructor that imports a GSL vector. The GSL object is owned |
---|
53 | /// by the created object. |
---|
54 | /// |
---|
55 | vector(gsl_vector*); |
---|
56 | |
---|
57 | /// |
---|
58 | /// The istream constructor. |
---|
59 | /// |
---|
60 | /// Elements should be separated with white space characters, the |
---|
61 | /// end of input to the vector is at end of file marker. |
---|
62 | /// |
---|
63 | explicit vector(std::istream &); |
---|
64 | |
---|
65 | /// |
---|
66 | /// The destructor. |
---|
67 | /// |
---|
68 | ~vector(void); |
---|
69 | |
---|
70 | /// |
---|
71 | /// Create a new copy of the internal GSL vector. |
---|
72 | /// |
---|
73 | /// Necessary memory for the new GSL vector is allocated and the |
---|
74 | /// caller is responsible for freeing the allocated memory. |
---|
75 | /// |
---|
76 | /// @return A pointer to a copy of the internal GSL vector. |
---|
77 | /// |
---|
78 | gsl_vector* gsl_vector_copy(void) const; |
---|
79 | |
---|
80 | /// |
---|
81 | /// @return A const pointer to the internal GSL vector, |
---|
82 | /// |
---|
83 | inline const gsl_vector* gsl_vector_pointer(void) const { return v_; } |
---|
84 | |
---|
85 | /// |
---|
86 | /// @return A pointer to the internal GSL vector, |
---|
87 | /// |
---|
88 | inline gsl_vector* gsl_vector_pointer(void) { return v_; }; |
---|
89 | |
---|
90 | /// |
---|
91 | /// This function multiplies all elements between two vectors and |
---|
92 | /// returns a third vector containing the result, \f$a_i = b_i * |
---|
93 | /// c_i \; \forall i\f$. |
---|
94 | /// |
---|
95 | /// @return The result vector. |
---|
96 | /// |
---|
97 | vector vector::mul_elements( const vector& other ) const; |
---|
98 | |
---|
99 | /// |
---|
100 | /// Set all elements to \a value. |
---|
101 | /// |
---|
102 | inline void set_all(const double& value) { gsl_vector_set_all(v_,value); } |
---|
103 | |
---|
104 | /// |
---|
105 | /// @return the number of elements in the vector. |
---|
106 | /// |
---|
107 | inline size_t size(void) const { return v_->size; } |
---|
108 | |
---|
109 | /// |
---|
110 | /// Calculate the sum of all vector elements. |
---|
111 | /// |
---|
112 | /// @return The sum. |
---|
113 | /// |
---|
114 | double vector::sum(void) const; |
---|
115 | |
---|
116 | /// |
---|
117 | /// Element access operator. |
---|
118 | /// |
---|
119 | /// @return Reference to element \a i. |
---|
120 | /// |
---|
121 | inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
122 | |
---|
123 | /// |
---|
124 | /// Const element access operator. |
---|
125 | /// |
---|
126 | /// @return The value of element \a i. |
---|
127 | /// |
---|
128 | inline const double& operator()(size_t i) const |
---|
129 | { return *gsl_vector_ptr(v_,i); } |
---|
130 | |
---|
131 | /// |
---|
132 | /// Element access operator. |
---|
133 | /// |
---|
134 | /// @return Reference to element \a i. |
---|
135 | /// |
---|
136 | inline double& operator[](size_t i) { return (*this)(i); } |
---|
137 | |
---|
138 | /// |
---|
139 | /// Const element access operator. |
---|
140 | /// |
---|
141 | /// @return The value of element \a i. |
---|
142 | /// |
---|
143 | inline const double& operator[](size_t i) const { return (*this)(i); } |
---|
144 | |
---|
145 | /// |
---|
146 | /// @return The negation of the vector. |
---|
147 | /// |
---|
148 | vector operator-(void) const; |
---|
149 | |
---|
150 | /// |
---|
151 | /// @return The dot product. |
---|
152 | /// |
---|
153 | double operator*( const vector &other ) const; |
---|
154 | |
---|
155 | /// |
---|
156 | /// Vector addition. |
---|
157 | /// |
---|
158 | /// @return The resulting vector. |
---|
159 | /// |
---|
160 | vector operator+( const vector& other ) const; |
---|
161 | |
---|
162 | /// |
---|
163 | /// Vector subtraction. |
---|
164 | /// |
---|
165 | /// @return The resulting vector. |
---|
166 | /// |
---|
167 | vector operator-( const vector& other ) const; |
---|
168 | |
---|
169 | /// |
---|
170 | /// The assignment operator. There is no requirements on |
---|
171 | /// dimensions. |
---|
172 | /// |
---|
173 | vector& operator=(const vector&); |
---|
174 | |
---|
175 | /// |
---|
176 | /// Multiply and assign operator. |
---|
177 | /// |
---|
178 | vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
179 | |
---|
180 | /// |
---|
181 | /// Multiply operator. |
---|
182 | /// |
---|
183 | vector& operator*(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
184 | |
---|
185 | |
---|
186 | private: |
---|
187 | |
---|
188 | gsl_vector* v_; |
---|
189 | |
---|
190 | }; |
---|
191 | |
---|
192 | /// |
---|
193 | /// The output operator for the vector class. |
---|
194 | /// |
---|
195 | std::ostream& operator<< ( std::ostream&, const vector& ); |
---|
196 | |
---|
197 | |
---|
198 | }} // of namespace gslapi and namespace theplu |
---|
199 | |
---|
200 | #endif |
---|