| 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 | class Vector;
|
|---|
| 25 |
|
|---|
| 26 | /** Dummy structure to create a unique signature.
|
|---|
| 27 | *
|
|---|
| 28 | */
|
|---|
| 29 | struct VectorBaseCase{};
|
|---|
| 30 |
|
|---|
| 31 | class VectorContent{
|
|---|
| 32 | friend std::ostream & operator<< (std::ostream& ost, const VectorContent &m);
|
|---|
| 33 | friend VectorContent const operator*(const VectorContent& a, const double m);
|
|---|
| 34 | friend VectorContent const operator*(const double m, const VectorContent& a);
|
|---|
| 35 | friend VectorContent const operator+(const VectorContent& a, const VectorContent& b);
|
|---|
| 36 | friend VectorContent const operator-(const VectorContent& a, const VectorContent& b);
|
|---|
| 37 |
|
|---|
| 38 | public:
|
|---|
| 39 | explicit VectorContent(size_t _dim);
|
|---|
| 40 | VectorContent(VectorBaseCase);
|
|---|
| 41 | VectorContent(const VectorContent * const src);
|
|---|
| 42 | VectorContent(const VectorContent & src);
|
|---|
| 43 | VectorContent(gsl_vector * _src);
|
|---|
| 44 | virtual ~VectorContent();
|
|---|
| 45 |
|
|---|
| 46 | // Accessing
|
|---|
| 47 | double &at(size_t m);
|
|---|
| 48 | const double at(size_t m) const;
|
|---|
| 49 | double & operator[](size_t i);
|
|---|
| 50 | const double operator[](size_t i) const;
|
|---|
| 51 | double *Pointer(size_t m) const;
|
|---|
| 52 | const double *const_Pointer(size_t m) const;
|
|---|
| 53 |
|
|---|
| 54 | // Assignment operator
|
|---|
| 55 | VectorContent &operator=(const VectorContent& src);
|
|---|
| 56 |
|
|---|
| 57 | // Initializing
|
|---|
| 58 | void setFromDoubleArray(double * x);
|
|---|
| 59 | void setFromVector(Vector &v);
|
|---|
| 60 | void setValue(double x);
|
|---|
| 61 | void setZero();
|
|---|
| 62 | int setBasis(size_t m);
|
|---|
| 63 |
|
|---|
| 64 | // Exchanging elements
|
|---|
| 65 | int SwapElements(size_t i, size_t j);
|
|---|
| 66 | int Reverse();
|
|---|
| 67 |
|
|---|
| 68 | // checking state
|
|---|
| 69 | bool IsZero() const;
|
|---|
| 70 | bool IsOne() const;
|
|---|
| 71 |
|
|---|
| 72 | // properties
|
|---|
| 73 | //bool IsNormalTo(const VectorContent &normal) const;
|
|---|
| 74 | //bool IsEqualTo(const VectorContent &a) const;
|
|---|
| 75 |
|
|---|
| 76 | // Norms
|
|---|
| 77 | double Norm() const;
|
|---|
| 78 | double NormSquared() const;
|
|---|
| 79 | void Normalize();
|
|---|
| 80 | VectorContent getNormalized() const;
|
|---|
| 81 |
|
|---|
| 82 | // properties relative to another VectorContent
|
|---|
| 83 | double DistanceSquared(const VectorContent &y) const;
|
|---|
| 84 | double ScalarProduct(const VectorContent &y) const;
|
|---|
| 85 | double Angle(const VectorContent &y) const;
|
|---|
| 86 |
|
|---|
| 87 | // operators
|
|---|
| 88 | bool operator==(const VectorContent& b) const;
|
|---|
| 89 | const VectorContent& operator+=(const VectorContent& b);
|
|---|
| 90 | const VectorContent& operator-=(const VectorContent& b);
|
|---|
| 91 | const VectorContent& operator*=(const double m);
|
|---|
| 92 | const double operator*(const VectorContent& b) const;
|
|---|
| 93 |
|
|---|
| 94 | size_t dimension;
|
|---|
| 95 | gsl_vector *content;
|
|---|
| 96 | private:
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | std::ostream & operator << (std::ostream& ost, const VectorContent &m);
|
|---|
| 100 | VectorContent const operator*(const VectorContent& a, const double m);
|
|---|
| 101 | VectorContent const operator*(const double m, const VectorContent& a);
|
|---|
| 102 | VectorContent const operator+(const VectorContent& a, const VectorContent& b);
|
|---|
| 103 | VectorContent const operator-(const VectorContent& a, const VectorContent& b);
|
|---|
| 104 |
|
|---|
| 105 | /** Vector view.
|
|---|
| 106 | * Extension of VectorContent to contain not a gsl_vector but only a view on a
|
|---|
| 107 | * gsl_vector (or row/column in a gsl_matrix).
|
|---|
| 108 | *
|
|---|
| 109 | * We need the above VectorBaseCase here:
|
|---|
| 110 | * content, i.e. the gsl_vector, must not be allocated, as it is just a view
|
|---|
| 111 | * with an internal gsl_vector_view. Hence, VectorBaseCase is just dummy class
|
|---|
| 112 | * to give the constructor a unique signature.
|
|---|
| 113 | */
|
|---|
| 114 | struct VectorViewContent : public VectorContent
|
|---|
| 115 | {
|
|---|
| 116 | VectorViewContent(gsl_vector_view _view) :
|
|---|
| 117 | VectorContent(VectorBaseCase()),
|
|---|
| 118 | view(_view)
|
|---|
| 119 | {
|
|---|
| 120 | dimension = _view.vector.size;
|
|---|
| 121 | content=&view.vector;
|
|---|
| 122 | }
|
|---|
| 123 | ~VectorViewContent(){
|
|---|
| 124 | content=0;
|
|---|
| 125 | }
|
|---|
| 126 | gsl_vector_view view;
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | #endif /* VECTORCONTENT_HPP_ */
|
|---|