v0.3.0-dev-9 #324

Merged
altavir merged 265 commits from dev into master 2021-05-08 17:16:29 +03:00
4 changed files with 30 additions and 15 deletions
Showing only changes of commit 41ac72b4fb - Show all commits

View File

@ -4,7 +4,6 @@ import space.kscience.kmath.structures.*
import space.kscience.kmath.tensors.TensorStructure import space.kscience.kmath.tensors.TensorStructure
public open class BufferedTensor<T>( public open class BufferedTensor<T>(
override val shape: IntArray, override val shape: IntArray,
public val buffer: MutableBuffer<T>, public val buffer: MutableBuffer<T>,
@ -70,6 +69,9 @@ public class DoubleTensor internal constructor(
{ {
internal constructor(bufferedTensor: BufferedTensor<Double>): internal constructor(bufferedTensor: BufferedTensor<Double>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart) this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
override fun toString(): String = toPrettyString()
} }
internal fun BufferedTensor<Int>.asTensor(): IntTensor = IntTensor(this) 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 { internal inline fun luMatrixDet(lu: MutableStructure2D<Double>, pivots: MutableStructure1D<Int>): Double {
val lu = luTensor.as2D()
val pivots = pivotsTensor.as1D()
if (lu[0, 0] == 0.0) { if (lu[0, 0] == 0.0) {
return 0.0 return 0.0
} }

View File

@ -1,11 +1,11 @@
package space.kscience.kmath.tensors.core package space.kscience.kmath.tensors.core
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.nd.as1D import space.kscience.kmath.nd.as1D
import space.kscience.kmath.nd.as2D
import space.kscience.kmath.samplers.GaussianSampler import space.kscience.kmath.samplers.GaussianSampler
import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.stat.RandomGenerator
import space.kscience.kmath.structures.* 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]. * 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) 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 var offset = 0
val shape = this@toPrettyString.shape val shape = this@toPrettyString.shape
val linearStructure = this@toPrettyString.linearStructure val linearStructure = this@toPrettyString.linearStructure
@ -79,12 +97,9 @@ public fun DoubleTensor.toPrettyString(): String = buildString {
append("[") append("[")
charOffset += 1 charOffset += 1
} }
// todo refactor
val values = mutableListOf<Double>() val values = vector.as1D().toMutableList().map(::format)
for (i in 0 until vectorSize) {
values.add(vector[intArrayOf(i)])
}
// todo apply exp formatting
append(values.joinToString(", ")) append(values.joinToString(", "))
append("]") append("]")
charOffset -= 1 charOffset -= 1

View File

@ -2,7 +2,7 @@
import space.kscience.kmath.tensors.core.DoubleTensor import space.kscience.kmath.tensors.core.DoubleTensor
import space.kscience.kmath.tensors.core.vectorSequence import space.kscience.kmath.tensors.core.vectorSequence
import java.lang.StringBuilder import java.lang.StringBuilder
/*
internal fun format(value: Double, digits: Int = 4): String { internal fun format(value: Double, digits: Int = 4): String {
val res = "%.${digits}e".format(value).replace(',', '.') val res = "%.${digits}e".format(value).replace(',', '.')
if (value < 0.0) { if (value < 0.0) {
@ -59,6 +59,6 @@ public fun DoubleTensor.toPrettyString(): String {
} }
return builder.toString() return builder.toString()
} }
*/