- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 Python 相当陌生,我有一个与多项式相关的问题。让我们在 GF(2) 中得到一个高次多项式,例如:x^n + x^m + ... + 1
,其中 n
、m
最多可达 10000。我需要找到这个的逆多项式。在 Python 中最快的方法是什么(可能使用 numpy)?
谢谢
最佳答案
您基本上有两个选择:
使用sympy.polys.galoistools,Sympy是一个优秀的符号数学Python库。然而,至少根据我的经验,如果多项式的次数非常高(这在纠错编码理论中很常见),sympy 就会非常慢。
from sympy.polys.galoistools import gf_gcdex, gf_strip
def gf_inv(a, irr_poly): # irriducible polynomial
return gf_gcdex(gf_strip(a), irr_poly, 2 , ZZ)[0]
该解决方案适用于所有 GF(2^p) 字段,例如GF(2)/(x^p+1)。
就我个人而言,因为我必须对 GF(2) 中的多项式求逆(一万次!),所以我也使用 numpy 开发了 EEA,但仅适用于 GF2[x] 多项式:
def gf2_xgcd(b, a):
"""Perform Extended Euclidean Algorithm over GF2
Given polynomials ``b`` and ``a`` in ``GF(p)[x]``, computes polynomials
``s``, ``t`` and ``h``, such that ``h = gcd(f, g)`` and ``s*b + t*a = h``.
The typical application of EEA is solving polynomial diophantine equations and findining multiplicative inverse.
Parameters
----------
b : ndarray (uint8 or bool) or list
b polynomial's coefficients.
a : ndarray (uint8 or bool) or list
a polynomial's coefficients.
Returns
-------
y2 : ndarray of uint8
s polynomial's coefficients.
x2 : ndarray of uint8
t polynomial's coefficients.
b : ndarray of uint8
h polynomial's coefficients.
Notes
-----
Rightmost element in the arrays is the leading coefficient of the polynomial.
In other words, the ordering for the coefficients of the polynomials is like the one used in MATLAB while
in Sympy, for example, the leftmost element is the leading coefficient.
Examples
========
>>> x = np.array([1, 1, 1, 1, 1, 0, 1, 0, 1], dtype="uint8")
>>> y = np.array([1, 0, 1], dtype="uint8")
>>> gf2_xgcd(x,y)
(array([0, 1, 1, 1], dtype=uint8),
array([1, 1], dtype=uint8),
array([1], dtype=uint8))
"""
x1 = np.array([1], dtype="uint8")
y0 = np.array([1], dtype="uint8")
x0 = np.array([], dtype="uint8")
y1 = np.array([], dtype="uint8")
while True:
q, r = gf2_div(b, a)
b = a
if not r.any():
break
a = r
if not (q.any() and x1.any()): # if q is zero or x1 is zero
x2 = x0
elif not x0.any(): # if x0 is zero
x2 = mul(x1, q)
else:
mulres = mul(x1, q)
x2 = gf2_add(x0, mulres)
if not (q.any() and y1.any()):
y2 = y0
elif not y0.any():
y2 = mul(y1, q)
else:
mulres = mul(y1, q)
y2 = gf2_add(y0, mulres)
# update
y0 = y1
x0 = x1
y1 = y2
x1 = x2
return y2, x2, b
当然,要执行 EEA,首先需要定义允许在 GF2[x 上执行加法、乘法和除法的函数] 字段。
事实上,我已经开发了一个python模块,可以在GitHub中找到:pyGF2它可以在 GF2[x] 上有效地计算多项式算术,包括比 Sympy 快几个数量级的多项式求逆。这里的 EEA 代码直接取自该存储库。
关于python - 在 GF2 中用 python 查找逆多项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207222/
要在 R 中绘制正态分布曲线,我们可以使用: (x = seq(-4,4, length=100)) y = dnorm(x) plot(x, y) 如 dnorm将 y 计算为 x 的函数,R 是否
@XmlTransient 阻止将 JavaBeans 属性映射到 XML 表示。是否存在与此相反的情况,这意味着即使 WebService 未使用的方法也会被映射?如果这不可能,是否存在解决方法?
我有以下键数组: var keys = [{userId: "333"}, {userId: "334"}] 这个对象数组: var users = [ {id: "333", firstName:
我正在寻找将字符串转换为类型的通用方法。 例如: class SomeThing { public void Add(T value) { //... } pub
我看到了this question , 并弹出这个想法。 有没有一种在 PHP 中执行此操作的有效方法? 编辑 有演示最好吗? 最佳答案 你可以使用 pear 包 Math_Matrix为此。 关于矩
如何在 python 中求逆矩阵?我自己实现了它,但它是纯 python,我怀疑那里有更快的模块可以做到这一点。 最佳答案 你应该看看 numpy如果您进行矩阵操作。这是一个主要用C语言编写的模块,比
是否有比使用 IF ELSE 构造更简单的方法来反转 bool 值? 通常我会使用! bool 值前面。但这在 Navision 中不起作用 最佳答案 您可以使用 NOT 关键字代替 !。 关于nav
假设我有一个对象响应。现在我想检查一个 bool 变量,success,在 Response 下并做一个早期返回是 response 不成功。 if(response == null || !resp
任何人都可以提供/引用多维行主要顺序的“索引->偏移”*转换的倒数。此外,(伪)代码将不胜感激。 http://en.wikipedia.org/wiki/Row-major_order 举个例子,简
我有一个看起来像这样的系统: z1 = 5*x1 + x2*cos(x3) z2 = x1*sin(x3) + 3*x2 z3 = 3*x1 - 2*x2 这是微分方程组的变换(只是为了提供一些背景信
我正在使用org.apache.commons.math3.transform类FastFourierTransformer,我现在尝试在真实数据集上应用FFT,并应用逆FFT来获取原始数据集。我的问
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
背景 我需要使用已知的累积分布函数 (CDF) 从相当复杂的概率密度函数 (PDF) 中随机采样,并且我正在尝试使用 inverse transform sampling 。这应该很容易做到,因为我有
是否有任何 System.identityHashCode (object) 的逆函数能够从 System.identityHashCode (object) 的结果中提供对象的值? 最佳答案 Sys
有没有办法在mysql中获取group by语句的逆?我的用例是删除所有重复项。 假设我的表格如下所示: ID | columnA | ... 1 | A 2 | A 3 | A 4
我有一个查询,它给我一个公司列表(tblprov)及其相应的类别(tblrubro) 两个表通过查找表 (tblprovxrubro) 相关 SELECT p.id, p.name, r.idCat,
我有一个 jpg 图像,在矩形中有一个圆形物体,我想使圆形物体的环境透明... (本例去除红色区域) 借助这个iOS make part of an UIImage transparent和“UIBe
我想知道是否可以在不需要临时数组的情况下通过 Cholesky 分解获得矩阵的逆。截至目前,我可以在不使用临时数组的情况下进行 cholesky 分解,但从那里我还没有想出一种方法来获得原始矩阵的逆矩
是否可以在 Angular 中使用逆$watch? 我的问题 我使用 Angular-translate,并且我想对每个缺少的翻译使用 $http.put 。但我收到此错误: "10 $digets(
我正在执行 radix-2 dif 逆 fft。我正在使用共轭和缩放的属性来返回结果。我共轭我的输入 vector ,执行常规 radix-2 fft(不是 ifft),共轭结果,然后按 1.0/N
我是一名优秀的程序员,十分优秀!