From 835d64d797411141a1b21948566651d6dace882e Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Sat, 16 Jan 2021 19:29:47 +0000 Subject: [PATCH] Initial draft for TensorAlgebra --- .gitignore | 1 + .../kmath/structures/TensorAlgebra.kt | 49 +++++++++++++++++++ .../kmath/structures/TensorStructure.kt | 11 +++++ 3 files changed, 61 insertions(+) create mode 100644 kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt create mode 100644 kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorStructure.kt diff --git a/.gitignore b/.gitignore index bade7f08c..ea8e65fb4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ build/ out/ .idea/ +.vscode/ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) !gradle-wrapper.jar diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt new file mode 100644 index 000000000..f960742ee --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt @@ -0,0 +1,49 @@ +package kscience.kmath.structures + + +import kscience.kmath.operations.* + +public interface TensorAlgebra> : Ring { + + public operator fun T.plus(other: TorchTensorType): TorchTensorType + public operator fun TorchTensorType.plus(value: T): TorchTensorType + public operator fun TorchTensorType.plusAssign(value: T): Unit + public operator fun TorchTensorType.plusAssign(b: TorchTensorType): Unit + + public operator fun T.minus(other: TorchTensorType): TorchTensorType + public operator fun TorchTensorType.minus(value: T): TorchTensorType + public operator fun TorchTensorType.minusAssign(value: T): Unit + public operator fun TorchTensorType.minusAssign(b: TorchTensorType): Unit + + public operator fun T.times(other: TorchTensorType): TorchTensorType + public operator fun TorchTensorType.times(value: T): TorchTensorType + public operator fun TorchTensorType.timesAssign(value: T): Unit + public operator fun TorchTensorType.timesAssign(b: TorchTensorType): Unit + + public infix fun TorchTensorType.dot(b: TorchTensorType): TorchTensorType + + public fun diagonalEmbedding( + diagonalEntries: TorchTensorType, + offset: Int = 0, dim1: Int = -2, dim2: Int = -1 + ): TorchTensorType + + public fun TorchTensorType.transpose(i: Int, j: Int): TorchTensorType + public fun TorchTensorType.view(shape: IntArray): TorchTensorType + + public fun TorchTensorType.abs(): TorchTensorType + public fun TorchTensorType.sum(): TorchTensorType + +} + +public interface TensorFieldAlgebra> : + TensorAlgebra, Field { + + public operator fun TorchTensorType.divAssign(b: TorchTensorType) + + public fun TorchTensorType.exp(): TorchTensorType + public fun TorchTensorType.log(): TorchTensorType + + public fun TorchTensorType.svd(): Triple + public fun TorchTensorType.symEig(eigenvectors: Boolean = true): Pair + +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorStructure.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorStructure.kt new file mode 100644 index 000000000..6e3ac0b7b --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorStructure.kt @@ -0,0 +1,11 @@ +package kscience.kmath.structures + +public abstract class TensorStructure: MutableNDStructure { + + // A tensor can have empty shape, in which case it represents just a value + public abstract fun value(): T + + // Tensors are mutable and might hold shared resources + override fun equals(other: Any?): Boolean = false + override fun hashCode(): Int = 0 +} \ No newline at end of file