source: src/gslvector.cpp@ 51be2a

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 51be2a was 5e8e02, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Repaired broken NO_MEMDEBUG build-path

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