Skip to content

Commit 9e809cb

Browse files
vgneculaTolisChal
authored andcommitted
update_position complexity improvement
1 parent cb55d25 commit 9e809cb

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

include/random_walks/gaussian_hamiltonian_monte_carlo_exact_walk.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,22 @@ private :
226226

227227
inline void update_position(Point &p, Point &v, NT const& T, NT const& omega)
228228
{
229-
NT C, Phi;
230-
for (size_t i = 0; i < p.dimension(); i++)
229+
NT next_p, next_v;
230+
231+
NT sinVal = std::sin(omega * T);
232+
NT cosVal = std::cos(omega * T);
233+
234+
NT factor1 = sinVal / omega;
235+
NT factor2 = -omega * sinVal;
236+
237+
for (size_t i = 0; i < p.dimension(); i++)
231238
{
232-
C = std::sqrt(p[i] * p[i] + (v[i] * v[i]) / (omega * omega));
233-
Phi = std::atan((-v[i]) / (p[i] * omega));
234-
if (v[i] < 0.0 && Phi < 0.0) {
235-
Phi += M_PI;
236-
} else if (v[i] > 0.0 && Phi > 0.0) {
237-
Phi -= M_PI;
238-
}
239-
p.set_coord(i, C * std::cos(omega * T + Phi));
240-
v.set_coord(i, -C * omega * std::sin(omega * T + Phi));
241-
}
239+
next_p = cosVal * p[i] + v[i] * factor1;
240+
next_v = factor2 * p[i] + cosVal * v[i];
242241

242+
p.set_coord(i, next_p);
243+
v.set_coord(i, next_v);
244+
}
243245
}
244246

245247
inline double get_max_distance(std::vector<Point> &pointset, Point const& q, double &rad)

0 commit comments

Comments
 (0)