Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 74 lines (63 sloc) 1.578 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// 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)
}

fn eval_A_times_u(u: ~[const float], Au: ~[mut float]) {
    let N = vec::len(u);
    let mut i = 0u;
    while i < N {
        Au[i] = 0.0;
        let mut j = 0u;
        while j < N {
            Au[i] += eval_A(i, j) * u[j];
            j += 1u;
        }
        i += 1u;
    }
}

fn eval_At_times_u(u: ~[const float], Au: ~[mut float]) {
    let N = vec::len(u);
    let mut i = 0u;
    while i < N {
        Au[i] = 0.0;
        let mut j = 0u;
        while j < N {
            Au[i] += eval_A(j, i) * u[j];
            j += 1u;
        }
        i += 1u;
    }
}

fn eval_AtA_times_u(u: ~[const float], AtAu: ~[mut float]) {
    let v = vec::to_mut(vec::from_elem(vec::len(u), 0.0));
    eval_A_times_u(u, v);
    eval_At_times_u(v, AtAu);
}

fn main(args: ~[str]) {
    let args = if os::getenv("RUST_BENCH").is_some() {
        ~["", "2000"]
    } else if args.len() <= 1u {
        ~["", "1000"]
    } else {
        args
    };

    let N = uint::from_str(args[1]).get();

    let u = vec::to_mut(vec::from_elem(N, 1.0));
    let v = vec::to_mut(vec::from_elem(N, 0.0));
    let mut i = 0u;
    while i < 10u {
        eval_AtA_times_u(u, v);
        eval_AtA_times_u(v, u);
        i += 1u;
    }

    let mut vBv = 0.0;
    let mut vv = 0.0;
    let mut i = 0u;
    while i < N {
        vBv += u[i] * v[i];
        vv += v[i] * v[i];
        i += 1u;
    }

    io::println(#fmt("%0.9f\n", float::sqrt(vBv / vv)));
}
Something went wrong with that request. Please try again.