-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVectorModel.cpp
More file actions
91 lines (77 loc) · 1.54 KB
/
Copy pathVectorModel.cpp
File metadata and controls
91 lines (77 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "VectorModel.hpp"
#include <iostream>
#include "EnzymeWrapper.hpp"
VectorModel::VectorModel(size_t n)
: x_(n),
f_(n),
df_dx_(n, n)
{
}
inline double VectorModel::square_scalar(double x)
{
return x * x;
}
void VectorModel::square(std::vector<double>& x, std::vector<double>& y)
{
for (size_t idx = 0; idx < x.size(); ++idx)
{
y[idx] = 0.0;
for (size_t idy = 0; idy <= idx; idy++)
{
y[idx] += this->square_scalar(x[idy]);
}
}
}
void VectorModel::setVariable(std::vector<double> x)
{
for (size_t idx = 0; idx < x.size(); ++idx)
{
x_[idx] = x[idx];
}
}
void VectorModel::evalResidual()
{
square(x_, f_);
}
void VectorModel::evalJacobian()
{
const size_t n = x_.size();
std::vector<double> v(n);
VectorModel d_vector_model(n);
for (size_t idy = 0; idy < n; ++idy)
{
// Elementary vector for Jacobian-vector product
for (size_t idx = 0; idx < n; ++idx)
{
v[idx] = 0.0;
}
v[idy] = 1.0;
d_vector_model.setVariable(v);
// Autodiff
std::vector<double> d_res = __enzyme_fwddiff<VectorModel>(
(std::vector<double>*) wrapper<VectorModel>,
enzyme_dup,
this,
&d_vector_model);
// Store result
for (size_t idx = 0; idx < n; ++idx)
{
df_dx_.setValue(idx, idy, d_res[idx]);
}
}
}
std::vector<double>& VectorModel::getVariable()
{
return x_;
}
std::vector<double>& VectorModel::getResidual()
{
return f_;
}
DenseMatrix& VectorModel::getJacobian()
{
return df_dx_;
}
VectorModel::~VectorModel()
{
}