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

3G开发

开发平台:

Visual C++

  1. // poly_pf.cpp
  2. //
  3. #include <iostream>
  4. #include <fstream>
  5. #include "poly_xf.h"
  6. #include "pfelem.h"
  7. #include "stdlib.h"
  8. extern ofstream DebugFile;
  9. PolyOvrExtenField::PolyOvrExtenField( void )
  10. {
  11.   
  12.   Exten_Field = NULL;
  13.   Degree=0;
  14.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  15.   Coeff_Xf[0] = ExtenFieldElem(Exten_Field,0); 
  16. }
  17. PolyOvrExtenField::PolyOvrExtenField(GaloisField *ext_field)
  18. {
  19.   
  20.   Exten_Field = ext_field;
  21.   Degree=0;
  22.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  23.   Coeff_Xf[0] = ExtenFieldElem(Exten_Field,0); 
  24. }
  25. //======================================
  26. PolyOvrExtenField::PolyOvrExtenField( GaloisField *ext_field,
  27.                                       int degree)
  28. {
  29.   int i;
  30.   Exten_Field = ext_field;
  31.   Degree = degree;
  32.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  33.   for(i=0; i<=Degree; i++)
  34.       Coeff_Xf[i] = ExtenFieldElem(Exten_Field,0);
  35. }
  36. //======================================
  37. PolyOvrExtenField::PolyOvrExtenField( GaloisField *ext_field,
  38.                                       int degree, 
  39.                                       ExtenFieldElem coeff)
  40. {
  41.   int i;
  42.   Exten_Field = ext_field;
  43.   Degree = degree;
  44.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  45.   for(i=0; i<=Degree; i++)
  46.       Coeff_Xf[i] = ExtenFieldElem(Exten_Field,0);
  47.   Coeff_Xf[degree] = coeff;
  48.   Coeff_Xf[0] = ExtenFieldElem(Exten_Field,1);
  49. }
  50. //======================================
  51. PolyOvrExtenField::PolyOvrExtenField( GaloisField *ext_field,
  52.                                       ExtenFieldElem coeff)
  53. {
  54.   Exten_Field = ext_field;
  55.   Degree = 1;
  56.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  57.   Coeff_Xf[0] = coeff;
  58.   Coeff_Xf[1] = ExtenFieldElem(Exten_Field,0);
  59. }
  60. #if 0
  61. //============================================================
  62. PolyOvrExtenField::PolyOvrExtenField( GaloisField *ext_field,
  63.                                       PolyOvrIntegers *poly)
  64. {
  65.   int i, raw_val;
  66.   Exten_Field = ext_field;
  67.   Degree = poly->MaxDegree();
  68.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  69.   for(i=0; i<=Degree; i++)
  70.     {
  71.     raw_val = (Prime_Base+(poly->Coefficient(i)))%Prime_Base;
  72.     Coeff_Xf[i] = PrimeFieldElem(Prime_Base,raw_val);
  73.     }
  74. }
  75. #endif
  76. #if 0
  77. //============================================================
  78. PolyOvrExtenField::PolyOvrExtenField( int prime_base,
  79.                                       int degree,
  80.                                       rowvec_pf *coeff)
  81. {
  82.   int i;
  83.   Prime_Base = prime_base;
  84.   Degree = degree;
  85.   Coeff_Xf = new PrimeFieldElem[Degree+1];
  86.   for(i=0; i<=Degree; i++)
  87.     {
  88.     Coeff_Xf[i] = ((*coeff)[i]);
  89.     }
  90. }
  91. //============================================================
  92. PolyOvrExtenField::PolyOvrExtenField( int prime_base,
  93.                                       rowvec_pf *coeff)
  94. {
  95.   int i;
  96.   int len = coeff->getlen();
  97.   Prime_Base = prime_base;
  98.   Degree = 0;
  99.   for(i=0; i<len; i++)
  100.     {
  101.     if( (*coeff)[i].Value != 0 ) Degree=i;
  102.     }
  103.   Coeff_Xf = new PrimeFieldElem[Degree+1];
  104.   for(i=0; i<=Degree; i++)
  105.     {
  106.     Coeff_Xf[i] = (*coeff)[i];
  107.     }
  108. }
  109. #endif
  110. //============================================================
  111. //int PolyOvrExtenField::PrimeBase(void)
  112. //{
  113. //  return Prime_Base;
  114. //}
  115. //============================================================
  116. int PolyOvrExtenField::MaxDegree(void)
  117. {
  118.   return Degree;
  119. }
  120. //========================================================
  121. int PolyOvrExtenField::NumberOfTerms(void)
  122. {
  123.   int num_terms=0;
  124.   for(int i=0; i<=Degree; i++)
  125.     {
  126.     if( Coeff_Xf[i].Value != 0 ) num_terms++;
  127.     }
  128.   return(num_terms);
  129. }
  130. //========================================================
  131. int PolyOvrExtenField::PenultimateDegree(void)
  132. {
  133.   int pen_deg=0;
  134.   for(int i=Degree-1; i>=0; i--)
  135.     {
  136.     if( Coeff_Xf[i].Value == 0 ) continue;
  137.       pen_deg = i;
  138.       break;
  139.     }
  140.   return(pen_deg);
  141. }
  142. //========================================================
  143. ExtenFieldElem PolyOvrExtenField::Coefficient(int degree)
  144. {
  145.   return Coeff_Xf[degree];
  146. }
  147. //========================================================
  148. //void PolyOvrExtenField::GetCoeffs( ExtenFieldElem *coeff_vec)
  149. //{
  150. //  for(int i=0; i<=Degree; i++)
  151. //    {
  152. //    coeff_vec[i] = ExtenFieldElem(Exten_Field, Coeff_Xf[i].Value); 
  153. //    }
  154. //}
  155. //===========================================================
  156. PolyOvrExtenField& PolyOvrExtenField::operator* 
  157.                               (const PolyOvrExtenField &right)
  158. {
  159.   PolyOvrExtenField *result;
  160.   ExtenFieldElem work;
  161.   result = new PolyOvrExtenField( Exten_Field, 
  162.                                   Degree + right.Degree );
  163.   //--------------------------------
  164.   // perform multiplication
  165.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++){
  166.     for( int this_idx=0; this_idx <=Degree; this_idx++){
  167.          work = (Coeff_Xf[this_idx] * right.Coeff_Xf[rgt_idx]);
  168.          work = work + result->Coeff_Xf[this_idx+rgt_idx];
  169.          result->Coeff_Xf[this_idx+rgt_idx] = work;
  170.       //result->Coeff_Xf[this_idx+rgt_idx] +=
  171.       //          (Coeff_Xf[this_idx] * right.Coeff_Xf[rgt_idx]);
  172.       }
  173.     }
  174.   return *result;
  175. }
  176. //===========================================================
  177. void PolyOvrExtenField::operator= 
  178.                             (const PolyOvrExtenField &right)
  179. {
  180.   delete[] Coeff_Xf;
  181.   Degree = right.Degree;
  182.   Exten_Field = right.Exten_Field;
  183.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  184.   for(int i=0; i<=Degree; i++)
  185.       Coeff_Xf[i] = right.Coeff_Xf[i];
  186. }
  187. //===========================================================
  188. PolyOvrExtenField& PolyOvrExtenField::operator*= 
  189.                             (const PolyOvrExtenField &right)
  190. {
  191.    ExtenFieldElem work;
  192.   //---------------------------------------------------
  193.   // save pointer to original coefficient array so that
  194.   // this array can be deleted once no longer needed
  195.   ExtenFieldElem *orig_coeff = Coeff_Xf;
  196.   int orig_degree = Degree;
  197.   int i;
  198.   //------------------------------------------------------
  199.   // create new longer array to hold the new coefficients
  200.   Degree += right.Degree;
  201.   Coeff_Xf = new ExtenFieldElem[Degree+1];
  202.   for( i=0; i<=Degree; i++)
  203.       Coeff_Xf[i] = ExtenFieldElem(Exten_Field);
  204.       //Coeff_Xf[i] = ExtenFieldElem(Exten_Field,0);
  205.   //--------------------------------
  206.   // perform multiplication
  207.   for(int rgt_idx=0; rgt_idx<= right.Degree; rgt_idx++)
  208.     {
  209.     for( int orig_idx=0; orig_idx <=orig_degree; orig_idx++){
  210.          work = Coeff_Xf[orig_idx+rgt_idx] + (orig_coeff[orig_idx] * right.Coeff_Xf[rgt_idx]);
  211.       Coeff_Xf[orig_idx+rgt_idx] = work;
  212.       //Coeff_Xf[orig_idx+rgt_idx] +=
  213.       //          (orig_coeff[orig_idx] * right.Coeff_Xf[rgt_idx]);
  214.       }
  215.     }
  216.   //delete []orig_coeff;
  217.   return *this;
  218. }
  219. //===========================================================
  220. PolyOvrExtenField& PolyOvrExtenField::operator-= 
  221.                             (const ExtenFieldElem &const_term)
  222. {
  223.   Coeff_Xf[0] = Coeff_Xf[0] - const_term;
  224.   return *this;
  225. }
  226. #if 0
  227. //===========================================================
  228. PolyOvrExtenField& PolyOvrExtenField::operator/ 
  229.                             (const PolyOvrExtenField &divisor)
  230. {
  231.   int dividend_deg, divisor_deg, j, k;
  232.   PolyOvrExtenField *result;
  233.   dividend_deg = Degree;
  234.   divisor_deg = divisor.Degree;
  235.   if(dividend_deg < divisor_deg)
  236.     {
  237.     // if degree of divisor is larger than degree
  238.     // of the dividend, the quotient is zero
  239.     PolyOvrExtenField *result = new PolyOvrExtenField(Exten_Field,0);
  240.     }
  241.   else
  242.     {    
  243.     // perform "synthetic" division to generate quotient
  244.     ExtenFieldElem *work = new ExtenFieldElem[dividend_deg+1];
  245.     result = new PolyOvrExtenField( Exten_Field,
  246.                                     dividend_deg-divisor_deg);
  247.     for(j=0; j<= dividend_deg; j++)
  248.       {
  249.       work[j] = Coeff_Xf[j];
  250.       }
  251.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  252.       {
  253.       result->Coeff_Xf[k] = work[divisor_deg+k]/divisor.Coeff_Xf[divisor_deg];
  254.       work[divisor_deg+k]=ExtenFieldElem(Exten_Field,0);
  255.       for(j=divisor_deg+k-1; j>=k; j--)
  256.         work[j] = work[j] - (result->Coeff_Xf[k]) * divisor.Coeff_Xf[j-k];
  257.       }
  258.     result->Degree = dividend_deg-divisor_deg;
  259.     delete[] work;
  260.     }
  261.   return *result;
  262. }
  263. #endif
  264. #if 0
  265. //===========================================================
  266. PolyOvrExtenField& PolyOvrExtenField::operator% 
  267.                             (const PolyOvrExtenField &divisor)
  268. {
  269.   int dividend_deg, divisor_deg, j, k;
  270.   PolyOvrExtenField *result;
  271.   dividend_deg = Degree;
  272.   divisor_deg = divisor.Degree;
  273.   if(dividend_deg < divisor_deg)
  274.     {
  275.     // if degree of divisor is larger than degree of the
  276.     // dividend, the remainder is equal to the dividend
  277.     PolyOvrExtenField *result = new PolyOvrExtenField(Exten_Field,0);
  278.     *result = *this;
  279.     DebugFile << "Error - dividend smaller than divisor" << endl;
  280.     exit(0);
  281.     }
  282.   else
  283.     {    
  284.     // perform "synthetic" division to genrate remainder
  285.     ExtenFieldElem *work = new ExtenFieldElem[dividend_deg+1];
  286.     ExtenFieldElem *quotient = new ExtenFieldElem[dividend_deg-divisor_deg+1];
  287.     for(j=0; j<= dividend_deg; j++)
  288.       {
  289.       work[j] = Coeff_Xf[j];
  290.       }
  291.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  292.       {
  293.       quotient[k] = work[divisor_deg+k]/divisor.Coeff_Xf[divisor_deg];
  294.       work[divisor_deg+k]=ExtenFieldElem(Exten_Field,0);
  295.       for(j=divisor_deg+k-1; j>=k; j--)
  296.         work[j] = work[j] - quotient[k] * divisor.Coeff_Xf[j-k];
  297.       }
  298.     //result->Degree = dividend_deg-divisor_deg;
  299.     int rem_deg = 0;
  300.     for(j=0; j<=dividend_deg ; j++)
  301.       {
  302.       if(!(work[j].EqualsZero())) rem_deg = j;
  303.       }
  304.     result = new PolyOvrExtenField( Exten_Field, rem_deg);
  305.     for(j=0; j<=rem_deg; j++)
  306.       {
  307.       result->Coeff_Xf[j]=work[j];
  308.       }
  309.     delete[] work;
  310.     delete[] quotient;
  311.     }
  312.   return *result;
  313. }
  314. #endif
  315. #if 0
  316. //============================================================
  317. PolyOvrExtenField& PolyOvrExtenField::operator/= 
  318.                             (const PolyOvrExtenField &divisor)
  319. {
  320.   int dividend_deg, divisor_deg, j, k;
  321.   //------------------------------------------
  322.   //
  323.   dividend_deg = Degree;
  324.   divisor_deg = divisor.Degree;
  325.   if(dividend_deg < divisor_deg)
  326.     {
  327.     // if degree of divisor is larger than degree
  328.     // of the dividend, the quotient is zero,
  329.     // and the remainder is equal to the dividend
  330.     Degree = 0;
  331.     Coeff_Xf[0] = ExtenFieldElem(Exten_Field, 0);
  332.     }
  333.   else
  334.     {    
  335.     // perform "synthetic" division to generate
  336.     // quotient and remainder
  337.     ExtenFieldElem *work = new ExtenFieldElem[dividend_deg+1];
  338.     for(j=0; j<= dividend_deg; j++)
  339.       {
  340.       work[j] = Coeff_Xf[j];
  341.       Coeff_Xf[j] = ExtenFieldElem(Exten_Field,0);
  342.       }
  343.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  344.       {
  345.       Coeff_Xf[k] = work[divisor_deg+k]/divisor.Coeff_Xf[divisor_deg];
  346.       work[divisor_deg+k]=ExtenFieldElem(Exten_Field,0);
  347.       for(j=divisor_deg+k-1; j>=k; j--)
  348.         work[j] = work[j] - Coeff_Xf[k] * divisor.Coeff_Xf[j-k];
  349.       }
  350.     Degree = dividend_deg-divisor_deg;
  351.     delete[] work;
  352.     }
  353.   return *this;
  354. }
  355. #endif
  356. #if 0
  357. //============================================================
  358. PolyOvrExtenField& PolyOvrExtenField::operator%= 
  359.                             (const PolyOvrExtenField &divisor)
  360. {
  361.   int dividend_deg, divisor_deg, j, k;
  362.   //------------------------------------------
  363.   //
  364.   dividend_deg = Degree;
  365.   divisor_deg = divisor.Degree;
  366.   if(dividend_deg < divisor_deg)
  367.     {
  368.     // if degree of divisor is larger than degree of the
  369.     // dividend, the remainder is equal to the dividend
  370.     }
  371.   else
  372.     {    
  373.     // perform "synthetic" division to generate
  374.     // quotient and remainder
  375.     ExtenFieldElem *work = new ExtenFieldElem[dividend_deg+1];
  376.     ExtenFieldElem *quotient = new ExtenFieldElem[dividend_deg-divisor_deg+1];
  377.     for(j=0; j<= dividend_deg; j++)
  378.       {
  379.       work[j] = Coeff_Xf[j];
  380.       //Coeff[j] = PrimeFieldElem(Prime_Base,0);
  381.       }
  382.     for(k=dividend_deg-divisor_deg; k>=0; k--)
  383.       {
  384.       quotient[k] = work[divisor_deg+k]/divisor.Coeff_Xf[divisor_deg];
  385.       work[divisor_deg+k]=ExtenFieldElem(Exten_Field,0);
  386.       for(j=divisor_deg+k-1; j>=k; j--)
  387.         work[j] = work[j] - quotient[k] * divisor.Coeff_Xf[j-k];
  388.       }
  389.     //Degree = dividend_deg-divisor_deg;
  390.     int rem_deg = 0;
  391.     for(j=0; j<=dividend_deg ; j++)
  392.       {
  393.       if(!(work[j].EqualsZero())) rem_deg = j;
  394.       }
  395.     Degree = rem_deg;
  396.     delete[] Coeff_Xf;
  397.     Coeff_Xf = new ExtenFieldElem[Degree+1];
  398.     for(j=0; j<=rem_deg; j++)
  399.       Coeff_Xf[j] = work[j];
  400.     delete[] work;
  401.     delete[] quotient;
  402.     }
  403.   return *this;
  404. }
  405. #endif
  406. //=========================================================
  407. //  dump polynomial to an output stream
  408. void PolyOvrExtenField::DumpToStream( ostream* output_stream)
  409. {
  410.  (*output_stream) << "Degree = " << Degree << endl;
  411.  
  412.  for(int i=Degree; i>=0; i--)
  413.    {
  414.     (*output_stream) << "Coeff[" << i << "] = " 
  415.                      << Coeff_Xf[i] << endl;
  416.    }
  417.  return;
  418. }
  419. #if 0
  420. //===========================================================
  421. PolyOvrExtenField* Derivative(const PolyOvrExtenField *right)
  422. {
  423.   PolyOvrExtenField *result;
  424.   result = new PolyOvrExtenField( right->Exten_Field, 
  425.                                   right->Degree-1 );
  426.   for(int i=0; i<=right->Degree-1; i++)
  427.     {
  428.     (result->Coeff_Xf)[i] = (i+1)*((right->Coeff_Xf)[i+1]);
  429.     }
  430.   return result;
  431. }
  432. #endif
  433. #if 0
  434. //===========================================================
  435. PolyOvrExtenField& EuclideanAlgorithm( const PolyOvrExtenField *poly1,
  436.                                        const PolyOvrExtenField *poly2)
  437. {
  438.   PolyOvrExtenField r_poly, u_poly, v_poly, *gcf_poly;
  439.   if( (poly1->Degree) > (poly2->Degree) )
  440.     {
  441.     u_poly = *poly1;
  442.     v_poly = *poly2;
  443.     }
  444.   else
  445.     {
  446.     u_poly = *poly2;
  447.     v_poly = *poly1;
  448.     }
  449.   int modulus = u_poly.Prime_Base;
  450.   bool done = false;
  451.   while( !done )
  452.     {
  453.     // calculate remainder of u/v
  454.     r_poly = u_poly % v_poly;
  455.     //DebugFile << "u_poly: " << endl;
  456.     //u_poly.DumpToStream(&DebugFile);
  457.     //DebugFile << "v_poly: " << endl;
  458.     //v_poly.DumpToStream(&DebugFile);
  459.     //DebugFile << "r_poly: " << endl;
  460.     //r_poly.DumpToStream(&DebugFile);
  461.     // examine remainder
  462.     if( r_poly.Degree == 0 )
  463.       {
  464.       done = true;
  465.       if( r_poly.Coeff[0].Value != 0 )
  466.         {
  467.         // set gcf to 1
  468.         v_poly.Degree = 0;
  469.         v_poly.Coeff[0].Value = 1;
  470. //        (v_poly->Coeff[0]).Modulus = modulus;
  471. // this should be okay as is, and it will be OBE when deep-copy
  472. // assignments are implemented
  473.         }
  474.       }
  475.     else
  476.       {
  477.       u_poly = v_poly;
  478.       v_poly = r_poly;
  479.       }
  480.     } // end of while( !done )
  481.   int gcf_deg = v_poly.Degree;
  482.   gcf_poly = new PolyOvrExtenField(modulus,gcf_deg);
  483.   for(int i=0; i<=gcf_deg; i++)
  484.     {
  485.     gcf_poly->Coeff[i] = v_poly.Coeff[i];
  486.     }
  487.   return( *gcf_poly );
  488. }
  489. #endif