【问题标题】:How to calculate numpy arrays on galois field?如何计算 galois 场上的 numpy 数组?
【发布时间】:2013-06-07 07:12:14
【问题描述】:

我想在 galois 场 (GF4) 上使用 numpy 数组。 所以,我将 GF4 类设置为数组元素。 它适用于数组+整数计算,但不适用于数组+数组计算。

import numpy

class GF4(object):
    """class for galois field"""
    def __init__(self, number):
        self.number = number
        self.__addL__ = ((0,1,2,3),(1,0,3,2),(2,3,0,1),(3,2,1,0))
        self.__mulL__ = ((0,0,0,0),(0,1,2,3),(0,2,3,1),(0,3,1,2))
    def __add__(self, x):
        return self.__addL__[self.number][x]
    def __mul__(self, x):
        return self.__mulL__[self.number][x]
    def __sub__(self, x):
        return self.__addL__[self.number][x]
    def __div__(self, x):
        return self.__mulL__[self.number][x]
    def __repr__(self):
        return str(self.number)

a = numpy.array([GF4(numpy.random.randint(4)) for i in range(18)]).reshape(3,6)
b = numpy.array([GF4(numpy.random.randint(4)) for i in range(18)]).reshape(3,6)

""""
In [261]: a
Out[261]: 
array([[1, 1, 2, 0, 2, 1],
       [0, 3, 1, 0, 3, 1],
       [1, 2, 0, 3, 2, 1]], dtype=object)

In [262]: b
Out[262]: 
array([[0, 0, 3, 1, 0, 0],
       [0, 1, 0, 1, 1, 1],
       [3, 2, 2, 0, 2, 0]], dtype=object)

In [263]: a+1
Out[263]: 
array([[0, 0, 3, 1, 3, 0],
       [1, 2, 0, 1, 2, 0],
       [0, 3, 1, 2, 3, 0]], dtype=object)

In [264]: a+b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-264-f1d53b280433> in <module>()
----> 1 a+b

<ipython-input-260-0679b73b59a4> in __add__(self, x)
      8         self.__mulL__ = ((0,0,0,0),(0,1,2,3),(0,2,3,1),(0,3,1,2))
      9     def __add__(self, x):
---> 10         return self.__addL__[self.number][x]
     11     def __mul__(self, x):
     12         return self.__mulL__[self.number][x]

TypeError: tuple indices must be integers, not GF4
"""

但它也适用于数组和数组*整数计算。

"""
In [265]: a+b*1
Out[265]: 
array([[1, 1, 1, 1, 2, 1],
       [0, 2, 1, 1, 2, 0],
       [2, 0, 2, 3, 0, 1]], dtype=object)
"""

我应该如何更正以下代码? 我想使用我的类 GF4。

【问题讨论】:

  • 我正在尝试实现 rabin 的信息分散算法,其中所有计算都在一个 galois 域中;你的代码听起来很有希望,但我不明白,因为我是 python 新手。有没有关于这个案子的文件可以推荐给我?
  • 没有文档,我忘记了我的代码细节......这些代码是针对 GF - GF 和 GF - Integer 定义的 Galois Field 类和重载运算符(add/sub/mul/div)。 GF4 操作可以简单地定义为数字映射(__addL__ 和 __mulL__)。另外,GF4 sub 和 div 的操作只是 add 和 mul 的一个子集。

标签: python arrays numpy galois-field


【解决方案1】:

问题是当xGF4 对象时,Python 不知道如何索引元组。你可以这样做来解决这个问题:

def __add__(self, x):
    if isinstance(x, GF4):
        x = x.number
    return self.__addL__[self.number][x]

还有一个您可能想要查看的潜在问题,它解释了为什么您的第三个测试用例有效:当您将 int 添加到 GF4 时,返回的是 int,而不是 GF4。除非这是一种期望的行为,否则我认为您的 __add__ 代码应该更像:

def __add__(self, x):
    if isinstance(x, GF4):
        x = x.number
    return GF4(self.__addL__[self.number][x])

您可能需要考虑所有可能性并决定是否需要构建更多保护措施并自己抛出一些错误,例如如果您尝试将float 添加到GF4,应该返回什么?

【讨论】:

  • 哇!!我可以解决它!谢谢 :) 顺便问一下,使用“isinstance”的处理是通用的吗?
  • 说实话,这不是很 Pythonic。我认为对于您的特定课程,更好的选择是subclass the int numeric type 并重载运算符。你也可以使用 duck typing 来实现你的类,可能使用 try/except 子句。
  • 因为您的回复,我能深刻理解。谢谢。
猜你喜欢
  • 2021-08-13
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多