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