Rob Pike | 8764ebe | 2009-08-05 17:25:38 -0700 | [diff] [blame] | 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 | /* -*- mode: c -*- |
| 31 | * |
| 32 | * The Great Computer Language Shootout |
| 33 | * http://shootout.alioth.debian.org/ |
| 34 | * |
| 35 | * Contributed by Sebastien Loisel |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <math.h> |
| 41 | |
| 42 | double eval_A(int i, int j) { return 1.0/((i+j)*(i+j+1)/2+i+1); } |
| 43 | |
| 44 | void eval_A_times_u(int N, const double u[], double Au[]) |
| 45 | { |
| 46 | int i,j; |
| 47 | for(i=0;i<N;i++) |
| 48 | { |
| 49 | Au[i]=0; |
| 50 | for(j=0;j<N;j++) Au[i]+=eval_A(i,j)*u[j]; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void eval_At_times_u(int N, const double u[], double Au[]) |
| 55 | { |
| 56 | int i,j; |
| 57 | for(i=0;i<N;i++) |
| 58 | { |
| 59 | Au[i]=0; |
| 60 | for(j=0;j<N;j++) Au[i]+=eval_A(j,i)*u[j]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void eval_AtA_times_u(int N, const double u[], double AtAu[]) |
| 65 | { double v[N]; eval_A_times_u(N,u,v); eval_At_times_u(N,v,AtAu); } |
| 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
| 69 | int i; |
| 70 | int N = ((argc == 2) ? atoi(argv[1]) : 2000); |
| 71 | double u[N],v[N],vBv,vv; |
| 72 | for(i=0;i<N;i++) u[i]=1; |
| 73 | for(i=0;i<10;i++) |
| 74 | { |
| 75 | eval_AtA_times_u(N,u,v); |
| 76 | eval_AtA_times_u(N,v,u); |
| 77 | } |
| 78 | vBv=vv=0; |
| 79 | for(i=0;i<N;i++) { vBv+=u[i]*v[i]; vv+=v[i]*v[i]; } |
| 80 | printf("%0.9f\n",sqrt(vBv/vv)); |
| 81 | return 0; |
| 82 | } |