div operator

This commit is contained in:
Roland Grinis 2021-04-23 14:22:52 +01:00
parent 559e8b24ab
commit 287e2aeba2
2 changed files with 7 additions and 4 deletions

View File

@ -8,6 +8,7 @@ package space.kscience.kmath.tensors.api
// https://proofwiki.org/wiki/Definition:Division_Algebra
public interface TensorPartialDivisionAlgebra<T> :
TensorAlgebra<T> {
public operator fun T.div(other: TensorStructure<T>): TensorStructure<T>
public operator fun TensorStructure<T>.div(value: T): TensorStructure<T>
public operator fun TensorStructure<T>.div(other: TensorStructure<T>): TensorStructure<T>
public operator fun TensorStructure<T>.divAssign(value: T)

View File

@ -178,13 +178,15 @@ public open class DoubleTensorAlgebra : TensorPartialDivisionAlgebra<Double> {
}
}
override fun TensorStructure<Double>.div(value: Double): DoubleTensor {
val resBuffer = DoubleArray(tensor.numElements) { i ->
tensor.buffer.array()[tensor.bufferStart + i] / value
override fun Double.div(other: TensorStructure<Double>): DoubleTensor {
val resBuffer = DoubleArray(other.tensor.numElements) { i ->
other.tensor.buffer.array()[other.tensor.bufferStart + i] / this
}
return DoubleTensor(tensor.shape, resBuffer)
return DoubleTensor(other.shape, resBuffer)
}
override fun TensorStructure<Double>.div(value: Double): DoubleTensor = value / tensor
override fun TensorStructure<Double>.div(other: TensorStructure<Double>): DoubleTensor {
checkShapesCompatible(tensor, other)
val resBuffer = DoubleArray(tensor.numElements) { i ->