diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/tensors/TensorAlgebra.kt similarity index 76% rename from kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt rename to kmath-core/src/commonMain/kotlin/kscience/kmath/tensors/TensorAlgebra.kt index ca7619429..c40544579 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/TensorAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/tensors/TensorAlgebra.kt @@ -1,32 +1,36 @@ -package kscience.kmath.structures +package kscience.kmath.tensors -public interface TensorStructure : MutableNDStructure { - // A tensor can have empty shape, in which case it represents just a value - public abstract fun value(): T -} +import kscience.kmath.operations.Ring +import kscience.kmath.structures.MutableNDStructure + +public typealias Tensor = MutableNDStructure + +public val Tensor.value: T + get() { + require(shape.contentEquals(intArrayOf(1))) { "Value available only for a tensor with no dimensions" } + return get(intArrayOf(0)) + } // https://proofwiki.org/wiki/Definition:Algebra_over_Ring - -public interface TensorAlgebra> { +/** + * To be moved to a separate project + */ +public interface TensorAlgebra> : Ring { public operator fun T.plus(other: TensorType): TensorType public operator fun TensorType.plus(value: T): TensorType - public operator fun TensorType.plus(other: TensorType): TensorType public operator fun TensorType.plusAssign(value: T): Unit public operator fun TensorType.plusAssign(other: TensorType): Unit public operator fun T.minus(other: TensorType): TensorType public operator fun TensorType.minus(value: T): TensorType - public operator fun TensorType.minus(other: TensorType): TensorType public operator fun TensorType.minusAssign(value: T): Unit public operator fun TensorType.minusAssign(other: TensorType): Unit public operator fun T.times(other: TensorType): TensorType public operator fun TensorType.times(value: T): TensorType - public operator fun TensorType.times(other: TensorType): TensorType public operator fun TensorType.timesAssign(value: T): Unit public operator fun TensorType.timesAssign(other: TensorType): Unit - public operator fun TensorType.unaryMinus(): TensorType public infix fun TensorType.dot(other: TensorType): TensorType @@ -35,7 +39,7 @@ public interface TensorAlgebra> { public fun diagonalEmbedding( diagonalEntries: TensorType, - offset: Int = 0, dim1: Int = -2, dim2: Int = -1 + offset: Int = 0, dim1: Int = -2, dim2: Int = -1, ): TensorType public fun TensorType.transpose(i: Int, j: Int): TensorType @@ -51,8 +55,7 @@ public interface TensorAlgebra> { // https://proofwiki.org/wiki/Definition:Division_Algebra -public interface TensorPartialDivisionAlgebra> : - TensorAlgebra { +public interface TensorPartialDivisionAlgebra> : TensorAlgebra { public operator fun TensorType.div(other: TensorType): TensorType public operator fun TensorType.divAssign(other: TensorType)