source: src/LinearAlgebra/VectorContent.cpp@ 0fd3f2

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

Scalar product operator implemented, taken from Vector.

  • Property mode set to 100644
File size: 11.0 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_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 */
36VectorContent::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 */
49VectorContent::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 */
56VectorContent::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 */
67VectorContent::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 */
78VectorContent::VectorContent(gsl_vector * _src) :
79 dimension(_src->size)
80{
81 content = _src;
82}
83
84/** Destructor of class VectorContent.
85 * Frees GSL structures
86 */
87VectorContent::~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 */
100double &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 */
111const 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 */
123double *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 */
133const 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 */
142VectorContent& 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 */
156void 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 */
165void 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 */
176void 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 */
183void 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 */
192int 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 */
202int 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 */
209int 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 */
220double &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 */
231const 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 */
244bool 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 */
258const 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 */
271const 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 */
284const 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 */
296VectorContent 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 */
310VectorContent 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 */
324VectorContent 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 */
337VectorContent 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
345ostream& 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 */
363bool 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 */
374bool 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 */
387const double VectorContent::operator*(const VectorContent& b)
388{
389 double res = 0.;
390 gsl_blas_ddot(content, b.content, &res);
391 return res;
392};
Note: See TracBrowser for help on using the repository browser.