]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-spectralnorm.rs
remove `extra::iter`
[rust.git] / src / test / bench / shootout-spectralnorm.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::from_str::FromStr;
12 use std::os;
13 use std::vec;
14
15 #[inline]
16 fn A(i: i32, j: i32) -> i32 {
17     (i+j) * (i+j+1) / 2 + i + 1
18 }
19
20 fn dot(v: &[f64], u: &[f64]) -> f64 {
21     let mut sum = 0.0;
22     for (i, &v_i) in v.iter().enumerate() {
23         sum += v_i * u[i];
24     }
25     sum
26 }
27
28 fn mult_Av(v: &mut [f64], out: &mut [f64]) {
29     for (i, out_i) in out.mut_iter().enumerate() {
30         let mut sum = 0.0;
31         for (j, &v_j) in v.mut_iter().enumerate() {
32             sum += v_j / (A(i as i32, j as i32) as f64);
33         }
34         *out_i = sum;
35     }
36 }
37
38 fn mult_Atv(v: &mut [f64], out: &mut [f64]) {
39     for (i, out_i) in out.mut_iter().enumerate() {
40         let mut sum = 0.0;
41         for (j, &v_j) in v.mut_iter().enumerate() {
42             sum += v_j / (A(j as i32, i as i32) as f64);
43         }
44         *out_i = sum;
45     }
46 }
47
48 fn mult_AtAv(v: &mut [f64], out: &mut [f64], tmp: &mut [f64]) {
49     mult_Av(v, tmp);
50     mult_Atv(tmp, out);
51 }
52
53 #[fixed_stack_segment]
54 fn main() {
55     let n: uint = FromStr::from_str(os::args()[1]).unwrap();
56     let mut u = vec::from_elem(n, 1f64);
57     let mut v = u.clone();
58     let mut tmp = u.clone();
59     for _ in range(0, 8u) {
60         mult_AtAv(u, v, tmp);
61         mult_AtAv(v, u, tmp);
62     }
63
64     printfln!("%.9f", (dot(u,v) / dot(v,v)).sqrt() as float);
65 }