Constant Coefficient Polynomial Division (Delphi)

Source: Internet
Author: User

When I recently studied the QR code, I found that polynomial division is required. So I wrote an algorithm to activate my mind.

// Initialize the array and assign a value of 0.

Procedure initarray (var bb: array of double );
VaR
A: integer;
Begin
For a: = 0 to high (bb) Do
Begin
Bb [A]: = 0;
End;
End;

// Polynomial computation.

Procedure countmultiexpress (VAR dxishu1, dxishu2, dxishu3, dxishu4: array of double; amaxmici: integer );
VaR
// Polynomial expression rules: the input is a standard expression f (x) = A0 * x ^ 0 + A1 * x ^ 1 + A2 * x ^ 2 +... + an * x ^ n
// Coefficient from 0-20 memory a0-a20, the power is the corresponding array index
// Dxishu1 is the denominator, dxishu2 is the numerator, and 3 is the quotient 4 is the remainder.
// The calculation here only supports the constant coefficient.
Dxishutmp: array of double;
Dtmp1, dtmp2, dmaxmici, dmaxxishu: Double;
I, j, itmpmici, imaxmici: integer;

Procedure initarray (var bb: array of double );
VaR
A: integer;
Begin
For a: = 0 to high (bb) Do
Begin
Bb [A]: = 0;
End;
End;
Begin
Setlength (dxishutmp, amaxmici );
// Obtain the maximum power and coefficient of a polynomial.
For I: = 20 downto 0 do
Begin
If dxishu2 [I] <> 0.0 then
Begin
Dmaxmici: = I;
Dmaxxishu: = dxishu2 [I];
Break;
End;
End;
// Computing, from high to low
Imaxmici: = trunc (dmaxmici );
For I: = amaxmici downto imaxmici do
Begin
If dxishu1 [I] <> 0.0 then
Begin
Dtmp1: = dxishu1 [I]/dmaxxishu;
Itmpmici: = I-imaxmici;
Dxishu3 [itmpmici]: = dtmp1; // vendor.
Initarray (dxishutmp );
// Numerator multiplication dtmp1
For J: = imaxmici downto 0 do
Begin
Dxishutmp [J + itmpmici]: = dxishu2 [J] * dtmp1;
End;
// Perform the subtraction denominator-dxishutmp
For J: = I downto 0 do
Begin
Dxishu1 [J]: = dxishu1 [J]-dxishutmp [J];
End;
End;
End;
// Remainder
For J: = 0 to amaxmici do
Begin
Dxishu4 [J]: = dxishu1 [J];
End;
Setlength (dxishutmp, 0 );
End;

Usage:

Procedure tform1.button5click (Sender: tobject );
VaR
Dxishu1, dxishu2, dxishu3, dxishu4: array [0 .. 20] of double;
Strout1, strout2: string;
J: integer;
Begin
// Initialization
Initarray (dxishu1 );
Initarray (dxishu2 );
Initarray (dxishu3 );
Initarray (dxishu4 );
// Denominator, molecular value assignment
Dxishu1 [0]: = 7;
Dxishu1 [1]: = 4;
Dxishu1 [2]: = 6;
Dxishu1 [3]: = 0;
Dxishu1 [4]: = 0;
Dxishu1 [5]: = 0;

Dxishu2 [0]: = 3;
Dxishu2 [1]: = 2;
Dxishu2 [2]: = 0;
Dxishu2 [3]: = 0;
Dxishu2 [4]: = 0;
Dxishu2 [5]: = 0;
Countmultiexpress (dxishu1, dxishu2, dxishu3, dxishu4, 20 );
// Output result
Strout1: = '';
Strout2: = '';
For J: = 0 to 20 do
Begin
// Vendor
If dxishu3 [J]> 0.0 then
Begin
Strout2: = '+ floattostr (dxishu3 [J]) + 'x ^' + inttostr (j) + ''+ strout2;
End;
If dxishu3 [J] <0.0 then
Begin
Strout2: = ''+ floattostr (dxishu3 [J]) + 'x ^ '+ inttostr (j) +'' + strout2;
End;
// Remainder
If dxishu4 [J]> 0.0 then
Begin
Strout1: = '+ floattostr (dxishu4 [J]) + 'x ^' + inttostr (j) + ''+ strout1;
End;
If dxishu4 [J] <0.0 then
Begin
Strout1: = ''+ floattostr (dxishu4 [J]) + 'x ^ '+ inttostr (j) +'' + strout1;
End;
End;
Strout1: = trim (strout1 );
Strout2: = trim (strout2 );
If strout1 [1] = '+' then
Strout1: = copy (strout1, 2, length (strout1)-1 );
If strout2 [1] = '+' then
Strout2: = copy (strout2, 2, length (strout2)-1 );
Memo3.lines. Clear ();
Memo3.lines. append ('business: '+ strout2 );
Memo3.lines. append ('remainder: '+ strout1 );

End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.