Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ca0294

Browse files
committed
PiecewiseLinearConvexFn documentation
1 parent 4c3cddc commit 5ca0294

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Rather than try to persuade you with words, this repository aims to show by exam
5050
- [Number theory](src/math/mod.rs): canonical solution to Bezout's identity, Miller's primality test
5151
- [FFT](src/math/fft.rs): fast Fourier transform, number theoretic transform, convolution
5252
- [Arithmetic](src/math/num.rs): rational and complex numbers, linear algebra, safe modular arithmetic
53-
- [Ordering algorithms](src/order.rs): binary search, mergesort, coordinate compression, amortized convex hull trick
53+
- [Ordering algorithms](src/order.rs): binary search, mergesort, coordinate compression, online convex hull trick
5454
- [Associative range query](src/range_query): static and dynamic ARQ trees (a.k.a. segtrees), Mo's query square root decomposition
5555
- [Scanner](src/scanner.rs): utility for reading input data ergonomically
5656
- [String processing](src/string_proc.rs): Knuth-Morris-Pratt and Aho-Corasick string matching, suffix array, Manacher's linear-time palindrome search

‎src/order.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,22 @@ impl SparseIndex {
6565
}
6666
}
6767

68-
/// Represents a maximum (upper envelope) of a collection of linear functions of a variable,
69-
/// evaluated using the convex hull trick with square root decomposition.
70-
pub struct PiecewiseLinearFn {
68+
/// Represents a maximum (upper envelope) of a collection of linear functions of one
69+
/// variable, evaluated using an online version of the convex hull trick.
70+
/// It combines the offline algorithm with square root decomposition, resulting in an
71+
/// asymptotically suboptimal but simple algorithm with good amortized performnce:
72+
/// For N inserts interleaved with Q queries, a threshold of N/sqrt(Q) yields
73+
/// O(N sqrt Q + Q log N) time complexity. If all queries come after all inserts,
74+
/// any threshold less than N (e.g., 0) yields O(N + Q log N) time complexity.
75+
pub struct PiecewiseLinearConvexFn {
7176
recent_lines: Vec<(f64, f64)>,
7277
sorted_lines: Vec<(f64, f64)>,
7378
intersections: Vec<f64>,
7479
merge_threshold: usize,
7580
}
7681

77-
impl PiecewiseLinearFn {
78-
/// For N inserts interleaved with Q queries, a threshold of N/sqrt(Q) yields
79-
/// O(N sqrt Q + Q log N) time complexity. If all queries come after all inserts,
80-
/// any threshold less than N (e.g., 0) yields O(N + Q log N) time complexity.
82+
impl PiecewiseLinearConvexFn {
83+
/// Initializes with a given threshold for re-running the convex hull algorithm
8184
pub fn with_merge_threshold(merge_threshold: usize) -> Self {
8285
Self {
8386
recent_lines: vec![],
@@ -210,7 +213,7 @@ mod test {
210213
[1, -1, -2, -1, 0, 1],
211214
];
212215
for threshold in 0..=lines.len() {
213-
let mut func = PiecewiseLinearFn::with_merge_threshold(threshold);
216+
let mut func = PiecewiseLinearConvexFn::with_merge_threshold(threshold);
214217
assert_eq!(func.evaluate(0.0), -1e18);
215218
for (&(slope, intercept), expected) in lines.iter().zip(results.iter()) {
216219
func.max_with(slope as f64, intercept as f64);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /