Skip to content

Commit 4435f36

Browse files
committed
添加施密特正交化函数
1 parent 829c22d commit 4435f36

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "org.mechdancer"
11-
version = "0.2.7-dev-3"
11+
version = "0.2.7-dev-4"
1212

1313
repositories {
1414
mavenCentral()

src/main/kotlin/org/mechdancer/algebra/function/vector/Cauculate.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,18 @@ fun Vector.norm(n: Int = 2) =
9797
2 -> length
9898
else -> toList().sumByDouble { it.pow(n) }.pow(1.0 / n)
9999
} ?: .0
100+
101+
/** 施密特正交化 */
102+
fun Sequence<Vector>.orthogonalize(): List<Vector> =
103+
fold(mutableListOf<Vector>()) { result, v0 ->
104+
result += sequence { yield(v0); for (v in result) yield(v * -(v0 dot v)) }.sum().normalize()
105+
result
106+
}
107+
108+
/** 施密特正交化 */
109+
fun Iterable<Vector>.orthogonalize() =
110+
asSequence().orthogonalize()
111+
112+
/** 施密特正交化 */
113+
fun Array<Vector>.orthogonalize() =
114+
asSequence().orthogonalize()

src/main/kotlin/org/mechdancer/algebra/function/vector/Copy.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ fun Vector.copy(vararg change: Pair<Int, Number>): Vector {
3838
* Select some dimension of the vector, then copy them to a new vector
3939
* 从向量选取指定的维度组成新的向量
4040
*/
41-
fun Vector.select(vararg indexes: Int): Vector =
42-
toList().filterIndexed { i, _ -> i in indexes }.let(::ListVector)
41+
fun Vector.select(vararg indices: Int): Vector =
42+
toList().filterIndexed { i, _ -> i in indices }.let(::ListVector)
4343

4444
/**
4545
* Select a range of index of the dimensions, then copy them to a new vector
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.mechdancer.algebra.vector
2+
3+
import org.junit.Test
4+
import org.mechdancer.algebra.doubleEquals
5+
import org.mechdancer.algebra.function.vector.dot
6+
import org.mechdancer.algebra.function.vector.isNormalized
7+
import org.mechdancer.algebra.function.vector.orthogonalize
8+
import org.mechdancer.algebra.implement.vector.vector3D
9+
10+
class TestOrthogonalize {
11+
@Test
12+
fun testOrthogonalize() {
13+
val orthogonalized =
14+
listOf(vector3D(1, 0, 0),
15+
vector3D(1, 2, 3),
16+
vector3D(5, 6, 7)
17+
).orthogonalize()
18+
orthogonalized.forEachIndexed { i, v ->
19+
assert(v.isNormalized())
20+
for (j in 0 until i) assert(doubleEquals(.0, v dot orthogonalized[j]))
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)