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.
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.
11 use std::from_str::FromStr;
16 fn A(i: i32, j: i32) -> i32 {
17 (i+j) * (i+j+1) / 2 + i + 1
20 fn dot(v: &[f64], u: &[f64]) -> f64 {
22 for (i, &v_i) in v.iter().enumerate() {
28 fn mult_Av(v: &mut [f64], out: &mut [f64]) {
29 for (i, out_i) in out.mut_iter().enumerate() {
31 for (j, &v_j) in v.mut_iter().enumerate() {
32 sum += v_j / (A(i as i32, j as i32) as f64);
38 fn mult_Atv(v: &mut [f64], out: &mut [f64]) {
39 for (i, out_i) in out.mut_iter().enumerate() {
41 for (j, &v_j) in v.mut_iter().enumerate() {
42 sum += v_j / (A(j as i32, i as i32) as f64);
48 fn mult_AtAv(v: &mut [f64], out: &mut [f64], tmp: &mut [f64]) {
53 #[fixed_stack_segment]
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();
64 printfln!("%.9f", (dot(u,v) / dot(v,v)).sqrt() as float);