v0.3.0-dev-9 #324

Merged
altavir merged 265 commits from dev into master 2021-05-08 17:16:29 +03:00
8 changed files with 243 additions and 243 deletions
Showing only changes of commit f66ac653e0 - Show all commits

View File

@ -41,7 +41,7 @@ public class FloatTensor(
buffer: FloatArray
) : BufferedTensor<Float>(shape, FloatBuffer(buffer))
public class RealTensor(
public class DoubleTensor(
shape: IntArray,
buffer: DoubleArray
) : BufferedTensor<Double>(shape, RealBuffer(buffer))

View File

@ -0,0 +1,150 @@
package space.kscience.kmath.tensors
public class RealAnalyticTensorAlgebra:
AnalyticTensorAlgebra<Double, DoubleTensor>,
RealTensorAlgebra()
{
override fun DoubleTensor.exp(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.log(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.sqrt(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.square(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.cos(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.acos(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.cosh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.acosh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.sin(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.asin(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.sinh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.asinh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.tan(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.atan(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.tanh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.atanh(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.ceil(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.floor(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.clamp(min: Double, max: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.erf(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.erfinv(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.erfc(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.lerp(end: DoubleTensor, weight: DoubleTensor): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.lgamma(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.logit(eps: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.igamma(other: DoubleTensor): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.igammac(other: DoubleTensor): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.mvlgamma(dimensions: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.polygamma(order: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.pow(exponent: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.round(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.sigmoid(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.sinc(): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.heaviside(values: DoubleTensor): DoubleTensor {
TODO("Not yet implemented")
}
override fun DoubleTensor.trapz(xValues: DoubleTensor, dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
}
public inline fun <R> RealAnalyticTensorAlgebra(block: RealAnalyticTensorAlgebra.() -> R): R =
RealAnalyticTensorAlgebra().block()

View File

@ -1,13 +1,13 @@
package space.kscience.kmath.tensors
public class RealLinearOpsTensorAlgebra :
LinearOpsTensorAlgebra<Double, RealTensor>,
LinearOpsTensorAlgebra<Double, DoubleTensor>,
RealTensorAlgebra()
{
override fun eye(n: Int): RealTensor {
override fun eye(n: Int): DoubleTensor {
val shape = intArrayOf(n, n)
val buffer = DoubleArray(n * n) { 0.0 }
val res = RealTensor(shape, buffer)
val res = DoubleTensor(shape, buffer)
for (i in 0 until n) {
res[intArrayOf(i, i)] = 1.0
}
@ -15,16 +15,16 @@ public class RealLinearOpsTensorAlgebra :
}
override fun RealTensor.dot(other: RealTensor): RealTensor {
override fun DoubleTensor.dot(other: DoubleTensor): DoubleTensor {
TODO("Alya")
}
override fun diagonalEmbedding(diagonalEntries: RealTensor, offset: Int, dim1: Int, dim2: Int): RealTensor {
override fun diagonalEmbedding(diagonalEntries: DoubleTensor, offset: Int, dim1: Int, dim2: Int): DoubleTensor {
TODO("Alya")
}
override fun RealTensor.lu(): Pair<RealTensor, IntTensor> {
override fun DoubleTensor.lu(): Pair<DoubleTensor, IntTensor> {
// todo checks
val lu = this.copy()
val m = this.shape[0]
@ -74,7 +74,7 @@ public class RealLinearOpsTensorAlgebra :
return Pair(lu, IntTensor(intArrayOf(m), pivot))
}
override fun luPivot(lu: RealTensor, pivots: IntTensor): Triple<RealTensor, RealTensor, RealTensor> {
override fun luPivot(lu: DoubleTensor, pivots: IntTensor): Triple<DoubleTensor, DoubleTensor, DoubleTensor> {
// todo checks
val n = lu.shape[0]
val p = lu.zeroesLike()
@ -100,28 +100,28 @@ public class RealLinearOpsTensorAlgebra :
return Triple(p, l, u)
}
override fun RealTensor.det(): RealTensor {
override fun DoubleTensor.det(): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.inv(): RealTensor {
override fun DoubleTensor.inv(): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cholesky(): RealTensor {
override fun DoubleTensor.cholesky(): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.qr(): RealTensor {
override fun DoubleTensor.qr(): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.svd(): Triple<RealTensor, RealTensor, RealTensor> {
override fun DoubleTensor.svd(): Triple<DoubleTensor, DoubleTensor, DoubleTensor> {
TODO("Not yet implemented")
}
override fun RealTensor.symEig(eigenvectors: Boolean): Pair<RealTensor, RealTensor> {
override fun DoubleTensor.symEig(eigenvectors: Boolean): Pair<DoubleTensor, DoubleTensor> {
TODO("Not yet implemented")
}

View File

@ -1,146 +1,146 @@
package space.kscience.kmath.tensors
public open class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealTensor> {
public open class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, DoubleTensor> {
override fun RealTensor.value(): Double {
override fun DoubleTensor.value(): Double {
check(this.shape contentEquals intArrayOf(1)) {
"Inconsistent value for tensor of shape ${shape.toList()}"
}
return this.buffer.unsafeToDoubleArray()[0]
}
override fun zeros(shape: IntArray): RealTensor {
override fun zeros(shape: IntArray): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.zeroesLike(): RealTensor {
override fun DoubleTensor.zeroesLike(): DoubleTensor {
val shape = this.shape
val buffer = DoubleArray(this.buffer.size) { 0.0 }
return RealTensor(shape, buffer)
return DoubleTensor(shape, buffer)
}
override fun ones(shape: IntArray): RealTensor {
override fun ones(shape: IntArray): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.onesLike(): RealTensor {
override fun DoubleTensor.onesLike(): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.copy(): RealTensor {
override fun DoubleTensor.copy(): DoubleTensor {
// should be rework as soon as copy() method for NDBuffer will be available
return RealTensor(this.shape, this.buffer.unsafeToDoubleArray().copyOf())
return DoubleTensor(this.shape, this.buffer.unsafeToDoubleArray().copyOf())
}
override fun Double.plus(other: RealTensor): RealTensor {
override fun Double.plus(other: DoubleTensor): DoubleTensor {
val resBuffer = DoubleArray(other.buffer.size) { i ->
other.buffer.unsafeToDoubleArray()[i] + this
}
return RealTensor(other.shape, resBuffer)
return DoubleTensor(other.shape, resBuffer)
}
override fun RealTensor.plus(value: Double): RealTensor = value + this
override fun DoubleTensor.plus(value: Double): DoubleTensor = value + this
override fun RealTensor.plus(other: RealTensor): RealTensor {
override fun DoubleTensor.plus(other: DoubleTensor): DoubleTensor {
val broadcast = broadcastTensors(this, other)
val newThis = broadcast[0]
val newOther = broadcast[1]
val resBuffer = DoubleArray(newThis.buffer.size) { i ->
newThis.buffer.unsafeToDoubleArray()[i] + newOther.buffer.unsafeToDoubleArray()[i]
}
return RealTensor(newThis.shape, resBuffer)
return DoubleTensor(newThis.shape, resBuffer)
}
override fun RealTensor.plusAssign(value: Double) {
override fun DoubleTensor.plusAssign(value: Double) {
for (i in this.buffer.unsafeToDoubleArray().indices) {
this.buffer.unsafeToDoubleArray()[i] += value
}
}
override fun RealTensor.plusAssign(other: RealTensor) {
override fun DoubleTensor.plusAssign(other: DoubleTensor) {
//todo should be change with broadcasting
for (i in this.buffer.unsafeToDoubleArray().indices) {
this.buffer.unsafeToDoubleArray()[i] += other.buffer.unsafeToDoubleArray()[i]
}
}
override fun Double.minus(other: RealTensor): RealTensor {
override fun Double.minus(other: DoubleTensor): DoubleTensor {
val resBuffer = DoubleArray(other.buffer.size) { i ->
this - other.buffer.unsafeToDoubleArray()[i]
}
return RealTensor(other.shape, resBuffer)
return DoubleTensor(other.shape, resBuffer)
}
override fun RealTensor.minus(value: Double): RealTensor {
override fun DoubleTensor.minus(value: Double): DoubleTensor {
val resBuffer = DoubleArray(this.buffer.size) { i ->
this.buffer.unsafeToDoubleArray()[i] - value
}
return RealTensor(this.shape, resBuffer)
return DoubleTensor(this.shape, resBuffer)
}
override fun RealTensor.minus(other: RealTensor): RealTensor {
override fun DoubleTensor.minus(other: DoubleTensor): DoubleTensor {
val broadcast = broadcastTensors(this, other)
val newThis = broadcast[0]
val newOther = broadcast[1]
val resBuffer = DoubleArray(newThis.buffer.size) { i ->
newThis.buffer.unsafeToDoubleArray()[i] - newOther.buffer.unsafeToDoubleArray()[i]
}
return RealTensor(newThis.shape, resBuffer)
return DoubleTensor(newThis.shape, resBuffer)
}
override fun RealTensor.minusAssign(value: Double) {
override fun DoubleTensor.minusAssign(value: Double) {
for (i in this.buffer.unsafeToDoubleArray().indices) {
this.buffer.unsafeToDoubleArray()[i] -= value
}
}
override fun RealTensor.minusAssign(other: RealTensor) {
override fun DoubleTensor.minusAssign(other: DoubleTensor) {
TODO("Alya")
}
override fun Double.times(other: RealTensor): RealTensor {
override fun Double.times(other: DoubleTensor): DoubleTensor {
//todo should be change with broadcasting
val resBuffer = DoubleArray(other.buffer.size) { i ->
other.buffer.unsafeToDoubleArray()[i] * this
}
return RealTensor(other.shape, resBuffer)
return DoubleTensor(other.shape, resBuffer)
}
//todo should be change with broadcasting
override fun RealTensor.times(value: Double): RealTensor = value * this
override fun DoubleTensor.times(value: Double): DoubleTensor = value * this
override fun RealTensor.times(other: RealTensor): RealTensor {
override fun DoubleTensor.times(other: DoubleTensor): DoubleTensor {
//todo should be change with broadcasting
val resBuffer = DoubleArray(this.buffer.size) { i ->
this.buffer.unsafeToDoubleArray()[i] * other.buffer.unsafeToDoubleArray()[i]
}
return RealTensor(this.shape, resBuffer)
return DoubleTensor(this.shape, resBuffer)
}
override fun RealTensor.timesAssign(value: Double) {
override fun DoubleTensor.timesAssign(value: Double) {
//todo should be change with broadcasting
for (i in this.buffer.unsafeToDoubleArray().indices) {
this.buffer.unsafeToDoubleArray()[i] *= value
}
}
override fun RealTensor.timesAssign(other: RealTensor) {
override fun DoubleTensor.timesAssign(other: DoubleTensor) {
//todo should be change with broadcasting
for (i in this.buffer.unsafeToDoubleArray().indices) {
this.buffer.unsafeToDoubleArray()[i] *= other.buffer.unsafeToDoubleArray()[i]
}
}
override fun RealTensor.unaryMinus(): RealTensor {
override fun DoubleTensor.unaryMinus(): DoubleTensor {
val resBuffer = DoubleArray(this.buffer.size) { i ->
this.buffer.unsafeToDoubleArray()[i].unaryMinus()
}
return RealTensor(this.shape, resBuffer)
return DoubleTensor(this.shape, resBuffer)
}
override fun RealTensor.transpose(i: Int, j: Int): RealTensor {
override fun DoubleTensor.transpose(i: Int, j: Int): DoubleTensor {
checkTranspose(this.dimension, i, j)
val n = this.buffer.size
val resBuffer = DoubleArray(n)
@ -148,7 +148,7 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealT
val resShape = this.shape.copyOf()
resShape[i] = resShape[j].also { resShape[j] = resShape[i] }
val resTensor = RealTensor(resShape, resBuffer)
val resTensor = DoubleTensor(resShape, resBuffer)
for (offset in 0 until n) {
val oldMultiIndex = this.strides.index(offset)
@ -162,117 +162,117 @@ public open class RealTensorAlgebra : TensorPartialDivisionAlgebra<Double, RealT
}
override fun RealTensor.view(shape: IntArray): RealTensor {
return RealTensor(shape, this.buffer.unsafeToDoubleArray())
override fun DoubleTensor.view(shape: IntArray): DoubleTensor {
return DoubleTensor(shape, this.buffer.unsafeToDoubleArray())
}
override fun RealTensor.viewAs(other: RealTensor): RealTensor {
override fun DoubleTensor.viewAs(other: DoubleTensor): DoubleTensor {
return this.view(other.shape)
}
override fun RealTensor.abs(): RealTensor {
override fun DoubleTensor.abs(): DoubleTensor {
TODO("Not yet implemented")
}
override fun full(shape: IntArray, value: Double): RealTensor {
override fun full(shape: IntArray, value: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.fullLike(value: Double): RealTensor {
override fun DoubleTensor.fullLike(value: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sum(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.sum(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cumsum(dim: Int): RealTensor {
override fun DoubleTensor.cumsum(dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.prod(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.prod(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cumprod(dim: Int): RealTensor {
override fun DoubleTensor.cumprod(dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.max(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.max(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cummax(dim: Int): RealTensor {
override fun DoubleTensor.cummax(dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.min(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.min(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cummin(dim: Int): RealTensor {
override fun DoubleTensor.cummin(dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.median(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.median(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun maximum(lhs: RealTensor, rhs: RealTensor) {
override fun maximum(lhs: DoubleTensor, rhs: DoubleTensor) {
TODO("Not yet implemented")
}
override fun minimum(lhs: RealTensor, rhs: RealTensor) {
override fun minimum(lhs: DoubleTensor, rhs: DoubleTensor) {
TODO("Not yet implemented")
}
override fun RealTensor.sort(dim: Int, keepDim: Boolean, descending: Boolean): RealTensor {
override fun DoubleTensor.sort(dim: Int, keepDim: Boolean, descending: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun cat(tensors: List<RealTensor>, dim: Int): RealTensor {
override fun cat(tensors: List<DoubleTensor>, dim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.div(value: Double): RealTensor {
override fun DoubleTensor.div(value: Double): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.div(other: RealTensor): RealTensor {
override fun DoubleTensor.div(other: DoubleTensor): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.flatten(startDim: Int, endDim: Int): RealTensor {
override fun DoubleTensor.flatten(startDim: Int, endDim: Int): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.divAssign(value: Double) {
override fun DoubleTensor.divAssign(value: Double) {
TODO("Not yet implemented")
}
override fun RealTensor.divAssign(other: RealTensor) {
override fun DoubleTensor.divAssign(other: DoubleTensor) {
TODO("Not yet implemented")
}
override fun RealTensor.mean(dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.mean(dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.quantile(q: Double, dim: Int, keepDim: Boolean): RealTensor {
override fun DoubleTensor.quantile(q: Double, dim: Int, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.std(dim: Int, unbiased: Boolean, keepDim: Boolean): RealTensor {
override fun DoubleTensor.std(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): RealTensor {
override fun DoubleTensor.variance(dim: Int, unbiased: Boolean, keepDim: Boolean): DoubleTensor {
TODO("Not yet implemented")
}
override fun RealTensor.histc(bins: Int, min: Double, max: Double): RealTensor {
override fun DoubleTensor.histc(bins: Int, min: Double, max: Double): DoubleTensor {
TODO("Not yet implemented")
}

View File

@ -1,150 +0,0 @@
package space.kscience.kmath.tensors
public class RealAnalyticTensorAlgebra:
AnalyticTensorAlgebra<Double, RealTensor>,
RealTensorAlgebra()
{
override fun RealTensor.exp(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.log(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sqrt(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.square(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cos(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.acos(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.cosh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.acosh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sin(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.asin(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sinh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.asinh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.tan(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.atan(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.tanh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.atanh(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.ceil(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.floor(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.clamp(min: Double, max: Double): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.erf(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.erfinv(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.erfc(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.lerp(end: RealTensor, weight: RealTensor): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.lgamma(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.logit(eps: Double): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.igamma(other: RealTensor): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.igammac(other: RealTensor): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.mvlgamma(dimensions: Int): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.polygamma(order: Int): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.pow(exponent: Double): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.round(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sigmoid(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.sinc(): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.heaviside(values: RealTensor): RealTensor {
TODO("Not yet implemented")
}
override fun RealTensor.trapz(xValues: RealTensor, dim: Int): RealTensor {
TODO("Not yet implemented")
}
}
public inline fun <R> RealAnalyticTensorAlgebra(block: RealAnalyticTensorAlgebra.() -> R): R =
RealAnalyticTensorAlgebra().block()

View File

@ -32,13 +32,13 @@ internal inline fun broadcastShapes(vararg shapes: IntArray): IntArray {
return totalShape
}
internal inline fun broadcastTensors(vararg tensors: RealTensor): List<RealTensor> {
internal inline fun broadcastTensors(vararg tensors: DoubleTensor): List<DoubleTensor> {
val totalShape = broadcastShapes(*(tensors.map { it.shape }).toTypedArray())
val n = totalShape.reduce { acc, i -> acc * i }
val res = ArrayList<RealTensor>(0)
val res = ArrayList<DoubleTensor>(0)
for (tensor in tensors) {
val resTensor = RealTensor(totalShape, DoubleArray(n))
val resTensor = DoubleTensor(totalShape, DoubleArray(n))
for (linearIndex in 0 until n) {
val totalMultiIndex = resTensor.strides.index(linearIndex)

View File

@ -11,13 +11,13 @@ class TestRealTensor {
@Test
fun valueTest() = RealTensorAlgebra {
val value = 12.5
val tensor = RealTensor(intArrayOf(1), doubleArrayOf(value))
val tensor = DoubleTensor(intArrayOf(1), doubleArrayOf(value))
assertEquals(tensor.value(), value)
}
@Test
fun stridesTest(){
val tensor = RealTensor(intArrayOf(2,2), doubleArrayOf(3.5,5.8,58.4,2.4))
val tensor = DoubleTensor(intArrayOf(2,2), doubleArrayOf(3.5,5.8,58.4,2.4))
assertEquals(tensor[intArrayOf(0,1)], 5.8)
assertTrue(tensor.elements().map{ it.second }.toList().toDoubleArray() contentEquals tensor.buffer.toDoubleArray())
}

View File

@ -8,14 +8,14 @@ class TestRealTensorAlgebra {
@Test
fun doublePlus() = RealTensorAlgebra {
val tensor = RealTensor(intArrayOf(2), doubleArrayOf(1.0, 2.0))
val tensor = DoubleTensor(intArrayOf(2), doubleArrayOf(1.0, 2.0))
val res = 10.0 + tensor
assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(11.0,12.0))
}
@Test
fun transpose1x1() = RealTensorAlgebra {
val tensor = RealTensor(intArrayOf(1), doubleArrayOf(0.0))
val tensor = DoubleTensor(intArrayOf(1), doubleArrayOf(0.0))
val res = tensor.transpose(0, 0)
assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(0.0))
@ -24,7 +24,7 @@ class TestRealTensorAlgebra {
@Test
fun transpose3x2() = RealTensorAlgebra {
val tensor = RealTensor(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor = DoubleTensor(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val res = tensor.transpose(1, 0)
assertTrue(res.buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))
@ -33,7 +33,7 @@ class TestRealTensorAlgebra {
@Test
fun transpose1x2x3() = RealTensorAlgebra {
val tensor = RealTensor(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor = DoubleTensor(intArrayOf(1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val res01 = tensor.transpose(0, 1)
val res02 = tensor.transpose(0, 2)
val res12 = tensor.transpose(1, 2)
@ -60,9 +60,9 @@ class TestRealTensorAlgebra {
@Test
fun broadcastTensors() = RealTensorAlgebra {
val tensor1 = RealTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = RealTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = RealTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
val tensor1 = DoubleTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = DoubleTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = DoubleTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
val res = broadcastTensors(tensor1, tensor2, tensor3)
@ -77,9 +77,9 @@ class TestRealTensorAlgebra {
@Test
fun minusTensor() = RealTensorAlgebra {
val tensor1 = RealTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = RealTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = RealTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
val tensor1 = DoubleTensor(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
val tensor2 = DoubleTensor(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
val tensor3 = DoubleTensor(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
assertTrue((tensor2 - tensor1).shape contentEquals intArrayOf(2, 3))
assertTrue((tensor2 - tensor1).buffer.unsafeToDoubleArray() contentEquals doubleArrayOf(9.0, 18.0, 27.0, 6.0, 15.0, 24.0))