1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * VectorContent.cpp
|
---|
10 | *
|
---|
11 | * Created on: Nov 15, 2010
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <gsl/gsl_blas.h>
|
---|
23 | #include <gsl/gsl_vector.h>
|
---|
24 | #include <cmath>
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | #include "Helpers/Assert.hpp"
|
---|
28 | #include "Helpers/defs.hpp"
|
---|
29 | #include "LinearAlgebra/Vector.hpp"
|
---|
30 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
31 |
|
---|
32 | /** Constructor of class VectorContent.
|
---|
33 | * Allocates GSL structures
|
---|
34 | * \param _dim number of components
|
---|
35 | */
|
---|
36 | VectorContent::VectorContent(size_t _dim) :
|
---|
37 | dimension(_dim)
|
---|
38 | {
|
---|
39 | content = gsl_vector_calloc(dimension);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** Constructor of class VectorContent.
|
---|
43 | * We need this VectorBaseCase for the VectorContentView class.
|
---|
44 | * There no content should be allocated, as it is just a view with an internal
|
---|
45 | * gsl_vector_view. Hence, VectorBaseCase is just dummy class to give the
|
---|
46 | * constructor a unique signature.
|
---|
47 | * \param VectorBaseCase
|
---|
48 | */
|
---|
49 | VectorContent::VectorContent(VectorBaseCase)
|
---|
50 | {}
|
---|
51 |
|
---|
52 | /** Copy constructor of class VectorContent.
|
---|
53 | * Allocates GSL structures and copies components from \a *src.
|
---|
54 | * \param *src source vector
|
---|
55 | */
|
---|
56 | VectorContent::VectorContent(const VectorContent * const src) :
|
---|
57 | dimension(src->dimension)
|
---|
58 | {
|
---|
59 | content = gsl_vector_alloc(dimension);
|
---|
60 | gsl_vector_memcpy (content, src->content);
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Copy constructor of class VectorContent.
|
---|
64 | * Allocates GSL structures and copies components from \a *src.
|
---|
65 | * \param *src source vector
|
---|
66 | */
|
---|
67 | VectorContent::VectorContent(const VectorContent & src) :
|
---|
68 | dimension(src.dimension)
|
---|
69 | {
|
---|
70 | content = gsl_vector_alloc(dimension);
|
---|
71 | gsl_vector_memcpy (content, src.content);
|
---|
72 | };
|
---|
73 |
|
---|
74 | /** Copy constructor of class VectorContent.
|
---|
75 | * No allocation, we just take over gsl_vector.
|
---|
76 | * \param *src source gsl_vector
|
---|
77 | */
|
---|
78 | VectorContent::VectorContent(gsl_vector * _src) :
|
---|
79 | dimension(_src->size)
|
---|
80 | {
|
---|
81 | content = _src;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Destructor of class VectorContent.
|
---|
85 | * Frees GSL structures
|
---|
86 | */
|
---|
87 | VectorContent::~VectorContent()
|
---|
88 | {
|
---|
89 | if(content != NULL){
|
---|
90 | gsl_vector_free(content);
|
---|
91 | content = NULL;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* ============================ Accessing =============================== */
|
---|
96 | /** Accessor for manipulating component (i).
|
---|
97 | * \param i component number
|
---|
98 | * \return reference to component (i)
|
---|
99 | */
|
---|
100 | double &VectorContent::at(size_t i)
|
---|
101 | {
|
---|
102 | ASSERT((i>=0) && (i<dimension),
|
---|
103 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
104 | return *gsl_vector_ptr (content, i);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Constant accessor for (value of) component (i).
|
---|
108 | * \param i component number
|
---|
109 | * \return const component (i)
|
---|
110 | */
|
---|
111 | const double VectorContent::at(size_t i) const
|
---|
112 | {
|
---|
113 | ASSERT((i>=0) && (i<dimension),
|
---|
114 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
115 | return gsl_vector_get(content, i);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** These functions return a pointer to the \a m-th element of a vector.
|
---|
119 | * If \a m lies outside the allowed range of 0 to VectorContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
120 | * \param m m-th element
|
---|
121 | * \return pointer to \a m-th element
|
---|
122 | */
|
---|
123 | double *VectorContent::Pointer(size_t m) const
|
---|
124 | {
|
---|
125 | return gsl_vector_ptr (content, m);
|
---|
126 | };
|
---|
127 |
|
---|
128 | /** These functions return a constant pointer to the \a m-th element of a vector.
|
---|
129 | * If \a m lies outside the allowed range of 0 to VectorContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
130 | * \param m m-th element
|
---|
131 | * \return const pointer to \a m-th element
|
---|
132 | */
|
---|
133 | const double *VectorContent::const_Pointer(size_t m) const
|
---|
134 | {
|
---|
135 | return gsl_vector_const_ptr (content, m);
|
---|
136 | };
|
---|
137 |
|
---|
138 | /** Assignment operator.
|
---|
139 | * \param &src source vector to assign \a *this to
|
---|
140 | * \return reference to \a *this
|
---|
141 | */
|
---|
142 | VectorContent& VectorContent::operator=(const VectorContent& src)
|
---|
143 | {
|
---|
144 | ASSERT(dimension == src.dimension, "Dimensions have to be the same to assign VectorContent onto another!");
|
---|
145 | // check for self assignment
|
---|
146 | if(&src!=this){
|
---|
147 | gsl_vector_memcpy(content, src.content);
|
---|
148 | }
|
---|
149 | return *this;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* ========================== Initializing =============================== */
|
---|
153 | /** This function sets all the elements of the vector to the value \a x.
|
---|
154 | * \param x value to set to
|
---|
155 | */
|
---|
156 | void VectorContent::setValue(double x)
|
---|
157 | {
|
---|
158 | gsl_vector_set_all (content, x);
|
---|
159 | };
|
---|
160 |
|
---|
161 | /** This function sets the vector from a double array.
|
---|
162 | * Creates a vector view of the array and performs a memcopy.
|
---|
163 | * \param *x array of values (no dimension check is performed)
|
---|
164 | */
|
---|
165 | void VectorContent::setFromDoubleArray(double * x)
|
---|
166 | {
|
---|
167 | gsl_vector_view m = gsl_vector_view_array (x, dimension);
|
---|
168 | gsl_vector_memcpy (content, &m.vector);
|
---|
169 | };
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * This function sets the GSLvector from an ordinary vector.
|
---|
173 | *
|
---|
174 | * Takes access to the internal gsl_vector and copies it
|
---|
175 | */
|
---|
176 | void VectorContent::setFromVector(Vector &v)
|
---|
177 | {
|
---|
178 | gsl_vector_memcpy (content, v.get()->content);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** This function sets all the elements of the vector to zero.
|
---|
182 | */
|
---|
183 | void VectorContent::setZero()
|
---|
184 | {
|
---|
185 | gsl_vector_set_zero (content);
|
---|
186 | };
|
---|
187 |
|
---|
188 | /** This function makes a basis vector by setting all the elements of the vector to zero except for the i-th element which is set to one.
|
---|
189 | * \param i i-th component to set to unity (all other to zero)
|
---|
190 | * \return vector set
|
---|
191 | */
|
---|
192 | int VectorContent::setBasis(size_t i)
|
---|
193 | {
|
---|
194 | return gsl_vector_set_basis (content, i);
|
---|
195 | };
|
---|
196 |
|
---|
197 | /* ====================== Exchanging elements ============================ */
|
---|
198 | /** This function exchanges the \a i-th and \a j-th elements of the vector in-place.
|
---|
199 | * \param i i-th element to swap with ...
|
---|
200 | * \param j ... j-th element to swap against
|
---|
201 | */
|
---|
202 | int VectorContent::SwapElements(size_t i, size_t j)
|
---|
203 | {
|
---|
204 | return gsl_vector_swap_elements (content, i, j);
|
---|
205 | };
|
---|
206 |
|
---|
207 | /** This function reverses the order of the elements of the vector.
|
---|
208 | */
|
---|
209 | int VectorContent::Reverse()
|
---|
210 | {
|
---|
211 | return gsl_vector_reverse (content);
|
---|
212 | };
|
---|
213 |
|
---|
214 |
|
---|
215 | /* ========================== Operators =============================== */
|
---|
216 | /** Accessor for manipulating component (i).
|
---|
217 | * \param i component number
|
---|
218 | * \return reference to component (i)
|
---|
219 | */
|
---|
220 | double &VectorContent::operator[](size_t i)
|
---|
221 | {
|
---|
222 | ASSERT((i>=0) && (i<dimension),
|
---|
223 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
224 | return *gsl_vector_ptr (content, i);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** Constant accessor for (value of) component (i).
|
---|
228 | * \param i component number
|
---|
229 | * \return const component (i)
|
---|
230 | */
|
---|
231 | const double VectorContent::operator[](size_t i) const
|
---|
232 | {
|
---|
233 | ASSERT((i>=0) && (i<dimension),
|
---|
234 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
235 | return gsl_vector_get(content, i);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /** Compares VectorContent \a to VectorContent \a b component-wise.
|
---|
240 | * \param a base VectorContent
|
---|
241 | * \param b VectorContent components to add
|
---|
242 | * \return a == b
|
---|
243 | */
|
---|
244 | bool VectorContent::operator==(const VectorContent& b) const
|
---|
245 | {
|
---|
246 | bool status = true;
|
---|
247 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
248 | for (size_t i=0;i<dimension;i++)
|
---|
249 | status = status && (fabs(at(i) - b.at(i)) < MYEPSILON);
|
---|
250 | return status;
|
---|
251 | };
|
---|
252 |
|
---|
253 | /** Sums VectorContent \a to this lhs component-wise.
|
---|
254 | * \param a base VectorContent
|
---|
255 | * \param b VectorContent components to add
|
---|
256 | * \return lhs + a
|
---|
257 | */
|
---|
258 | const VectorContent& VectorContent::operator+=(const VectorContent& b)
|
---|
259 | {
|
---|
260 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
261 | for (size_t i=0;i<dimension;i++)
|
---|
262 | at(i) = at(i)+b.at(i);
|
---|
263 | return *this;
|
---|
264 | };
|
---|
265 |
|
---|
266 | /** Subtracts VectorContent \a from this lhs component-wise.
|
---|
267 | * \param a base VectorContent
|
---|
268 | * \param b VectorContent components to add
|
---|
269 | * \return lhs - a
|
---|
270 | */
|
---|
271 | const VectorContent& VectorContent::operator-=(const VectorContent& b)
|
---|
272 | {
|
---|
273 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
274 | for (size_t i=0;i<dimension;i++)
|
---|
275 | at(i) = at(i)-b.at(i);
|
---|
276 | return *this;
|
---|
277 | };
|
---|
278 |
|
---|
279 | /** factor each component of \a a times a double \a m.
|
---|
280 | * \param a base VectorContent
|
---|
281 | * \param m factor
|
---|
282 | * \return lhs.Get(i) * m
|
---|
283 | */
|
---|
284 | const VectorContent& VectorContent::operator*=(const double m)
|
---|
285 | {
|
---|
286 | for (size_t i=0;i<dimension;i++)
|
---|
287 | at(i) = at(i)*m;
|
---|
288 | return *this;
|
---|
289 | };
|
---|
290 |
|
---|
291 | /** Sums two VectorContents \a and \b component-wise.
|
---|
292 | * \param a first VectorContent
|
---|
293 | * \param b second VectorContent
|
---|
294 | * \return a + b
|
---|
295 | */
|
---|
296 | VectorContent const operator+(const VectorContent& a, const VectorContent& b)
|
---|
297 | {
|
---|
298 | ASSERT(a.dimension == b.dimension, "VectorContent::operator+() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
299 | VectorContent x(a);
|
---|
300 | for (size_t i=0;i<a.dimension;i++)
|
---|
301 | x.at(i) = a.at(i)+b.at(i);
|
---|
302 | return x;
|
---|
303 | };
|
---|
304 |
|
---|
305 | /** Subtracts VectorContent \a from \b component-wise.
|
---|
306 | * \param a first VectorContent
|
---|
307 | * \param b second VectorContent
|
---|
308 | * \return a - b
|
---|
309 | */
|
---|
310 | VectorContent const operator-(const VectorContent& a, const VectorContent& b)
|
---|
311 | {
|
---|
312 | ASSERT(a.dimension == b.dimension, "VectorContent::operator-() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
313 | VectorContent x(a);
|
---|
314 | for (size_t i=0;i<a.dimension;i++)
|
---|
315 | x.at(i) = a.at(i)-b.at(i);
|
---|
316 | return x;
|
---|
317 | };
|
---|
318 |
|
---|
319 | /** Factors given VectorContent \a a times \a m.
|
---|
320 | * \param a VectorContent
|
---|
321 | * \param m factor
|
---|
322 | * \return m * a
|
---|
323 | */
|
---|
324 | VectorContent const operator*(const VectorContent& a, const double m)
|
---|
325 | {
|
---|
326 | VectorContent x(a);
|
---|
327 | for (size_t i=0;i<a.dimension;i++)
|
---|
328 | x.at(i) = a.at(i)*m;
|
---|
329 | return x;
|
---|
330 | };
|
---|
331 |
|
---|
332 | /** Factors given VectorContent \a a times \a m.
|
---|
333 | * \param m factor
|
---|
334 | * \param a VectorContent
|
---|
335 | * \return m * a
|
---|
336 | */
|
---|
337 | VectorContent const operator*(const double m, const VectorContent& a )
|
---|
338 | {
|
---|
339 | VectorContent x(a);
|
---|
340 | for (size_t i=0;i<a.dimension;i++)
|
---|
341 | x.at(i) = a.at(i)*m;
|
---|
342 | return x;
|
---|
343 | };
|
---|
344 |
|
---|
345 | ostream& operator<<(ostream& ost, const VectorContent& m)
|
---|
346 | {
|
---|
347 | ost << "(";
|
---|
348 | for (size_t i=0;i<m.dimension;i++) {
|
---|
349 | ost << m.at(i);
|
---|
350 | if (i != m.dimension-1)
|
---|
351 | ost << ",";
|
---|
352 | }
|
---|
353 | ost << ")";
|
---|
354 | return ost;
|
---|
355 | };
|
---|
356 |
|
---|
357 | /* ====================== Checking State ============================ */
|
---|
358 | /** Checks whether vector has all components zero.
|
---|
359 | * TODO: This might see some numerical instabilities for huge dimension and small number.
|
---|
360 | * For stability one should sort the order of summing!
|
---|
361 | * @return true - vector is zero, false - vector is not
|
---|
362 | */
|
---|
363 | bool VectorContent::IsZero() const
|
---|
364 | {
|
---|
365 | double result = 0.;
|
---|
366 | for (size_t i = dimension; i--; )
|
---|
367 | result += fabs(at(i));
|
---|
368 | return (result < MYEPSILON);
|
---|
369 | };
|
---|
370 |
|
---|
371 | /** Checks whether vector has length of 1.
|
---|
372 | * @return true - vector is normalized, false - vector is not
|
---|
373 | */
|
---|
374 | bool VectorContent::IsOne() const
|
---|
375 | {
|
---|
376 | double NormValue = 0.;
|
---|
377 | for (size_t i=dimension;--i;)
|
---|
378 | NormValue += at(i)*at(i);
|
---|
379 | return (fabs(NormValue - 1.) < MYEPSILON);
|
---|
380 | };
|
---|
381 |
|
---|
382 | /* ======================== Properties ============================= */
|
---|
383 | /** Calculates scalar product between \a *this and \a b.
|
---|
384 | * @param b other vector
|
---|
385 | * @return \f$| (*this) \cdot (b)|^2\f$
|
---|
386 | */
|
---|
387 | const double VectorContent::operator*(const VectorContent& b)
|
---|
388 | {
|
---|
389 | double res = 0.;
|
---|
390 | gsl_blas_ddot(content, b.content, &res);
|
---|
391 | return res;
|
---|
392 | };
|
---|