From b7da52edb196769ac5afb42ae5dbdca6bedd58f5 Mon Sep 17 00:00:00 2001 From: Andrei Kislitsyn Date: Thu, 15 Apr 2021 23:10:15 +0300 Subject: [PATCH] pretty printer --- .../kscience/kmath/tensors/core/utils.kt | 48 +++++++++++++- .../src/jvmMain/kotlin/tensorPrettyPrinter.kt | 64 +++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt index 392abd1c2..e67591c9e 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt @@ -1,5 +1,7 @@ package space.kscience.kmath.tensors.core +import space.kscience.kmath.nd.as1D +import space.kscience.kmath.nd.as2D import space.kscience.kmath.samplers.GaussianSampler import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.structures.* @@ -57,4 +59,48 @@ internal inline fun minusIndexFrom(n: Int, i: Int) : Int = if (i >= 0) i else { ii } -internal inline fun BufferedTensor.minusIndex(i: Int): Int = minusIndexFrom(this.linearStructure.dim, i) \ No newline at end of file +internal inline fun BufferedTensor.minusIndex(i: Int): Int = minusIndexFrom(this.linearStructure.dim, i) + +public fun DoubleTensor.toPrettyString(): String = buildString { + var offset = 0 + val shape = this@toPrettyString.shape + val linearStructure = this@toPrettyString.linearStructure + var vectorSize = shape.last() + val initString = "DoubleTensor(\n" + append(initString) + var charOffset = 3 + for (vector in vectorSequence()) { + append(" ".repeat(charOffset)) + val index = linearStructure.index(offset) + for (ind in index.reversed()) { + if (ind != 0) { + break + } + append("[") + charOffset += 1 + } + // todo refactor + val values = mutableListOf() + for (i in 0 until vectorSize) { + values.add(vector[intArrayOf(i)]) + } + // todo apply exp formatting + append(values.joinToString(", ")) + append("]") + charOffset -= 1 + for ((ind, maxInd) in index.reversed().zip(shape.reversed()).drop(1)){ + if (ind != maxInd - 1) { + break + } + append("]") + charOffset -=1 + } + offset += vectorSize + // todo refactor + if (this@toPrettyString.numel == offset) { + break + } + append(",\n") + } + append("\n)") +} diff --git a/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt b/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt new file mode 100644 index 000000000..669a5494b --- /dev/null +++ b/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt @@ -0,0 +1,64 @@ + +import space.kscience.kmath.tensors.core.DoubleTensor +import space.kscience.kmath.tensors.core.vectorSequence +import java.lang.StringBuilder + +internal fun format(value: Double, digits: Int = 4): String { + val res = "%.${digits}e".format(value).replace(',', '.') + if (value < 0.0) { + return res + } + return StringBuilder().append(" ").append(res).toString() +} + +public fun DoubleTensor.toPrettyString(): String { + val builder = StringBuilder() + with(builder) { + var offset = 0 + val shape = this@toPrettyString.shape + val linearStructure = this@toPrettyString.linearStructure + var vectorSize = shape.last() + val initString = "DoubleTensor(\n" + append(initString) + var charOffset = 3 + for (vector in vectorSequence()) { + append(" ".repeat(charOffset)) + val index = linearStructure.index(offset) + for (ind in index.reversed()) { + if (ind != 0) { + break + } + append("[") + charOffset += 1 + } + // todo refactor + val values = mutableListOf() + for (i in 0 until vectorSize) { + values.add(vector[intArrayOf(i)]) + } + append(values.map { format(it) }.joinToString(", ")) + append("]") + charOffset -= 1 + for (i in shape.size - 2 downTo 0){ + val ind = index[i] + val maxInd = shape[i] + if (ind != maxInd - 1) { + break + } + append("]") + charOffset -=1 + } + offset += vectorSize + // todo refactor + if (this@toPrettyString.numel == offset) { + break + } + append(",\n") + } + append("\n)") + } + return builder.toString() +} + + + -- 2.34.1