chore: refactor code blocks

This commit is contained in:
2026-07-16 00:53:01 +08:00
parent b8e7cb33f9
commit aa14c220f7
6 changed files with 346 additions and 345 deletions
+59 -59
View File
@@ -24,57 +24,57 @@ description: 高斯-约旦消元法的 Kotlin 实现
* @return 矩阵的秩
*/
fun computeRREF(matrix: Array<DoubleArray>): Int {
val rows = matrix.size
val cols = if (rows > 0) matrix[0].size else 0
var rank = 0
var pivotRow = 0
var pivotCol = 0
val rows = matrix.size
val cols = if (rows > 0) matrix[0].size else 0
var rank = 0
var pivotRow = 0
var pivotCol = 0
while (pivotRow < rows && pivotCol < cols) {
// 寻找当前列的主元
var maxRow = pivotRow
for (i in pivotRow + 1..<rows) {
if (abs(matrix[i][pivotCol]) > abs(matrix[maxRow][pivotCol])) {
maxRow = i
}
}
while (pivotRow < rows && pivotCol < cols) {
// 寻找当前列的主元
var maxRow = pivotRow
for (i in pivotRow + 1..<rows) {
if (abs(matrix[i][pivotCol]) > abs(matrix[maxRow][pivotCol])) {
maxRow = i
}
}
// 如果当前列所有元素都是0,则处理下一列
if (abs(matrix[maxRow][pivotCol]) < 1e-10) {
pivotCol++
continue
}
// 如果当前列所有元素都是0,则处理下一列
if (abs(matrix[maxRow][pivotCol]) < 1e-10) {
pivotCol++
continue
}
// 交换行
if (maxRow != pivotRow) {
val temp = matrix[pivotRow]
matrix[pivotRow] = matrix[maxRow]
matrix[maxRow] = temp
}
// 交换行
if (maxRow != pivotRow) {
val temp = matrix[pivotRow]
matrix[pivotRow] = matrix[maxRow]
matrix[maxRow] = temp
}
// 归一化主元行
val divisor = matrix[pivotRow][pivotCol]
for (j in pivotCol..<cols) {
matrix[pivotRow][j] /= divisor
}
// 归一化主元行
val divisor = matrix[pivotRow][pivotCol]
for (j in pivotCol..<cols) {
matrix[pivotRow][j] /= divisor
}
// 消元操作,使主元所在列的其他元素为0
for (i in 0..<rows) {
if (i != pivotRow) {
val factor = matrix[i][pivotCol]
for (j in pivotCol..<cols) {
matrix[i][j] -= factor * matrix[pivotRow][j]
}
}
}
// 消元操作,使主元所在列的其他元素为0
for (i in 0..<rows) {
if (i != pivotRow) {
val factor = matrix[i][pivotCol]
for (j in pivotCol..<cols) {
matrix[i][j] -= factor * matrix[pivotRow][j]
}
}
}
// 处理下一行和下一列
pivotRow++
pivotCol++
rank++
}
// 处理下一行和下一列
pivotRow++
pivotCol++
rank++
}
return rank
return rank
}
```
@@ -87,15 +87,15 @@ return rank
// 寻找主元
var maxRow = pivotRow
for (i in pivotRow + 1 until rows) {
if (abs(matrix[i][pivotCol]) > abs(matrix[maxRow][pivotCol])) {
maxRow = i
}
if (abs(matrix[i][pivotCol]) > abs(matrix[maxRow][pivotCol])) {
maxRow = i
}
}
// 检查主元是否为0
if (abs(matrix[maxRow][pivotCol]) < 1e-10) {
pivotCol++
continue
pivotCol++
continue
}
```
@@ -107,15 +107,15 @@ continue
```kotlin
// 行交换
if (maxRow != pivotRow) {
val temp = matrix[pivotRow]
matrix[pivotRow] = matrix[maxRow]
matrix[maxRow] = temp
val temp = matrix[pivotRow]
matrix[pivotRow] = matrix[maxRow]
matrix[maxRow] = temp
}
// 归一化主元行
val divisor = matrix[pivotRow][pivotCol]
for (j in pivotCol until cols) {
matrix[pivotRow][j] /= divisor
matrix[pivotRow][j] /= divisor
}
```
@@ -127,12 +127,12 @@ matrix[pivotRow][j] /= divisor
```kotlin
// 消元操作
for (i in 0 until rows) {
if (i != pivotRow) {
val factor = matrix[i][pivotCol]
for (j in pivotCol until cols) {
matrix[i][j] -= factor * matrix[pivotRow][j]
}
}
if (i != pivotRow) {
val factor = matrix[i][pivotCol]
for (j in pivotCol until cols) {
matrix[i][j] -= factor * matrix[pivotRow][j]
}
}
}
```