Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / bench / spectral-norm.go
blob6667f3e04a5e1bc39238d4ff7d603e34c7997e9d
1 /*
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright
6 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
12 * Neither the name of "The Computer Language Benchmarks Game" nor the
13 name of "The Computer Language Shootout Benchmarks" nor the names of
14 its contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
30 /* The Computer Language Benchmarks Game
31 * http://shootout.alioth.debian.org/
33 * contributed by The Go Authors.
34 * Based on spectral-norm.c by Sebastien Loisel
37 package main
39 import (
40 "flag"
41 "fmt"
42 "math"
45 var n = flag.Int("n", 2000, "count")
47 func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
49 type Vec []float64
51 func (v Vec) Times(u Vec) {
52 for i := 0; i < len(v); i++ {
53 v[i] = 0
54 for j := 0; j < len(u); j++ {
55 v[i] += evalA(i, j) * u[j]
60 func (v Vec) TimesTransp(u Vec) {
61 for i := 0; i < len(v); i++ {
62 v[i] = 0
63 for j := 0; j < len(u); j++ {
64 v[i] += evalA(j, i) * u[j]
69 func (v Vec) ATimesTransp(u Vec) {
70 x := make(Vec, len(u))
71 x.Times(u)
72 v.TimesTransp(x)
75 func main() {
76 flag.Parse()
77 N := *n
78 u := make(Vec, N)
79 for i := 0; i < N; i++ {
80 u[i] = 1
82 v := make(Vec, N)
83 for i := 0; i < 10; i++ {
84 v.ATimesTransp(u)
85 u.ATimesTransp(v)
87 var vBv, vv float64
88 for i := 0; i < N; i++ {
89 vBv += u[i] * v[i]
90 vv += v[i] * v[i]
92 fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv))