分解功能

时间:2013-07-16 01:13:04

标签: python math

我一直在困惑如何在Python中使用某个函数。函数本身就是这样的:

(Phi_m)x((n2)) = (Phi_m)(x(m*n + r)) = m*x[n1] + r*(x([n1] + 1) - x[n1])

注意:这里的n只是指定一些倍数。它不是列表元素,但在应用x时它将成为列表元素。在下面的示例中,例如,我们可能认为n大于列表中的任何元素。例如。列表有9个元素,最大的是3,m = 1 - 这里n = 9 = / =列表的元素。

其中n2和n1是输入字符串的两个不同值,其中n1是通过'分解'n2导出的。我们认为x[0] = 0r始终为零或正数且小于mn(其中任何一个)的所有值均为正整数。通常,函数接受一串数字并输出另一个字符串。通常情况下我们会修复m,比如m = 2。现在我们分解n2。说n2 = 5。然后是F(x(5)) = F(x(2*2+1)) 2x[2] + 1(x[3] - x[2])。因此,如果我们的完整输入序列为0, 1, 1, 2, 3, 3,我们就会2*1+0=2。所以我们的第五个输出项是2

我最初想过要做的事情:

x = [0,1,1,2,3,3,3,3]

def F(n,j,r,x):
return j * x[n] + r(x[n + 1] - x[n])
for n in range(len(x) - 1):
    print n

但这显然不符合我的目的。

问题是,对于Python来说,它必须知道如何分解每个数字。所以它知道2是固定的,知道2*3太多,所以选择2*2。然后它必须知道这太少并添加余量1。只有一旦完成,它才能真正抓住n = 5。也就是说,它可以运行该功能。很明显,一旦它知道如何做到这一点,它就可以贯穿我们范围内的每个n,但我真的不确定如何编程这个函数的内容。

1 个答案:

答案 0 :(得分:1)

以下是我如何以n2 = m * n1 + r

的形式分解数字
>>> def decompose(number):
...     # returns a generator of tuples (m, n1, r)
...     for m in range(1, number+1):
...         yield m, number // m, number % m
... 
>>> for m, n1, r in decompose(5):
...     print "5 = %s * %s + %s" % (m, n1, r)
... 
5 = 1 * 5 + 0
5 = 2 * 2 + 1
5 = 3 * 1 + 2
5 = 4 * 1 + 1
5 = 5 * 1 + 0

或使用固定m,这与常规divmod相同:

>>> def decompose(number):
...     return number // m, number % m
... 
>>> m = 2
>>> n1, r = decompose(5)
>>> print "5 = %s * %s + %s" % (m, n1, r)
5 = 2 * 2 + 1
>>> m = 4
>>> n1, r = decompose(5)
>>> print "5 = %s * %s + %s" % (m, n1, r)
5 = 4 * 1 + 1

或更简单地使用lambda

>>> decompose = lambda number: divmod(number, m)
>>> 
>>> m = 2
>>> decompose(5)
(2, 1)
>>> m = 4
>>> decompose(5)
(1, 1)

现在,完整的例子:

>>> decompose = lambda number: divmod(number, m)
>>> 
>>> class Phi_m(list):
...     def __init__(self, items):
...         list.__init__(self)
...         # you need to know at least m numbers.
...         assert len(items) >= m, 'Not enough data'
...         list.extend(self, items)
...     # this is a sparse list
...     # http://stackoverflow.com/questions/1857780/sparse-assignment-list-in-python
...     def __setitem__(self, index, value):
...         missing = index - len(self) + 1
...         if missing > 0:
...             self.extend([None] * missing)
...             list.__setitem__(self, index, value)
...     def __getitem__(self, index):
...         try:
...             value = list.__getitem__(self, index)
...             if value is not None:
...                 # the item is in the list, yeah!
...                 return value
...             # the item is in the list because it was resized
...             # but it is None, so go on and calculate it. 
...         except IndexError:
...             # the item is not in the list, calculate it.
...             pass
...         print 'calculating Fm[%s]' % index
...         A, B = decompose(index)
...         value1 = self.__getitem__(A)
...         value2 = self.__getitem__(A + 1)
...         print 'Fm[A=%s] = %s, Fm[A+1=%s] = %s' % (A, value1, A+1, value2)
...         print 'back to calculating Fm[%s]' % index
...         # m * x[n1] + r * (x[n1 + 1] - x[n1]) = (m - r) * x[n1] + r * x[n1 + 1]
...         # A = n1 ; B = r ; value1 = x[n1] ; value2 = x[n+1]
...         value = (m - B) * value1 + B * value2
...         self.__setitem__(index, value)
...         return value
... 
>>> x = Phi_m([0, 1, 1])
>>> 
>>> x[5]
calculating Fm[5]
calculating Fm[3]
Fm[A=1] = 1, Fm[A+1=2] = 1
back to calculating Fm[3]
Fm[A=2] = 1, Fm[A+1=3] = 2
back to calculating Fm[5]
3
>>> 
>>>