forked from kscience/kmath
commit
512ed1139c
@ -9,17 +9,17 @@ import space.kscience.kmath.tensors.api.Tensor
|
||||
import space.kscience.kmath.tensors.core.internal.tensor
|
||||
|
||||
/**
|
||||
* Casts [Tensor<Double>] to [DoubleTensor]
|
||||
* Casts [Tensor] of [Double] to [DoubleTensor]
|
||||
*/
|
||||
public fun Tensor<Double>.toDoubleTensor(): DoubleTensor = this.tensor
|
||||
|
||||
/**
|
||||
* Casts [Tensor<Int>] to [IntTensor]
|
||||
* Casts [Tensor] of [Int] to [IntTensor]
|
||||
*/
|
||||
public fun Tensor<Int>.toIntTensor(): IntTensor = this.tensor
|
||||
|
||||
/**
|
||||
* @return [DoubleArray] of tensor elements
|
||||
* Returns [DoubleArray] of tensor elements
|
||||
*/
|
||||
public fun DoubleTensor.toDoubleArray(): DoubleArray {
|
||||
return DoubleArray(numElements) { i ->
|
||||
@ -28,42 +28,10 @@ public fun DoubleTensor.toDoubleArray(): DoubleArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return [IntArray] of tensor elements
|
||||
* Returns [IntArray] of tensor elements
|
||||
*/
|
||||
public fun IntTensor.toIntArray(): IntArray {
|
||||
return IntArray(numElements) { i ->
|
||||
mutableBuffer[bufferStart + i]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts [Array<DoubleArray>] to [DoubleTensor]
|
||||
*/
|
||||
public fun Array<DoubleArray>.toDoubleTensor(): DoubleTensor {
|
||||
val n = size
|
||||
check(n > 0) { "An empty array cannot be casted to tensor" }
|
||||
val m = first().size
|
||||
check(m > 0) { "Inner arrays must have at least 1 argument" }
|
||||
check(all { size == m }) { "Inner arrays must be the same size" }
|
||||
|
||||
val shape = intArrayOf(n, m)
|
||||
val buffer = this.flatMap { arr -> arr.map { it } }.toDoubleArray()
|
||||
|
||||
return DoubleTensor(shape, buffer, 0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts [Array<IntArray>] to [IntTensor]
|
||||
*/
|
||||
public fun Array<IntArray>.toIntTensor(): IntTensor {
|
||||
val n = size
|
||||
check(n > 0) { "An empty array cannot be casted to tensor" }
|
||||
val m = first().size
|
||||
check(m > 0) { "Inner arrays must have at least 1 argument" }
|
||||
check(all { size == m }) { "Inner arrays must be the same size" }
|
||||
|
||||
val shape = intArrayOf(n, m)
|
||||
val buffer = this.flatMap { arr -> arr.map { it } }.toIntArray()
|
||||
|
||||
return IntTensor(shape, buffer, 0)
|
||||
}
|
||||
|
@ -102,4 +102,4 @@ internal class TestBroadcasting {
|
||||
assertTrue(tensor32.mutableBuffer.array() contentEquals doubleArrayOf(490.0, 480.0, 470.0))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -155,4 +155,4 @@ internal class TestDoubleAnalyticTensorAlgebra {
|
||||
)}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -194,5 +194,3 @@ private fun DoubleTensorAlgebra.testSVDFor(tensor: DoubleTensor, epsilon: Double
|
||||
|
||||
assertTrue(tensor.eq(tensorSVD, epsilon))
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,4 +86,4 @@ internal class TestDoubleTensor {
|
||||
assertEquals(ndArray[intArrayOf(0)], 1.0)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -164,4 +164,4 @@ internal class TestDoubleTensorAlgebra {
|
||||
assertFalse(tensor1.eq(tensor3))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user