source: src/gslvector.cpp@ b47bfc

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 b47bfc was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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