diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt index 9a4d13d2c..9e393f1a8 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BufferedTensor.kt @@ -4,7 +4,6 @@ import space.kscience.kmath.structures.* import space.kscience.kmath.tensors.TensorStructure - public open class BufferedTensor( override val shape: IntArray, public val buffer: MutableBuffer, @@ -70,6 +69,9 @@ public class DoubleTensor internal constructor( { internal constructor(bufferedTensor: BufferedTensor): this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart) + + override fun toString(): String = toPrettyString() + } internal fun BufferedTensor.asTensor(): IntTensor = IntTensor(this) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/linutils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/linutils.kt index ee159b614..ba5e0caaf 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/linutils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/linutils.kt @@ -190,9 +190,7 @@ internal inline fun choleskyHelper( } } -internal inline fun luMatrixDet(luTensor: MutableStructure2D, pivotsTensor: MutableStructure1D): Double { - val lu = luTensor.as2D() - val pivots = pivotsTensor.as1D() +internal inline fun luMatrixDet(lu: MutableStructure2D, pivots: MutableStructure1D): Double { if (lu[0, 0] == 0.0) { return 0.0 } 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 e67591c9e..daab79016 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,11 +1,11 @@ package space.kscience.kmath.tensors.core +import space.kscience.kmath.misc.UnstableKMathAPI 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.* -import kotlin.math.sqrt +import kotlin.math.* /** * Returns a reference to [IntArray] containing all of the elements of this [Buffer]. @@ -61,7 +61,25 @@ internal inline fun minusIndexFrom(n: Int, i: Int) : Int = if (i >= 0) i else { internal inline fun BufferedTensor.minusIndex(i: Int): Int = minusIndexFrom(this.linearStructure.dim, i) -public fun DoubleTensor.toPrettyString(): String = buildString { +internal inline fun format(value: Double, digits: Int = 4): String { + val ten = 10.0 + val approxOrder = ceil(log10(abs(value))).toInt() + val order = if( + ((value % ten) == 0.0) or + (value == 1.0) or + ((1/value) % ten == 0.0)) approxOrder else approxOrder - 1 + + val lead = value / ten.pow(order) + val leadDisplay = round(lead*ten.pow(digits)) / ten.pow(digits) + val orderDisplay = if(order >= 0) "+$order" else "$order" + val valueDisplay = "${leadDisplay}E$orderDisplay" + val res = if(value < 0.0) valueDisplay else " $valueDisplay" + val fLength = digits + 6 + val endSpace = " ".repeat(fLength - res.length) + return "$res$endSpace" +} + +internal inline fun DoubleTensor.toPrettyString(): String = buildString { var offset = 0 val shape = this@toPrettyString.shape val linearStructure = this@toPrettyString.linearStructure @@ -79,12 +97,9 @@ public fun DoubleTensor.toPrettyString(): String = buildString { append("[") charOffset += 1 } - // todo refactor - val values = mutableListOf() - for (i in 0 until vectorSize) { - values.add(vector[intArrayOf(i)]) - } - // todo apply exp formatting + + val values = vector.as1D().toMutableList().map(::format) + append(values.joinToString(", ")) append("]") charOffset -= 1 diff --git a/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt b/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt index 669a5494b..d3ce5933e 100644 --- a/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt +++ b/kmath-tensors/src/jvmMain/kotlin/tensorPrettyPrinter.kt @@ -2,7 +2,7 @@ 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) { @@ -59,6 +59,6 @@ public fun DoubleTensor.toPrettyString(): String { } return builder.toString() } - +*/