2017-09-22 15 views
0

私はこのような機能を持っていますが、問題は最初の列を置き換え、次の列を置き換えるときに前の列が元の形に戻りません。numpyの列を1つだけ置き換える方法は?

import numpy as np 
from numpy import linalg as la 

def CramersRule(A,b): 
    for c in range (n): 
     detA1 = la.det(A) 
     A[:,c] = b.transpose() 
     print A 
     x = la.det(A)/detA1 
     print ("X%d: ")%(c+1),x 

n = input ("Enter size of matrix nxn: ") 
coeff_matrix = input ("\nEnter coefficient matrix A: ") 
vec_constants = input ("Enter vector of constants b: ") 
A = np.array(coeff_matrix) 
b = np.array(vec_constants) 
print A 

CramersRule(A, b) 

答えて

0

これは一時的にAの置き換え列を格納A

def CramersRule(A,b): 
    for c in range (n): 
     detA1 = la.det(A) 
     temp = np.array(A[:,c]) 
     A[:,c] = b.transpose() 
     print A 
     x = la.det(A)/detA1 
     print ("X%d: ")%(c+1),x 
     A[:,c]=temp.transpose() 

temp = np.array(A[:,c])の置き換え列を戻します。興味深いことに明示的にnp.array()tempが変更されるとA[:,c]が変更されます。 A[:,c]=temp.transpose()は、古い列をAに再割り当てします。

関連する問題