2013-06-16 22:54:08 +02:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-05 21:11:25 -07:00
|
|
|
// xfail-test reading from os::args()[1] - bogus!
|
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
use std::from_str::FromStr;
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::os;
|
|
|
|
use std::vec;
|
2012-12-10 17:32:48 -08:00
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
#[inline]
|
|
|
|
fn A(i: i32, j: i32) -> i32 {
|
|
|
|
(i+j) * (i+j+1) / 2 + i + 1
|
|
|
|
}
|
2012-02-08 01:05:53 -08:00
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
fn dot(v: &[f64], u: &[f64]) -> f64 {
|
|
|
|
let mut sum = 0.0;
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, &v_i) in v.iter().enumerate() {
|
2013-04-16 16:11:16 -07:00
|
|
|
sum += v_i * u[i];
|
|
|
|
}
|
|
|
|
sum
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
fn mult_Av(v: &mut [f64], out: &mut [f64]) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, out_i) in out.mut_iter().enumerate() {
|
2013-04-16 16:11:16 -07:00
|
|
|
let mut sum = 0.0;
|
2013-08-03 12:45:23 -04:00
|
|
|
for (j, &v_j) in v.mut_iter().enumerate() {
|
2013-04-16 16:11:16 -07:00
|
|
|
sum += v_j / (A(i as i32, j as i32) as f64);
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
2013-04-16 16:11:16 -07:00
|
|
|
*out_i = sum;
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
fn mult_Atv(v: &mut [f64], out: &mut [f64]) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, out_i) in out.mut_iter().enumerate() {
|
2013-04-16 16:11:16 -07:00
|
|
|
let mut sum = 0.0;
|
2013-08-03 12:45:23 -04:00
|
|
|
for (j, &v_j) in v.mut_iter().enumerate() {
|
2013-04-16 16:11:16 -07:00
|
|
|
sum += v_j / (A(j as i32, i as i32) as f64);
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
2013-04-16 16:11:16 -07:00
|
|
|
*out_i = sum;
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
fn mult_AtAv(v: &mut [f64], out: &mut [f64], tmp: &mut [f64]) {
|
|
|
|
mult_Av(v, tmp);
|
|
|
|
mult_Atv(tmp, out);
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
|
|
|
|
2013-04-16 16:11:16 -07:00
|
|
|
#[fixed_stack_segment]
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
2013-08-04 01:59:24 +02:00
|
|
|
let n: uint = FromStr::from_str(os::args()[1]).unwrap();
|
2013-06-04 21:43:41 -07:00
|
|
|
let mut u = vec::from_elem(n, 1f64);
|
|
|
|
let mut v = u.clone();
|
|
|
|
let mut tmp = u.clone();
|
2013-08-05 23:43:06 -04:00
|
|
|
for _ in range(0, 8u) {
|
2013-04-16 16:11:16 -07:00
|
|
|
mult_AtAv(u, v, tmp);
|
|
|
|
mult_AtAv(v, u, tmp);
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|
|
|
|
|
2013-09-24 22:16:43 -07:00
|
|
|
println!("{:.9f}", (dot(u,v) / dot(v,v)).sqrt() as float);
|
2012-02-08 01:05:53 -08:00
|
|
|
}
|