source: src/LinearAlgebra/VectorContent.cpp@ f453d2

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 f453d2 was f453d2, checked in by Frederik Heber <heber@…>, 14 years ago

New VectorContent copy constructor for gsl_vector.

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