Added bisection root finding algorithm with custom upper bound
This commit is contained in:
@@ -29,18 +29,43 @@ type
|
||||
{ TPolynomialRootsTestCase }
|
||||
|
||||
TPolynomialRootsTestCase = class(TTestCase)
|
||||
private
|
||||
procedure AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref AExpectedRoots:
|
||||
array of Cardinal);
|
||||
protected
|
||||
FRootIsolation: TRootIsolation;
|
||||
procedure SetUp; override;
|
||||
procedure TearDown; override;
|
||||
published
|
||||
procedure TestBisectionRootIsolation;
|
||||
procedure TestBisectNoBound;
|
||||
procedure TestBisectWithBound;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TPolynomialRootsTestCase }
|
||||
|
||||
procedure TPolynomialRootsTestCase.AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref
|
||||
AExpectedRoots: array of Cardinal);
|
||||
var
|
||||
exp: Cardinal;
|
||||
ri: TIsolatingInterval;
|
||||
found: Boolean;
|
||||
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
|
||||
begin
|
||||
found := True;
|
||||
Break;
|
||||
end;
|
||||
AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TPolynomialRootsTestCase.SetUp;
|
||||
begin
|
||||
inherited SetUp;
|
||||
@@ -53,32 +78,34 @@ begin
|
||||
inherited TearDown;
|
||||
end;
|
||||
|
||||
procedure TPolynomialRootsTestCase.TestBisectionRootIsolation;
|
||||
procedure TPolynomialRootsTestCase.TestBisectNoBound;
|
||||
const
|
||||
expRoots: array of Cardinal = (34000, 23017, 5);
|
||||
var
|
||||
exp: Cardinal;
|
||||
a: TBigIntPolynomial;
|
||||
r: TIsolatingIntervals;
|
||||
ri: TIsolatingInterval;
|
||||
found: Boolean;
|
||||
begin
|
||||
// y = 3 * (x - 34000) * (x - 23017) * (x - 5) * (x^2 - 19) * (x + 112)
|
||||
// = 3 * x^6 - 170730 * x^5 + 2329429920 * x^4 + 251300082690 * x^3 - 1270471872603 * x^2 + 4774763204640 * x - 24979889760000
|
||||
a := TBigIntPolynomial.Create([-24979889760000, 4774763204640, -1270471872603, 251300082690, 2329429920, -170730, 3]);
|
||||
r := FRootIsolation.Bisect(a);
|
||||
AssertEquals(Length(expRoots), r.Count);
|
||||
for exp in expRoots do
|
||||
begin
|
||||
found := False;
|
||||
for ri in r do
|
||||
if (ri.A <= exp) and (exp <= ri.B) then
|
||||
begin
|
||||
found := True;
|
||||
Break;
|
||||
end;
|
||||
AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found);
|
||||
end;
|
||||
AssertBisectResult(r, expRoots);
|
||||
r.Free;
|
||||
end;
|
||||
|
||||
procedure TPolynomialRootsTestCase.TestBisectWithBound;
|
||||
const
|
||||
expRoots: array of Cardinal = (23017, 5);
|
||||
var
|
||||
a: TBigIntPolynomial;
|
||||
r: TIsolatingIntervals;
|
||||
begin
|
||||
// y = 3 * (x - 34000) * (x - 23017) * (x - 5) * (x^2 - 19) * (x + 112)
|
||||
// = 3 * x^6 - 170730 * x^5 + 2329429920 * x^4 + 251300082690 * x^3 - 1270471872603 * x^2 + 4774763204640 * x - 24979889760000
|
||||
a := TBigIntPolynomial.Create([-24979889760000, 4774763204640, -1270471872603, 251300082690, 2329429920, -170730, 3]);
|
||||
r := FRootIsolation.Bisect(a, TBigInt.One << 15);
|
||||
AssertBisectResult(r, expRoots);
|
||||
r.Free;
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
Reference in New Issue
Block a user