| 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 | // include config.h
|
|---|
| 12 | #ifdef HAVE_CONFIG_H
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | #include "CodePatterns/Assert.hpp"
|
|---|
| 17 |
|
|---|
| 18 | #include <boost/version.hpp>
|
|---|
| 19 | #if BOOST_VERSION > 106300
|
|---|
| 20 | #include <boost/serialization/array_wrapper.hpp>
|
|---|
| 21 | #endif
|
|---|
| 22 | #include <boost/serialization/array.hpp>
|
|---|
| 23 | #include <boost/serialization/split_member.hpp>
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * !file
|
|---|
| 27 | * The way GSL works does not allow for forward definitions of the structures.
|
|---|
| 28 | * Because of this the pointer to the gsl_vector struct is wrapped inside another
|
|---|
| 29 | * (dumb) object that allows for forward definitions.
|
|---|
| 30 | *
|
|---|
| 31 | * DO NOT USE OUTSIDE OF VECTOR UNLESS YOU ABSOLUTELY HAVE TO AND KNOW WHAT YOU ARE DOING.
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #include <iosfwd>
|
|---|
| 35 |
|
|---|
| 36 | #include <gsl/gsl_vector.h>
|
|---|
| 37 |
|
|---|
| 38 | #include "MatrixVector_ops.hpp"
|
|---|
| 39 |
|
|---|
| 40 | class Vector;
|
|---|
| 41 | class MatrixContent;
|
|---|
| 42 | struct VectorViewContent;
|
|---|
| 43 | namespace boost {
|
|---|
| 44 | namespace serialization {
|
|---|
| 45 | class access;
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /** This class safely stores vector dimension away.
|
|---|
| 50 | *
|
|---|
| 51 | * This way we simulate const-ness of the dimension while still allowing
|
|---|
| 52 | * serialization to modify them.
|
|---|
| 53 | *
|
|---|
| 54 | */
|
|---|
| 55 | struct VectorDimension {
|
|---|
| 56 | friend struct VectorViewContent;
|
|---|
| 57 | public:
|
|---|
| 58 | VectorDimension(size_t _dimension) : dimension(_dimension) {}
|
|---|
| 59 |
|
|---|
| 60 | size_t getDimension() const {return dimension;}
|
|---|
| 61 |
|
|---|
| 62 | private:
|
|---|
| 63 | void setDim(size_t _dimension) {dimension = _dimension;}
|
|---|
| 64 |
|
|---|
| 65 | friend class boost::serialization::access;
|
|---|
| 66 | // serialization (due to gsl_vector separate versions needed)
|
|---|
| 67 | template<class Archive>
|
|---|
| 68 | void serialize(Archive & ar, const unsigned int version)
|
|---|
| 69 | {
|
|---|
| 70 | ar & dimension;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | private:
|
|---|
| 74 | size_t dimension; // store for internal purposes
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /** Dummy structure to create a unique signature.
|
|---|
| 78 | *
|
|---|
| 79 | */
|
|---|
| 80 | struct VectorBaseCase{};
|
|---|
| 81 |
|
|---|
| 82 | class VectorContent : public VectorDimension
|
|---|
| 83 | {
|
|---|
| 84 | friend std::ostream & operator<< (std::ostream& ost, const VectorContent &m);
|
|---|
| 85 | friend VectorContent const operator*(const VectorContent& a, const double m);
|
|---|
| 86 | friend VectorContent const operator*(const double m, const VectorContent& a);
|
|---|
| 87 | friend VectorContent const operator+(const VectorContent& a, const VectorContent& b);
|
|---|
| 88 | friend VectorContent const operator-(const VectorContent& a, const VectorContent& b);
|
|---|
| 89 |
|
|---|
| 90 | // matrix vector products
|
|---|
| 91 | friend VectorContent const operator*(const VectorContent& vec, const MatrixContent& mat);
|
|---|
| 92 | friend VectorContent const operator*(const MatrixContent& mat, const VectorContent& vec);
|
|---|
| 93 |
|
|---|
| 94 | public:
|
|---|
| 95 | explicit VectorContent(size_t _dim);
|
|---|
| 96 | VectorContent(VectorBaseCase);
|
|---|
| 97 | VectorContent(const VectorContent * const src);
|
|---|
| 98 | VectorContent(const VectorContent & src);
|
|---|
| 99 | VectorContent(gsl_vector * _src, bool _free_content_on_exit = false);
|
|---|
| 100 | VectorContent(const size_t _dimension, std::istream &inputstream);
|
|---|
| 101 | virtual ~VectorContent();
|
|---|
| 102 |
|
|---|
| 103 | static size_t preparseVectorDimensions(std::istream &inputstream);
|
|---|
| 104 |
|
|---|
| 105 | // Accessing
|
|---|
| 106 | double &at(size_t m);
|
|---|
| 107 | const double at(size_t m) const;
|
|---|
| 108 | double & operator[](size_t i);
|
|---|
| 109 | const double operator[](size_t i) const;
|
|---|
| 110 | double *Pointer(size_t m) const;
|
|---|
| 111 | const double *const_Pointer(size_t m) const;
|
|---|
| 112 | void write(std::ostream &ost) const;
|
|---|
| 113 |
|
|---|
| 114 | // Assignment operator
|
|---|
| 115 | VectorContent &operator=(const VectorContent& src);
|
|---|
| 116 |
|
|---|
| 117 | // Initializing
|
|---|
| 118 | void setFromDoubleArray(double * x);
|
|---|
| 119 | void setFromVector(Vector &v);
|
|---|
| 120 | void setValue(double x);
|
|---|
| 121 | void setZero();
|
|---|
| 122 | int setBasis(size_t m);
|
|---|
| 123 |
|
|---|
| 124 | // Exchanging elements
|
|---|
| 125 | int SwapElements(size_t i, size_t j);
|
|---|
| 126 | int Reverse();
|
|---|
| 127 |
|
|---|
| 128 | // checking state
|
|---|
| 129 | bool IsZero() const;
|
|---|
| 130 | bool IsOne() const;
|
|---|
| 131 |
|
|---|
| 132 | // properties
|
|---|
| 133 | //bool IsNormalTo(const VectorContent &normal) const;
|
|---|
| 134 | //bool IsEqualTo(const VectorContent &a) const;
|
|---|
| 135 |
|
|---|
| 136 | // Norms
|
|---|
| 137 | double Norm() const;
|
|---|
| 138 | double NormSquared() const;
|
|---|
| 139 | void Normalize();
|
|---|
| 140 | VectorContent getNormalized() const;
|
|---|
| 141 |
|
|---|
| 142 | // properties relative to another VectorContent
|
|---|
| 143 | double DistanceSquared(const VectorContent &y) const;
|
|---|
| 144 | double ScalarProduct(const VectorContent &y) const;
|
|---|
| 145 | double Angle(const VectorContent &y) const;
|
|---|
| 146 |
|
|---|
| 147 | // operators
|
|---|
| 148 | bool operator==(const VectorContent& b) const;
|
|---|
| 149 | const VectorContent& operator+=(const VectorContent& b);
|
|---|
| 150 | const VectorContent& operator-=(const VectorContent& b);
|
|---|
| 151 | const VectorContent& operator*=(const double m);
|
|---|
| 152 | const double operator*(const VectorContent& b) const;
|
|---|
| 153 |
|
|---|
| 154 | bool operator!=(const VectorContent &other) const {
|
|---|
| 155 | return !(*this == other);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | private:
|
|---|
| 159 | VectorContent();
|
|---|
| 160 | friend class boost::serialization::access;
|
|---|
| 161 | // serialization (due to gsl_vector separate versions needed)
|
|---|
| 162 | template<class Archive>
|
|---|
| 163 | void save(Archive & ar, const unsigned int version) const
|
|---|
| 164 | {
|
|---|
| 165 | ar & boost::serialization::base_object<VectorDimension>(*this);
|
|---|
| 166 | ar & content->size;
|
|---|
| 167 | ar & content->stride;
|
|---|
| 168 | ar & boost::serialization::make_array<double>(content->data, content->size);
|
|---|
| 169 | }
|
|---|
| 170 | template<class Archive>
|
|---|
| 171 | void load(Archive & ar, const unsigned int version)
|
|---|
| 172 | {
|
|---|
| 173 | ar & boost::serialization::base_object<VectorDimension>(*this);
|
|---|
| 174 | content = gsl_vector_calloc(getDimension());
|
|---|
| 175 | ar & content->size;
|
|---|
| 176 | ASSERT(getDimension() == content->size,
|
|---|
| 177 | "MatrixContent::load() - dimension mismatch: "
|
|---|
| 178 | +toString(getDimension())+" != "+toString(content->size)+".");
|
|---|
| 179 | ar & content->stride;
|
|---|
| 180 | ar & boost::serialization::make_array<double>(content->data, content->size);
|
|---|
| 181 | // this is always a copy, hence always free
|
|---|
| 182 | free_content_on_exit = true;
|
|---|
| 183 | }
|
|---|
| 184 | BOOST_SERIALIZATION_SPLIT_MEMBER()
|
|---|
| 185 |
|
|---|
| 186 | public:
|
|---|
| 187 | gsl_vector *content;
|
|---|
| 188 | private:
|
|---|
| 189 | bool free_content_on_exit;
|
|---|
| 190 | };
|
|---|
| 191 | //BOOST_CLASS_EXPORT_GUID(VectorContent, "VectorContent")
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 | std::ostream & operator << (std::ostream& ost, const VectorContent &m);
|
|---|
| 195 | VectorContent const operator*(const VectorContent& a, const double m);
|
|---|
| 196 | VectorContent const operator*(const double m, const VectorContent& a);
|
|---|
| 197 | VectorContent const operator+(const VectorContent& a, const VectorContent& b);
|
|---|
| 198 | VectorContent const operator-(const VectorContent& a, const VectorContent& b);
|
|---|
| 199 |
|
|---|
| 200 | /** Vector view.
|
|---|
| 201 | * Extension of VectorContent to contain not a gsl_vector but only a view on a
|
|---|
| 202 | * gsl_vector (or row/column in a gsl_matrix).
|
|---|
| 203 | *
|
|---|
| 204 | * We need the above VectorBaseCase here:
|
|---|
| 205 | * content, i.e. the gsl_vector, must not be allocated, as it is just a view
|
|---|
| 206 | * with an internal gsl_vector_view. Hence, VectorBaseCase is just dummy class
|
|---|
| 207 | * to give the constructor a unique signature.
|
|---|
| 208 | */
|
|---|
| 209 | struct VectorViewContent : public VectorContent
|
|---|
| 210 | {
|
|---|
| 211 | VectorViewContent(gsl_vector_view _view) :
|
|---|
| 212 | VectorContent(VectorBaseCase()),
|
|---|
| 213 | view(_view)
|
|---|
| 214 | {
|
|---|
| 215 | dimension = _view.vector.size;
|
|---|
| 216 | content=&view.vector;
|
|---|
| 217 | }
|
|---|
| 218 | ~VectorViewContent(){
|
|---|
| 219 | content=0;
|
|---|
| 220 | }
|
|---|
| 221 | gsl_vector_view view;
|
|---|
| 222 | };
|
|---|
| 223 |
|
|---|
| 224 | #endif /* VECTORCONTENT_HPP_ */
|
|---|