KMP library for tensors #300

Merged
grinisrit merged 215 commits from feature/tensor-algebra into dev 2021-05-08 09:48:04 +03:00
3 changed files with 44 additions and 25 deletions
Showing only changes of commit 90f4ff06fd - Show all commits

View File

@ -1,6 +1,5 @@
package space.kscience.kmath.tensors package space.kscience.kmath.tensors
import space.kscience.kmath.linear.LupDecompositionFeature
import space.kscience.kmath.nd.MutableNDBuffer import space.kscience.kmath.nd.MutableNDBuffer
import space.kscience.kmath.structures.RealBuffer import space.kscience.kmath.structures.RealBuffer
import space.kscience.kmath.structures.array import space.kscience.kmath.structures.array
@ -15,6 +14,9 @@ public class RealTensor(
TensorStrides(shape), TensorStrides(shape),
RealBuffer(buffer) RealBuffer(buffer)
) { ) {
/*
* TODO: Andrei remove item()
*/
override fun item(): Double { override fun item(): Double {
check(buffer.size > 0) { "The tensor is empty" } check(buffer.size > 0) { "The tensor is empty" }
return buffer[0] return buffer[0]
@ -24,6 +26,10 @@ public class RealTensor(
public class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealTensor> { public class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealTensor> {
override fun RealTensor.value(): Double {
TODO("Andrei")
}
override fun Double.plus(other: RealTensor): RealTensor { override fun Double.plus(other: RealTensor): RealTensor {
val n = other.buffer.size val n = other.buffer.size
val arr = other.buffer.array val arr = other.buffer.array
@ -34,95 +40,95 @@ public class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealTensor
} }
override fun RealTensor.plus(value: Double): RealTensor { override fun RealTensor.plus(value: Double): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.plus(other: RealTensor): RealTensor { override fun RealTensor.plus(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.plusAssign(value: Double) { override fun RealTensor.plusAssign(value: Double) {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.plusAssign(other: RealTensor) { override fun RealTensor.plusAssign(other: RealTensor) {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun Double.minus(other: RealTensor): RealTensor { override fun Double.minus(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.minus(value: Double): RealTensor { override fun RealTensor.minus(value: Double): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.minus(other: RealTensor): RealTensor { override fun RealTensor.minus(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.minusAssign(value: Double) { override fun RealTensor.minusAssign(value: Double) {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.minusAssign(other: RealTensor) { override fun RealTensor.minusAssign(other: RealTensor) {
TODO("Not yet implemented") TODO("Alya")
} }
override fun Double.times(other: RealTensor): RealTensor { override fun Double.times(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.times(value: Double): RealTensor { override fun RealTensor.times(value: Double): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.times(other: RealTensor): RealTensor { override fun RealTensor.times(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.timesAssign(value: Double) { override fun RealTensor.timesAssign(value: Double) {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.timesAssign(other: RealTensor) { override fun RealTensor.timesAssign(other: RealTensor) {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.unaryMinus(): RealTensor { override fun RealTensor.unaryMinus(): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.dot(other: RealTensor): RealTensor { override fun RealTensor.dot(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.dotAssign(other: RealTensor) { override fun RealTensor.dotAssign(other: RealTensor) {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.dotRightAssign(other: RealTensor) { override fun RealTensor.dotRightAssign(other: RealTensor) {
TODO("Not yet implemented") TODO("Alya")
} }
override fun diagonalEmbedding(diagonalEntries: RealTensor, offset: Int, dim1: Int, dim2: Int): RealTensor { override fun diagonalEmbedding(diagonalEntries: RealTensor, offset: Int, dim1: Int, dim2: Int): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.transpose(i: Int, j: Int): RealTensor { override fun RealTensor.transpose(i: Int, j: Int): RealTensor {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.transposeAssign(i: Int, j: Int) { override fun RealTensor.transposeAssign(i: Int, j: Int) {
TODO("Not yet implemented") TODO("Alya")
} }
override fun RealTensor.view(shape: IntArray): RealTensor { override fun RealTensor.view(shape: IntArray): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.view_as(other: RealTensor): RealTensor { override fun RealTensor.view_as(other: RealTensor): RealTensor {
TODO("Not yet implemented") TODO("Andrei")
} }
override fun RealTensor.abs(): RealTensor { override fun RealTensor.abs(): RealTensor {
@ -174,6 +180,10 @@ public class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealTensor
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun lu_unpack(A_LU: RealTensor, pivots: RealTensor): Triple<RealTensor, RealTensor, RealTensor> {
TODO("Not yet implemented")
}
override fun RealTensor.svd(): Triple<RealTensor, RealTensor, RealTensor> { override fun RealTensor.svd(): Triple<RealTensor, RealTensor, RealTensor> {
/** /**
* Main first task for @AlyaNovikova * Main first task for @AlyaNovikova

View File

@ -3,6 +3,8 @@ package space.kscience.kmath.tensors
// https://proofwiki.org/wiki/Definition:Algebra_over_Ring // https://proofwiki.org/wiki/Definition:Algebra_over_Ring
public interface TensorAlgebra<T, TensorType : TensorStructure<T>> { public interface TensorAlgebra<T, TensorType : TensorStructure<T>> {
public fun TensorType.value(): T
public operator fun T.plus(other: TensorType): TensorType public operator fun T.plus(other: TensorType): TensorType
public operator fun TensorType.plus(value: T): TensorType public operator fun TensorType.plus(value: T): TensorType
public operator fun TensorType.plus(other: TensorType): TensorType public operator fun TensorType.plus(other: TensorType): TensorType
@ -69,6 +71,9 @@ public interface TensorPartialDivisionAlgebra<T, TensorType : TensorStructure<T>
//https://pytorch.org/docs/stable/generated/torch.lu.html //https://pytorch.org/docs/stable/generated/torch.lu.html
public fun TensorType.lu(): Pair<TensorType, TensorType> public fun TensorType.lu(): Pair<TensorType, TensorType>
//https://pytorch.org/docs/stable/generated/torch.lu_unpack.html
public fun lu_unpack(A_LU: TensorType, pivots: TensorType): Triple<TensorType, TensorType, TensorType>
//https://pytorch.org/docs/stable/generated/torch.svd.html //https://pytorch.org/docs/stable/generated/torch.svd.html
public fun TensorType.svd(): Triple<TensorType, TensorType, TensorType> public fun TensorType.svd(): Triple<TensorType, TensorType, TensorType>

View File

@ -3,6 +3,10 @@ package space.kscience.kmath.tensors
import space.kscience.kmath.nd.MutableNDStructure import space.kscience.kmath.nd.MutableNDStructure
public interface TensorStructure<T> : MutableNDStructure<T> { public interface TensorStructure<T> : MutableNDStructure<T> {
/*
* TODO: Andrei remove item() and value()
*/
public fun item(): T public fun item(): T
// A tensor can have empty shape, in which case it represents just a value // A tensor can have empty shape, in which case it represents just a value