The Computer Language
Benchmarks Game

spectral-norm Pascal Free Pascal program

source code

{ The Computer Language Benchmarks Game
  http://benchmarksgame.alioth.debian.org

  contributed by Ian Osgood
  modified by Vincent Snijders
}

program spectralNorm;


var n,i : integer;
    u,v,tmp : array of double;
    vBv,vv : double;

function A(i,j : integer): double; inline;
begin
  A := 1 / ((i+j)*(i+j+1) div 2 + i+1);
end;

procedure mulAv(var v, Av : array of double);
var i,j : integer;
begin
  for i := low(Av) to high(Av) do
  begin
    Av[i] := 0.0;
    for j := low(v) to high(v) do
      Av[i] := Av[i] + A(i,j) * v[j];
  end;
end;

procedure mulAtv(var v, Atv : array of double);
var i,j : integer;
begin
  for i := low(Atv) to high(Atv) do
  begin
    Atv[i] := 0.0;
    for j := low(v) to high(v) do
      Atv[i] := Atv[i] + A(j,i) * v[j];
  end;
end;

procedure mulAtAv(var v, AtAv : array of double);
begin
  mulAv(v, tmp);
  mulAtv(tmp, AtAv);
end;

begin
  Val(paramstr(1), n, i);
  SetLength(u, n);
  SetLength(v, n);
  SetLength(tmp, n);

  for i := low(u) to high(u) do u[i] := 1.0;

  for i := 1 to 10 do begin mulAtAv(u,v); mulAtAv(v,u) end;

  for i := low(u) to high(u) do
  begin
    vBv := vBv + u[i]*v[i];
    vv  := vv  + v[i]*v[i];
  end;

  writeln(sqrt(vBv/vv):0:9);
end.
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Free Pascal Compiler version 3.0.0 [2015/11/24] for i386


Mon, 30 Nov 2015 00:53:10 GMT

MAKE:
mv spectralnorm.fpascal spectralnorm.pas
/usr/local/src/fpc-3.0.0.i386-linux/bin/fpc -FuInclude/fpascal -XXs -Oppentiumm -Cppentiumm -O3 -Cfsse2  -oFPASCAL_RUN spectralnorm.pas
Free Pascal Compiler version 3.0.0 [2015/11/24] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for i386
Compiling spectralnorm.pas
spectralnorm.pas(60,12) Warning: Variable "vBv" does not seem to be initialized
spectralnorm.pas(61,12) Warning: Variable "vv" does not seem to be initialized
Linking FPASCAL_RUN




/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
65 lines compiled, 0.1 sec
2 warning(s) issued
mv FPASCAL_RUN spectralnorm.fpascal_run
rm spectralnorm.pas
0.13s to complete and log all make actions

COMMAND LINE:
./spectralnorm.fpascal_run 5500

PROGRAM OUTPUT:
1.274224153