# spectral-norm Free Pascal program

## source code

```{ The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

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:
64-bit Ubuntu quad core
Free Pascal Compiler version 3.0.4 [2017/10/03] for x86_64

Mon, 26 Nov 2018 18:40:07 GMT

MAKE:
mv spectralnorm.fpascal spectralnorm.pas
/opt/src/fpc-3.0.4/bin/fpc -FuInclude/fpascal -XXs -O4 -Tlinux -Fi Include/fpascal -oFPASCAL_RUN spectralnorm.pas
Warning: Only one source file supported, changing source file to compile from "Include/fpascal" into "spectralnorm.pas"
Free Pascal Compiler version 3.0.4 [2017/10/03] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
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
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
65 lines compiled, 1.1 sec
3 warning(s) issued
mv FPASCAL_RUN spectralnorm.fpascal_run
rm spectralnorm.pas

1.42s to complete and log all make actions

COMMAND LINE:
./spectralnorm.fpascal_run 5500

PROGRAM OUTPUT:
1.274224153
```