MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
ba_dense_vector.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann, Fabian Langguth
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef SFM_BA_DENSE_VECTOR_HEADER
11#define SFM_BA_DENSE_VECTOR_HEADER
12
13#include <cmath>
14#include <stdexcept>
15#include <vector>
16
17#include "sfm/defines.h"
18
21
22template <typename T>
24{
25public:
26 DenseVector (void) = default;
27 DenseVector (std::size_t size, T const& value = T(0));
28 void resize (std::size_t size, T const& value = T(0));
29 void clear (void);
30 void fill (T const& value);
31 std::size_t size (void) const;
32
33 T* data (void);
34 T const* data (void) const;
35 T* begin (void);
36 T const* begin (void) const;
37 T* end (void);
38 T const* end (void) const;
39
40 DenseVector operator- (void) const;
41 bool operator== (DenseVector const& rhs) const;
42 T& operator[] (std::size_t index);
43 T const& operator[] (std::size_t index) const;
44 T& at (std::size_t index);
45 T const& at (std::size_t index) const;
46
47 T norm (void) const;
48 T squared_norm (void) const;
49 T dot (DenseVector const& rhs) const;
50 DenseVector add (DenseVector const& rhs) const;
51 DenseVector subtract (DenseVector const& rhs) const;
52 DenseVector multiply (T const& factor) const;
53 void multiply_self (T const& factor);
54 void negate_self (void);
55
56private:
57 std::vector<T> values;
58};
59
60/* ------------------------ Implementation ------------------------ */
61
62template <typename T>
63inline
64DenseVector<T>::DenseVector (std::size_t size, T const& value)
65{
66 this->resize(size, value);
67}
68
69template <typename T>
70inline void
71DenseVector<T>::resize (std::size_t size, T const& value)
72{
73 this->values.clear();
74 this->values.resize(size, value);
75}
76
77template <typename T>
78inline void
80{
81 this->values.clear();
82}
83
84template <typename T>
85inline void
86DenseVector<T>::fill (T const& value)
87{
88 std::fill(this->values.begin(), this->values.end(), value);
89}
90
91template <typename T>
92std::size_t
94{
95 return this->values.size();
96}
97
98template <typename T>
99T*
101{
102 return this->values.data();
103}
104
105template <typename T>
106T const*
108{
109 return this->values.data();
110}
111
112template <typename T>
113T*
115{
116 return this->values.data();
117}
118
119template <typename T>
120T const*
122{
123 return this->values.data();
124}
125
126template <typename T>
127T*
129{
130 return this->values.data() + this->values.size();
131}
132
133template <typename T>
134T const*
136{
137 return this->values.data() + this->values.size();
138}
139
140template <typename T>
143{
144 DenseVector ret(this->size());
145 for (std::size_t i = 0; i < this->size(); ++i)
146 ret[i] = -this->at(i);
147 return ret;
148}
149
150template <typename T>
151bool
153{
154 if (this->size() != rhs.size())
155 return false;
156 for (std::size_t i = 0; i < this->size(); ++i)
157 if (this->at(i) != rhs.at(i))
158 return false;
159 return true;
160}
161
162template <typename T>
163T&
165{
166 return this->values[index];
167}
168
169template <typename T>
170T const&
171DenseVector<T>::operator[] (std::size_t index) const
172{
173 return this->values[index];
174}
175
176template <typename T>
177T&
178DenseVector<T>::at (std::size_t index)
179{
180 return this->values[index];
181}
182
183template <typename T>
184T const&
185DenseVector<T>::at (std::size_t index) const
186{
187 return this->values[index];
188}
189
190template <typename T>
191inline T
193{
194 return std::sqrt(this->squared_norm());
195}
196
197template <typename T>
198T
200{
201 return this->dot(*this);
202}
203
204template <typename T>
205T
207{
208 if (this->size() != rhs.size())
209 throw std::invalid_argument("Incompatible vector dimensions");
210
211 T ret(0);
212 for (std::size_t i = 0; i < this->size(); ++i)
213 ret += this->values[i] * rhs.values[i];
214 return ret;
215}
216
217template <typename T>
220{
221 if (this->size() != rhs.size())
222 throw std::invalid_argument("Incompatible vector dimensions");
223
224 DenseVector<T> ret(this->size(), T(0));
225 for (std::size_t i = 0; i < this->size(); ++i)
226 ret.values[i] = this->values[i] - rhs.values[i];
227 return ret;
228}
229
230template <typename T>
233{
234 if (this->size() != rhs.size())
235 throw std::invalid_argument("Incompatible vector dimensions");
236
237 DenseVector<T> ret(this->size(), T(0));
238 for (std::size_t i = 0; i < this->size(); ++i)
239 ret.values[i] = this->values[i] + rhs.values[i];
240 return ret;
241}
242
243template <typename T>
245DenseVector<T>::multiply (T const& factor) const
246{
247 DenseVector<T> ret(this->size(), T(0));
248 for (std::size_t i = 0; i < this->size(); ++i)
249 ret[i] = this->at(i) * factor;
250 return ret;
251}
252
253template <typename T>
254void
256{
257 for (std::size_t i = 0; i < this->size(); ++i)
258 this->at(i) *= factor;
259}
260
261template <typename T>
262void
264{
265 for (std::size_t i = 0; i < this->size(); ++i)
266 this->at(i) = -this->at(i);
267}
268
271
272#endif // SFM_BA_DENSE_VECTOR_HEADER
DenseVector(void)=default
T & at(std::size_t index)
std::size_t size(void) const
#define SFM_BA_NAMESPACE_BEGIN
Definition defines.h:22
#define SFM_NAMESPACE_END
Definition defines.h:14
#define SFM_NAMESPACE_BEGIN
Definition defines.h:13
#define SFM_BA_NAMESPACE_END
Definition defines.h:23