首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有限域:计算矩阵的逆

有限域:计算矩阵的逆
EN

Stack Overflow用户
提问于 2017-08-17 12:37:37
回答 2查看 2.3K关注 0票数 2

我正在使用Python中的有限字段。我有一个包含多项式的矩阵,每个多项式都表示为一个整数。例如,多项式x^3 + x + 1表示为11,因为:

代码语言:javascript
运行
复制
x^3 + x + 1 ==> (1,0,1,1) ==> (8,0,2,1) ==> 8 + 0 + 2 + 1 ==> 11

给定一个矩阵,例如:

代码语言:javascript
运行
复制
6, 2, 1
7, 0, 4
1, 7, 3

如何在Python中计算矩阵的求逆?我已经实现了以下函数(用于多项式,而不是多项式的矩阵):add()sub()mul()div()inv()

EN

回答 2

Stack Overflow用户

发布于 2021-05-01 23:40:23

我写了一个python包,在伽罗华域上实现numpy数组。除了普通的数组运算外,它还支持线性代数。https://github.com/mhostetter/galois

这里有一个例子来做你想做的事情。

代码语言:javascript
运行
复制
In [1]: import numpy as np                                                              

In [2]: import galois                                                                   

In [3]: GF = galois.GF(2**3)                                                            

In [4]: print(GF.properties)                                                            
GF(2^3):
  characteristic: 2
  degree: 3
  order: 8
  irreducible_poly: Poly(x^3 + x + 1, GF(2))
  is_primitive_poly: True
  primitive_element: GF(2, order=2^3)

In [5]: A = GF([[6,2,1],[7,0,4],[1,7,3]]); A                                            
Out[5]: 
GF([[6, 2, 1],
    [7, 0, 4],
    [1, 7, 3]], order=2^3)

In [6]: np.linalg.inv(A)                                                                
Out[6]: 
GF([[5, 5, 4],
    [3, 0, 1],
    [4, 3, 7]], order=2^3)

In [7]: np.linalg.inv(A) @ A                                                            
Out[7]: 
GF([[1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]], order=2^3)
票数 1
EN

Stack Overflow用户

发布于 2017-09-14 22:47:48

假设你的有限域是$GF(8)$,你的所有计算函数(如add()mul())必须在同一域(GF(8))上计算。

  • 计算有限域上的矩阵逆的方法与计算其他域上的矩阵逆的方法完全相同,高斯-乔丹消元法正好可以做到这一点。

下面是计算GF(256)上的矩阵逆的一部分代码(摘自:https://github.com/foool/nclib/blob/master/gmatrixu8.cc)。希望能对你有所帮助。

代码语言:javascript
运行
复制
void GMatrixU8::Inverse(){
    GMatrixU8 mat_inv;
    int w = this->ww;
    int dim;
    int i, nzero_row, j;
    int temp, jtimes;

    dim = this->rr;
    mat_inv.Make_identity(this->rr, this->cc, this->ww);

    for(i = 0; i < dim; i++){
        nzero_row = i;
        if(0 == Get(i,i)){
            do{
                ++nzero_row;
                if(nzero_row >= dim){ ERROR("Non-full rank matrix!"); }
                temp = Get(nzero_row, i);
            }while((0 == temp)&&(nzero_row < dim));
            Swap_rows(i, nzero_row);
            mat_inv.Swap_rows(i, nzero_row);
        }
        for(j = 0; j < dim; j++){
            if(0 == Get(j,i))
                continue;
            if(j != i){
                jtimes = galois_single_divide(Get(j,i), Get(i,i), w);
                Row_plus_irow(j, i, jtimes);
                mat_inv.Row_plus_irow(j, i, jtimes);
            }else{
                jtimes = galois_inverse(Get(i, i), w);
                Row_to_irow(i , jtimes);
                mat_inv.Row_to_irow(i , jtimes);
            }
        }
    }
    this->ele = mat_inv.ele;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45726670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档