2012-02-08 01:05:53 -08:00
|
|
|
// Based on spectalnorm.gcc by Sebastien Loisel
|
|
|
|
|
|
|
|
use std;
|
|
|
|
|
|
|
|
fn eval_A(i: uint, j: uint) -> float {
|
|
|
|
1.0/(((i+j)*(i+j+1u)/2u+i+1u) as float)
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:26:56 -07:00
|
|
|
fn eval_A_times_u(u: ~[const float], Au: ~[mut float]) {
|
2012-02-08 01:05:53 -08:00
|
|
|
let N = vec::len(u);
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut i = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while i < N {
|
|
|
|
Au[i] = 0.0;
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut j = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while j < N {
|
|
|
|
Au[i] += eval_A(i, j) * u[j];
|
|
|
|
j += 1u;
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:26:56 -07:00
|
|
|
fn eval_At_times_u(u: ~[const float], Au: ~[mut float]) {
|
2012-02-08 01:05:53 -08:00
|
|
|
let N = vec::len(u);
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut i = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while i < N {
|
|
|
|
Au[i] = 0.0;
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut j = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while j < N {
|
|
|
|
Au[i] += eval_A(j, i) * u[j];
|
|
|
|
j += 1u;
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:26:56 -07:00
|
|
|
fn eval_AtA_times_u(u: ~[const float], AtAu: ~[mut float]) {
|
2012-03-12 15:52:30 -07:00
|
|
|
let v = vec::to_mut(vec::from_elem(vec::len(u), 0.0));
|
2012-02-08 01:05:53 -08:00
|
|
|
eval_A_times_u(u, v);
|
|
|
|
eval_At_times_u(v, AtAu);
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn main(args: ~[~str]) {
|
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"2000"]
|
2012-05-23 22:53:50 -07:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-13 22:57:48 -07:00
|
|
|
~[~"", ~"1000"]
|
2012-02-08 01:05:53 -08:00
|
|
|
} else {
|
2012-05-23 22:53:50 -07:00
|
|
|
args
|
2012-02-08 01:05:53 -08:00
|
|
|
};
|
|
|
|
|
2012-05-23 22:53:50 -07:00
|
|
|
let N = uint::from_str(args[1]).get();
|
|
|
|
|
2012-03-12 15:52:30 -07:00
|
|
|
let u = vec::to_mut(vec::from_elem(N, 1.0));
|
|
|
|
let v = vec::to_mut(vec::from_elem(N, 0.0));
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut i = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while i < 10u {
|
|
|
|
eval_AtA_times_u(u, v);
|
|
|
|
eval_AtA_times_u(v, u);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut vBv = 0.0;
|
|
|
|
let mut vv = 0.0;
|
|
|
|
let mut i = 0u;
|
2012-02-08 01:05:53 -08:00
|
|
|
while i < N {
|
|
|
|
vBv += u[i] * v[i];
|
|
|
|
vv += v[i] * v[i];
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
2012-03-12 20:04:27 -07:00
|
|
|
io::println(#fmt("%0.9f\n", float::sqrt(vBv / vv)));
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|