Skip to content

Commit aeceb91

Browse files
committed
再增加一处 0 约化
1 parent d5914e5 commit aeceb91

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

  • src/main/kotlin/org/mechdancer/algebra/function/matrix

src/main/kotlin/org/mechdancer/algebra/function/matrix/Assign.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ fun ValueMutableMatrix.transposeAssign() {
7676
fun ValueMutableMatrix.rowEchelonAssignWith(
7777
other: Iterable<ValueMutableMatrix>
7878
): Double {
79-
for (r in 0 until row)
80-
for (c in 0 until column)
81-
if (doubleEquals(get(r, c), .0))
82-
set(r, c, .0)
79+
// 对接近零的数字规范化
80+
forEachAssign { if (doubleEquals(it, .0)) .0 else it }
8381
// 记录交换次数
8482
var sum = 0
8583
// 固定行数
@@ -130,6 +128,14 @@ fun ValueMutableMatrix.simplifyAssignWith(
130128
tail = get(r, --c)
131129
} while (c >= 0 && tail == .0)
132130

131+
// 在此进行 0 约化会导致 40 阶希尔伯特矩阵求逆的测试无法通过
132+
// do {
133+
// tail = get(r, --c)
134+
// if (!doubleEquals(tail, .0)) break
135+
// // 接近 0 视作 0
136+
// set(r, c, .0)
137+
// } while (c >= 0)
138+
133139
// 有全零行,不再继续化简
134140
if (c < 0) break
135141

@@ -147,15 +153,16 @@ fun ValueMutableMatrix.simplifyAssignWith(
147153
// 主对角线归一化
148154
for (r in 0 until row)
149155
while (c < column) {
150-
get(r, c++)
151-
.takeIf { it != .0 }
152-
?.let { 1 / it }
153-
?.let { k ->
154-
scale *= k
155-
timesRow(r, k)
156-
other.forEach { it.timesRow(r, k) }
157-
}
158-
?: continue
156+
val item = get(r, c++)
157+
if (doubleEquals(item, .0)) {
158+
set(r, c - 1, .0)
159+
continue
160+
}
161+
162+
val k = 1 / item
163+
scale *= k
164+
timesRow(r, k)
165+
other.forEach { it.timesRow(r, k) }
159166
break
160167
}
161168

0 commit comments

Comments
 (0)