📄 testdetcholeskyunit.pas
字号:
unit testdetcholeskyunit;
interface
uses Math, Ap, Sysutils, cholesky, spddet;
function TestDetCholesky(Silent : Boolean):Boolean;
function testdetcholeskyunit_test_silent():Boolean;
function testdetcholeskyunit_test():Boolean;
implementation
function TestDetCholesky(Silent : Boolean):Boolean;
var
L : TReal2DArray;
A : TReal2DArray;
N : Integer;
Pass : Integer;
I : Integer;
J : Integer;
MinIJ : Integer;
UpperIn : Boolean;
CR : Boolean;
V : Double;
D : Double;
D2 : Double;
Err : Double;
WF : Boolean;
WasErrors : Boolean;
PassCount : Integer;
MaxN : Integer;
HTask : Integer;
Threshold : Double;
i_ : Integer;
begin
Err := 0;
WF := False;
PassCount := 10;
MaxN := 20;
Threshold := 1000*MachineEpsilon;
WasErrors := False;
//
// Test
//
N:=1;
while N<=MaxN do
begin
SetLength(L, N-1+1, N-1+1);
SetLength(A, N-1+1, N-1+1);
HTask:=0;
while HTask<=1 do
begin
Pass:=1;
while Pass<=PassCount do
begin
UpperIn := HTask=0;
//
// Prepare task:
// * A contains upper (or lower) half of SPD matrix
// * L contains its Cholesky factor (upper or lower)
// * D contains det(A)
//
D := 1;
I:=0;
while I<=N-1 do
begin
J:=I+1;
while J<=N-1 do
begin
L[I,J] := 2*RandomReal-1;
L[J,I] := L[I,J];
Inc(J);
end;
L[I,I] := 1.1+RandomReal;
D := D*Sqr(L[I,I]);
Inc(I);
end;
I:=0;
while I<=N-1 do
begin
J:=0;
while J<=N-1 do
begin
MinIJ := Min(I, J);
V := 0.0;
for i_ := 0 to MinIJ do
begin
V := V + L[I,i_]*L[i_,J];
end;
A[I,J] := V;
Inc(J);
end;
Inc(I);
end;
I:=0;
while I<=N-1 do
begin
J:=0;
while J<=N-1 do
begin
if UpperIn then
begin
if J<I then
begin
L[I,J] := 0;
end;
end
else
begin
if I<J then
begin
L[I,J] := 0;
end;
end;
Inc(J);
end;
Inc(I);
end;
//
// test det(A)
//
D2 := SPDMatrixDet(A, N, UpperIn);
if D2<0 then
begin
WF := True;
Inc(Pass);
Continue;
end;
err := Max(err, AbsReal(D2-D)/D);
//
// test det(cholesky(A))
//
D2 := SPDMatrixCholeskyDet(L, N);
if D2<0 then
begin
WF := True;
Inc(Pass);
Continue;
end;
err := Max(err, AbsReal(D2-D)/D);
Inc(Pass);
end;
Inc(HTask);
end;
Inc(N);
end;
//
// report
//
WasErrors := (Err>Threshold) or WF;
if not Silent then
begin
Write(Format('TESTING SPD DET'#13#10'',[]));
Write(Format('ERROR: %5.4e'#13#10'',[
Err]));
Write(Format('ALWAYS SUCCEDED: ',[]));
if WF then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
if WasErrors then
begin
Write(Format('TEST FAILED'#13#10'',[]));
end
else
begin
Write(Format('TEST PASSED'#13#10'',[]));
end;
Write(Format(''#13#10''#13#10'',[]));
end;
Result := not WasErrors;
end;
(*************************************************************************
Silent unit test
*************************************************************************)
function testdetcholeskyunit_test_silent():Boolean;
begin
Result := TestDetCholesky(True);
end;
(*************************************************************************
Unit test
*************************************************************************)
function testdetcholeskyunit_test():Boolean;
begin
Result := TestDetCholesky(False);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -