FlexELA
Loading...
Searching...
No Matches
svector.h
1#ifndef SVECTOR_H
2#define SVECTOR_H
3
4#include <vector>
5
6#include "element.h"
7
9namespace svec {
10
11class NormalizedSVector;
12
25class SVector {
26 public:
27 // Constructors
28
33 SVector() = default;
34
40 SVector(const Element& elm);
41
50 SVector(const Element* const buff);
51
52 // Get Functions
53
59 Value sum() const;
60
67 std::size_t NNZ() const noexcept;
68
75 bool isEmpty() const noexcept;
76
83
90
99
106 const Element* data() const noexcept;
107
114 bool containsNaN() const;
115
123 std::vector<Element>::const_iterator begin() const noexcept;
124
132 std::vector<Element>::const_iterator end() const noexcept;
133
134 // Modifiers
135
147 void add(const SVector& a, const Value& C = 1.0);
148
161 void add(const NormalizedSVector& a, const Value& C = 1.0);
162
170 void normalize(const Value& total = 1.0);
171
180 void chop(const Value& ref = 0.0);
181
192 void zeroEntry(const Label& l);
193
202 void clear() noexcept;
203
204 friend SVector fma(const SVector& a, const Value& C, const SVector& b);
205 friend SVector operator/(const SVector& a, const Value& C);
206 friend SVector operator*(const SVector& a, const Value& C);
207
208 private:
209 std::vector<Element> vec;
210};
211
228 public:
234 {
235 factor = 0;
236 };
237
244 NormalizedSVector(const SVector& a, const Value& total = 1.0);
245
255 inline operator SVector() const
256 {
257 return base * factor;
258 }
259
266 inline void clear() noexcept
267 {
268 base.clear();
269 factor = 0;
270 }
271
272 friend void SVector::add(const NormalizedSVector& a, const Value& C);
273
274 private:
275 SVector base;
276 Value factor;
277};
278
292SVector fma(const SVector& a, const Value& C, const SVector& b);
293
306SVector operator/(const SVector& a, const Value& C);
307
320SVector operator*(const SVector& a, const Value& C);
321
322inline SVector::SVector(const Element& elm) : vec(1, elm)
323{
324}
325
326inline std::size_t SVector::NNZ() const noexcept
327{
328 return vec.size();
329}
330
331inline bool SVector::isEmpty() const noexcept
332{
333 return vec.empty();
334}
335
336inline const Element* SVector::data() const noexcept
337{
338 return vec.data();
339}
340
341inline std::vector<Element>::const_iterator SVector::begin() const noexcept
342{
343 return vec.cbegin();
344}
345inline std::vector<Element>::const_iterator SVector::end() const noexcept
346{
347 return vec.cend();
348}
349
350inline void SVector::clear() noexcept
351{
352 vec.clear();
353 // vec.shrink_to_fit();
354}
355
356} // namespace svec
357
358#endif
A normalized SVector.
Definition svector.h:227
void clear() noexcept
Clear out all entries.
Definition svector.h:266
NormalizedSVector(const SVector &a, const Value &total=1.0)
Construct an NormalizedSVector from a SVector.
NormalizedSVector()
Construct an empty NormalizedSVector.
Definition svector.h:233
A container for sparse vectors.
Definition svector.h:25
bool containsNaN() const
Check if any elements have a NaN value.
Value getMinValue() const
Get the minimum non-zero entry.
friend SVector operator*(const SVector &a, const Value &C)
Multiplication, a*C.
Label getMaxLabel() const
Get the maximum label.
Value getMaxValue() const
Get the maximum non-zero entry.
bool isEmpty() const noexcept
Check if any non-zero elements.
Definition svector.h:331
friend SVector fma(const SVector &a, const Value &C, const SVector &b)
Fused Multiply and Add, C*a+b.
void clear() noexcept
Clear out all non-zero entries.
Definition svector.h:350
SVector()=default
Construct a new SVector object with no non-zero elements.
Value sum() const
Get the sum of values.
friend SVector operator/(const SVector &a, const Value &C)
Division, a/C.
std::vector< Element >::const_iterator begin() const noexcept
Start of the non-zero elements.
Definition svector.h:341
void zeroEntry(const Label &l)
Set the entry at label l to zero.
void add(const SVector &a, const Value &C=1.0)
Inplace Addition (and Multiplication), s=s+a*C.
const Element * data() const noexcept
Directly access the non-zero elements.
Definition svector.h:336
std::vector< Element >::const_iterator end() const noexcept
End of the non-zero elements.
Definition svector.h:345
void normalize(const Value &total=1.0)
s=s/sum(s) * total
void chop(const Value &ref=0.0)
Remove small elements in s.
std::size_t NNZ() const noexcept
Get the number of non-zero elements.
Definition svector.h:326
SVector(const Element *const buff)
Construct a new SVector object with non-zero elements given in buff.
For sparse vector containers and operations.
Definition element.h:10
double Value
Definition types.h:21
unsigned int Label
Definition types.h:15
Contains the label and value of a vector element.
Definition element.h:23