BufferedTensor2D & BufferedTensor1D

This commit is contained in:
Roland Grinis 2021-03-26 20:36:21 +00:00
parent 516cd90677
commit 22b68e5ca4
5 changed files with 152 additions and 24 deletions

View File

@ -2745,6 +2745,34 @@ public class space/kscience/kmath/tensors/core/BufferedTensor : space/kscience/k
public final fun vectorSequence ()Lkotlin/sequences/Sequence;
}
public final class space/kscience/kmath/tensors/core/BufferedTensor1D : space/kscience/kmath/tensors/core/BufferedTensor, space/kscience/kmath/nd/MutableStructure1D {
public fun copy ()Lspace/kscience/kmath/structures/MutableBuffer;
public fun get (I)Ljava/lang/Object;
public fun get ([I)Ljava/lang/Object;
public fun getDimension ()I
public fun getSize ()I
public fun iterator ()Ljava/util/Iterator;
public fun set (ILjava/lang/Object;)V
public fun set ([ILjava/lang/Object;)V
}
public final class space/kscience/kmath/tensors/core/BufferedTensor2D : space/kscience/kmath/tensors/core/BufferedTensor, space/kscience/kmath/nd/MutableStructure2D {
public fun elements ()Lkotlin/sequences/Sequence;
public fun get (II)Ljava/lang/Object;
public fun get ([I)Ljava/lang/Object;
public fun getColNum ()I
public fun getColumns ()Ljava/util/List;
public fun getRowNum ()I
public fun getRows ()Ljava/util/List;
public fun getShape ()[I
public fun set (IILjava/lang/Object;)V
}
public final class space/kscience/kmath/tensors/core/BufferedTensorKt {
public static final fun as1D (Lspace/kscience/kmath/tensors/core/BufferedTensor;)Lspace/kscience/kmath/tensors/core/BufferedTensor1D;
public static final fun as2D (Lspace/kscience/kmath/tensors/core/BufferedTensor;)Lspace/kscience/kmath/tensors/core/BufferedTensor2D;
}
public final class space/kscience/kmath/tensors/core/DoubleAnalyticTensorAlgebra : space/kscience/kmath/tensors/core/DoubleTensorAlgebra, space/kscience/kmath/tensors/AnalyticTensorAlgebra {
public fun <init> ()V
public synthetic fun acos (Lspace/kscience/kmath/nd/MutableStructureND;)Lspace/kscience/kmath/nd/MutableStructureND;

View File

@ -1,23 +1,26 @@
package space.kscience.kmath.tensors.core
import space.kscience.kmath.nd.*
import space.kscience.kmath.nd.MutableStructure1D
import space.kscience.kmath.nd.MutableStructure2D
import space.kscience.kmath.structures.*
import space.kscience.kmath.tensors.TensorStructure
import kotlin.math.atanh
public open class BufferedTensor<T>(
override val shape: IntArray,
public val buffer: MutableBuffer<T>,
internal val bufferStart: Int
) : TensorStructure<T>
{
) : TensorStructure<T> {
public val linearStructure: TensorLinearStructure
get() = TensorLinearStructure(shape)
public val numel: Int
get() = linearStructure.size
internal constructor(tensor: BufferedTensor<T>) :
this(tensor.shape, tensor.buffer, tensor.bufferStart)
override fun get(index: IntArray): T = buffer[bufferStart + linearStructure.offset(index)]
override fun set(index: IntArray, value: T) {
@ -32,7 +35,7 @@ public open class BufferedTensor<T>(
override fun hashCode(): Int = 0
public fun vectorSequence(): Sequence<MutableStructure1D<T>> = sequence {
public fun vectorSequence(): Sequence<BufferedTensor1D<T>> = sequence {
check(shape.size >= 1) { "todo" }
val n = shape.size
val vectorOffset = shape[n - 1]
@ -43,7 +46,7 @@ public open class BufferedTensor<T>(
}
}
public fun matrixSequence(): Sequence<MutableStructure2D<T>> = sequence {
public fun matrixSequence(): Sequence<BufferedTensor2D<T>> = sequence {
check(shape.size >= 2) { "todo" }
val n = shape.size
val matrixOffset = shape[n - 1] * shape[n - 2]
@ -54,13 +57,13 @@ public open class BufferedTensor<T>(
}
}
public inline fun forEachVector(vectorAction : (MutableStructure1D<T>) -> Unit): Unit {
public inline fun forEachVector(vectorAction: (BufferedTensor1D<T>) -> Unit): Unit {
for (vector in vectorSequence()) {
vectorAction(vector)
}
}
public inline fun forEachMatrix(matrixAction : (MutableStructure2D<T>) -> Unit): Unit {
public inline fun forEachMatrix(matrixAction: (BufferedTensor2D<T>) -> Unit): Unit {
for (matrix in matrixSequence()) {
matrixAction(matrix)
}
@ -74,21 +77,125 @@ public class IntTensor internal constructor(
buffer: IntArray,
offset: Int = 0
) : BufferedTensor<Int>(shape, IntBuffer(buffer), offset)
{
internal constructor(bufferedTensor: BufferedTensor<Int>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
}
public class LongTensor internal constructor(
shape: IntArray,
buffer: LongArray,
offset: Int = 0
) : BufferedTensor<Long>(shape, LongBuffer(buffer), offset)
{
internal constructor(bufferedTensor: BufferedTensor<Long>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
}
public class FloatTensor internal constructor(
shape: IntArray,
buffer: FloatArray,
offset: Int = 0
) : BufferedTensor<Float>(shape, FloatBuffer(buffer), offset)
{
internal constructor(bufferedTensor: BufferedTensor<Float>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
}
public class DoubleTensor internal constructor(
shape: IntArray,
buffer: DoubleArray,
offset: Int = 0
) : BufferedTensor<Double>(shape, DoubleBuffer(buffer), offset)
{
internal constructor(bufferedTensor: BufferedTensor<Double>):
this(bufferedTensor.shape, bufferedTensor.buffer.array(), bufferedTensor.bufferStart)
}
public class BufferedTensor2D<T> internal constructor(
private val tensor: BufferedTensor<T>,
) : BufferedTensor<T>(tensor), MutableStructure2D<T> {
init {
check(shape.size == 2) {
"Shape ${shape.toList()} not compatible with DoubleTensor2D"
}
}
override val shape: IntArray
get() = tensor.shape
override val rowNum: Int
get() = shape[0]
override val colNum: Int
get() = shape[1]
override fun get(i: Int, j: Int): T = tensor[intArrayOf(i, j)]
override fun get(index: IntArray): T = tensor[index]
override fun elements(): Sequence<Pair<IntArray, T>> = tensor.elements()
override fun set(i: Int, j: Int, value: T) {
tensor[intArrayOf(i, j)] = value
}
override val rows: List<BufferedTensor1D<T>>
get() = List(rowNum) { i ->
BufferedTensor1D(
BufferedTensor(
shape = intArrayOf(colNum),
buffer = VirtualMutableBuffer(colNum) { j -> get(i, j) },
bufferStart = 0
)
)
}
override val columns: List<BufferedTensor1D<T>>
get() = List(colNum) { j ->
BufferedTensor1D(
BufferedTensor(
shape = intArrayOf(rowNum),
buffer = VirtualMutableBuffer(rowNum) { i -> get(i, j) },
bufferStart = 0
)
)
}
}
public class BufferedTensor1D<T> internal constructor(
private val tensor: BufferedTensor<T>
) : BufferedTensor<T>(tensor), MutableStructure1D<T> {
init {
check(shape.size == 1) {
"Shape ${shape.toList()} not compatible with DoubleTensor1D"
}
}
override fun get(index: IntArray): T = tensor[index]
override fun set(index: IntArray, value: T) {
tensor[index] = value
}
override val size: Int
get() = tensor.linearStructure.size
override fun get(index: Int): T = tensor[intArrayOf(index)]
override fun set(index: Int, value: T) {
tensor[intArrayOf(index)] = value
}
override fun copy(): MutableBuffer<T> = tensor.buffer.copy()
}
internal fun BufferedTensor<Int>.asIntTensor(): IntTensor = IntTensor(this)
internal fun BufferedTensor<Long>.asLongTensor(): LongTensor = LongTensor(this)
internal fun BufferedTensor<Float>.asFloatTensor(): FloatTensor = FloatTensor(this)
internal fun BufferedTensor<Double>.asDoubleTensor(): DoubleTensor = DoubleTensor(this)
public fun <T> BufferedTensor<T>.as2D(): BufferedTensor2D<T> = BufferedTensor2D(this)
public fun <T> BufferedTensor<T>.as1D(): BufferedTensor1D<T> = BufferedTensor1D(this)

View File

@ -1,8 +1,5 @@
package space.kscience.kmath.tensors.core
import space.kscience.kmath.nd.MutableStructure2D
import space.kscience.kmath.nd.Structure1D
import space.kscience.kmath.nd.Structure2D
import space.kscience.kmath.tensors.LinearOpsTensorAlgebra
import kotlin.math.sqrt
@ -153,7 +150,7 @@ public class DoubleLinearOpsTensorAlgebra :
TODO("ANDREI")
}
private fun luMatrixDet(lu: Structure2D<Double>, pivots: Structure1D<Int>): Double {
private fun luMatrixDet(lu: BufferedTensor2D<Double>, pivots: BufferedTensor1D<Int>): Double {
val m = lu.shape[0]
val sign = if((pivots[m] - m) % 2 == 0) 1.0 else -1.0
return (0 until m).asSequence().map { lu[it, it] }.fold(sign) { left, right -> left * right }
@ -180,9 +177,9 @@ public class DoubleLinearOpsTensorAlgebra :
}
private fun luMatrixInv(
lu: Structure2D<Double>,
pivots: Structure1D<Int>,
invMatrix : MutableStructure2D<Double>
lu: BufferedTensor2D<Double>,
pivots: BufferedTensor1D<Int>,
invMatrix : BufferedTensor2D<Double>
): Unit {
val m = lu.shape[0]

View File

@ -1,7 +1,6 @@
package space.kscience.kmath.tensors.core
import kotlin.math.abs
import kotlin.math.exp
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

View File

@ -1,8 +1,5 @@
package space.kscience.kmath.tensors.core
import space.kscience.kmath.nd.as1D
import space.kscience.kmath.nd.as2D
import space.kscience.kmath.structures.toDoubleArray
import kotlin.test.Test
import kotlin.test.assertEquals