我有一个数组,我正在对其进行一些计算。数组开始为
e = array([[1,2,1,3],
[3,-1,-3,-1],
[2,3,1,4]])
我对其进行了一些修改以将其转换为:
array([[ 1, 2, 1, 3],
[ 0, -7, -6, -10],
[ 0, -1, -1, -2]])
然后我在上面运行这段代码:
import numpy as np
from fractions import Fraction
def ref(x):
dimension = x.shape
row_counter = 1
first_values = [x[i][row_counter] for i in range(dimension[0])] #gets a list of elements of the column
first_values = [number != 0 for number in first_values] #0 is a pivot element?
if False in first_values:
true_index = first_values.index(True); false_index = first_values.index(False)
if true_index > false_index: #not any more
x[[false_index, true_index]] = x[[true_index, false_index]]
for i in range(row_counter+1,dimension[0]):
multiplier = Fraction(x[row_counter][row_counter], x[i][row_counter])**-1
row1 = multiplier*x[row_counter]
row1 = x[i]-row1
print row1
x[i] = row1
return x
运行此返回:
[0 0 -1/7 -4/7]
array([[ 1, 2, 1, 3],
[ 0, -7, -6, -10],
[ 0, 0, 0, 0]])
所以结果应该是
array([[ 1, 2, 1, 3],
[ 0, -7, -6, -10],
[ 0, 0, -1/7, -4/7]])
它打印正确的行条目,但不会添加到数组中,而是添加一行零。有人可以告诉我为什么吗?谢谢。