source: ThirdParty/LinearAlgebra/src/LinearAlgebra/VectorContent.hpp@ 912a6f

Candidate_v1.7.0 stable
Last change on this file since 912a6f was 912a6f, checked in by Frederik Heber <frederik.heber@…>, 5 years ago

Adapted to boost 1.64.

  • need array_wrapper.hpp when using make_array. Otherwise make_array cannot be found.
  • unit_test has an issue with ::get.
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[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
[c5038e]16#include "CodePatterns/Assert.hpp"
[56f73b]17
[912a6f]18#include <boost/version.hpp>
19#if BOOST_VERSION > 106300
20#include <boost/serialization/array_wrapper.hpp>
21#endif
[29cbe9]22#include <boost/serialization/array.hpp>
23#include <boost/serialization/split_member.hpp>
24
[ce3d2b]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
[9d5ddf]29 * (dumb) object that allows for forward definitions.
[bc8a41]30 *
31 * DO NOT USE OUTSIDE OF VECTOR UNLESS YOU ABSOLUTELY HAVE TO AND KNOW WHAT YOU ARE DOING.
[ce3d2b]32 */
33
[bbf1bd]34#include <iosfwd>
35
[ce3d2b]36#include <gsl/gsl_vector.h>
37
[bf4b9f]38#include "MatrixVector_ops.hpp"
[3da2fb]39
[bbf1bd]40class Vector;
[3da2fb]41class MatrixContent;
[c5038e]42struct VectorViewContent;
43namespace 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 */
55struct VectorDimension {
56 friend struct VectorViewContent;
57public:
58 VectorDimension(size_t _dimension) : dimension(_dimension) {}
59
60 size_t getDimension() const {return dimension;}
61
62private:
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
73private:
74 size_t dimension; // store for internal purposes
75};
[bbf1bd]76
77/** Dummy structure to create a unique signature.
78 *
79 */
[8e9ce1]80struct VectorBaseCase{};
[8e17d6]81
[c5038e]82class VectorContent : public VectorDimension
83{
[bbf1bd]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);
[8e17d6]89
[3da2fb]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
[bbf1bd]94public:
95 explicit VectorContent(size_t _dim);
[8e9ce1]96 VectorContent(VectorBaseCase);
[bbf1bd]97 VectorContent(const VectorContent * const src);
98 VectorContent(const VectorContent & src);
[d2bac9]99 VectorContent(gsl_vector * _src, bool _free_content_on_exit = false);
[e420f9]100 VectorContent(const size_t _dimension, std::istream &inputstream);
[bbf1bd]101 virtual ~VectorContent();
102
[c5038e]103 static size_t preparseVectorDimensions(std::istream &inputstream);
104
[bbf1bd]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;
[c5038e]112 void write(std::ostream &ost) const;
[bbf1bd]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
[3dd9c7]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;
[bbf1bd]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);
[3dd9c7]152 const double operator*(const VectorContent& b) const;
[bbf1bd]153
[29cbe9]154 bool operator!=(const VectorContent &other) const {
155 return !(*this == other);
156 }
157
158private:
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
186public:
[ce3d2b]187 gsl_vector *content;
[bbf1bd]188private:
[abfb12]189 bool free_content_on_exit;
[ce3d2b]190};
[29cbe9]191//BOOST_CLASS_EXPORT_GUID(VectorContent, "VectorContent")
192
[ce3d2b]193
[bbf1bd]194std::ostream & operator << (std::ostream& ost, const VectorContent &m);
195VectorContent const operator*(const VectorContent& a, const double m);
196VectorContent const operator*(const double m, const VectorContent& a);
197VectorContent const operator+(const VectorContent& a, const VectorContent& b);
198VectorContent 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 *
[8e9ce1]204 * We need the above VectorBaseCase here:
[bbf1bd]205 * content, i.e. the gsl_vector, must not be allocated, as it is just a view
[8e9ce1]206 * with an internal gsl_vector_view. Hence, VectorBaseCase is just dummy class
207 * to give the constructor a unique signature.
[bbf1bd]208 */
209struct VectorViewContent : public VectorContent
210{
[8e17d6]211 VectorViewContent(gsl_vector_view _view) :
[8e9ce1]212 VectorContent(VectorBaseCase()),
[8e17d6]213 view(_view)
214 {
[bbf1bd]215 dimension = _view.vector.size;
[8e17d6]216 content=&view.vector;
217 }
[bbf1bd]218 ~VectorViewContent(){
[3dbb9d]219 content=0;
220 }
[8e17d6]221 gsl_vector_view view;
222};
223
[ce3d2b]224#endif /* VECTORCONTENT_HPP_ */
Note: See TracBrowser for help on using the repository browser.