2013-07-24 16 views
0

這是麻煩的代碼的工作,應該是不言自明與評論:印刷無法在一些特定的Python函數

import numpy as np 
import sys 

A = np.matrix([[1, 1], [2, 0]]) 
x0 = np.matrix([1, 0]).reshape(2, 1) 
thresh = 1e-3 

def inv_powerm(A, x0, thresh): 
    m0 = x0.flat[abs(x0).argmax()] 
    x1 = np.linalg.solve(A, (x0/m0)) 
    m1 = x1.flat[abs(x1).argmax()] 
    while abs(m1 - m0) > thresh: 
     m0 = m1 
     x1 = np.linalg.solve(A, (x1/m1)) 
     m1 = x1.flat[abs(x1).argmax()] 
     print(x1) 
     print(m1) 
    return m1; 

def pmat(m): 
    i = 0 
    while i < 10: 
     print(m) 
     i = i + 1 
    return m 

# I can print the matrix 
print(A) 
# I can print the matrix in pmat() 
pmat(A) 
# But I cannot print matrices in inv_powerm() 
inv_powerm(A, x0, thresh) 
+0

你可以在各自的函數中做一個'print(type(m1))'和一個'print(type(A))'來獲得更多信息 – Sebastian

回答

1

這不是print不行。你的代碼的邏輯失敗了。在inv_powerm,第一次m0 == 1m1 == 1.0,所以m0 - m1 == 0。所以while測試失敗。 while中的所有代碼都未執行。

+0

事實上,初始向量必須改變以使這項工作。謝謝! – qed