numpy矩阵乘法和功率不正确

时间:2016-05-19 08:45:22

标签: python numpy

我写了一些函数来计算Fibonacci数。我发现fib2fib3的某些值与其他值不相同。

要复制到以下内容:

import numpy as np

def fib1(n):
    if n == 0: return 0
    a, b = 0, 1
    for i in range(2, n + 1):
        a, b = b, a + b
    return b

# numpy matrix power
def fib2(n):
    m = np.matrix([[1, 1], [1, 0]]) ** n
    return m.flat[1]

# numpy matrix multiplication
def fib3(n):
    r = m = np.array([[1, 1], [1, 0]])
    for _ in range(n - 1):
        r = r.dot(m)
    return r.flat[1]

# manual matrix multiplication
def fib4(n):
    def matmult(X, Y):
        return [[sum(el_x * el_y for el_x, el_y in zip(row_x, col_y)) 
                 for col_y in zip(*Y)] for row_x in X]
    r = m = [[1, 1], [1, 0]]
    for _ in range(n - 1):
        r = matmult(r, m)
    return r[0][1]

# print results
print("{0:>5}{1:>25}{2:>25}{3:>25}{4:>25}".format("N", "fib1", "fib2", "fib3", "fib4"))
for i in range(90, 101):
    print("{0:>5}".format(i),
          "{0:>25}".format(fib1(i)),
          "{0:>25}".format(fib2(i)),
          "{0:>25}".format(fib3(i)),
          "{0:>25}".format(fib4(i)))

输出:

N   fib1    fib2    fib3    fib4
90       2880067194370816120       2880067194370816120       2880067194370816120       2880067194370816120
91       4660046610375530309       4660046610375530309       4660046610375530309       4660046610375530309
92       7540113804746346429       7540113804746346429       7540113804746346429       7540113804746346429
93      12200160415121876738      -6246583658587674878      -6246583658587674878      12200160415121876738
94      19740274219868223167       1293530146158671551       1293530146158671551      19740274219868223167
95      31940434634990099905      -4953053512429003327      -4953053512429003327      31940434634990099905
96      51680708854858323072      -3659523366270331776      -3659523366270331776      51680708854858323072
97      83621143489848422977      -8612576878699335103      -8612576878699335103      83621143489848422977
98     135301852344706746049       6174643828739884737       6174643828739884737     135301852344706746049
99     218922995834555169026      -2437933049959450366      -2437933049959450366     218922995834555169026
100    354224848179261915075       3736710778780434371       3736710778780434371     354224848179261915075

如您所见fib1fib4作品是正确的。有什么想法吗?

版本:

  • python 3.5.1
  • numpy 1.11.0

1 个答案:

答案 0 :(得分:0)

似乎dtype=object param numpy.matrix解决问题:

def fib2(n):
    m = np.matrix([[1, 1], [1, 0]], dtype=object) ** n
    return m.flat[1]
fib2(93) # 12200160415121876738