我的命令行有两个输入,一个素数p和一个正整数n。我把它们放在GF(p ^ n)形式的伽罗瓦场中。
我的目标是打印出该领域的所有元素,添加和乘法。
我可以打印出该字段的元素,但是如何获得添加和乘法?如果p和n为2,我希望它们像这样:
(0) + (0) = 0
(0) + (x) = x
(0) + (x + 1) = x + 1
(0) + (1) = 1
(x) + (0) = x
(x) + (x) = 0
(x) + (x + 1) = 1
(x) + (1) = x + 1
(x + 1) + (0) = x + 1
(x + 1) + (x) = 1
(x + 1) + (x + 1) = 0
(x + 1) + (1) = x
(1) + (0) = 1
(1) + (x) = x + 1
(1) + (x + 1) = x
到目前为止,这是我的代码:
import sys
p = int(sys.argv[1])
n = int(sys.argv[2])
k = GF(p**n, 'x')
for i,x in enumerate(k): print x
print '(%s) + (%s) = %s' % (i, j, i + j)
答案 0 :(得分:1)
你可以简单地在k
的元素上使用嵌套循环,而不是元素的 indices :
sage: for e0 in k:
....: for e1 in k:
....: print '(%s) + (%s) = %s' % (e0, e1, e0+e1)
....:
(0) + (0) = 0
(0) + (x) = x
(0) + (x + 1) = x + 1
(0) + (1) = 1
(x) + (0) = x
(x) + (x) = 0
(x) + (x + 1) = 1
(x) + (1) = x + 1
(x + 1) + (0) = x + 1
(x + 1) + (x) = 1
(x + 1) + (x + 1) = 0
(x + 1) + (1) = x
(1) + (0) = 1
(1) + (x) = x + 1
(1) + (x + 1) = x
(1) + (1) = 0
或者,您可以在纯Python中使用CartesianProduct
(或itertools.product
):
sage: for e0, e1 in CartesianProduct(k,k):
....: print '(%s) + (%s) = %s' % (e0, e1, e0+e1)
....:
(0) + (0) = 0
(0) + (x) = x
[etc.]