poly_int.cpp
上传用户:jtjnyq9001
上传日期:2014-11-21
资源大小:3974k
文件大小:7k
源码类别:

3G开发

开发平台:

Visual C++

  1. // poly_int.cpp
  2. //
  3. #include <iostream>
  4. using std::ofstream;
  5. #include "poly_int.h"
  6. #include <fstream>
  7. extern ofstream DebugFile;
  8. PolyOvrIntegers::PolyOvrIntegers(void)
  9. {
  10.   Degree=0;
  11.   Coeff = new int[Degree+1];
  12.   Coeff[0] = 0;
  13.   
  14.   Rem_Coeff = NULL;
  15. }
  16. //======================================
  17. PolyOvrIntegers::PolyOvrIntegers( int degree)
  18. {
  19.   int i;
  20.   Degree = degree;
  21.   Coeff = new int[Degree+1];
  22.   for(i=0; i<=Degree; i++)
  23.       Coeff[i] = 0;
  24.   Rem_Degree = 0;
  25.   Rem_Coeff = new int[Rem_Degree+1];
  26.   for(i=0; i<=Rem_Degree; i++)
  27.       Rem_Coeff[i] = 0;
  28. }
  29. //======================================
  30. PolyOvrIntegers::PolyOvrIntegers( int degree, 
  31.                                   int coeff)
  32. {
  33.   int i;
  34.   //cout << "in PolyOvrIntegers ctor(int,int)" << endl;
  35.   Degree = degree;
  36.   Coeff = new int[Degree+1];
  37.   for(i=0; i<=Degree; i++)
  38.       Coeff[i] = 0;
  39.   Coeff[0] = -1;
  40.   Coeff[degree] = coeff;
  41.   Rem_Degree = 0;
  42.   Rem_Coeff = new int[Rem_Degree+1];
  43. }
  44. int PolyOvrIntegers::MaxDegree(void)
  45. {
  46.   return Degree;
  47. }
  48. int PolyOvrIntegers::Coefficient(int degree)
  49. {
  50.   return Coeff[degree];
  51. }
  52. //========================================================
  53. void PolyOvrIntegers::SetRemainder( PolyOvrIntegers* poly )
  54. {
  55.   //DebugFile << "Rem_Coeff = " << Rem_Coeff << endl;
  56.   //cout << "Rem_Coeff = " << Rem_Coeff << endl;
  57.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  58.   //DebugFile << "Rem_Coeff = " << Rem_Coeff << endl;
  59.   //cout << "Rem_Coeff = " << Rem_Coeff << endl;
  60.   Rem_Degree = poly->Degree;
  61.   Rem_Coeff = new int[Rem_Degree+1];
  62.   for(int i=0; i<=Rem_Degree; i++)
  63.     {
  64.     Rem_Coeff[i] = poly->Coeff[i];
  65.     }
  66. }
  67. //========================================================
  68. void PolyOvrIntegers::SetRemainder( int degree, 
  69.                                     int* coeff )
  70. {
  71.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  72.   Rem_Degree = degree;
  73.   Rem_Coeff = new int[Rem_Degree+1];
  74.   for(int i=0; i<=Rem_Degree; i++)
  75.     {
  76.     Rem_Coeff[i] = coeff[i];
  77.     }
  78. }
  79. //===========================================================
  80. PolyOvrIntegers& PolyOvrIntegers::operator* 
  81.                               (const PolyOvrIntegers &right)
  82. {
  83.   PolyOvrIntegers *result;
  84.   result = new PolyOvrIntegers( Degree + right.Degree );
  85.   //--------------------------------
  86.   // perform multiplication
  87.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++)
  88.     {
  89.     for( int this_idx=0; this_idx <=Degree; this_idx++)
  90.       {
  91.       result->Coeff[this_idx+rgt_idx] +=
  92.                 (Coeff[this_idx] * right.Coeff[rgt_idx]);
  93.       }
  94.     }
  95.   return *result;
  96. }
  97. //===========================================================
  98. PolyOvrIntegers& PolyOvrIntegers::operator*= 
  99.                             (const PolyOvrIntegers &right)
  100. {
  101.   //---------------------------------------------------
  102.   // save pointer to original coefficient array so that
  103.   // this array can be deleted once no longer needed
  104.   int *orig_coeff = Coeff;
  105.   int orig_degree = Degree;
  106.   int i;
  107.   //------------------------------------------------------
  108.   // create new longer array to hold the new coefficients
  109.   Degree += right.Degree;
  110.   Coeff = new int[Degree+1];
  111.   for( i=0; i<=Degree; i++)
  112.       Coeff[i] = 0;
  113.   //--------------------------------
  114.   // perform multiplication
  115.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++)
  116.     {
  117.     for( int orig_idx=0; orig_idx <=orig_degree; orig_idx++)
  118.       {
  119.       Coeff[orig_idx+rgt_idx] +=
  120.                 (orig_coeff[orig_idx] * right.Coeff[rgt_idx]);
  121.       }
  122.     }
  123.   //delete []orig_coeff;
  124.   return *this;
  125. }
  126. //===========================================================
  127. PolyOvrIntegers& PolyOvrIntegers::operator/ 
  128.                             (const PolyOvrIntegers &divisor)
  129. {
  130.   int dividend_deg, divisor_deg, j, k;
  131.   PolyOvrIntegers *result;
  132.   dividend_deg = Degree;
  133.   divisor_deg = divisor.Degree;
  134.   int *work = new int[dividend_deg+1];
  135.   if(dividend_deg < divisor_deg)
  136.     {
  137.     // if degree of divisor is larger than degree
  138.     // of the dividend, the quotient is zero,
  139.     // and the remainder is equal to the dividend
  140.     PolyOvrIntegers *result = new PolyOvrIntegers(0);
  141.     result->SetRemainder(this);
  142.     }
  143.   else
  144.     {    
  145.     // perform "synthetic" division to generate
  146.     // quotient and remainder
  147.     result = new PolyOvrIntegers( dividend_deg-divisor_deg);
  148.     for(j=0; j<= dividend_deg; j++)
  149.       {
  150.       work[j] = Coeff[j];
  151.       }
  152.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  153.       {
  154.       result->Coeff[k] = work[divisor_deg+k]/divisor.Coeff[divisor_deg];
  155.       work[divisor_deg+k]=0;
  156.       for(j=divisor_deg+k-1; j>=k; j--)
  157.         work[j] = work[j] - (result->Coeff[k]) * divisor.Coeff[j-k];
  158.       }
  159.     result->Degree = dividend_deg-divisor_deg;
  160.     int rem_deg = 0;
  161.     for(j=0; j=dividend_deg ; j++)
  162.       {
  163.       if(work[j] != 0) rem_deg = j;
  164.       }
  165.     result->SetRemainder(rem_deg, work);
  166.     }
  167.   return *result;
  168. }
  169. //============================================================
  170. PolyOvrIntegers& PolyOvrIntegers::operator/= 
  171.                             (const PolyOvrIntegers &divisor)
  172. {
  173.   int dividend_deg, divisor_deg, j, k;
  174.   //cout << "in PolyOvrIntegers operator/=" << endl;
  175.   //------------------------------------------
  176.   //
  177.   if(Rem_Coeff != NULL) delete[] Rem_Coeff;
  178.   dividend_deg = Degree;
  179.   divisor_deg = divisor.Degree;
  180.   int *work = new int[dividend_deg+1];
  181.   if(dividend_deg < divisor_deg)
  182.     {
  183.     // if degree of divisor is larger than degree
  184.     // of the dividend, the quotient is zero,
  185.     // and the remainder is equal to the dividend
  186.     Rem_Degree = Degree;
  187.     Rem_Coeff = new int[Rem_Degree+1];
  188.     }
  189.   else
  190.     {    
  191.     // perform "synthetic" division to generate
  192.     // quotient and remainder
  193.     for(j=0; j<= dividend_deg; j++)
  194.       {
  195.       work[j] = Coeff[j];
  196.       Coeff[j] = 0;
  197.       }
  198.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  199.       {
  200.       Coeff[k] = work[divisor_deg+k]/divisor.Coeff[divisor_deg];
  201.       work[divisor_deg+k]=0;
  202.       for(j=divisor_deg+k-1; j>=k; j--)
  203.         work[j] = work[j] - Coeff[k] * divisor.Coeff[j-k];
  204.       }
  205.     Degree = dividend_deg-divisor_deg;
  206.     int rem_deg = 0;
  207.     for(j=0; j<=dividend_deg ; j++)
  208.       {
  209.       if(work[j] != 0) rem_deg = j;
  210.       }
  211.     Rem_Degree = rem_deg;
  212.     Rem_Coeff = new int[Rem_Degree+1];
  213.     for(j=0; j<=rem_deg; j++)
  214.       Rem_Coeff[j] = work[j];
  215.     }
  216.   return *this;
  217. }
  218. //=========================================================
  219. //  dump polynomial to an output stream
  220. void PolyOvrIntegers::DumpToStream( ostream* output_stream)
  221. {
  222.    (*output_stream) << "Degree = " << Degree << std::endl;
  223.  
  224.  for(int i=Degree; i>=0; i--)
  225.    {
  226.     (*output_stream) << "Coeff[" << i << "] = " 
  227.        << Coeff[i] << std::endl;
  228.    }
  229.  return;
  230. }
  231. //===========================================================
  232. PolyOvrIntegers* Derivative(const PolyOvrIntegers *right)
  233. {
  234.   PolyOvrIntegers *result;
  235.   result = new PolyOvrIntegers( right->Degree-1 );
  236.   for(int i=0; i<=right->Degree-1; i++)
  237.     {
  238.     (result->Coeff)[i] = (i+1)*((right->Coeff)[i+1]);
  239.     }
  240.   return result;
  241. }