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