MPP toString for DoubleTensor

This commit is contained in:
Roland Grinis 2021-04-16 07:45:31 +01:00
parent b7da52edb1
commit 41ac72b4fb
4 changed files with 30 additions and 15 deletions

View File

@ -4,7 +4,6 @@ import space.kscience.kmath.structures.*
import space.kscience.kmath.tensors.TensorStructure
public open class BufferedTensor<T>(
override val shape: IntArray,
public val buffer: MutableBuffer<T>,
@ -70,6 +69,9 @@ public class DoubleTensor internal constructor(
{
internal constructor(bufferedTensor: BufferedTensor<Double>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
override fun toString(): String = toPrettyString()
}
internal fun BufferedTensor<Int>.asTensor(): IntTensor = IntTensor(this)

View File

@ -190,9 +190,7 @@ internal inline fun choleskyHelper(
}
}
internal inline fun luMatrixDet(luTensor: MutableStructure2D<Double>, pivotsTensor: MutableStructure1D<Int>): Double {
val lu = luTensor.as2D()
val pivots = pivotsTensor.as1D()
internal inline fun luMatrixDet(lu: MutableStructure2D<Double>, pivots: MutableStructure1D<Int>): Double {
if (lu[0, 0] == 0.0) {
return 0.0
}

View File

@ -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 <T> BufferedTensor<T>.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<Double>()
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

View File

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