OGS
EigenVector.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <span>
14#include <vector>
15
16#ifndef NDEBUG
17#include <string>
18#endif
19
20#include <Eigen/Sparse>
21
22namespace MathLib
23{
25class EigenVector final
26{
27public:
28 using RawVectorType = Eigen::VectorXd;
29
30 // The Index type of the Eigen::VectorXd class differs from the
31 // Eigen::SparseMatrix<double> index type. Maybe an Eigen::SparseVector is a
32 // more appropriate RawVectorType for the global vectors.
33 using IndexType = Eigen::SparseMatrix<double>::Index;
34
35 // TODO: preliminary
36 EigenVector() = default;
37
40 explicit EigenVector(IndexType length) : vec_(length) {}
41
43 IndexType size() const { return static_cast<IndexType>(vec_.size()); }
44
46 static constexpr IndexType getRangeBegin() { return 0; }
47
49 IndexType getRangeEnd() const { return size(); }
50
51 // TODO preliminary
52 void setZero() { vec_.setZero(); }
53
55 double const& operator[](IndexType rowId) const { return vec_[rowId]; }
56 double& operator[](IndexType rowId) { return vec_[rowId]; }
57
59 double get(IndexType rowId) const { return vec_[rowId]; }
60
62 std::vector<double> get(std::vector<IndexType> const& indices) const
63 {
64 std::vector<double> local_x;
65 local_x.reserve(indices.size());
66
67 transform(cbegin(indices), cend(indices), back_inserter(local_x),
68 [&](auto const i) { return vec_[i]; });
69
70 return local_x;
71 }
72
74 void set(IndexType rowId, double v) { vec_[rowId] = v; }
75
77 void add(IndexType rowId, double v) { vec_[rowId] += v; }
78
80 template <class T_SUBVEC>
81 void set(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
82 {
83 auto const length = pos.size();
84 for (std::size_t i = 0; i < length; ++i)
85 {
86 set(pos[i], sub_vec[i]);
87 }
88 }
89
91 template <class T_SUBVEC>
92 void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
93 {
94 auto const length = pos.size();
95 for (std::size_t i = 0; i < length; ++i)
96 {
97 add(pos[i], sub_vec[i]);
98 }
99 }
100
104 void copyValues(std::vector<double>& u) const;
105
109 void copyValues(std::span<double> u) const;
110
111#ifndef NDEBUG
113 void write(const std::string& filename) const;
114#endif
115
118
120 const RawVectorType& getRawVector() const { return vec_; }
121
122private:
124};
125
126} // namespace MathLib
Global vector based on Eigen vector.
Definition EigenVector.h:26
IndexType getRangeEnd() const
return an end index of the active data range
Definition EigenVector.h:49
void copyValues(std::vector< double > &u) const
RawVectorType vec_
void add(IndexType rowId, double v)
add entry
Definition EigenVector.h:77
IndexType size() const
return a vector length
Definition EigenVector.h:43
double get(IndexType rowId) const
get entry
Definition EigenVector.h:59
void set(IndexType rowId, double v)
set entry
Definition EigenVector.h:74
void set(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
set entries
Definition EigenVector.h:81
Eigen::VectorXd RawVectorType
Definition EigenVector.h:28
EigenVector(IndexType length)
Definition EigenVector.h:40
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition EigenVector.h:92
const RawVectorType & getRawVector() const
return a raw Eigen vector object
double & operator[](IndexType rowId)
Definition EigenVector.h:56
RawVectorType & getRawVector()
return a raw Eigen vector object
double const & operator[](IndexType rowId) const
access entry
Definition EigenVector.h:55
std::vector< double > get(std::vector< IndexType > const &indices) const
get entries
Definition EigenVector.h:62
Eigen::SparseMatrix< double >::Index IndexType
Definition EigenVector.h:33
void write(const std::string &filename) const
write this vector to a file for debugging
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition EigenVector.h:46
static const double u
static const double v