source: src/gslvector.cpp@ ba9f5b

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 ba9f5b was 1dc9ec, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'VectorRefactoring' into stable

Conflicts:

molecuilder/src/vector.cpp

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