source: src/gslvector.cpp@ 9d5ddf

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 9d5ddf was ce3d2b, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added a "forward declaration" to the gsl_vector struct to avoid inclusion of the GSL-Headers in too many files

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