Fixed calculation of root-isolating intervals and tests

This commit is contained in:
Stefan Müller 2024-05-25 02:50:24 +02:00
parent 748964c871
commit ae30889bbb
2 changed files with 9 additions and 7 deletions

View File

@ -28,8 +28,8 @@ type
{ TIsolatingInterval } { TIsolatingInterval }
// Represents an isolating interval of the form [C / 2^K, (C + H) / 2^K] in respect to [0, 1] or [A / 2^K, B / 2^K] in // Represents an isolating interval of the form [C / 2^K, (C + H) / 2^K] in respect to [0, 1] or [A, B] in respect to
// respect to [0, bound], with A = C * bound and B = (C + H) * bound. // [0, bound], with A = C * bound / 2^K and B = (C + H) * bound / 2^K.
TIsolatingInterval = record TIsolatingInterval = record
C, K, H: Cardinal; C, K, H: Cardinal;
Bound, A, B: TBigInt; Bound, A, B: TBigInt;
@ -82,8 +82,8 @@ begin
Result.K := AK; Result.K := AK;
Result.H := AH; Result.H := AH;
Result.Bound := ABound; Result.Bound := ABound;
Result.A := AC * ABound; Result.A := (AC * ABound) >> AK;
Result.B := (AC + AH) * ABound; Result.B := ((AC + AH) * ABound) >> AK;
end; end;
function TRootIsolation.Bisect(constref APolynomial: TBigIntPolynomial): TIsolatingIntervals; function TRootIsolation.Bisect(constref APolynomial: TBigIntPolynomial): TIsolatingIntervals;

View File

@ -49,20 +49,22 @@ procedure TPolynomialRootsTestCase.AssertBisectResult(constref AIsolatingInterva
AExpectedRoots: array of Cardinal); AExpectedRoots: array of Cardinal);
var var
exp: Cardinal; exp: Cardinal;
ri: TIsolatingInterval;
found: Boolean; found: Boolean;
i, foundIndex: Integer;
begin begin
AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), AIsolatingIntervals.Count); AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), AIsolatingIntervals.Count);
for exp in AExpectedRoots do for exp in AExpectedRoots do
begin begin
found := False; found := False;
for ri in AIsolatingIntervals do for i := 0 to AIsolatingIntervals.Count - 1 do
if (ri.A <= exp) and (exp <= ri.B) then if (AIsolatingIntervals[i].A <= exp) and (exp <= AIsolatingIntervals[i].B) then
begin begin
found := True; found := True;
foundIndex := i;
Break; Break;
end; end;
AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found); AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found);
AIsolatingIntervals.Delete(foundIndex);
end; end;
end; end;