Crate iterative_methods[][src]

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 with f(x).

AnnotatedResult

Store a generic annotation next to the state.

Enumerate

An adaptor that enumerates items.

ExtractValue

An adaptor that converts items from WeightedDatum<T> to T.

Numbered

A struct that wraps an Item as Option<Item> and annotates it with an i64. Used by Enumerate.

ReservoirSample

Adaptor to reservoir sample.

StepBy

An iterator for stepping iterators by a custom amount.

TakeUntil

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.

TimedResult

Wrapper for Time.

Weight

Adaptor wrapping items with a computed weight.

WeightedDatum

Wrapper for Weight.

WeightedReservoirSample

Adaptor that reservoir samples with weights

WriteYamlDocuments

Write items of StreamingIterator to a Yaml file.

Enums

UntilState

Traits

YamlDataType

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 with Item = 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.