簡體   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