NTL使用教程3(Polynomials)

NTL广泛支持快速多项式运算提供(这是最初NTL与其他代数系统库的区别)。

ZZX类代表具有整数系数的单变量多项式(univariate polynomials)

例1:读取一个多项式,对其进行分解并打印出分解。

#include <NTL/ZZXFactoring.h>

using namespace std;
using namespace NTL;

int main()
{
   ZZX f;

   cin >> f;

   Vec< Pair< ZZX, long > > factors;
   ZZ c;

   factor(c, factors, f);

   cout << c << "\n";
   cout << factors << "\n";
}

执行测试:
输入多项式系数[2 10 14 6],表示多项式 2 + 10 x + 14 x 2 + 6 x 3
输出结果为:
这里写图片描述
则表示分解结果为: 2 ( 1 + 3 x ) ( 1 + x ) 2

关于函数factor的参数定义

P a i r < Z Z X , l o n g > :
类Pair

例2:以下程序打印出前100个分圆多项式(cyclotomic polynomails)
~至于分圆多项式具体是什么就搞不清楚了,看了又忘…查资料,分圆多项式的系数一般只有-1,0,1;但从n=105开始出现2…

#include <NTL/ZZX.h>

using namespace std;
using namespace NTL;

int main()
{
   Vec<ZZX> phi(INIT_SIZE, 100);  
   //改写:Vec<ZZX> phi; phi.SetLength(100);

   for (long i = 1; i <= 100; i++) {
      ZZX t;
      t = 1;

      for (long j = 1; j <= i-1; j++)
         if (i % j == 0)
            t *= phi(j); //mul(t, t, phi(j))

      phi(i) = (ZZX(INIT_MONO, i) - 1)/t;  
      /*改写为:
      ZZX t1; 
      SetCoeff(t1, i);
      SetCoeff(t1, 0, -1);
      div(phi(i), t1, t);
      或者:
      ZZX t1;
      t1.SetLength(i+1); // all vector elements are initialized to zero
      t1[i] = 1;
      t1[0] = -1;
      t1.normalize();  // not necessary here, but good practice in general
      div(phi(i), t1, t);
      */

      cout << phi(i) << "\n";
   }
}

使用 S e t C o e f f 来设置或者更改系数:SetCoeff(t,i,-1) //t[i]=-1
使用 c o e f f 来读取系数:coeff(f,i) == 0

参考资料:
http://www.shoup.net/ntl/doc/tour-ex3.html

猜你喜欢

转载自blog.csdn.net/qi_1221/article/details/79546495