2019-07-06 16:29:50 +02:00
|
|
|
/*
|
|
|
|
https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/spectralnorm.html
|
|
|
|
Added: Pradeep Verghese
|
2019-12-22 00:22:32 +01:00
|
|
|
Benchmarks:
|
2019-07-06 17:13:34 +02:00
|
|
|
Used v -prod spectral.v
|
|
|
|
Command: time ./spectral 5500
|
|
|
|
Output: 1.274224153
|
2019-07-06 16:29:50 +02:00
|
|
|
|
2019-07-06 17:13:34 +02:00
|
|
|
Time: 11.67s user 0.02s system 99% cpu 11.721 total
|
2019-07-06 16:29:50 +02:00
|
|
|
*/
|
|
|
|
module main
|
2019-12-22 00:22:32 +01:00
|
|
|
|
2019-07-06 16:29:50 +02:00
|
|
|
import math
|
|
|
|
import os
|
2020-05-29 06:41:56 +02:00
|
|
|
import strconv
|
2019-07-06 16:29:50 +02:00
|
|
|
|
2020-10-26 12:14:21 +01:00
|
|
|
fn evala(i int, j int) int {
|
2019-12-22 00:22:32 +01:00
|
|
|
return ((i + j) * (i + j + 1) / 2 + i + 1)
|
2019-07-06 16:29:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn times(mut v []f64, u []f64) {
|
2020-10-26 12:14:21 +01:00
|
|
|
for i in 0 .. v.len {
|
2019-12-22 00:22:32 +01:00
|
|
|
mut a := f64(0)
|
2020-10-26 12:14:21 +01:00
|
|
|
for j in 0 .. u.len {
|
2019-12-22 00:22:32 +01:00
|
|
|
a += u[j] / f64(evala(i, j))
|
|
|
|
}
|
|
|
|
v[i] = a
|
|
|
|
}
|
2019-07-06 16:29:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn times_trans(mut v []f64, u []f64) {
|
2020-10-26 12:14:21 +01:00
|
|
|
for i in 0 .. v.len {
|
2019-12-22 00:22:32 +01:00
|
|
|
mut a := f64(0)
|
2020-10-26 12:14:21 +01:00
|
|
|
for j in 0 .. u.len {
|
2019-12-22 00:22:32 +01:00
|
|
|
a += u[j] / f64(evala(j, i))
|
|
|
|
}
|
|
|
|
v[i] = a
|
|
|
|
}
|
2019-07-06 16:29:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn a_times_transp(mut v []f64, u []f64) {
|
2020-10-26 12:14:21 +01:00
|
|
|
mut x := []f64{len: u.len, init: 0}
|
2019-12-22 00:22:32 +01:00
|
|
|
times(mut x, u)
|
|
|
|
times_trans(mut v, x)
|
|
|
|
}
|
2019-07-06 16:29:50 +02:00
|
|
|
|
|
|
|
fn main() {
|
2019-12-22 00:22:32 +01:00
|
|
|
args := os.args
|
2020-05-29 06:41:56 +02:00
|
|
|
mut n := 0
|
2019-12-22 00:22:32 +01:00
|
|
|
if args.len == 2 {
|
2020-05-29 06:41:56 +02:00
|
|
|
n = strconv.atoi(args[1])
|
2020-10-26 12:14:21 +01:00
|
|
|
} else {
|
2019-12-22 00:22:32 +01:00
|
|
|
n = 0
|
|
|
|
}
|
2020-10-26 12:14:21 +01:00
|
|
|
mut u := []f64{len: n, init: 1}
|
|
|
|
mut v := []f64{len: n, init: 1}
|
|
|
|
for _ in 0 .. 10 {
|
2019-12-22 00:22:32 +01:00
|
|
|
a_times_transp(mut v, u)
|
|
|
|
a_times_transp(mut u, v)
|
|
|
|
}
|
|
|
|
mut vbv := f64(0)
|
|
|
|
mut vv := f64(0)
|
2020-10-26 12:14:21 +01:00
|
|
|
for i in 0 .. n {
|
2019-12-22 00:22:32 +01:00
|
|
|
vbv += u[i] * v[i]
|
|
|
|
vv += v[i] * v[i]
|
|
|
|
}
|
|
|
|
ans := math.sqrt(vbv / vv)
|
|
|
|
println('${ans:0.9f}')
|
2019-07-06 16:29:50 +02:00
|
|
|
}
|