-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve names and documentation (#50)
* Link tutorial from top level doc * Add top level 1d gd example and link to blog. * Rename WdIterable to Weight and similarly other structs. * Also make WriteToFile private for now.
- Loading branch information
1 parent
f442997
commit a3372da
Showing
9 changed files
with
229 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "iterative_methods" | ||
version = "0.2.0" | ||
version = "0.2.1" | ||
authors = ["Daniel Vainsencher <[email protected]>", "Daniel Fox <[email protected]>"] | ||
edition = "2018" | ||
description = "Iterative methods and associated utilities as StreamingIterators." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! Example from top level crate documentation | ||
use iterative_methods::derivative_descent::*; | ||
use iterative_methods::*; | ||
use streaming_iterator::*; | ||
|
||
fn main() { | ||
// 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() | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Library code for example from crate top-level documentation | ||
use super::*; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct DerivativeDescent<V, D> | ||
where | ||
V: Fn(f64) -> f64, | ||
D: Fn(f64) -> f64, | ||
{ | ||
pub value: V, | ||
pub derivative: D, | ||
pub step_size: f64, | ||
pub x: f64, | ||
} | ||
|
||
impl<V, D> DerivativeDescent<V, D> | ||
where | ||
V: Fn(f64) -> f64, | ||
D: Fn(f64) -> f64, | ||
{ | ||
pub fn new(value: V, derivative: D, step_size: f64, x_0: f64) -> DerivativeDescent<V, D> { | ||
DerivativeDescent { | ||
value, | ||
derivative, | ||
step_size, | ||
x: x_0, | ||
} | ||
} | ||
|
||
pub fn value(&self) -> f64 { | ||
(&self.value)(self.x) | ||
} | ||
} | ||
|
||
impl<V, D> StreamingIterator for DerivativeDescent<V, D> | ||
where | ||
V: Fn(f64) -> f64, | ||
D: Fn(f64) -> f64, | ||
{ | ||
type Item = DerivativeDescent<V, D>; | ||
fn advance(&mut self) { | ||
self.x -= self.step_size * (self.derivative)(self.x); | ||
} | ||
|
||
fn get(&self) -> Option<&Self::Item> { | ||
Some(&self) | ||
} | ||
} |
Oops, something went wrong.