| [ce3d2b] | 1 | /*
 | 
|---|
 | 2 |  * VectorContent.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jul 2, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef VECTORCONTENT_HPP_
 | 
|---|
 | 9 | #define VECTORCONTENT_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
| [56f73b] | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | 
 | 
|---|
| [ce3d2b] | 17 | /**
 | 
|---|
 | 18 |  * !file
 | 
|---|
 | 19 |  * The way GSL works does not allow for forward definitions of the structures.
 | 
|---|
 | 20 |  * Because of this the pointer to the gsl_vector struct is wrapped inside another
 | 
|---|
| [9d5ddf] | 21 |  * (dumb) object that allows for forward definitions.
 | 
|---|
| [bc8a41] | 22 |  *
 | 
|---|
 | 23 |  * DO NOT USE OUTSIDE OF VECTOR UNLESS YOU ABSOLUTELY HAVE TO AND KNOW WHAT YOU ARE DOING.
 | 
|---|
| [ce3d2b] | 24 |  */
 | 
|---|
 | 25 | 
 | 
|---|
| [bbf1bd] | 26 | #include <iosfwd>
 | 
|---|
 | 27 | 
 | 
|---|
| [ce3d2b] | 28 | #include <gsl/gsl_vector.h>
 | 
|---|
 | 29 | 
 | 
|---|
| [3da2fb] | 30 | #include "LinearAlgebra/MatrixVector_ops.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
| [bbf1bd] | 32 | class Vector;
 | 
|---|
| [3da2fb] | 33 | class MatrixContent;
 | 
|---|
| [bbf1bd] | 34 | 
 | 
|---|
 | 35 | /** Dummy structure to create a unique signature.
 | 
|---|
 | 36 |  *
 | 
|---|
 | 37 |  */
 | 
|---|
| [8e9ce1] | 38 | struct VectorBaseCase{};
 | 
|---|
| [8e17d6] | 39 | 
 | 
|---|
| [bbf1bd] | 40 | class VectorContent{
 | 
|---|
 | 41 |   friend std::ostream & operator<< (std::ostream& ost, const VectorContent &m);
 | 
|---|
 | 42 |   friend VectorContent const operator*(const VectorContent& a, const double m);
 | 
|---|
 | 43 |   friend VectorContent const operator*(const double m, const VectorContent& a);
 | 
|---|
 | 44 |   friend VectorContent const operator+(const VectorContent& a, const VectorContent& b);
 | 
|---|
 | 45 |   friend VectorContent const operator-(const VectorContent& a, const VectorContent& b);
 | 
|---|
| [8e17d6] | 46 | 
 | 
|---|
| [3da2fb] | 47 |   // matrix vector products
 | 
|---|
 | 48 |   friend VectorContent const operator*(const VectorContent& vec, const MatrixContent& mat);
 | 
|---|
 | 49 |   friend VectorContent const operator*(const MatrixContent& mat, const VectorContent& vec);
 | 
|---|
 | 50 | 
 | 
|---|
| [bbf1bd] | 51 | public:
 | 
|---|
 | 52 |   explicit VectorContent(size_t _dim);
 | 
|---|
| [8e9ce1] | 53 |   VectorContent(VectorBaseCase);
 | 
|---|
| [bbf1bd] | 54 |   VectorContent(const VectorContent * const src);
 | 
|---|
 | 55 |   VectorContent(const VectorContent & src);
 | 
|---|
| [f453d2] | 56 |   VectorContent(gsl_vector * _src);
 | 
|---|
| [bbf1bd] | 57 |   virtual ~VectorContent();
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 |   // Accessing
 | 
|---|
 | 60 |   double &at(size_t m);
 | 
|---|
 | 61 |   const double at(size_t m) const;
 | 
|---|
 | 62 |   double & operator[](size_t i);
 | 
|---|
 | 63 |   const double operator[](size_t i) const;
 | 
|---|
 | 64 |   double *Pointer(size_t m) const;
 | 
|---|
 | 65 |   const double *const_Pointer(size_t m) const;
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |   // Assignment operator
 | 
|---|
 | 68 |   VectorContent &operator=(const VectorContent& src);
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |   // Initializing
 | 
|---|
 | 71 |   void setFromDoubleArray(double * x);
 | 
|---|
 | 72 |   void setFromVector(Vector &v);
 | 
|---|
 | 73 |   void setValue(double x);
 | 
|---|
 | 74 |   void setZero();
 | 
|---|
 | 75 |   int setBasis(size_t m);
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |   // Exchanging elements
 | 
|---|
 | 78 |   int SwapElements(size_t i, size_t j);
 | 
|---|
 | 79 |   int Reverse();
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |   // checking state
 | 
|---|
 | 82 |   bool IsZero() const;
 | 
|---|
 | 83 |   bool IsOne() const;
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |   // properties
 | 
|---|
| [3dd9c7] | 86 |   //bool IsNormalTo(const VectorContent &normal) const;
 | 
|---|
 | 87 |   //bool IsEqualTo(const VectorContent &a) const;
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |   // Norms
 | 
|---|
 | 90 |   double Norm() const;
 | 
|---|
 | 91 |   double NormSquared() const;
 | 
|---|
 | 92 |   void Normalize();
 | 
|---|
 | 93 |   VectorContent getNormalized() const;
 | 
|---|
| [bbf1bd] | 94 | 
 | 
|---|
 | 95 |   // properties relative to another VectorContent
 | 
|---|
 | 96 |   double DistanceSquared(const VectorContent &y) const;
 | 
|---|
 | 97 |   double ScalarProduct(const VectorContent &y) const;
 | 
|---|
 | 98 |   double Angle(const VectorContent &y) const;
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |   // operators
 | 
|---|
 | 101 |   bool operator==(const VectorContent& b) const;
 | 
|---|
 | 102 |   const VectorContent& operator+=(const VectorContent& b);
 | 
|---|
 | 103 |   const VectorContent& operator-=(const VectorContent& b);
 | 
|---|
 | 104 |   const VectorContent& operator*=(const double m);
 | 
|---|
| [3dd9c7] | 105 |   const double operator*(const VectorContent& b) const;
 | 
|---|
| [bbf1bd] | 106 | 
 | 
|---|
| [14cce6] | 107 |   const size_t getDimension() const;
 | 
|---|
 | 108 | 
 | 
|---|
| [bbf1bd] | 109 |   size_t dimension;
 | 
|---|
| [ce3d2b] | 110 |   gsl_vector *content;
 | 
|---|
| [bbf1bd] | 111 | private:
 | 
|---|
| [abfb12] | 112 |   bool free_content_on_exit;
 | 
|---|
| [ce3d2b] | 113 | };
 | 
|---|
 | 114 | 
 | 
|---|
| [bbf1bd] | 115 | std::ostream & operator << (std::ostream& ost, const VectorContent &m);
 | 
|---|
 | 116 | VectorContent const operator*(const VectorContent& a, const double m);
 | 
|---|
 | 117 | VectorContent const operator*(const double m, const VectorContent& a);
 | 
|---|
 | 118 | VectorContent const operator+(const VectorContent& a, const VectorContent& b);
 | 
|---|
 | 119 | VectorContent const operator-(const VectorContent& a, const VectorContent& b);
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | /** Vector view.
 | 
|---|
 | 122 |  * Extension of VectorContent to contain not a gsl_vector but only a view on a
 | 
|---|
 | 123 |  * gsl_vector (or row/column in a gsl_matrix).
 | 
|---|
 | 124 |  *
 | 
|---|
| [8e9ce1] | 125 |  * We need the above VectorBaseCase here:
 | 
|---|
| [bbf1bd] | 126 |  * content, i.e. the gsl_vector, must not be allocated, as it is just a view
 | 
|---|
| [8e9ce1] | 127 |  * with an internal gsl_vector_view. Hence, VectorBaseCase is just dummy class
 | 
|---|
 | 128 |  * to give the constructor a unique signature.
 | 
|---|
| [bbf1bd] | 129 |  */
 | 
|---|
 | 130 | struct VectorViewContent : public VectorContent
 | 
|---|
 | 131 | {
 | 
|---|
| [8e17d6] | 132 |   VectorViewContent(gsl_vector_view _view) :
 | 
|---|
| [8e9ce1] | 133 |     VectorContent(VectorBaseCase()),
 | 
|---|
| [8e17d6] | 134 |     view(_view)
 | 
|---|
 | 135 |   {
 | 
|---|
| [bbf1bd] | 136 |     dimension = _view.vector.size;
 | 
|---|
| [8e17d6] | 137 |     content=&view.vector;
 | 
|---|
 | 138 |   }
 | 
|---|
| [bbf1bd] | 139 |   ~VectorViewContent(){
 | 
|---|
| [3dbb9d] | 140 |     content=0;
 | 
|---|
 | 141 |   }
 | 
|---|
| [8e17d6] | 142 |   gsl_vector_view view;
 | 
|---|
 | 143 | };
 | 
|---|
 | 144 | 
 | 
|---|
| [ce3d2b] | 145 | #endif /* VECTORCONTENT_HPP_ */
 | 
|---|