2024-04-04 20:34:57 +02:00
|
|
|
{
|
|
|
|
Solutions to the Advent Of Code.
|
|
|
|
Copyright (C) 2024 Stefan Müller
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
}
|
|
|
|
|
|
|
|
unit UPolynomialRootsTestCases;
|
|
|
|
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, fpcunit, testregistry, UPolynomial, UPolynomialRoots, UBigInt;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TPolynomialRootsTestCase }
|
|
|
|
|
|
|
|
TPolynomialRootsTestCase = class(TTestCase)
|
2024-05-24 20:47:52 +02:00
|
|
|
private
|
|
|
|
procedure AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref AExpectedRoots:
|
|
|
|
array of Cardinal);
|
2024-04-04 20:34:57 +02:00
|
|
|
published
|
2024-05-24 20:47:52 +02:00
|
|
|
procedure TestBisectNoBound;
|
|
|
|
procedure TestBisectWithBound;
|
2024-04-04 20:34:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TPolynomialRootsTestCase }
|
|
|
|
|
2024-05-24 20:47:52 +02:00
|
|
|
procedure TPolynomialRootsTestCase.AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref
|
|
|
|
AExpectedRoots: array of Cardinal);
|
|
|
|
var
|
|
|
|
exp: Cardinal;
|
|
|
|
found: Boolean;
|
2024-05-25 02:50:24 +02:00
|
|
|
i, foundIndex: Integer;
|
2024-05-24 20:47:52 +02:00
|
|
|
begin
|
|
|
|
AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), AIsolatingIntervals.Count);
|
|
|
|
for exp in AExpectedRoots do
|
|
|
|
begin
|
|
|
|
found := False;
|
2024-05-25 02:50:24 +02:00
|
|
|
for i := 0 to AIsolatingIntervals.Count - 1 do
|
|
|
|
if (AIsolatingIntervals[i].A <= exp) and (exp <= AIsolatingIntervals[i].B) then
|
2024-05-24 20:47:52 +02:00
|
|
|
begin
|
|
|
|
found := True;
|
2024-05-25 02:50:24 +02:00
|
|
|
foundIndex := i;
|
2024-05-24 20:47:52 +02:00
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found);
|
2024-05-25 02:50:24 +02:00
|
|
|
AIsolatingIntervals.Delete(foundIndex);
|
2024-05-24 20:47:52 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TPolynomialRootsTestCase.TestBisectNoBound;
|
2024-05-24 20:20:28 +02:00
|
|
|
const
|
|
|
|
expRoots: array of Cardinal = (34000, 23017, 5);
|
2024-04-04 20:34:57 +02:00
|
|
|
var
|
|
|
|
a: TBigIntPolynomial;
|
2024-05-24 20:20:28 +02:00
|
|
|
r: TIsolatingIntervals;
|
2024-04-04 20:34:57 +02:00
|
|
|
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]);
|
2024-05-26 14:23:31 +02:00
|
|
|
r := TPolynomialRoots.BisectIsolation(a);
|
2024-05-24 20:47:52 +02:00
|
|
|
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]);
|
2024-05-26 14:23:31 +02:00
|
|
|
r := TPolynomialRoots.BisectIsolation(a, TBigInt.One << 15);
|
2024-05-24 20:47:52 +02:00
|
|
|
AssertBisectResult(r, expRoots);
|
|
|
|
r.Free;
|
2024-04-04 20:34:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
RegisterTest(TPolynomialRootsTestCase);
|
|
|
|
end.
|
|
|
|
|