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:以下程序打印出前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";
}
}
使用
来设置或者更改系数:SetCoeff(t,i,-1) //t[i]=-1
;
使用
来读取系数:coeff(f,i) == 0
;