source: src/gslvector.cpp@ 6cd16b

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

Added operators and state checkers to GSLVector.

  • added operators +,-,*, +=, -=, *=, == and =
  • added IsZero() and IsOne().
  • gslvectorunittest extended with state and operator test.

Signed-off-by: Frederik Heber <heber@…>

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