pretty printer

This commit is contained in:
Andrei Kislitsyn 2021-04-15 23:10:15 +03:00
parent 0fa73e1e9e
commit b7da52edb1
2 changed files with 111 additions and 1 deletions

View File

@ -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 <T> BufferedTensor<T>.minusIndex(i: Int): Int = minusIndexFrom(this.linearStructure.dim, i)
internal inline fun <T> BufferedTensor<T>.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<Double>()
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)")
}

View File

@ -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<Double>()
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()
}