From 29ed51c50a0cb79c811cf040b086d994366e901d Mon Sep 17 00:00:00 2001 From: margarita0303 Date: Sat, 30 Jul 2022 19:03:43 +0300 Subject: [PATCH] added new parameter iterations to svdGolubKahan --- .../space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt | 4 ++-- .../space/kscience/kmath/tensors/core/internal/linUtils.kt | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index 8b5e9e470..742a3d7a7 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -837,7 +837,7 @@ public open class DoubleTensorAlgebra : return this.svdGolubKahan() } - public fun StructureND.svdGolubKahan(): Triple { + public fun StructureND.svdGolubKahan(iterations: Int = 30): Triple { val size = tensor.dimension val commonShape = tensor.shape.sliceArray(0 until size - 2) val (n, m) = tensor.shape.sliceArray(size - 2 until size) @@ -859,7 +859,7 @@ public open class DoubleTensorAlgebra : .slice(matrix.bufferStart until matrix.bufferStart + matrixSize) .toDoubleArray() ) - curMatrix.as2D().svdGolubKahanHelper(uTensors[index].as2D(), sTensorVectors[index], vTensors[index].as2D()) + curMatrix.as2D().svdGolubKahanHelper(uTensors[index].as2D(), sTensorVectors[index], vTensors[index].as2D(), iterations) } return Triple(uTensor.transpose(), sTensor, vTensor) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt index a6372bc65..0f58cf47c 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt @@ -372,7 +372,8 @@ private fun SIGN(a: Double, b: Double): Double { else return -abs(a) } -internal fun MutableStructure2D.svdGolubKahanHelper(u: MutableStructure2D, w: BufferedTensor, v: MutableStructure2D) { +internal fun MutableStructure2D.svdGolubKahanHelper(u: MutableStructure2D, w: BufferedTensor, + v: MutableStructure2D, iterations: Int) { val shape = this.shape val m = shape.component1() val n = shape.component2() @@ -531,7 +532,7 @@ internal fun MutableStructure2D.svdGolubKahanHelper(u: MutableStructure2 var z = 0.0 var x = 0.0 for (k in n - 1 downTo 0) { - for (its in 1 until 30) { + for (its in 1 until iterations) { flag = 1 for (newl in k downTo 0) { nm = newl - 1