...

Text file src/golang.org/x/exp/shootout/spectral-norm.c

Documentation: golang.org/x/exp/shootout

     1// +build ignore
     2
     3/*
     4Redistribution and use in source and binary forms, with or without
     5modification, are permitted provided that the following conditions are met:
     6
     7    * Redistributions of source code must retain the above copyright
     8    notice, this list of conditions and the following disclaimer.
     9
    10    * Redistributions in binary form must reproduce the above copyright
    11    notice, this list of conditions and the following disclaimer in the
    12    documentation and/or other materials provided with the distribution.
    13
    14    * Neither the name of "The Computer Language Benchmarks Game" nor the
    15    name of "The Computer Language Shootout Benchmarks" nor the names of
    16    its contributors may be used to endorse or promote products derived
    17    from this software without specific prior written permission.
    18
    19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    22ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    29POSSIBILITY OF SUCH DAMAGE.
    30*/
    31
    32/* -*- mode: c -*-
    33 *
    34 * The Great Computer Language Shootout
    35 * http://shootout.alioth.debian.org/
    36 *
    37 * Contributed by Sebastien Loisel
    38 */
    39
    40#include <stdio.h>
    41#include <stdlib.h>
    42#include <math.h>
    43
    44double eval_A(int i, int j) { return 1.0/((i+j)*(i+j+1)/2+i+1); }
    45
    46void eval_A_times_u(int N, const double u[], double Au[])
    47{
    48  int i,j;
    49  for(i=0;i<N;i++)
    50    {
    51      Au[i]=0;
    52      for(j=0;j<N;j++) Au[i]+=eval_A(i,j)*u[j];
    53    }
    54}
    55
    56void eval_At_times_u(int N, const double u[], double Au[])
    57{
    58  int i,j;
    59  for(i=0;i<N;i++)
    60    {
    61      Au[i]=0;
    62      for(j=0;j<N;j++) Au[i]+=eval_A(j,i)*u[j];
    63    }
    64}
    65
    66void eval_AtA_times_u(int N, const double u[], double AtAu[])
    67{ double v[N]; eval_A_times_u(N,u,v); eval_At_times_u(N,v,AtAu); }
    68
    69int main(int argc, char *argv[])
    70{
    71  int i;
    72  int N = ((argc == 2) ? atoi(argv[1]) : 2000);
    73  double u[N],v[N],vBv,vv;
    74  for(i=0;i<N;i++) u[i]=1;
    75  for(i=0;i<10;i++)
    76    {
    77      eval_AtA_times_u(N,u,v);
    78      eval_AtA_times_u(N,v,u);
    79    }
    80  vBv=vv=0;
    81  for(i=0;i<N;i++) { vBv+=u[i]*v[i]; vv+=v[i]*v[i]; }
    82  printf("%0.9f\n",sqrt(vBv/vv));
    83  return 0;
    84}

View as plain text