Skip to content

Instantly share code, notes, and snippets.

/aitken.rs Secret

Created February 15, 2017 15:15
/// Accelerate convergence of a sequance, using Aitken acceleration
fn aitken(v: &Vec<f64>) -> Vec<f64>{
let n = v.len();
let mut v_ = vec![0.0; n-2];
for i in 0..(n-2){
v_[i] = v[i] - (v[i+1]-v[i])*(v[i+1]-v[i])/(v[i+2]-2.0*v[i+1]+v[i])
}
v_
}
/// Create a vector of \sum_{i=1}^{n} 1/i^2
fn invsqrsum(n: i32) -> Vec<f64>{
let mut v = vec![0.0; n as usize];
for idx in 0..n {
let mut cum = 0.0;
for i in 1..idx+2{
// accumurate from small values to reduce errors
cum += 1.0/(((idx+2-i)*(idx+2-i)) as f64)
}
v[idx as usize] = cum
}
v
}
fn print_seq(xs: &Vec<f64>){
let n = xs.len();
for i in 0..(n-1){
print!("{:.6}, ", xs[i])
}
print!("{:.6}", xs[n-1])
}
fn main(){
let mut v = invsqrsum(20);
for i in 0..10{
println!("xs^{}:", i);
print_seq(&v);
println!("");
v = aitken(&v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment