diff --git a/UPolynomial.pas b/UPolynomial.pas index 5d33937..f4cec85 100644 --- a/UPolynomial.pas +++ b/UPolynomial.pas @@ -46,6 +46,9 @@ type // Returns f(s * x), given a polynomial f(x) and scale factor s. function ScaleVariable(const AScaleFactor: TBigInt): TBigIntPolynomial; + // Returns f(2^n * x), given a polynomial f(x) and an exponent n. + function ScaleVariableByPowerOfTwo(const AExponent: Cardinal): TBigIntPolynomial; + // Returns f(x / 2), given a polynomial f(x). function ScaleVariableByHalf: TBigIntPolynomial; @@ -144,6 +147,21 @@ begin end; end; +function TBigIntPolynomial.ScaleVariableByPowerOfTwo(const AExponent: Cardinal): TBigIntPolynomial; +var + len, i: Integer; + shift: Cardinal; +begin + len := Length(FCoefficients); + SetLength(Result.FCoefficients, len); + Result.FCoefficients[0] := FCoefficients[0]; + shift := AExponent; + for i := 1 to len - 1 do begin + Result.FCoefficients[i] := FCoefficients[i] << shift; + Inc(shift, AExponent); + end; +end; + function TBigIntPolynomial.ScaleVariableByHalf: TBigIntPolynomial; var len, i: Integer; diff --git a/tests/UPolynomialTestCases.pas b/tests/UPolynomialTestCases.pas index e260f97..28c92dc 100644 --- a/tests/UPolynomialTestCases.pas +++ b/tests/UPolynomialTestCases.pas @@ -42,6 +42,7 @@ type procedure TestSignVariations; procedure TestScaleByPowerOfTwo; procedure TestScaleVariable; + procedure TestScaleVariableByPowerOfTwo; procedure TestTranslateVariableByOne; procedure TestRevertOrderOfCoefficients; procedure TestDivideByVariable; @@ -143,6 +144,15 @@ begin AssertTrue('Polynomials are not equal.', a = b); end; +procedure TBigIntPolynomialTestCase.TestScaleVariableByPowerOfTwo; +var + a, b: TBigIntPolynomial; +begin + a := TBigIntPolynomial.Create([10, 7, 5, 1034]).ScaleVariableByPowerOfTwo(5); + b := TBigIntPolynomial.Create([10, 7 * 32, 5 * 32 * 32, 1034 * 32 * 32 * 32]); + AssertTrue('Polynomials are not equal.', a = b); +end; + procedure TBigIntPolynomialTestCase.TestTranslateVariableByOne; var a, b: TBigIntPolynomial;