在python中使用切片进行高斯消去-旋转
我试图在Python中实现高斯消去法的旋转,但遇到了一些问题在python中使用切片进行高斯消去-旋转,python,numpy,pivot,slice,Python,Numpy,Pivot,Slice,我试图在Python中实现高斯消去法的旋转,但遇到了一些问题 def pivot2(matrix,i): # matrix is a N*N matrix # i is the column I want to start with m = matrix.shape[1] for n in range(i,m): colMax = np.argmax(abs(matrix[n:,i]), axis=0) #rowindex of highest
def pivot2(matrix,i):
# matrix is a N*N matrix
# i is the column I want to start with
m = matrix.shape[1]
for n in range(i,m):
colMax = np.argmax(abs(matrix[n:,i]), axis=0) #rowindex of highest absolute value in column
if(colMax == 0): #if max in column is in first row, stop
break;
tmpRow = copy.copy(matrix[n,:]) #create new object of same row
matrix[n,:] = matrix[colMax,:] #overwrite first row with row of max value
matrix[colMax,:] = tmpRow #overwrite old row of max value
return matrix
该代码适用于i=0
很好。但是对于i=1
,我无法在整个列中搜索最大值的索引,因为它显然总是0
当我从3x3矩阵切片此矩阵时:
array([[ 1., 2.],
[-3., -2.]])
使用我的argmax
函数,索引为1
。但在我原来的矩阵中,同一行的索引是2,它交换了错误的行。我该如何解决这个问题
是否有一种更简单的方法可以使用切片实现旋转?在检查0后,只需将
i
添加到colmax
:
...
if(colMax == 0): #if max in column is in first row, stop
break;
colmax += i # add this string
...
看起来您可以在检查0后将
i
添加到colmax
,它会很好地工作感谢您的帮助,它工作了!