NTL中对矩阵和向量的一般定义包含在头文件
#include <NTL/ZZ.h>
#include <NTL/vector.h>
using namespace std;
using namespace NTL;
ZZ sum(const Vec<ZZ>& v) //Vec<ZZ>定义一个动态长度数组
{
ZZ acc;
acc = 0;
for (long i = 0; i < v.length(); i++)
acc += v[i];
return acc;
}
例2:打印“回文”向量
#include <NTL/ZZ.h>
#include <NTL/vector.h>
using namespace std;
using namespace NTL;
int main()
{
Vec<ZZ> v;
cin >> v;
long n = v.length();
v.SetLength(2*n);
long i;
for (i = 0 ; i < n; i++)
v[n+i] = v[n-1-i];
cout << v << "\n";
}
例3:矩阵乘法
#include <NTL/ZZ.h>
#include <NTL/matrix.h>
using namespace std;
using namespace NTL;
void mul(Mat<ZZ>& X, const Mat<ZZ>& A, const Mat<ZZ>& B)
{
long n = A.NumRows();
long l = A.NumCols();
long m = B.NumCols();
if (l != B.NumRows())
Error("matrix mul: dimension mismatch");
X.SetDims(n, m); // make X have n rows and m columns
long i, j, k;
ZZ acc, tmp;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
acc = 0;
for(k = 1; k <= l; k++) {
mul(tmp, A(i,k), B(k,j));
add(acc, acc, tmp);
}
X(i,j) = acc;
}
}
}
可以直接使用