source: ThirdParty/LinearAlgebra/src/LinearAlgebra/VectorContent.hpp@ 7a434f

Candidate_v1.7.0 stable
Last change on this file since 7a434f was 4ecb2d, checked in by Frederik Heber <heber@…>, 9 years ago

Moved LinearAlgebra sub-package into ThirdParty folder.

  • needed to adapt location of libLinearAlgebra.la in all Makefile.am's.
  • relinked m4 subfolder, relinked am_doxygen_include.am. Both point to those present in molecuilder parent folder.
  • adapted configure.ac's:
  • Property mode set to 100644
File size: 6.4 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
[29cbe9]18#include <boost/serialization/array.hpp>
19#include <boost/serialization/split_member.hpp>
20
[ce3d2b]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
[9d5ddf]25 * (dumb) object that allows for forward definitions.
[bc8a41]26 *
27 * DO NOT USE OUTSIDE OF VECTOR UNLESS YOU ABSOLUTELY HAVE TO AND KNOW WHAT YOU ARE DOING.
[ce3d2b]28 */
29
[bbf1bd]30#include <iosfwd>
31
[ce3d2b]32#include <gsl/gsl_vector.h>
33
[bf4b9f]34#include "MatrixVector_ops.hpp"
[3da2fb]35
[bbf1bd]36class Vector;
[3da2fb]37class MatrixContent;
[c5038e]38struct VectorViewContent;
39namespace 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 */
51struct VectorDimension {
52 friend struct VectorViewContent;
53public:
54 VectorDimension(size_t _dimension) : dimension(_dimension) {}
55
56 size_t getDimension() const {return dimension;}
57
58private:
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
69private:
70 size_t dimension; // store for internal purposes
71};
[bbf1bd]72
73/** Dummy structure to create a unique signature.
74 *
75 */
[8e9ce1]76struct VectorBaseCase{};
[8e17d6]77
[c5038e]78class VectorContent : public VectorDimension
79{
[bbf1bd]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);
[8e17d6]85
[3da2fb]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
[bbf1bd]90public:
91 explicit VectorContent(size_t _dim);
[8e9ce1]92 VectorContent(VectorBaseCase);
[bbf1bd]93 VectorContent(const VectorContent * const src);
94 VectorContent(const VectorContent & src);
[d2bac9]95 VectorContent(gsl_vector * _src, bool _free_content_on_exit = false);
[e420f9]96 VectorContent(const size_t _dimension, std::istream &inputstream);
[bbf1bd]97 virtual ~VectorContent();
98
[c5038e]99 static size_t preparseVectorDimensions(std::istream &inputstream);
100
[bbf1bd]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;
[c5038e]108 void write(std::ostream &ost) const;
[bbf1bd]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
[3dd9c7]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;
[bbf1bd]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);
[3dd9c7]148 const double operator*(const VectorContent& b) const;
[bbf1bd]149
[29cbe9]150 bool operator!=(const VectorContent &other) const {
151 return !(*this == other);
152 }
153
154private:
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
182public:
[ce3d2b]183 gsl_vector *content;
[bbf1bd]184private:
[abfb12]185 bool free_content_on_exit;
[ce3d2b]186};
[29cbe9]187//BOOST_CLASS_EXPORT_GUID(VectorContent, "VectorContent")
188
[ce3d2b]189
[bbf1bd]190std::ostream & operator << (std::ostream& ost, const VectorContent &m);
191VectorContent const operator*(const VectorContent& a, const double m);
192VectorContent const operator*(const double m, const VectorContent& a);
193VectorContent const operator+(const VectorContent& a, const VectorContent& b);
194VectorContent 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 *
[8e9ce1]200 * We need the above VectorBaseCase here:
[bbf1bd]201 * content, i.e. the gsl_vector, must not be allocated, as it is just a view
[8e9ce1]202 * with an internal gsl_vector_view. Hence, VectorBaseCase is just dummy class
203 * to give the constructor a unique signature.
[bbf1bd]204 */
205struct VectorViewContent : public VectorContent
206{
[8e17d6]207 VectorViewContent(gsl_vector_view _view) :
[8e9ce1]208 VectorContent(VectorBaseCase()),
[8e17d6]209 view(_view)
210 {
[bbf1bd]211 dimension = _view.vector.size;
[8e17d6]212 content=&view.vector;
213 }
[bbf1bd]214 ~VectorViewContent(){
[3dbb9d]215 content=0;
216 }
[8e17d6]217 gsl_vector_view view;
218};
219
[ce3d2b]220#endif /* VECTORCONTENT_HPP_ */
Note: See TracBrowser for help on using the repository browser.