From ae30889bbba139ed49a3a848170e1514efcec29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 25 May 2024 02:50:24 +0200 Subject: [PATCH] Fixed calculation of root-isolating intervals and tests --- UPolynomialRoots.pas | 8 ++++---- tests/UPolynomialRootsTestCases.pas | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/UPolynomialRoots.pas b/UPolynomialRoots.pas index b2636f8..3d9fafa 100644 --- a/UPolynomialRoots.pas +++ b/UPolynomialRoots.pas @@ -28,8 +28,8 @@ type { 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 - // respect to [0, bound], with A = C * bound and B = (C + H) * bound. + // 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 + // [0, bound], with A = C * bound / 2^K and B = (C + H) * bound / 2^K. TIsolatingInterval = record C, K, H: Cardinal; Bound, A, B: TBigInt; @@ -82,8 +82,8 @@ begin Result.K := AK; Result.H := AH; Result.Bound := ABound; - Result.A := AC * ABound; - Result.B := (AC + AH) * ABound; + Result.A := (AC * ABound) >> AK; + Result.B := ((AC + AH) * ABound) >> AK; end; function TRootIsolation.Bisect(constref APolynomial: TBigIntPolynomial): TIsolatingIntervals; diff --git a/tests/UPolynomialRootsTestCases.pas b/tests/UPolynomialRootsTestCases.pas index 3ae8ffe..e1d765d 100644 --- a/tests/UPolynomialRootsTestCases.pas +++ b/tests/UPolynomialRootsTestCases.pas @@ -49,20 +49,22 @@ procedure TPolynomialRootsTestCase.AssertBisectResult(constref AIsolatingInterva AExpectedRoots: array of Cardinal); var exp: Cardinal; - ri: TIsolatingInterval; found: Boolean; + i, foundIndex: Integer; begin AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), AIsolatingIntervals.Count); for exp in AExpectedRoots do begin found := False; - for ri in AIsolatingIntervals do - if (ri.A <= exp) and (exp <= ri.B) then + for i := 0 to AIsolatingIntervals.Count - 1 do + if (AIsolatingIntervals[i].A <= exp) and (exp <= AIsolatingIntervals[i].B) then begin found := True; + foundIndex := i; Break; end; AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found); + AIsolatingIntervals.Delete(foundIndex); end; end;