繁体   English   中英

如何在Sage中打印Galois字段的所有加法和乘法

[英]How to print all the additions and multiplications of a Galois Field in Sage

我的命令行有两个输入,素数p和正整数n。 我将它们以GF(p ^ n)的形式放在Galois字段中。

我的目标是打印出该字段的所有元素,加法和乘法。

我可以打印出该字段的元素,但是如何获得加法和乘法? 如果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)

您可以在k的元素上简单地使用嵌套循环,而不是在元素的索引上使用嵌套循环:

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

另外,您可以使用CartesianProduct (或纯Python中的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.] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM