GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
9
namespace
gpr
{
10
11
// ============================================================
12
// Matrix mutation helpers (namespace gpr::field)
13
// ============================================================
14
namespace
field
{
15
16
template
<
typename
Derived>
17
inline
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
30
template
<
typename
Derived>
31
inline
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
45
template
<
typename
Derived>
46
inline
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
54
template
<
typename
Derived>
55
inline
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
67
template
<
typename
Derived>
68
inline
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
80
template
<
typename
Derived>
81
inline
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
98
template
<
typename
DerivedA,
typename
DerivedB>
99
inline
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
// ============================================================
111
namespace
coord
{
112
113
inline
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
119
inline
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
127
inline
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
135
inline
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
146
inline
Index_t
numPoints
(
const
FieldMatrixd
& mat)
147
{
148
return
static_cast<
Index_t
>
(mat.size() / 3);
149
}
150
151
inline
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
173
inline
void
deleteColumnAt
(
FieldMatrixd
& mat,
Index_t
j)
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
180
inline
void
normalizeRow
(
FieldMatrixd
& mat,
Index_t
i)
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_
Macros.h
gpr::coord
Definition
EigenHelpers.h:111
gpr::coord::normalizeRow
void normalizeRow(FieldMatrixd &mat, Index_t i)
Definition
EigenHelpers.h:180
gpr::coord::at
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
Definition
EigenHelpers.h:113
gpr::coord::set
void set(FieldMatrixd &mat, Index_t i, const Eigen::Vector3d &vec)
Definition
EigenHelpers.h:127
gpr::coord::deleteColumnAt
void deleteColumnAt(FieldMatrixd &mat, Index_t j)
Definition
EigenHelpers.h:173
gpr::coord::appendVector3D
void appendVector3D(FieldMatrixd &mat, const Eigen::Vector3d &vec)
Definition
EigenHelpers.h:151
gpr::coord::numPoints
Index_t numPoints(const FieldMatrixd &mat)
Definition
EigenHelpers.h:146
gpr::field
Definition
EigenHelpers.h:14
gpr::field::appendScalarAsRow
void appendScalarAsRow(Eigen::PlainObjectBase< Derived > &mat, typename Derived::Scalar value)
Definition
EigenHelpers.h:46
gpr::field::flatDot
double flatDot(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
Definition
EigenHelpers.h:99
gpr::field::deleteColumn
void deleteColumn(Eigen::PlainObjectBase< Derived > &mat, Eigen::Index j)
Definition
EigenHelpers.h:68
gpr::field::appendRow
void appendRow(Eigen::PlainObjectBase< Derived > &mat, const Eigen::PlainObjectBase< Derived > &other, Eigen::Index i)
Definition
EigenHelpers.h:31
gpr::field::appendRows
void appendRows(Eigen::PlainObjectBase< Derived > &mat, const Eigen::PlainObjectBase< Derived > &other)
Definition
EigenHelpers.h:17
gpr::field::deleteRow
void deleteRow(Eigen::PlainObjectBase< Derived > &mat, Eigen::Index i)
Definition
EigenHelpers.h:55
gpr::field::appendVector
void appendVector(Eigen::PlainObjectBase< Derived > &mat, const Eigen::VectorXd &vec)
Definition
EigenHelpers.h:81
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::Index_t
uint32_t Index_t
Definition
Macros.h:17
gpr::FieldMatrixd
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition
Macros.h:25
data_types
EigenHelpers.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf