Loading...
Searching...
No Matches
EigenHelpers.h
Go to the documentation of this file.
1#ifndef DATA_TYPES_EIGENHELPERS_H_
2#define DATA_TYPES_EIGENHELPERS_H_
3
4#include <Eigen/Core>
5#include <cmath>
6
7#include "../backend/Macros.h"
8
9namespace gpr {
10
11// ============================================================
12// Matrix mutation helpers (namespace gpr::field)
13// ============================================================
14namespace field {
15
16template <typename Derived>
17inline void appendRows(Eigen::PlainObjectBase<Derived>& mat,
18 const Eigen::PlainObjectBase<Derived>& other)
19{
20 if (other.rows() == 0) return;
21 if (mat.size() == 0) {
22 mat = other;
23 return;
24 }
25 const Eigen::Index oldRows = mat.rows();
26 mat.conservativeResize(oldRows + other.rows(), mat.cols());
27 mat.bottomRows(other.rows()) = other;
28}
29
30template <typename Derived>
31inline void appendRow(Eigen::PlainObjectBase<Derived>& mat,
32 const Eigen::PlainObjectBase<Derived>& other,
33 Eigen::Index i)
34{
35 if (mat.size() == 0) {
36 mat.resize(1, other.cols());
37 mat.row(0) = other.row(i);
38 return;
39 }
40 const Eigen::Index oldRows = mat.rows();
41 mat.conservativeResize(oldRows + 1, mat.cols());
42 mat.row(oldRows) = other.row(i);
43}
44
45template <typename Derived>
46inline void appendScalarAsRow(Eigen::PlainObjectBase<Derived>& mat,
47 typename Derived::Scalar value)
48{
49 const Eigen::Index oldRows = mat.rows();
50 mat.conservativeResize(oldRows + 1, mat.cols());
51 mat.row(oldRows).setConstant(value);
52}
53
54template <typename Derived>
55inline void deleteRow(Eigen::PlainObjectBase<Derived>& mat, Eigen::Index i)
56{
57 if (mat.rows() == 0) return;
58 const Eigen::Index rows = mat.rows();
59 const Eigen::Index cols = mat.cols();
60 if (i < rows - 1) {
61 mat.block(i, 0, rows - i - 1, cols) =
62 mat.block(i + 1, 0, rows - i - 1, cols);
63 }
64 mat.conservativeResize(rows - 1, cols);
65}
66
67template <typename Derived>
68inline void deleteColumn(Eigen::PlainObjectBase<Derived>& mat, Eigen::Index j)
69{
70 if (mat.cols() == 0) return;
71 const Eigen::Index rows = mat.rows();
72 const Eigen::Index cols = mat.cols();
73 if (j < cols - 1) {
74 mat.block(0, j, rows, cols - j - 1) =
75 mat.block(0, j + 1, rows, cols - j - 1);
76 }
77 mat.conservativeResize(rows, cols - 1);
78}
79
80template <typename Derived>
81inline void appendVector(Eigen::PlainObjectBase<Derived>& mat,
82 const Eigen::VectorXd& vec)
83{
84 if (mat.size() == 0) {
85 mat.resize(1, vec.size());
86 for (Eigen::Index n = 0; n < vec.size(); ++n)
87 mat(0, n) = vec(n);
88 return;
89 }
90 const Eigen::Index oldRows = mat.rows();
91 mat.conservativeResize(oldRows + 1, mat.cols());
92 for (Eigen::Index n = 0; n < mat.cols(); ++n)
93 mat(oldRows, n) = vec(n);
94}
95
98template <typename DerivedA, typename DerivedB>
99inline double flatDot(const Eigen::MatrixBase<DerivedA>& a,
100 const Eigen::MatrixBase<DerivedB>& b)
101{
102 return Eigen::Map<const Eigen::VectorXd>(a.derived().data(), a.size())
103 .dot(Eigen::Map<const Eigen::VectorXd>(b.derived().data(), b.size()));
104}
105
106} // namespace field
107
108// ============================================================
109// Coord-like 3D access helpers (namespace gpr::coord)
110// ============================================================
111namespace coord {
112
113inline Eigen::Vector3d at(const FieldMatrixd& mat, Index_t i)
114{
115 const double* p = mat.data() + 3 * static_cast<Eigen::Index>(i);
116 return Eigen::Vector3d(p[0], p[1], p[2]);
117}
118
119inline Eigen::Vector3d at(const FieldMatrixd& mat, Index_t row, Index_t col)
120{
121 Eigen::Index idx = 3 * static_cast<Eigen::Index>(col) +
122 mat.cols() * static_cast<Eigen::Index>(row);
123 const double* p = mat.data() + idx;
124 return Eigen::Vector3d(p[0], p[1], p[2]);
125}
126
127inline void set(FieldMatrixd& mat, Index_t i, const Eigen::Vector3d& vec)
128{
129 double* p = mat.data() + 3 * static_cast<Eigen::Index>(i);
130 p[0] = vec(0);
131 p[1] = vec(1);
132 p[2] = vec(2);
133}
134
135inline void set(FieldMatrixd& mat, Index_t row, Index_t col,
136 const Eigen::Vector3d& vec)
137{
138 Eigen::Index idx = 3 * static_cast<Eigen::Index>(col) +
139 mat.cols() * static_cast<Eigen::Index>(row);
140 double* p = mat.data() + idx;
141 p[0] = vec(0);
142 p[1] = vec(1);
143 p[2] = vec(2);
144}
145
147{
148 return static_cast<Index_t>(mat.size() / 3);
149}
150
151inline void appendVector3D(FieldMatrixd& mat, const Eigen::Vector3d& vec)
152{
153 Eigen::Index oldCols = mat.cols();
154 Eigen::Index ni = mat.rows() > 0 ? mat.rows() : 1;
155 Eigen::Index nj = oldCols + 3;
156
157 FieldMatrixd tmp = mat;
158 mat.resize(ni, nj);
159 mat.setZero();
160
161 // Copy old data
162 if (tmp.size() > 0) {
163 mat.block(0, 0, tmp.rows(), tmp.cols()) = tmp;
164 }
165
166 // Write new vector for each row
167 for (Eigen::Index i = 0; i < ni; ++i) {
168 set(mat, static_cast<Index_t>(i), static_cast<Index_t>(oldCols / 3),
169 vec);
170 }
171}
172
174{
175 field::deleteColumn(mat, 3 * static_cast<Eigen::Index>(j) + 2);
176 field::deleteColumn(mat, 3 * static_cast<Eigen::Index>(j) + 1);
177 field::deleteColumn(mat, 3 * static_cast<Eigen::Index>(j));
178}
179
181{
182 double norm = 0.;
183 Index_t npts = numPoints(mat);
184 for (Index_t j = 0; j < npts; ++j) {
185 Eigen::Vector3d v = at(mat, i, j);
186 norm += v.dot(v);
187 }
188 norm = std::sqrt(norm);
189 double norm_rec = 1. / norm;
190 for (Eigen::Index n = 0; n < mat.size(); ++n) {
191 *(mat.data() + n) *= norm_rec;
192 }
193}
194
195} // namespace coord
196
197} // namespace gpr
198
199#endif // DATA_TYPES_EIGENHELPERS_H_
void normalizeRow(FieldMatrixd &mat, Index_t i)
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
void set(FieldMatrixd &mat, Index_t i, const Eigen::Vector3d &vec)
void deleteColumnAt(FieldMatrixd &mat, Index_t j)
void appendVector3D(FieldMatrixd &mat, const Eigen::Vector3d &vec)
Index_t numPoints(const FieldMatrixd &mat)
void appendScalarAsRow(Eigen::PlainObjectBase< Derived > &mat, typename Derived::Scalar value)
double flatDot(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
void deleteColumn(Eigen::PlainObjectBase< Derived > &mat, Eigen::Index j)
void appendRow(Eigen::PlainObjectBase< Derived > &mat, const Eigen::PlainObjectBase< Derived > &other, Eigen::Index i)
void appendRows(Eigen::PlainObjectBase< Derived > &mat, const Eigen::PlainObjectBase< Derived > &other)
void deleteRow(Eigen::PlainObjectBase< Derived > &mat, Eigen::Index i)
void appendVector(Eigen::PlainObjectBase< Derived > &mat, const Eigen::VectorXd &vec)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25