source: src/LinearAlgebra/VectorContent.cpp@ bbf1bd

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since bbf1bd was bbf1bd, checked in by Frederik Heber <heber@…>, 14 years ago

Extended VectorContent class.

  • VectorContent formerly has been just a structure to contain the gsl_vector due to forward declaration reasons.
  • now VectorContent is a true wrapper to gsl_vector, i.e. all functionality that is now specific to 3 dimensions has been shifted from GSLVector over to VectorContent.
  • VectorContentView is preserved to allow for VectorContent as a view on a row or column of a matrix.
  • changed and renamed unit test gslvectorunittest -> VectorContentUnitTest
  • GSLVector is not used anymore anywhere
  • one long-sough error was a missing assignment operator filled-in in a wrong manner by the compiler for VectorContent.

Note that:

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