- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
此类采用有限域多项式字符串,对其进行解析、运算 (+-*/%),然后以与输入相同的格式输出。它工作得很好(到目前为止)。但是,现在我正在尝试对算术运算符实现特殊方法,但我无法超越简单连接字符串的范围。一般来说,想法是将输入初始化为类实例,但在这种情况下,输入上有一个正则表达式,这似乎使任何尝试执行此操作变得复杂。我正在自学 Python,所以这对我来说是一部恐怖电影,但对于任何经验丰富的 Python 程序员来说可能只是一个玩具。
这些似乎有很多信息,但我不确定它们在这种情况下有多大帮助:
http://stackoverflow.com/questions/10842166/programmatically-create-arithmetic-special-methods-in-python-aka-factory-funct
http://rosettacode.org/wiki/S-Expressions
http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap14.html
http://docs.cython.org/src/userguide/special_methods.html
这是我正在使用的类和示例:
import re
class gf2pim:
def id(self,lst):
"""returns modulus 2 (1,0,0,1,1,....) for input lists"""
return [int(lst[i])%2 for i in range(len(lst))]
def listToInt(self,lst):
"""converts list to integer for later use"""
result = self.id(lst)
return int(''.join(map(str,result)))
def parsePolyToListInput(self,poly):
"""performs regex on raw string and converts to list"""
c = [int(i.group(0)) for i in re.finditer(r'\d+', poly)]
return [1 if x in c else 0 for x in xrange(max(c), -1, -1)]
def prepBinary(self,x,y):
"""converts to base 2; bina,binb are binary values like 110100101100....."""
x = self.parsePolyToListInput(x); y = self.parsePolyToListInput(y)
a = self.listToInt(x); b = self.listToInt(y)
bina = int(str(a),2); binb = int(str(b),2)
return bina,binb #
def add(self,a,b):
"""a,b are GF(2) polynomials like x**7 + x**3 + x**0 ...; returns binary string"""
bina,binb = self.prepBinary(a,b)
return self.outFormat(bina^binb)
def subtract(self,x,y):
"""same as addition in GF(2)"""
return self.add(x,y)
def quotient(self,a,b):
"""a,b are GF(2) polynomials like x**7 + x**3 + x**0 ...; returns quotient formatted as polynomial"""
a,b = self.prepBinary(a,b)
return self.outFormat(a/b)
def remainder(self,a,b):
"""a,b are GF(2) polynomials like x**7 + x**3 + x**0 ...; returns remainder formatted as polynomial"""
a,b = self.prepBinary(a,b)
return self.outFormat(a%b)
def outFormat(self,raw):
"""process resulting values into polynomial format"""
raw = "{0:b}".format(raw); raw = str(raw[::-1]); g = [] #reverse binary string for enumeration
g = [i for i,c in enumerate(raw) if c == '1']
processed = "x**"+" + x**".join(map(str, g[::-1]))
if len(g) == 0: return 0 #return 0 if list empty
return processed #returns result in gf(2) polynomial form
def __add__(self,other):
return gf2pim.add(self,other)
底部的最后一个示例显示了问题:
obj = gf2pim()
a = "x**14 + x**1 + x**0"; b = "x**6 + x**2 + x**1"
c = "x**2 + x**1 + x**0"; d = "x**3 + x**1 + x**0"
e = "x**3 + x**2 + x**1 + x**0"; f = "x**2"; g = "x**1 + x**0"; h = "x**3 + x**2 + x**0"
p = "x**13 + x**1 + x**0"; q = "x**12 + x**1"; j = "x**4 + x**3 + x**1 + x**0"
print "add: [%s] + [%s] = %s "%(a,b,obj.add(a,b))
print "add: [%s] + [%s] = %s "%(c,d,obj.add(c,d))
print "quotient (max(a,b)/min(a,b): [%s] / [%s] = %s "%(a,b,obj.quotient(a,b))
print "remainder (max(a,b) mod min(a,b)): [%s] mod [%s] = %s "%(a,b,obj.remainder(a,b))
print "quotient (max(a,b)/min(a,b): [%s] / [%s] = %s "%(c,d,obj.quotient(c,d))
print "remainder (max(a,b) mod min(a,b): [%s] mod [%s] = %s "%(c,d,obj.remainder(c,d))
print "quotient (max(a,b)/min(a,b): [%s] / [%s] = %s "%(q,q,obj.quotient(q,q))
print "remainder (max(a,b) mod min(a,b): [%s] mod [%s] = %s "%(q,q,obj.remainder(q,q))
print "modular_inverse: [%s] * [%s] mod [%s] = 1 [%s]"%(p,valuemi2[0],q,valuemi2[1])
sum1 = obj.add(a,b); quotient1 = obj.quotient(sum1,c)
### HERE THE PROBLEM IS CLEAR
print "[(a+b)/c] = ",quotient1
smadd1 = a+b
print "smadd1 ",smadd1
输出:
>>>
add: [x**14 + x**1 + x**0] + [x**6 + x**2 + x**1] = x**14 + x**6 + x**2 + x**0
add: [x**2 + x**1 + x**0] + [x**3 + x**1 + x**0] = x**3 + x**2
quotient (max(a,b)/min(a,b): [x**14 + x**1 + x**0] / [x**6 + x**2 + x**1] = x**7 + x**6 + x**5 + x**3 + x**1
remainder (max(a,b) mod min(a,b)): [x**14 + x**1 + x**0] mod [x**6 + x**2 + x**1] = x**2 + x**1 + x**0
quotient (max(a,b)/min(a,b): [x**2 + x**1 + x**0] / [x**3 + x**1 + x**0] = 0
remainder (max(a,b) mod min(a,b): [x**2 + x**1 + x**0] mod [x**3 + x**1 + x**0] = x**2 + x**1 + x**0
quotient (max(a,b)/min(a,b): [x**12 + x**1] / [x**12 + x**1] = x**0
remainder (max(a,b) mod min(a,b): [x**12 + x**1] mod [x**12 + x**1] = 0
[(a+b)/c]*d = x**14 + x**12 + x**9 + x**1
smadd1 x**14 + x**1 + x**0x**6 + x**2 + x**1
>>>
正如您通过 smadd1
所看到的,我需要使用 + 添加这两个,而不仅仅是连接。另外,我想知道在这种情况下是否需要使用 S 表达式树。
编辑:
Multiply() 曾经有效,但现在无效:
def __mul__(self,other):
"""
__multiply__ is the special method for overriding the - operator
returns product of 2 polynomials in gf2; self,other are values 10110011...
"""
self = int(str(self),2)
bitsa = reversed("{0:b}".format(self))
g = [(other<<i)*int(bit) for i,bit in enumerate(bitsa)]
return gf2infix(self.outFormat(reduce(lambda x,y: x^y,g)))
它的原始形式是:
def multiply(self,a,b):
"""a,b are GF(2) polynomials like x**7 + x**3 + x**0... ; returns product of 2 polynomials in gf2"""
a = self.prepBinary(a); b = self.prepBinary(b)
bitsa = reversed("{0:b}".format(a))
g = [(b<<i)*int(bit) for i,bit in enumerate(bitsa)]
return self.outFormat(reduce(lambda x,y: x^y,g))
<小时/>
忽略 multiply()
的问题,我修复了它。更改的行是:
bitsa = reversed("{0:b}".format(self.bin))
并且之前的行被删除。
最佳答案
看起来您混淆了两个概念:表示有限域多项式的字符串和 gf2pim 类的对象,该类也表示有限域多项式。您应该为每个要操作的多项式实例化一个 gf2pim 对象,并且对 gf2pim 对象的操作应该返回其他 gf2pim 对象。目前您正在尝试在 2 个字符串上使用 + 运算符,这就是为什么您的 __add__
方法未被调用。 gf2pim 类的定义还存在其他几个问题。我在下面重构了你的代码,尽管它仍然不完美。我还遗漏了划分方法,但我认为我所做的应该让您走上正轨。请参阅本 link 的第 3.4.8 节用于运算符重载的更多特殊方法名称。
import re
class gf2pim(object):#Your classes should generally inherit from object
def __init__(self, binary):
'''__init__ is a standard special method used to initialize objects. Here __init__
will initialize a gf2pim object based on a binary representation.'''
self.bin = binary
@classmethod
def from_string(cls, string):
return cls(cls._string_to_binary(string))
def to_string(self):
raw = "{0:b}".format(self.bin); raw = str(raw[::-1]); g = [] #reverse binary string for enumeration
g = [i for i,c in enumerate(raw) if c == '1']
processed = "x**"+" + x**".join(map(str, g[::-1]))
if len(g) == 0: return 0 #return 0 if list empty
return processed #returns result in gf(2) polynomial form
@classmethod
def id(cls, lst):
"""returns modulus 2 (1,0,0,1,1,....) for input lists"""
return [int(lst[i])%2 for i in range(len(lst))]
@classmethod
def _list_to_int(self, lst):
"""converts list to integer for later use"""
result = self.id(lst)
return int(''.join(map(str,result)))
@classmethod
def _string_to_list(cls, string):
"""performs regex on raw string and converts to list"""
c = [int(i.group(0)) for i in re.finditer(r'\d+', string)]
return [1 if x in c else 0 for x in xrange(max(c), -1, -1)]
@classmethod
def _string_to_binary(cls, string):
"""converts to base 2; bina,binb are binary values like 110100101100....."""
x = cls._string_to_list(string)
a = cls._list_to_int(x)
bina = int(str(a),2)
return bina #
def __add__(self,other):
"""
__add__ is another special method, and is used to override the + operator. This will only
work for instances of gf2pim and its subclasses.
a,b are GF(2) polynomials like x**7 + x**3 + x**0 ...; returns binary string"""
return gf2pim(self.bin^other.bin)
def __sub__(self,other):
"""
__sub__ is the special method for overriding the - operator
same as addition in GF(2)"""
return self.add(other)
def __str__(self):
return self.to_string()
if __name__ == '__main__':
a = gf2pim.from_string("x**14 + x**1 + x**0")
b = gf2pim.from_string("x**6 + x**2 + x**1")
smadd1 = a+b
print "smadd1 ",smadd1
关于Python——使用现有类方法的特殊方法算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17633620/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!