Expand description
§Iterative methods
Implements iterative methods and utilities for using and developing them as StreamingIterators. A series of blog posts provide a gentle introduction.
… but ok fine, here is a really quick example:
// Problem: minimize the convex parabola f(x) = x^2 + x
let function = |x| x * x + x;
// An iterative solution by gradient descent
let derivative = |x| 2.0 * x + 1.0;
let step_size = 0.2;
let x_0 = 2.0;
// Au naturale:
let mut x = x_0;
for i in 0..10 {
x -= step_size * derivative(x);
println!("x_{} = {:.2}; f(x_{}) = {:.4}", i, x, i, x * x + x);
}
// Using replaceable components:
let dd = DerivativeDescent::new(function, derivative, step_size, x_0);
let dd = enumerate(dd);
let mut dd = dd.take(10);
while let Some(&Numbered{item: Some(ref curr), count}) = dd.next() {
println!("x_{} = {:.2}; f(x_{}) = {:.4}", count, curr.x, count, curr.value());
}
Both produce the exact same output (below), and the first common approach is much easier to look at, the descent step is right there. The second separates the algorithm and every other concern into an easily reusable and composable components. If that sounds useful, have fun exploring.
ⓘ
x_0 = 1.00; f(x_0) = 2.0000
x_1 = 0.40; f(x_1) = 0.5600
x_2 = 0.04; f(x_2) = 0.0416
x_3 = -0.18; f(x_3) = -0.1450
x_4 = -0.31; f(x_4) = -0.2122
x_5 = -0.38; f(x_5) = -0.2364
x_6 = -0.43; f(x_6) = -0.2451
x_7 = -0.46; f(x_7) = -0.2482
x_8 = -0.47; f(x_8) = -0.2494
x_9 = -0.48; f(x_9) = -0.2498
Modules§
- algorithms
- conjugate_
gradient - Implementation of conjugate gradient following lecture notes by Shen. Thanks Shen!
- derivative_
descent - Library code for example from crate top-level documentation
- utils
Structs§
- Annotate
- An adaptor that annotates every underlying item
x
withf(x)
. - Annotated
Result - Store a generic annotation next to the state.
- Enumerate
- An adaptor that enumerates items.
- Extract
Value - An adaptor that converts items from
WeightedDatum<T>
toT
. - Numbered
- A struct that wraps an
Item
asOption<Item>
and annotates it with ani64
. Used byEnumerate
. - Reservoir
Sample - Adaptor to reservoir sample.
- StepBy
- An iterator for stepping iterators by a custom amount.
- Take
Until - An adaptor that returns initial elements until and including the first satisfying a predicate.
- Time
- Adaptor that times every call to
advance
on adaptee. Stores start time and duration. - Timed
Result - Wrapper for Time.
- Weight
- Adaptor wrapping items with a computed weight.
- Weighted
Datum - Wrapper for Weight.
- Weighted
Reservoir Sample - Adaptor that reservoir samples with weights
- Write
Yaml Documents - Write items of StreamingIterator to a Yaml file.
Enums§
Traits§
- Yaml
Data Type - Define a trait object for converting to YAML objects.
Functions§
- assess
- Annotate every underlying item with its score, as defined by
f
. - enumerate
- A constructor for Enumerate.
- extract_
value - The constructor for ExtractValue. Apply it to a StreamingIterator with
Item = WeightedDatum<T>
and it returns a StreamingIterator withItem = T
. - inspect
- Apply
f(_)->()
to every underlying item (for side-effects). - last
- Get the item before the first None, assuming any exist.
- new_
datum - Constructor for WeightedDatum.
- reservoir_
sample - An adaptor for which the items are random samples of the underlying iterator up to the item processed. The constructor for ReservoirSample.
- step_by
- Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
- take_
until - Creates an iterator which returns initial elements until and including the first satisfying a predicate.
- time
- Wrap each value of a streaming iterator with the durations:
- wd_
iterable - Annotates items of an iterable with a weight using a function
f
. - weighted_
reservoir_ sample - Create a random sample of the underlying weighted stream.
- write_
yaml_ documents - Adaptor that writes each item to a YAML document.
- write_
yaml_ object - Function used by WriteYamlDocuments to specify how to write each item to file.