1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

Added Benchmark-games: spectralnorm

This commit is contained in:
pverghese 2019-07-06 19:59:50 +05:30 committed by Alexander Medvednikov
parent 59796a4168
commit 758267254d

73
examples/spectral.v Normal file
View File

@ -0,0 +1,73 @@
/*
https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/spectralnorm.html
Added: Pradeep Verghese
*/
module main
import math
import os
fn evala(i, j int) int {
return ((i+j)*(i+j+1)/2 + i + 1)
}
fn (v mut []f64) times(u []f64) {
for i := 0; i < v.len; i++ {
mut a := f64(0)
for j :=0; j< u.len; j++ {
a += u[j] /f64(evala(i,j))
}
v[i] = a
}
}
fn (v mut []f64) times_trans(u []f64) {
for i := 0; i< v.len; i++ {
mut a := f64(0)
for j :=0; j< u.len; j++ {
a += u[j] / f64(evala(j,i))
}
v[i] = a
}
}
fn (v mut []f64) a_times_transp(u []f64) {
mut x := [f64(0); u.len]
x.times(u)
v.times_trans(x)
}
fn main() {
args := os.args
mut n := int(0)
if args.len == 2 {
n = args[1].int()
}
else {
n = 0
}
mut u := [f64(1.0);n]
mut v := [f64(1.0);n]
for i := 0; i< 10; i++ {
v.a_times_transp(u)
u.a_times_transp(v)
}
mut vbv := f64(0)
mut vv := f64(0)
for i :=0; i< n; i++ {
vbv += u[i] * v[i]
vv += v[i] * v[i]
}
ans := math.sqrt(vbv/vv)
println('${ans:0.9f}')
}