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 "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <gsl/gsl_blas.h>
|
---|
23 | #include <gsl/gsl_vector.h>
|
---|
24 | #include <cmath>
|
---|
25 | #include <iostream>
|
---|
26 | #include <limits>
|
---|
27 |
|
---|
28 | #include "CodePatterns/Assert.hpp"
|
---|
29 | #include "Helpers/defs.hpp"
|
---|
30 | #include "LinearAlgebra/defs.hpp"
|
---|
31 | #include "LinearAlgebra/Vector.hpp"
|
---|
32 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
33 |
|
---|
34 | /** Constructor of class VectorContent.
|
---|
35 | * Allocates GSL structures
|
---|
36 | * \param _dim number of components
|
---|
37 | */
|
---|
38 | VectorContent::VectorContent(size_t _dim) :
|
---|
39 | dimension(_dim),
|
---|
40 | free_content_on_exit(true)
|
---|
41 | {
|
---|
42 | content = gsl_vector_calloc(dimension);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /** Constructor of class VectorContent.
|
---|
46 | * We need this VectorBaseCase for the VectorContentView class.
|
---|
47 | * There no content should be allocated, as it is just a view with an internal
|
---|
48 | * gsl_vector_view. Hence, VectorBaseCase is just dummy class to give the
|
---|
49 | * constructor a unique signature.
|
---|
50 | * \param VectorBaseCase
|
---|
51 | */
|
---|
52 | VectorContent::VectorContent(VectorBaseCase) :
|
---|
53 | free_content_on_exit(false)
|
---|
54 | {}
|
---|
55 |
|
---|
56 | /** Copy constructor of class VectorContent.
|
---|
57 | * Allocates GSL structures and copies components from \a *src.
|
---|
58 | * \param *src source vector
|
---|
59 | */
|
---|
60 | VectorContent::VectorContent(const VectorContent * const src) :
|
---|
61 | dimension(src->dimension),
|
---|
62 | free_content_on_exit(true)
|
---|
63 | {
|
---|
64 | content = gsl_vector_alloc(dimension);
|
---|
65 | gsl_vector_memcpy (content, src->content);
|
---|
66 | };
|
---|
67 |
|
---|
68 | /** Copy constructor of class VectorContent.
|
---|
69 | * Allocates GSL structures and copies components from \a *src.
|
---|
70 | * \param *src source vector
|
---|
71 | */
|
---|
72 | VectorContent::VectorContent(const VectorContent & src) :
|
---|
73 | dimension(src.dimension),
|
---|
74 | free_content_on_exit(true)
|
---|
75 | {
|
---|
76 | content = gsl_vector_alloc(dimension);
|
---|
77 | gsl_vector_memcpy (content, src.content);
|
---|
78 | };
|
---|
79 |
|
---|
80 | /** Copy constructor of class VectorContent.
|
---|
81 | * No allocation, we just take over gsl_vector.
|
---|
82 | * \param *src source gsl_vector
|
---|
83 | */
|
---|
84 | VectorContent::VectorContent(gsl_vector * _src) :
|
---|
85 | dimension(_src->size),
|
---|
86 | free_content_on_exit(false)
|
---|
87 | {
|
---|
88 | content = _src;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Destructor of class VectorContent.
|
---|
92 | * Frees GSL structures
|
---|
93 | */
|
---|
94 | VectorContent::~VectorContent()
|
---|
95 | {
|
---|
96 | if (free_content_on_exit) {
|
---|
97 | if(content != NULL)
|
---|
98 | gsl_vector_free(content);
|
---|
99 | content = NULL;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* ============================ Accessing =============================== */
|
---|
104 | /** Accessor for manipulating component (i).
|
---|
105 | * \param i component number
|
---|
106 | * \return reference to component (i)
|
---|
107 | */
|
---|
108 | double &VectorContent::at(size_t i)
|
---|
109 | {
|
---|
110 | ASSERT((i>=0) && (i<dimension),
|
---|
111 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
112 | return *gsl_vector_ptr (content, i);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Constant accessor for (value of) component (i).
|
---|
116 | * \param i component number
|
---|
117 | * \return const component (i)
|
---|
118 | */
|
---|
119 | const double VectorContent::at(size_t i) const
|
---|
120 | {
|
---|
121 | ASSERT((i>=0) && (i<dimension),
|
---|
122 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
123 | return gsl_vector_get(content, i);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** These functions return a pointer to the \a m-th element of a vector.
|
---|
127 | * 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.
|
---|
128 | * \param m m-th element
|
---|
129 | * \return pointer to \a m-th element
|
---|
130 | */
|
---|
131 | double *VectorContent::Pointer(size_t m) const
|
---|
132 | {
|
---|
133 | return gsl_vector_ptr (content, m);
|
---|
134 | };
|
---|
135 |
|
---|
136 | /** These functions return a constant pointer to the \a m-th element of a vector.
|
---|
137 | * 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.
|
---|
138 | * \param m m-th element
|
---|
139 | * \return const pointer to \a m-th element
|
---|
140 | */
|
---|
141 | const double *VectorContent::const_Pointer(size_t m) const
|
---|
142 | {
|
---|
143 | return gsl_vector_const_ptr (content, m);
|
---|
144 | };
|
---|
145 |
|
---|
146 | /** Assignment operator.
|
---|
147 | * \param &src source vector to assign \a *this to
|
---|
148 | * \return reference to \a *this
|
---|
149 | */
|
---|
150 | VectorContent& VectorContent::operator=(const VectorContent& src)
|
---|
151 | {
|
---|
152 | ASSERT(dimension == src.dimension,
|
---|
153 | "Dimensions have to be the same to assign VectorContent onto another:"
|
---|
154 | +toString(dimension)+" != "+toString(src.dimension)+"!");
|
---|
155 | // check for self assignment
|
---|
156 | if(&src!=this){
|
---|
157 | gsl_vector_memcpy(content, src.content);
|
---|
158 | }
|
---|
159 | return *this;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* ========================== Initializing =============================== */
|
---|
163 | /** This function sets all the elements of the vector to the value \a x.
|
---|
164 | * \param x value to set to
|
---|
165 | */
|
---|
166 | void VectorContent::setValue(double x)
|
---|
167 | {
|
---|
168 | gsl_vector_set_all (content, x);
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** This function sets the vector from a double array.
|
---|
172 | * Creates a vector view of the array and performs a memcopy.
|
---|
173 | * \param *x array of values (no dimension check is performed)
|
---|
174 | */
|
---|
175 | void VectorContent::setFromDoubleArray(double * x)
|
---|
176 | {
|
---|
177 | gsl_vector_view m = gsl_vector_view_array (x, dimension);
|
---|
178 | gsl_vector_memcpy (content, &m.vector);
|
---|
179 | };
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * This function sets the GSLvector from an ordinary vector.
|
---|
183 | *
|
---|
184 | * Takes access to the internal gsl_vector and copies it
|
---|
185 | */
|
---|
186 | void VectorContent::setFromVector(Vector &v)
|
---|
187 | {
|
---|
188 | gsl_vector_memcpy (content, v.get()->content);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** This function sets all the elements of the vector to zero.
|
---|
192 | */
|
---|
193 | void VectorContent::setZero()
|
---|
194 | {
|
---|
195 | gsl_vector_set_zero (content);
|
---|
196 | };
|
---|
197 |
|
---|
198 | /** 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.
|
---|
199 | * \param i i-th component to set to unity (all other to zero)
|
---|
200 | * \return vector set
|
---|
201 | */
|
---|
202 | int VectorContent::setBasis(size_t i)
|
---|
203 | {
|
---|
204 | return gsl_vector_set_basis (content, i);
|
---|
205 | };
|
---|
206 |
|
---|
207 | /* ====================== Exchanging elements ============================ */
|
---|
208 | /** This function exchanges the \a i-th and \a j-th elements of the vector in-place.
|
---|
209 | * \param i i-th element to swap with ...
|
---|
210 | * \param j ... j-th element to swap against
|
---|
211 | */
|
---|
212 | int VectorContent::SwapElements(size_t i, size_t j)
|
---|
213 | {
|
---|
214 | return gsl_vector_swap_elements (content, i, j);
|
---|
215 | };
|
---|
216 |
|
---|
217 | /** This function reverses the order of the elements of the vector.
|
---|
218 | */
|
---|
219 | int VectorContent::Reverse()
|
---|
220 | {
|
---|
221 | return gsl_vector_reverse (content);
|
---|
222 | };
|
---|
223 |
|
---|
224 |
|
---|
225 | /* ========================== Operators =============================== */
|
---|
226 | /** Accessor for manipulating component (i).
|
---|
227 | * \param i component number
|
---|
228 | * \return reference to component (i)
|
---|
229 | */
|
---|
230 | double &VectorContent::operator[](size_t i)
|
---|
231 | {
|
---|
232 | ASSERT((i>=0) && (i<dimension),
|
---|
233 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
234 | return *gsl_vector_ptr (content, i);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Constant accessor for (value of) component (i).
|
---|
238 | * \param i component number
|
---|
239 | * \return const component (i)
|
---|
240 | */
|
---|
241 | const double VectorContent::operator[](size_t i) const
|
---|
242 | {
|
---|
243 | ASSERT((i>=0) && (i<dimension),
|
---|
244 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
245 | return gsl_vector_get(content, i);
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /** Compares VectorContent \a to VectorContent \a b component-wise.
|
---|
250 | * \param a base VectorContent
|
---|
251 | * \param b VectorContent components to add
|
---|
252 | * \return a == b
|
---|
253 | */
|
---|
254 | bool VectorContent::operator==(const VectorContent& b) const
|
---|
255 | {
|
---|
256 | bool status = true;
|
---|
257 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
258 | for (size_t i=0;i<dimension;i++)
|
---|
259 | status = status && (fabs(at(i) - b.at(i)) <= LINALG_MYEPSILON);
|
---|
260 | return status;
|
---|
261 | };
|
---|
262 |
|
---|
263 | /** Sums VectorContent \a to this lhs component-wise.
|
---|
264 | * \param a base VectorContent
|
---|
265 | * \param b VectorContent components to add
|
---|
266 | * \return lhs + a
|
---|
267 | */
|
---|
268 | const VectorContent& VectorContent::operator+=(const VectorContent& b)
|
---|
269 | {
|
---|
270 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
271 | for (size_t i=0;i<dimension;i++)
|
---|
272 | at(i) = at(i)+b.at(i);
|
---|
273 | return *this;
|
---|
274 | };
|
---|
275 |
|
---|
276 | /** Subtracts VectorContent \a from this lhs component-wise.
|
---|
277 | * \param a base VectorContent
|
---|
278 | * \param b VectorContent components to add
|
---|
279 | * \return lhs - a
|
---|
280 | */
|
---|
281 | const VectorContent& VectorContent::operator-=(const VectorContent& b)
|
---|
282 | {
|
---|
283 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
284 | for (size_t i=0;i<dimension;i++)
|
---|
285 | at(i) = at(i)-b.at(i);
|
---|
286 | return *this;
|
---|
287 | };
|
---|
288 |
|
---|
289 | /** factor each component of \a a times a double \a m.
|
---|
290 | * \param a base VectorContent
|
---|
291 | * \param m factor
|
---|
292 | * \return lhs.Get(i) * m
|
---|
293 | */
|
---|
294 | const VectorContent& VectorContent::operator*=(const double m)
|
---|
295 | {
|
---|
296 | for (size_t i=0;i<dimension;i++)
|
---|
297 | at(i) = at(i)*m;
|
---|
298 | return *this;
|
---|
299 | };
|
---|
300 |
|
---|
301 | /** Sums two VectorContents \a and \b component-wise.
|
---|
302 | * \param a first VectorContent
|
---|
303 | * \param b second VectorContent
|
---|
304 | * \return a + b
|
---|
305 | */
|
---|
306 | VectorContent const operator+(const VectorContent& a, const VectorContent& b)
|
---|
307 | {
|
---|
308 | ASSERT(a.dimension == b.dimension, "VectorContent::operator+() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
309 | VectorContent x(a);
|
---|
310 | for (size_t i=0;i<a.dimension;i++)
|
---|
311 | x.at(i) = a.at(i)+b.at(i);
|
---|
312 | return x;
|
---|
313 | };
|
---|
314 |
|
---|
315 | /** Subtracts VectorContent \a from \b component-wise.
|
---|
316 | * \param a first VectorContent
|
---|
317 | * \param b second VectorContent
|
---|
318 | * \return a - b
|
---|
319 | */
|
---|
320 | VectorContent const operator-(const VectorContent& a, const VectorContent& b)
|
---|
321 | {
|
---|
322 | ASSERT(a.dimension == b.dimension, "VectorContent::operator-() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
323 | VectorContent x(a);
|
---|
324 | for (size_t i=0;i<a.dimension;i++)
|
---|
325 | x.at(i) = a.at(i)-b.at(i);
|
---|
326 | return x;
|
---|
327 | };
|
---|
328 |
|
---|
329 | /** Factors given VectorContent \a a times \a m.
|
---|
330 | * \param a VectorContent
|
---|
331 | * \param m factor
|
---|
332 | * \return m * a
|
---|
333 | */
|
---|
334 | VectorContent const operator*(const VectorContent& a, const double m)
|
---|
335 | {
|
---|
336 | VectorContent x(a);
|
---|
337 | for (size_t i=0;i<a.dimension;i++)
|
---|
338 | x.at(i) = a.at(i)*m;
|
---|
339 | return x;
|
---|
340 | };
|
---|
341 |
|
---|
342 | /** Factors given VectorContent \a a times \a m.
|
---|
343 | * \param m factor
|
---|
344 | * \param a VectorContent
|
---|
345 | * \return m * a
|
---|
346 | */
|
---|
347 | VectorContent const operator*(const double m, const VectorContent& a )
|
---|
348 | {
|
---|
349 | VectorContent x(a);
|
---|
350 | for (size_t i=0;i<a.dimension;i++)
|
---|
351 | x.at(i) = a.at(i)*m;
|
---|
352 | return x;
|
---|
353 | };
|
---|
354 |
|
---|
355 | ostream& operator<<(ostream& ost, const VectorContent& m)
|
---|
356 | {
|
---|
357 | ost << "[";
|
---|
358 | for (size_t i=0;i<m.dimension;i++) {
|
---|
359 | ost << m.at(i);
|
---|
360 | if (i != m.dimension-1)
|
---|
361 | ost << " ";
|
---|
362 | }
|
---|
363 | ost << "]";
|
---|
364 | return ost;
|
---|
365 | };
|
---|
366 |
|
---|
367 | /* ====================== Checking State ============================ */
|
---|
368 | /** Checks whether vector has all components zero.
|
---|
369 | * TODO: This might see some numerical instabilities for huge dimension and small number.
|
---|
370 | * For stability one should sort the order of summing!
|
---|
371 | * @return true - vector is zero, false - vector is not
|
---|
372 | */
|
---|
373 | bool VectorContent::IsZero() const
|
---|
374 | {
|
---|
375 | double result = 0.;
|
---|
376 | for (size_t i = dimension; i--; )
|
---|
377 | result += fabs(at(i));
|
---|
378 | return (result <= LINALG_MYEPSILON);
|
---|
379 | };
|
---|
380 |
|
---|
381 | /** Checks whether vector has length of 1.
|
---|
382 | * @return true - vector is normalized, false - vector is not
|
---|
383 | */
|
---|
384 | bool VectorContent::IsOne() const
|
---|
385 | {
|
---|
386 | double NormValue = 0.;
|
---|
387 | for (size_t i=dimension;--i;)
|
---|
388 | NormValue += at(i)*at(i);
|
---|
389 | return (fabs(NormValue - 1.) <= LINALG_MYEPSILON);
|
---|
390 | };
|
---|
391 |
|
---|
392 | /* ========================== Norm =============================== */
|
---|
393 | /** Calculates norm of this vector.
|
---|
394 | * \return \f$|x|\f$
|
---|
395 | */
|
---|
396 | double VectorContent::Norm() const
|
---|
397 | {
|
---|
398 | return (sqrt(NormSquared()));
|
---|
399 | };
|
---|
400 |
|
---|
401 | /** Calculates squared norm of this vector.
|
---|
402 | * \return \f$|x|^2\f$
|
---|
403 | */
|
---|
404 | double VectorContent::NormSquared() const
|
---|
405 | {
|
---|
406 | return (ScalarProduct(*this));
|
---|
407 | };
|
---|
408 |
|
---|
409 | /** Normalizes this vector.
|
---|
410 | */
|
---|
411 | void VectorContent::Normalize()
|
---|
412 | {
|
---|
413 | double factor = Norm();
|
---|
414 | (*this) *= 1/factor;
|
---|
415 | };
|
---|
416 |
|
---|
417 | VectorContent VectorContent::getNormalized() const{
|
---|
418 | VectorContent res= *this;
|
---|
419 | res.Normalize();
|
---|
420 | return res;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* ======================== Properties ============================= */
|
---|
424 | /** Calculates the squared distance to some other vector.
|
---|
425 | * @param y other vector
|
---|
426 | * @return \f$|(\text{*this})-y|^2\f$
|
---|
427 | */
|
---|
428 | double VectorContent::DistanceSquared(const VectorContent &y) const
|
---|
429 | {
|
---|
430 | double res = 0.;
|
---|
431 | for (int i=dimension;i--;)
|
---|
432 | res += (at(i)-y[i])*(at(i)-y[i]);
|
---|
433 | return (res);
|
---|
434 | }
|
---|
435 |
|
---|
436 | /** Calculates scalar product between \a *this and \a b.
|
---|
437 | * @param b other vector
|
---|
438 | * @return \f$| (*this) \cdot (b)|^2\f$
|
---|
439 | */
|
---|
440 | double VectorContent::ScalarProduct(const VectorContent &y) const
|
---|
441 | {
|
---|
442 | return ((*this)*y);
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Calculates the angle between \a *this and \a y.
|
---|
446 | *
|
---|
447 | * @param y other vector
|
---|
448 | * @return \f$\acos\bigl(frac{\langle \text{*this}, y \rangle}{|\text{*this}||y|}\bigr)\f$
|
---|
449 | */
|
---|
450 | double VectorContent::Angle(const VectorContent &y) const
|
---|
451 | {
|
---|
452 | double norm1 = Norm(), norm2 = y.Norm();
|
---|
453 | double angle = -1;
|
---|
454 | if ((fabs(norm1) > LINALG_MYEPSILON) && (fabs(norm2) > LINALG_MYEPSILON))
|
---|
455 | angle = this->ScalarProduct(y)/norm1/norm2;
|
---|
456 | // -1-LINALG_MYEPSILON occured due to numerical imprecision, catch ...
|
---|
457 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+LINALG_MYEPSILON) = " << acos(-1+LINALG_MYEPSILON) << ", acos(-1-LINALG_MYEPSILON) = " << acos(-1-LINALG_MYEPSILON) << "." << endl;
|
---|
458 | if (angle < -1)
|
---|
459 | angle = -1;
|
---|
460 | if (angle > 1)
|
---|
461 | angle = 1;
|
---|
462 | return acos(angle);
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* ======================== Properties ============================= */
|
---|
466 | /** Calculates scalar product between \a *this and \a b.
|
---|
467 | * @param b other vector
|
---|
468 | * @return \f$| (*this) \cdot (b)|^2\f$
|
---|
469 | */
|
---|
470 | const double VectorContent::operator*(const VectorContent& b) const
|
---|
471 | {
|
---|
472 | double res = 0.;
|
---|
473 | gsl_blas_ddot(content, b.content, &res);
|
---|
474 | return res;
|
---|
475 | };
|
---|
476 |
|
---|
477 | /** Getter for VectorContent::dimension.
|
---|
478 | * @return VectorContent::dimension
|
---|
479 | */
|
---|
480 | const size_t VectorContent::getDimension() const
|
---|
481 | {
|
---|
482 | return dimension;
|
---|
483 | }
|
---|