github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/test/bench/shootout/spectral-norm.go (about)

     1  /*
     2  Redistribution and use in source and binary forms, with or without
     3  modification, are permitted provided that the following conditions are met:
     4  
     5      * Redistributions of source code must retain the above copyright
     6      notice, this list of conditions and the following disclaimer.
     7  
     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.
    11  
    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.
    16  
    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.
    28  */
    29  
    30  /* The Computer Language Benchmarks Game
    31   * http://shootout.alioth.debian.org/
    32   *
    33   * contributed by The Go Authors.
    34   * Based on spectral-norm.c by Sebastien Loisel
    35   */
    36  
    37  package main
    38  
    39  import (
    40  	"flag"
    41  	"fmt"
    42  	"math"
    43  )
    44  
    45  var n = flag.Int("n", 2000, "count")
    46  
    47  func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
    48  
    49  type Vec []float64
    50  
    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]
    56  		}
    57  	}
    58  }
    59  
    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]
    65  		}
    66  	}
    67  }
    68  
    69  func (v Vec) ATimesTransp(u Vec) {
    70  	x := make(Vec, len(u))
    71  	x.Times(u)
    72  	v.TimesTransp(x)
    73  }
    74  
    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
    81  	}
    82  	v := make(Vec, N)
    83  	for i := 0; i < 10; i++ {
    84  		v.ATimesTransp(u)
    85  		u.ATimesTransp(v)
    86  	}
    87  	var vBv, vv float64
    88  	for i := 0; i < N; i++ {
    89  		vBv += u[i] * v[i]
    90  		vv += v[i] * v[i]
    91  	}
    92  	fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv))
    93  }