Skip to content

Commit

Permalink
Wrap adaptive simpson more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeletWithoutEgg committed Feb 4, 2024
1 parent 1ca7d51 commit c23f0fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions codes/Math/AdaptiveSimpson.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
llf simp(llf l, llf r) {
llf m = (l + r) / 2;
return (f(l) + f(r) + 4.0 * f(m)) * (r - l) / 6.0;
llf integrate(auto &&f, llf L, llf R) {
auto simp = [&](llf l, llf r) {
llf m = (l + r) / 2;
return (f(l) + f(r) + 4.0 * f(m)) * (r - l) / 6.0;
};
auto F = [&](auto Y, llf l, llf r, llf v, llf eps) {
llf m = (l+r)/2, vl = simp(l, m), vr = simp(m, r);
if (abs(vl + vr - v) <= 15 * eps)
return vl + vr + (vl + vr - v) / 15.0;
return Y(Y, l, m, vl, eps / 2.0) +
Y(Y, m, r, vr, eps / 2.0);
};
return F(F, L, R, simp(L, R), 1e-6);
}
llf F(llf L, llf R, llf v, llf eps) {
llf M = (L + R) / 2, vl = simp(L, M), vr = simp(M, R);
if (abs(vl + vr - v) <= 15 * eps)
return vl + vr + (vl + vr - v) / 15.0;
return F(L, M, vl, eps / 2.0) +
F(M, R, vr, eps / 2.0);
} // call F(l, r, simp(l, r), 1e-6)
2 changes: 1 addition & 1 deletion docs/Math.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Passed [Red and Black Tree](https://codeforces.com/contest/375/submission/243194
Simpson integration method.
Unknown time complexity.
### Test Status
Passed [Two Cylinders](https://codeforces.com/problemsets/acmsguru/submission/99999/227216042)
Passed [Two Cylinders](https://codeforces.com/problemsets/acmsguru/submission/99999/244862663).

## Golden Ratio Search
### Description
Expand Down

0 comments on commit c23f0fc

Please sign in to comment.