Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 71 lines (60 sloc) 1.46 kB
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
1 // Based on spectalnorm.gcc by Sebastien Loisel
2
3 use std;
4
5 fn eval_A(i: uint, j: uint) -> float {
6 1.0/(((i+j)*(i+j+1u)/2u+i+1u) as float)
7 }
8
9 fn eval_A_times_u(u: [const float], Au: [mutable float]) {
10 let N = vec::len(u);
11 let i = 0u;
12 while i < N {
13 Au[i] = 0.0;
14 let j = 0u;
15 while j < N {
16 Au[i] += eval_A(i, j) * u[j];
17 j += 1u;
18 }
19 i += 1u;
20 }
21 }
22
23 fn eval_At_times_u(u: [const float], Au: [mutable float]) {
24 let N = vec::len(u);
25 let i = 0u;
26 while i < N {
27 Au[i] = 0.0;
28 let j = 0u;
29 while j < N {
30 Au[i] += eval_A(j, i) * u[j];
31 j += 1u;
32 }
33 i += 1u;
34 }
35 }
36
37 fn eval_AtA_times_u(u: [const float], AtAu: [mutable float]) {
cd72b1f @brson Overhaul constructor naming in libs
brson authored
38 let v = vec::to_mut(vec::from_elem(vec::len(u), 0.0));
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
39 eval_A_times_u(u, v);
40 eval_At_times_u(v, AtAu);
41 }
42
43 fn main(args: [str]) {
44
45 let N = if vec::len(args) == 2u {
ffd50b9 @marijnh Make the various from_str functions return options
marijnh authored
46 option::get(uint::from_str(args[1]))
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
47 } else {
48 1000u
49 };
50
cd72b1f @brson Overhaul constructor naming in libs
brson authored
51 let u = vec::to_mut(vec::from_elem(N, 1.0));
52 let v = vec::to_mut(vec::from_elem(N, 0.0));
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
53 let i = 0u;
54 while i < 10u {
55 eval_AtA_times_u(u, v);
56 eval_AtA_times_u(v, u);
57 i += 1u;
58 }
59
60 let vBv = 0.0;
61 let vv = 0.0;
62 let i = 0u;
63 while i < N {
64 vBv += u[i] * v[i];
65 vv += v[i] * v[i];
66 i += 1u;
67 }
68
6f5853f @graydon Libc/os/run/rand/io reorganization. Close #1373. Close #1638.
graydon authored
69 io::println(#fmt("%0.9f\n", float::sqrt(vBv / vv)));
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
70 }
Something went wrong with that request. Please try again.