Changeset b11d3b


Ignore:
Timestamp:
Mar 1, 2010, 8:28:00 PM (15 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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
Children:
821907
Parents:
775d133
git-author:
Frederik Heber <heber@…> (03/01/10 20:24:45)
git-committer:
Frederik Heber <heber@…> (03/01/10 20:28:00)
Message:

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@…>

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/gslvector.cpp

    r775d133 rb11d3b  
    66 */
    77
     8#include <cassert>
     9#include <cmath>
     10
    811#include "gslvector.hpp"
     12#include "defs.hpp"
    913
    1014/** Constructor of class GSLVector.
     
    2731};
    2832
     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
    2943/** Destructor of class GSLVector.
    3044 * Frees GSL structures
     
    3347{
    3448  gsl_vector_free(vector);
    35   dimension = 0;
    3649};
    3750
     
    5265 * \return  m-th element of vector
    5366 */
    54 double GSLVector::Get(size_t m)
     67double GSLVector::Get(size_t m) const
    5568{
    5669  return gsl_vector_get (vector, m);
     
    7285 * \return pointer to \a m-th element
    7386 */
    74 double *GSLVector::Pointer(size_t m)
     87double *GSLVector::Pointer(size_t m) const
    7588{
    7689  return gsl_vector_ptr (vector, m);
     
    8295 * \return const pointer to \a m-th element
    8396 */
    84 const double *GSLVector::const_Pointer(size_t m)
     97const double *GSLVector::const_Pointer(size_t m) const
    8598{
    8699  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;
    87111};
    88112
     
    128152  return gsl_vector_reverse (vector);
    129153};
     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
  • TabularUnified src/gslvector.hpp

    r775d133 rb11d3b  
    1818#endif
    1919
     20#include <iostream>
    2021#include <gsl/gsl_vector.h>
    2122
     
    3233  GSLVector(size_t m);
    3334  GSLVector(const GSLVector * const src);
     35  GSLVector(const GSLVector & src);
    3436  ~GSLVector();
    3537
    3638  // Accessing
    3739  void SetFromDoubleArray(double *x);
    38   double Get(size_t m);
     40  double Get(size_t m) const;
    3941  void Set(size_t m, double x);
    40   double *Pointer(size_t m);
    41   const double *const_Pointer(size_t m);
     42  double *Pointer(size_t m) const;
     43  const double *const_Pointer(size_t m) const;
     44  size_t GetDimension() const;
    4245
    4346  // Initializing
     
    5053  int Reverse();
    5154
     55  // checking state
     56  bool IsZero() const;
     57  bool IsOne() const;
     58
    5259private:
    5360  gsl_vector *vector;
    5461
    55   size_t dimension;
     62  const size_t dimension;
    5663};
     64
     65ostream & operator << (ostream& ost, const GSLVector &m);
     66bool operator==(const GSLVector& a, const GSLVector& b);
     67const GSLVector& operator+=(GSLVector& a, const GSLVector& b);
     68const GSLVector& operator-=(GSLVector& a, const GSLVector& b);
     69const GSLVector& operator*=(GSLVector& a, const double m);
     70GSLVector const operator*(const GSLVector& a, const double m);
     71GSLVector const operator*(const double m, const GSLVector& a);
     72GSLVector const operator+(const GSLVector& a, const GSLVector& b);
     73GSLVector const operator-(const GSLVector& a, const GSLVector& b);
     74
    5775
    5876
  • TabularUnified src/unittests/gslvectorunittest.cpp

    r775d133 rb11d3b  
    115115
    116116
     117/** UnitTest for operators.
     118 */
     119void GSLVectorTest::OperatorIsTest()
     120{
     121  GSLVector zero(3);
     122  GSLVector unit(3);
     123  zero.SetZero();
     124  unit.SetZero();
     125  unit.Set(1,1.);
     126  // summation and scaling
     127  CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
     128  CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
     129  CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
     130  CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
     131};
     132
     133/** UnitTest for operators.
     134 */
     135void GSLVectorTest::OperatorAlgebraTest()
     136{
     137  GSLVector zero(3);
     138  GSLVector unit(3);
     139  zero.SetZero();
     140  unit.SetZero();
     141  unit.Set(1,1.);
     142  // summation and scaling
     143  CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
     144  CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
     145  CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
     146  CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
     147  CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
     148  CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
     149  CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
     150  CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
     151  CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
     152
     153  CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
     154  CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
     155  CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
     156
     157  unit += zero;
     158  CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
     159  unit *= 1.;
     160  CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
     161};
     162
    117163/********************************************** Main routine **************************************/
    118164
  • TabularUnified src/unittests/gslvectorunittest.hpp

    r775d133 rb11d3b  
    2222    CPPUNIT_TEST (CopyTest );
    2323    CPPUNIT_TEST (ExchangeTest );
     24    CPPUNIT_TEST (OperatorAlgebraTest );
     25    CPPUNIT_TEST (OperatorIsTest );
    2426    CPPUNIT_TEST_SUITE_END();
    2527
     
    3234    void CopyTest();
    3335    void ExchangeTest();
     36    void OperatorIsTest();
     37    void OperatorAlgebraTest();
    3438
    3539private:
Note: See TracChangeset for help on using the changeset viewer.