Added polynomial degree and coefficients as public properties

This commit is contained in:
Stefan Müller 2024-04-04 20:24:56 +02:00
parent 71c8462358
commit 0bbae0a83e
2 changed files with 56 additions and 5 deletions

View File

@ -32,7 +32,11 @@ type
TBigIntPolynomial = object TBigIntPolynomial = object
private private
FCoefficients: array of TBigInt; FCoefficients: array of TBigInt;
function GetDegree: Integer;
function GetCoefficient(const AIndex: Integer): TBigInt;
public public
property Degree: Integer read GetDegree;
property Coefficient[const AIndex: Integer]: TBigInt read GetCoefficient;
function CalcValueAt(const AX: Int64): TBigInt; function CalcValueAt(const AX: Int64): TBigInt;
function IsEqualTo(const AOther: TBigIntPolynomial): Boolean; function IsEqualTo(const AOther: TBigIntPolynomial): Boolean;
class function Create(const ACoefficients: array of TBigInt): TBigIntPolynomial; static; class function Create(const ACoefficients: array of TBigInt): TBigIntPolynomial; static;
@ -47,6 +51,16 @@ implementation
{ TBigIntPolynomial } { TBigIntPolynomial }
function TBigIntPolynomial.GetDegree: Integer;
begin
Result := Length(FCoefficients) - 1;
end;
function TBigIntPolynomial.GetCoefficient(const AIndex: Integer): TBigInt;
begin
Result := FCoefficients[AIndex];
end;
function TBigIntPolynomial.CalcValueAt(const AX: Int64): TBigInt; function TBigIntPolynomial.CalcValueAt(const AX: Int64): TBigInt;
var var
i: Integer; i: Integer;
@ -78,17 +92,20 @@ class function TBigIntPolynomial.Create(const ACoefficients: array of TBigInt):
var var
high, i: integer; high, i: integer;
begin begin
high := 1; high := -1;
for i := Length(ACoefficients) - 1 downto 1 do for i := Length(ACoefficients) - 1 downto 0 do
if ACoefficients[i] <> 0 then if ACoefficients[i] <> 0 then
begin begin
high := i; high := i;
Break; Break;
end; end;
if high >= 0 then
begin
SetLength(Result.FCoefficients, high + 1); SetLength(Result.FCoefficients, high + 1);
for i := 0 to high do for i := 0 to high do
Result.FCoefficients[i] := ACoefficients[i]; Result.FCoefficients[i] := ACoefficients[i];
end; end;
end;
{ Operators } { Operators }

View File

@ -29,7 +29,13 @@ type
{ TBigIntPolynomialTestCase } { TBigIntPolynomialTestCase }
TBigIntPolynomialTestCase = class(TTestCase) TBigIntPolynomialTestCase = class(TTestCase)
private
procedure TestCreateWithDegree(const ACoefficients: array of TBigInt; const ADegree: Integer);
published published
procedure TestCreate;
procedure TestCreateDegreeOne;
procedure TestCreateDegreeZero;
procedure TestCreateDegreeZeroTrim;
procedure TestEqual; procedure TestEqual;
procedure TestUnequalSameLength; procedure TestUnequalSameLength;
procedure TestUnequalDifferentLength; procedure TestUnequalDifferentLength;
@ -41,6 +47,34 @@ implementation
{ TBigIntPolynomialTestCase } { TBigIntPolynomialTestCase }
procedure TBigIntPolynomialTestCase.TestCreateWithDegree(const ACoefficients: array of TBigInt; const ADegree: Integer);
var
a: TBigIntPolynomial;
begin
a := TBigIntPolynomial.Create(ACoefficients);
AssertEquals('Degree of created polynomial incorrect.', ADegree, a.Degree);
end;
procedure TBigIntPolynomialTestCase.TestCreate;
begin
TestCreateWithDegree([992123, 7, 20, 4550022], 3);
end;
procedure TBigIntPolynomialTestCase.TestCreateDegreeOne;
begin
TestCreateWithDegree([4007], 0);
end;
procedure TBigIntPolynomialTestCase.TestCreateDegreeZero;
begin
TestCreateWithDegree([], -1);
end;
procedure TBigIntPolynomialTestCase.TestCreateDegreeZeroTrim;
begin
TestCreateWithDegree([0], -1);
end;
procedure TBigIntPolynomialTestCase.TestEqual; procedure TBigIntPolynomialTestCase.TestEqual;
var var
a, b: TBigIntPolynomial; a, b: TBigIntPolynomial;