gpt4 book ai didi

python - 高斯消元法 - 在 python 中使用切片进行旋转

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:01 25 4
gpt4 key购买 nike

我正在尝试在 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 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
...

关于python - 高斯消元法 - 在 python 中使用切片进行旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19954033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com