如何在galois字段上计算numpy数组? [英] How to calculate numpy arrays on galois field?

查看:107
本文介绍了如何在galois字段上计算numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在galois字段(GF4)上使用numpy数组. 因此,我将GF4类设置为数组元素. 它适用于数组+整数计算,但不适用于数组+数组计算.

I want to use numpy array on galois field (GF4). so, I set GF4 class to array elements. It works on array + integer calculation but it dosen't works on array + array calculation.

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
"""

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

But it also works on array and array * integer caliculation.

"""
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类.

How should I correct the following codes? I want to use my class GF4.

推荐答案

问题是,当xGF4对象时,Python不知道如何索引元组.您可以执行以下操作来解决该问题:

The problem is that Python doesn't know how to index tuples when x is a GF4 object. You could do something like this to solve that:

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

您可能还需要研究另一个潜在的问题,这说明了您的第三个测试用例起作用的原因:将int添加到GF4时,返回的是int,而不是GF4.除非这是理想的行为,否则我认为您的__add__代码应该更像:

There is another potential issue you may want to look at, that explains why your third test case works: when you add an int to a GF4 what gets returned is an int, not a GF4. Unless this is a desired behavior, I think your code for __add__ should be more like:

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

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

You may want to think over all the possibilities and decide if youneed to build more safeguards and throw a few errors of your own, e.g. what should be the return if you try to add a float to a GF4?

这篇关于如何在galois字段上计算numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆