我想在有限域上使用Python插值多项式,并得到具有该域系数的多项式。
目前我正在尝试使用SymPy,特别是插值(来自
sympy.polys.polyfuncs
),但我不知道如何强制插值发生在特定的gf中。如果不行,是否可以使用另一个模块完成此操作?
注:我对Python实现/库很感兴趣。sympy.polys.polyfuncs
),但我不知道如何强制插值发生在特定的gf中。如果不行,是否可以使用另一个模块完成此操作?
注:我对Python实现/库很感兴趣。SymPy的interpolating_poly不支持有限域上的多项式。但是SymPy底层有足够的细节可以组合成一个有限域类,并以一种非常直接的方式找到Lagrange polynomial的系数。
通常,有限域GF(pn)的元素用次数小于n的多项式表示,其系数在GF(p)中。乘法是模n次降幂多项式的余数进行的,该多项式在构建域时选择。倒数是用扩展欧几里得算法完成的。
这些多项式由系数列表表示,最高次数优先。例如,GF(32)的元素为:
[], [1], [2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]
空列表表示0。
实现算术方法包括add
、sub
、mul
、inv
(乘法逆元)。为了方便插值测试,还包括eval_poly
,它在GF(pn)的点上评估给定多项式的系数。
请注意,构造函数使用G(3, 2)而不是G(9),质数及其幂次方分别提供。
import itertools
from functools import reduce
from sympy import symbols, Dummy
from sympy.polys.domains import ZZ
from sympy.polys.galoistools import (gf_irreducible_p, gf_add, \
gf_sub, gf_mul, gf_rem, gf_gcdex)
from sympy.ntheory.primetest import isprime
class GF():
def __init__(self, p, n=1):
p, n = int(p), int(n)
if not isprime(p):
raise ValueError("p must be a prime number, not %s" % p)
if n <= 0:
raise ValueError("n must be a positive integer, not %s" % n)
self.p = p
self.n = n
if n == 1:
self.reducing = [1, 0]
else:
for c in itertools.product(range(p), repeat=n):
poly = (1, *c)
if gf_irreducible_p(poly, p, ZZ):
self.reducing = poly
break
def add(self, x, y):
return gf_add(x, y, self.p, ZZ)
def sub(self, x, y):
return gf_sub(x, y, self.p, ZZ)
def mul(self, x, y):
return gf_rem(gf_mul(x, y, self.p, ZZ), self.reducing, self.p, ZZ)
def inv(self, x):
s, t, h = gf_gcdex(x, self.reducing, self.p, ZZ)
return s
def eval_poly(self, poly, point):
val = []
for c in poly:
val = self.mul(val, point)
val = self.add(val, c)
return val
这个类更简单:它实现了多项式的加减乘法,在系数上使用了底层域。由于SymPy约定以最高幂次开头来列出单项式,所以有很多列表翻转[::-1]
。
class PolyRing():
def __init__(self, field):
self.K = field
def add(self, p, q):
s = [self.K.add(x, y) for x, y in \
itertools.zip_longest(p[::-1], q[::-1], fillvalue=[])]
return s[::-1]
def sub(self, p, q):
s = [self.K.sub(x, y) for x, y in \
itertools.zip_longest(p[::-1], q[::-1], fillvalue=[])]
return s[::-1]
def mul(self, p, q):
if len(p) < len(q):
p, q = q, p
s = [[]]
for j, c in enumerate(q):
s = self.add(s, [self.K.mul(b, c) for b in p] + \
[[]] * (len(q) - j - 1))
return s
对于给定的列表X中的x值和数组Y中相应的y值,构建拉格朗日多项式。它是基础多项式的线性组合,每个元素都有一个基础多项式。每个基础多项式通过乘以(x-x_k)
多项式得到,表示为[[1], K.sub([], x_k)]
。分母是一个标量,因此计算起来更容易。
def interp_poly(X, Y, K):
R = PolyRing(K)
poly = [[]]
for j, y in enumerate(Y):
Xe = X[:j] + X[j+1:]
numer = reduce(lambda p, q: R.mul(p, q), ([[1], K.sub([], x)] for x in Xe))
denom = reduce(lambda x, y: K.mul(x, y), (K.sub(X[j], x) for x in Xe))
poly = R.add(poly, R.mul(numer, [K.mul(y, K.inv(denom))]))
return poly
K = GF(2, 4)
X = [[], [1], [1, 0, 1]] # 0, 1, a^2 + 1
Y = [[1, 0], [1, 0, 0], [1, 0, 0, 0]] # a, a^2, a^3
intpoly = interp_poly(X, Y, K)
pprint(intpoly)
pprint([K.eval_poly(intpoly, x) for x in X]) # same as Y
[[1], [1, 1, 1], [1, 0]]
。为了方便阅读,我添加了一个函数将其转换为更熟悉的形式,其中符号a
是有限域的生成器,x
是多项式中的变量。def readable(poly, a, x):
return Poly(sum((sum((c*a**j for j, c in enumerate(coef[::-1])), S.Zero) * x**k \
for k, coef in enumerate(poly[::-1])), S.Zero), x)
所以我们可以这样做
a, x = symbols('a x')
print(readable(intpoly, a, x))
并获取
Poly(x**2 + (a**2 + a + 1)*x + a, x, domain='ZZ[a]')
这个代数对象不是我们领域内的多项式,这只是为了更易读的输出。
作为另一种选择,或者只是另一个安全检查,可以使用Sage中的lagrange_polynomial
来处理相同的数据。
field = GF(16, 'a')
a = field.gen()
R = PolynomialRing(field, "x")
points = [(0, a), (1, a^2), (a^2+1, a^3)]
R.lagrange_polynomial(points)
输出:x^2 + (a^2 + a + 1)*x + a
galois
Python 库的作者。使用 lagrange_poly()
函数可以进行多项式插值。以下是一个简单的示例。In [1]: import galois
In [2]: galois.__version__
Out[2]: '0.0.32'
In [3]: GF = galois.GF(3**5)
In [4]: x = GF.Random(10); x
Out[4]: GF([ 33, 58, 59, 21, 141, 133, 207, 182, 125, 162], order=3^5)
In [5]: y = GF.Random(10); y
Out[5]: GF([ 34, 239, 120, 170, 31, 165, 180, 79, 215, 215], order=3^5)
In [6]: f = galois.lagrange_poly(x, y); f
Out[6]: Poly(165x^9 + 96x^8 + 9x^7 + 111x^6 + 40x^5 + 208x^4 + 55x^3 + 17x^2 + 118x + 203, GF(3^5))
In [7]: f(x)
Out[7]: GF([ 34, 239, 120, 170, 31, 165, 180, 79, 215, 215], order=3^5)
In [8]: GF.display("poly"); f(x)
Out[8]:
GF([ α^3 + 2α + 1, 2α^4 + 2α^3 + 2α^2 + α + 2,
α^4 + α^3 + α^2 + α, 2α^4 + 2α + 2,
α^3 + α + 1, 2α^4 + α,
2α^4 + 2α^2, 2α^3 + 2α^2 + 2α + 1,
2α^4 + α^3 + 2α^2 + 2α + 2, 2α^4 + α^3 + 2α^2 + 2α + 2], order=3^5)
In [9]: GF.display("power"); f(x)
Out[9]:
GF([α^198, α^162, α^116, α^100, α^214, α^137, α^169, α^95, α^175, α^175],
order=3^5)
PolyRing
的类,它简化了多项式的计算。 - user6655984for c in itertools.product(range(p), repeat=n): poly = (1, *c); if gf_irreducible_p(poly, p, ZZ): self.reducing = poly
。 - user6655984