Changed TPolynomialRoots.BisectIsolation return type to array
This commit is contained in:
parent
8d4a5c2ed8
commit
7db8f948c5
|
@ -37,6 +37,8 @@ type
|
||||||
|
|
||||||
TIsolatingIntervals = specialize TList<TIsolatingInterval>;
|
TIsolatingIntervals = specialize TList<TIsolatingInterval>;
|
||||||
|
|
||||||
|
TIsolatingIntervalArray = array of TIsolatingInterval;
|
||||||
|
|
||||||
{ TPolynomialRoots }
|
{ TPolynomialRoots }
|
||||||
|
|
||||||
TPolynomialRoots = class
|
TPolynomialRoots = class
|
||||||
|
@ -48,10 +50,10 @@ type
|
||||||
TIsolatingInterval;
|
TIsolatingInterval;
|
||||||
public
|
public
|
||||||
// Returns root-isolating intervals for non-negative, non-multiple roots.
|
// Returns root-isolating intervals for non-negative, non-multiple roots.
|
||||||
class function BisectIsolation(constref APolynomial: TBigIntPolynomial): TIsolatingIntervals;
|
class function BisectIsolation(constref APolynomial: TBigIntPolynomial): TIsolatingIntervalArray;
|
||||||
// Returns root-isolating intervals for non-multiple roots in the interval [0, 2^boundexp].
|
// Returns root-isolating intervals for non-multiple roots in the interval [0, 2^boundexp].
|
||||||
class function BisectIsolation(constref APolynomial: TBigIntPolynomial; constref ABoundExp: Cardinal):
|
class function BisectIsolation(constref APolynomial: TBigIntPolynomial; constref ABoundExp: Cardinal):
|
||||||
TIsolatingIntervals;
|
TIsolatingIntervalArray;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -100,7 +102,7 @@ begin
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TPolynomialRoots.BisectIsolation(constref APolynomial: TBigIntPolynomial): TIsolatingIntervals;
|
class function TPolynomialRoots.BisectIsolation(constref APolynomial: TBigIntPolynomial): TIsolatingIntervalArray;
|
||||||
var
|
var
|
||||||
boundExp: Cardinal;
|
boundExp: Cardinal;
|
||||||
begin
|
begin
|
||||||
|
@ -111,7 +113,7 @@ end;
|
||||||
// This is adapted from
|
// This is adapted from
|
||||||
// https://en.wikipedia.org/wiki/Real-root_isolation#Bisection_method
|
// https://en.wikipedia.org/wiki/Real-root_isolation#Bisection_method
|
||||||
class function TPolynomialRoots.BisectIsolation(constref APolynomial: TBigIntPolynomial; constref ABoundExp: Cardinal):
|
class function TPolynomialRoots.BisectIsolation(constref APolynomial: TBigIntPolynomial; constref ABoundExp: Cardinal):
|
||||||
TIsolatingIntervals;
|
TIsolatingIntervalArray;
|
||||||
type
|
type
|
||||||
TWorkItem = record
|
TWorkItem = record
|
||||||
C, K: Cardinal;
|
C, K: Cardinal;
|
||||||
|
@ -123,8 +125,9 @@ var
|
||||||
stack: TWorkStack;
|
stack: TWorkStack;
|
||||||
n, v: Integer;
|
n, v: Integer;
|
||||||
varq: TBigIntPolynomial;
|
varq: TBigIntPolynomial;
|
||||||
|
iso: TIsolatingIntervals;
|
||||||
begin
|
begin
|
||||||
Result := TIsolatingIntervals.Create;
|
iso := TIsolatingIntervals.Create;
|
||||||
stack := TWorkStack.Create;
|
stack := TWorkStack.Create;
|
||||||
|
|
||||||
item.C := 0;
|
item.C := 0;
|
||||||
|
@ -141,7 +144,7 @@ begin
|
||||||
// Found an integer root at 0.
|
// Found an integer root at 0.
|
||||||
item.P := item.P.DivideByVariable;
|
item.P := item.P.DivideByVariable;
|
||||||
Dec(n);
|
Dec(n);
|
||||||
Result.Add(CreateIsolatingInterval(item.C, item.K, 0, ABoundExp));
|
iso.Add(CreateIsolatingInterval(item.C, item.K, 0, ABoundExp));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
varq := item.P.RevertOrderOfCoefficients.TranslateVariableByOne;
|
varq := item.P.RevertOrderOfCoefficients.TranslateVariableByOne;
|
||||||
|
@ -149,7 +152,7 @@ begin
|
||||||
if v = 1 then
|
if v = 1 then
|
||||||
begin
|
begin
|
||||||
// Found isolating interval.
|
// Found isolating interval.
|
||||||
Result.Add(CreateIsolatingInterval(item.C, item.K, 1, ABoundExp));
|
iso.Add(CreateIsolatingInterval(item.C, item.K, 1, ABoundExp));
|
||||||
end
|
end
|
||||||
else if v > 1 then
|
else if v > 1 then
|
||||||
begin
|
begin
|
||||||
|
@ -164,6 +167,8 @@ begin
|
||||||
stack.Push(item);
|
stack.Push(item);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
Result := iso.ToArray;
|
||||||
|
iso.Free;
|
||||||
stack.Free;
|
stack.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ type
|
||||||
|
|
||||||
TPolynomialRootsTestCase = class(TTestCase)
|
TPolynomialRootsTestCase = class(TTestCase)
|
||||||
private
|
private
|
||||||
procedure AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref AExpectedRoots:
|
procedure AssertBisectIntervals(AIsolatingIntervals: TIsolatingIntervalArray; constref AExpectedRoots:
|
||||||
array of Cardinal);
|
array of Cardinal);
|
||||||
published
|
published
|
||||||
procedure TestBisectNoBound;
|
procedure TestBisectNoBound;
|
||||||
|
@ -41,18 +41,18 @@ implementation
|
||||||
|
|
||||||
{ TPolynomialRootsTestCase }
|
{ TPolynomialRootsTestCase }
|
||||||
|
|
||||||
procedure TPolynomialRootsTestCase.AssertBisectResult(constref AIsolatingIntervals: TIsolatingIntervals; constref
|
procedure TPolynomialRootsTestCase.AssertBisectIntervals(AIsolatingIntervals: TIsolatingIntervalArray;
|
||||||
AExpectedRoots: array of Cardinal);
|
constref AExpectedRoots: array of Cardinal);
|
||||||
var
|
var
|
||||||
exp: Cardinal;
|
exp: Cardinal;
|
||||||
found: Boolean;
|
found: Boolean;
|
||||||
i, foundIndex: Integer;
|
i, foundIndex: Integer;
|
||||||
begin
|
begin
|
||||||
AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), AIsolatingIntervals.Count);
|
AssertEquals('Unexpected number of isolating intervals.', Length(AExpectedRoots), Length(AIsolatingIntervals));
|
||||||
for exp in AExpectedRoots do
|
for exp in AExpectedRoots do
|
||||||
begin
|
begin
|
||||||
found := False;
|
found := False;
|
||||||
for i := 0 to AIsolatingIntervals.Count - 1 do
|
for i := 0 to Length(AIsolatingIntervals) - 1 do
|
||||||
if (AIsolatingIntervals[i].A <= exp) and (exp <= AIsolatingIntervals[i].B) then
|
if (AIsolatingIntervals[i].A <= exp) and (exp <= AIsolatingIntervals[i].B) then
|
||||||
begin
|
begin
|
||||||
found := True;
|
found := True;
|
||||||
|
@ -60,7 +60,7 @@ begin
|
||||||
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);
|
Delete(AIsolatingIntervals, foundIndex, 1);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -69,14 +69,13 @@ const
|
||||||
expRoots: array of Cardinal = (34000, 23017, 5);
|
expRoots: array of Cardinal = (34000, 23017, 5);
|
||||||
var
|
var
|
||||||
a: TBigIntPolynomial;
|
a: TBigIntPolynomial;
|
||||||
r: TIsolatingIntervals;
|
r: TIsolatingIntervalArray;
|
||||||
begin
|
begin
|
||||||
// y = 3 * (x - 34000) * (x - 23017) * (x - 5) * (x^2 - 19) * (x + 112)
|
// 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
|
// = 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]);
|
a := TBigIntPolynomial.Create([-24979889760000, 4774763204640, -1270471872603, 251300082690, 2329429920, -170730, 3]);
|
||||||
r := TPolynomialRoots.BisectIsolation(a);
|
r := TPolynomialRoots.BisectIsolation(a);
|
||||||
AssertBisectResult(r, expRoots);
|
AssertBisectIntervals(r, expRoots);
|
||||||
r.Free;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TPolynomialRootsTestCase.TestBisectWithBound;
|
procedure TPolynomialRootsTestCase.TestBisectWithBound;
|
||||||
|
@ -84,14 +83,13 @@ const
|
||||||
expRoots: array of Cardinal = (23017, 5);
|
expRoots: array of Cardinal = (23017, 5);
|
||||||
var
|
var
|
||||||
a: TBigIntPolynomial;
|
a: TBigIntPolynomial;
|
||||||
r: TIsolatingIntervals;
|
r: TIsolatingIntervalArray;
|
||||||
begin
|
begin
|
||||||
// y = 3 * (x - 34000) * (x - 23017) * (x - 5) * (x^2 - 19) * (x + 112)
|
// 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
|
// = 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]);
|
a := TBigIntPolynomial.Create([-24979889760000, 4774763204640, -1270471872603, 251300082690, 2329429920, -170730, 3]);
|
||||||
r := TPolynomialRoots.BisectIsolation(a, 15);
|
r := TPolynomialRoots.BisectIsolation(a, 15);
|
||||||
AssertBisectResult(r, expRoots);
|
AssertBisectIntervals(r, expRoots);
|
||||||
r.Free;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
|
|
Loading…
Reference in New Issue