source: src/LinearAlgebra/gslvector.cpp@ db7cb0

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

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 8.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 * gslvector.cpp
10 *
11 * Created on: Jan 8, 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 <cassert>
23#include <cmath>
24#include <iostream>
25
26#include "LinearAlgebra/gslvector.hpp"
27#include "defs.hpp"
28#include "LinearAlgebra/Vector.hpp"
29#include "VectorContent.hpp"
30
31/** Constructor of class GSLVector.
32 * Allocates GSL structures
33 * \param m dimension of vector
34 */
35GSLVector::GSLVector(size_t m) : dimension(m)
36{
37 vector = gsl_vector_calloc(dimension);
38};
39
40/** Copy constructor of class GSLVector.
41 * Allocates GSL structures and copies components from \a *src.
42 * \param *src source vector
43 */
44GSLVector::GSLVector(const GSLVector * const src) : dimension(src->dimension)
45{
46 vector = gsl_vector_alloc(dimension);
47 gsl_vector_memcpy (vector, src->vector);
48};
49
50/** Copy constructor of class GSLVector.
51 * Allocates GSL structures and copies components from \a *src.
52 * \param *src source vector
53 */
54GSLVector::GSLVector(const GSLVector & src) : dimension(src.dimension)
55{
56 vector = gsl_vector_alloc(dimension);
57 gsl_vector_memcpy (vector, src.vector);
58};
59
60/** Destructor of class GSLVector.
61 * Frees GSL structures
62 */
63GSLVector::~GSLVector()
64{
65 gsl_vector_free(vector);
66};
67
68/* ============================ Accessing =============================== */
69/** This function sets the vector from a double array.
70 * Creates a vector view of the array and performs a memcopy.
71 * \param *x array of values (no dimension check is performed)
72 */
73void GSLVector::SetFromDoubleArray(double * x)
74{
75 gsl_vector_view m = gsl_vector_view_array (x, dimension);
76 gsl_vector_memcpy (vector, &m.vector);
77};
78
79/**
80 * This function sets the GSLvector from an ordinary vector.
81 *
82 * Takes access to the internal gsl_vector and copies it
83 */
84void GSLVector::SetFromVector(Vector &v){
85 gsl_vector_memcpy (vector, v.get()->content);
86}
87
88/** This function returns the i-th element of a vector.
89 * If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and 0 is returned.
90 * \param m m-th element
91 * \return m-th element of vector
92 */
93double GSLVector::Get(size_t m) const
94{
95 return gsl_vector_get (vector, m);
96};
97
98/** This function sets the value of the \a m -th element of a vector to \a x.
99 * If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked.
100 * \param m-th element to set
101 * \param x value to set to
102 */
103void GSLVector::Set(size_t m, double x)
104{
105 gsl_vector_set (vector, m, x);
106};
107
108/** These functions return a pointer to the \a m-th element of a vector.
109 * If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and a null pointer is returned.
110 * \param m m-th element
111 * \return pointer to \a m-th element
112 */
113double *GSLVector::Pointer(size_t m) const
114{
115 return gsl_vector_ptr (vector, m);
116};
117
118/** These functions return a constant pointer to the \a m-th element of a vector.
119 * If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and a null pointer is returned.
120 * \param m m-th element
121 * \return const pointer to \a m-th element
122 */
123const double *GSLVector::const_Pointer(size_t m) const
124{
125 return gsl_vector_const_ptr (vector, m);
126};
127
128/** Returns the dimension of the vector.
129 * \return dimension of vector
130 */
131#ifdef HAVE_INLINE
132inline
133#endif
134size_t GSLVector::GetDimension() const
135{
136 return dimension;
137};
138
139/* ========================== Initializing =============================== */
140/** This function sets all the elements of the vector to the value \a x.
141 * \param *x
142 */
143void GSLVector::SetAll(double x)
144{
145 gsl_vector_set_all (vector, x);
146};
147
148/** This function sets all the elements of the vector to zero.
149 */
150void GSLVector::SetZero()
151{
152 gsl_vector_set_zero (vector);
153};
154
155/** 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.
156 * \param i i-th component to set to unity (all other to zero)
157 * \return vector set
158 */
159int GSLVector::SetBasis(size_t i)
160{
161 return gsl_vector_set_basis (vector, i);
162};
163
164/* ====================== Exchanging elements ============================ */
165/** This function exchanges the \a i-th and \a j-th elements of the vector in-place.
166 * \param i i-th element to swap with ...
167 * \param j ... j-th element to swap against
168 */
169int GSLVector::SwapElements(size_t i, size_t j)
170{
171 return gsl_vector_swap_elements (vector, i, j);
172};
173
174/** This function reverses the order of the elements of the vector.
175 */
176int GSLVector::Reverse()
177{
178 return gsl_vector_reverse (vector);
179};
180
181
182/* ========================== Operators =============================== */
183/** Compares GSLVector \a to GSLVector \a b component-wise.
184 * \param a base GSLVector
185 * \param b GSLVector components to add
186 * \return a == b
187 */
188bool operator==(const GSLVector& a, const GSLVector& b)
189{
190 bool status = true;
191 assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
192 for (size_t i=0;i<a.GetDimension();i++)
193 status = status && (fabs(a.Get(i) - b.Get(i)) < MYEPSILON);
194 return status;
195};
196
197/** Sums GSLVector \a to this lhs component-wise.
198 * \param a base GSLVector
199 * \param b GSLVector components to add
200 * \return lhs + a
201 */
202const GSLVector& operator+=(GSLVector& a, const GSLVector& b)
203{
204 assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
205 for (size_t i=0;i<a.GetDimension();i++)
206 a.Set(i,a.Get(i)+b.Get(i));
207 return a;
208};
209
210/** Subtracts GSLVector \a from this lhs component-wise.
211 * \param a base GSLVector
212 * \param b GSLVector components to add
213 * \return lhs - a
214 */
215const GSLVector& operator-=(GSLVector& a, const GSLVector& b)
216{
217 assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
218 for (size_t i=0;i<a.GetDimension();i++)
219 a.Set(i,a.Get(i)-b.Get(i));
220 return a;
221};
222
223/** factor each component of \a a times a double \a m.
224 * \param a base GSLVector
225 * \param m factor
226 * \return lhs.Get(i) * m
227 */
228const GSLVector& operator*=(GSLVector& a, const double m)
229{
230 for (size_t i=0;i<a.GetDimension();i++)
231 a.Set(i,a.Get(i)*m);
232 return a;
233};
234
235/** Sums two GSLVectors \a and \b component-wise.
236 * \param a first GSLVector
237 * \param b second GSLVector
238 * \return a + b
239 */
240GSLVector const operator+(const GSLVector& a, const GSLVector& b)
241{
242 GSLVector x(a);
243 for (size_t i=0;i<a.GetDimension();i++)
244 x.Set(i,a.Get(i)+b.Get(i));
245 return x;
246};
247
248/** Subtracts GSLVector \a from \b component-wise.
249 * \param a first GSLVector
250 * \param b second GSLVector
251 * \return a - b
252 */
253GSLVector const operator-(const GSLVector& a, const GSLVector& b)
254{
255 assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
256 GSLVector x(a);
257 for (size_t i=0;i<a.GetDimension();i++)
258 x.Set(i,a.Get(i)-b.Get(i));
259 return x;
260};
261
262/** Factors given GSLVector \a a times \a m.
263 * \param a GSLVector
264 * \param m factor
265 * \return m * a
266 */
267GSLVector const operator*(const GSLVector& a, const double m)
268{
269 GSLVector x(a);
270 for (size_t i=0;i<a.GetDimension();i++)
271 x.Set(i,a.Get(i)*m);
272 return x;
273};
274
275/** Factors given GSLVector \a a times \a m.
276 * \param m factor
277 * \param a GSLVector
278 * \return m * a
279 */
280GSLVector const operator*(const double m, const GSLVector& a )
281{
282 GSLVector x(a);
283 for (size_t i=0;i<a.GetDimension();i++)
284 x.Set(i,a.Get(i)*m);
285 return x;
286};
287
288ostream& operator<<(ostream& ost, const GSLVector& m)
289{
290 ost << "(";
291 for (size_t i=0;i<m.GetDimension();i++) {
292 ost << m.Get(i);
293 if (i != 2)
294 ost << ",";
295 }
296 ost << ")";
297 return ost;
298};
299
300/* ====================== Checking State ============================ */
301/** Checks whether vector has all components zero.
302 * @return true - vector is zero, false - vector is not
303 */
304bool GSLVector::IsZero() const
305{
306 return (fabs(Get(0))+fabs(Get(1))+fabs(Get(2)) < MYEPSILON);
307};
308
309/** Checks whether vector has length of 1.
310 * @return true - vector is normalized, false - vector is not
311 */
312bool GSLVector::IsOne() const
313{
314 double NormValue = 0.;
315 for (size_t i=dimension;--i;)
316 NormValue += Get(i)*Get(i);
317 return (fabs(NormValue - 1.) < MYEPSILON);
318};
319
Note: See TracBrowser for help on using the repository browser.