forked from kscience/kmath
invoke refactor
This commit is contained in:
parent
ac6608b5b4
commit
48d86fac56
@ -9,12 +9,6 @@ import space.kscience.kmath.nd.Strides
|
||||
import kotlin.math.max
|
||||
|
||||
|
||||
internal fun offsetFromIndex(index: IntArray, shape: IntArray, strides: IntArray): Int =
|
||||
index.mapIndexed { i, value ->
|
||||
if (value < 0 || value >= shape[i]) throw IndexOutOfBoundsException("Index $value out of shape bounds: (0,${shape[i]})")
|
||||
value * strides[i]
|
||||
}.sum()
|
||||
|
||||
internal fun stridesFromShape(shape: IntArray): IntArray {
|
||||
val nDim = shape.size
|
||||
val res = IntArray(nDim)
|
||||
|
@ -241,7 +241,7 @@ internal fun DoubleLinearOpsTensorAlgebra.qrHelper(
|
||||
}
|
||||
}
|
||||
}
|
||||
r[j, j] = DoubleAnalyticTensorAlgebra.invoke { (v dot v).sqrt().value() }
|
||||
r[j, j] = DoubleAnalyticTensorAlgebra { (v dot v).sqrt().value() }
|
||||
for (i in 0 until n) {
|
||||
qM[i, j] = vv[i] / r[j, j]
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import kotlin.test.assertTrue
|
||||
internal class TestBroadcasting {
|
||||
|
||||
@Test
|
||||
fun broadcastShapes() = DoubleTensorAlgebra.invoke {
|
||||
fun broadcastShapes() = DoubleTensorAlgebra {
|
||||
assertTrue(
|
||||
broadcastShapes(
|
||||
intArrayOf(2, 3), intArrayOf(1, 3), intArrayOf(1, 1, 1)
|
||||
@ -24,7 +24,7 @@ internal class TestBroadcasting {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun broadcastTo() = DoubleTensorAlgebra.invoke {
|
||||
fun broadcastTo() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
|
||||
|
||||
@ -34,7 +34,7 @@ internal class TestBroadcasting {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun broadcastTensors() = DoubleTensorAlgebra.invoke {
|
||||
fun broadcastTensors() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
|
||||
val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
|
||||
@ -51,7 +51,7 @@ internal class TestBroadcasting {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun broadcastOuterTensors() = DoubleTensorAlgebra.invoke {
|
||||
fun broadcastOuterTensors() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor2 = fromArray(intArrayOf(1, 3), doubleArrayOf(10.0, 20.0, 30.0))
|
||||
val tensor3 = fromArray(intArrayOf(1, 1, 1), doubleArrayOf(500.0))
|
||||
@ -68,7 +68,7 @@ internal class TestBroadcasting {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun broadcastOuterTensorsShapes() = DoubleTensorAlgebra.invoke {
|
||||
fun broadcastOuterTensorsShapes() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 1, 3, 2, 3), DoubleArray(2 * 1 * 3 * 2 * 3) {0.0})
|
||||
val tensor2 = fromArray(intArrayOf(4, 2, 5, 1, 3, 3), DoubleArray(4 * 2 * 5 * 1 * 3 * 3) {0.0})
|
||||
val tensor3 = fromArray(intArrayOf(1, 1), doubleArrayOf(500.0))
|
||||
|
@ -27,7 +27,7 @@ internal class TestDoubleAnalyticTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExp() = DoubleAnalyticTensorAlgebra.invoke {
|
||||
fun testExp() = DoubleAnalyticTensorAlgebra {
|
||||
tensor.exp().let {
|
||||
assertTrue { shape contentEquals it.shape }
|
||||
assertTrue { buffer.fmap(::exp).epsEqual(it.mutableBuffer.array())}
|
||||
|
@ -10,7 +10,7 @@ import kotlin.test.assertTrue
|
||||
internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
|
||||
@Test
|
||||
fun testDetLU() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testDetLU() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor = fromArray(
|
||||
intArrayOf(2, 2, 2),
|
||||
doubleArrayOf(
|
||||
@ -35,7 +35,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDet() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testDet() = DoubleLinearOpsTensorAlgebra {
|
||||
val expectedValue = 0.019827417
|
||||
val m = fromArray(
|
||||
intArrayOf(3, 3), doubleArrayOf(
|
||||
@ -49,7 +49,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDetSingle() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testDetSingle() = DoubleLinearOpsTensorAlgebra {
|
||||
val expectedValue = 48.151623
|
||||
val m = fromArray(
|
||||
intArrayOf(1, 1), doubleArrayOf(
|
||||
@ -61,7 +61,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInvLU() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testInvLU() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor = fromArray(
|
||||
intArrayOf(2, 2, 2),
|
||||
doubleArrayOf(
|
||||
@ -86,14 +86,14 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScalarProduct() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testScalarProduct() = DoubleLinearOpsTensorAlgebra {
|
||||
val a = fromArray(intArrayOf(3), doubleArrayOf(1.8, 2.5, 6.8))
|
||||
val b = fromArray(intArrayOf(3), doubleArrayOf(5.5, 2.6, 6.4))
|
||||
assertEquals(a.dot(b).value(), 59.92)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testQR() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testQR() = DoubleLinearOpsTensorAlgebra {
|
||||
val shape = intArrayOf(2, 2, 2)
|
||||
val buffer = doubleArrayOf(
|
||||
1.0, 3.0,
|
||||
@ -114,7 +114,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLU() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testLU() = DoubleLinearOpsTensorAlgebra {
|
||||
val shape = intArrayOf(2, 2, 2)
|
||||
val buffer = doubleArrayOf(
|
||||
1.0, 3.0,
|
||||
@ -134,7 +134,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCholesky() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testCholesky() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor = randomNormal(intArrayOf(2, 5, 5), 0)
|
||||
val sigma = (tensor dot tensor.transpose()) + diagonalEmbedding(
|
||||
fromArray(intArrayOf(2, 5), DoubleArray(10) { 0.1 })
|
||||
@ -145,7 +145,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSVD1D() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testSVD1D() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor2 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
|
||||
val res = svd1d(tensor2)
|
||||
@ -156,13 +156,13 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSVD() = DoubleLinearOpsTensorAlgebra.invoke{
|
||||
fun testSVD() = DoubleLinearOpsTensorAlgebra{
|
||||
testSVDFor(fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)))
|
||||
testSVDFor(fromArray(intArrayOf(2, 2), doubleArrayOf(-1.0, 0.0, 239.0, 238.0)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBatchedSVD() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testBatchedSVD() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor = randomNormal(intArrayOf(2, 5, 3), 0)
|
||||
val (tensorU, tensorS, tensorV) = tensor.svd()
|
||||
val tensorSVD = tensorU dot (diagonalEmbedding(tensorS) dot tensorV.transpose())
|
||||
@ -170,7 +170,7 @@ internal class TestDoubleLinearOpsTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBatchedSymEig() = DoubleLinearOpsTensorAlgebra.invoke {
|
||||
fun testBatchedSymEig() = DoubleLinearOpsTensorAlgebra {
|
||||
val tensor = randomNormal(shape = intArrayOf(2, 3, 3), 0)
|
||||
val tensorSigma = tensor + tensor.transpose()
|
||||
val (tensorS, tensorV) = tensorSigma.symEig()
|
||||
|
@ -15,14 +15,14 @@ import kotlin.test.assertTrue
|
||||
internal class TestDoubleTensor {
|
||||
|
||||
@Test
|
||||
fun valueTest() = DoubleTensorAlgebra.invoke {
|
||||
fun valueTest() = DoubleTensorAlgebra {
|
||||
val value = 12.5
|
||||
val tensor = fromArray(intArrayOf(1), doubleArrayOf(value))
|
||||
assertEquals(tensor.value(), value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stridesTest() = DoubleTensorAlgebra.invoke {
|
||||
fun stridesTest() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
|
||||
assertEquals(tensor[intArrayOf(0, 1)], 5.8)
|
||||
assertTrue(
|
||||
@ -31,7 +31,7 @@ internal class TestDoubleTensor {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getTest() = DoubleTensorAlgebra.invoke {
|
||||
fun getTest() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(1, 2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))
|
||||
val matrix = tensor[0].as2D()
|
||||
assertEquals(matrix[0, 1], 5.8)
|
||||
|
@ -10,21 +10,21 @@ import kotlin.test.assertTrue
|
||||
internal class TestDoubleTensorAlgebra {
|
||||
|
||||
@Test
|
||||
fun doublePlus() = DoubleTensorAlgebra.invoke {
|
||||
fun doublePlus() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(2), doubleArrayOf(1.0, 2.0))
|
||||
val res = 10.0 + tensor
|
||||
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(11.0, 12.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun doubleDiv() = DoubleTensorAlgebra.invoke {
|
||||
fun doubleDiv() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(2), doubleArrayOf(2.0, 4.0))
|
||||
val res = 2.0/tensor
|
||||
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(1.0, 0.5))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun divDouble() = DoubleTensorAlgebra.invoke {
|
||||
fun divDouble() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(2), doubleArrayOf(10.0, 5.0))
|
||||
val res = tensor / 2.5
|
||||
assertTrue(res.mutableBuffer.array() contentEquals doubleArrayOf(4.0, 2.0))
|
||||
@ -40,7 +40,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun transpose3x2() = DoubleTensorAlgebra.invoke {
|
||||
fun transpose3x2() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val res = tensor.transpose(1, 0)
|
||||
|
||||
@ -49,7 +49,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun transpose1x2x3() = DoubleTensorAlgebra.invoke {
|
||||
fun transpose1x2x3() = DoubleTensorAlgebra {
|
||||
val tensor = fromArray(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(-3, 2)
|
||||
@ -65,7 +65,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun linearStructure() = DoubleTensorAlgebra.invoke {
|
||||
fun linearStructure() = DoubleTensorAlgebra {
|
||||
val shape = intArrayOf(3)
|
||||
val tensorA = full(value = -4.5, shape = shape)
|
||||
val tensorB = full(value = 10.9, shape = shape)
|
||||
@ -97,7 +97,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun dot() = DoubleTensorAlgebra.invoke {
|
||||
fun dot() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor11 = fromArray(intArrayOf(3, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor2 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0))
|
||||
@ -133,7 +133,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun diagonalEmbedding() = DoubleTensorAlgebra.invoke {
|
||||
fun diagonalEmbedding() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(3), doubleArrayOf(10.0, 20.0, 30.0))
|
||||
val tensor2 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor3 = zeros(intArrayOf(2, 3, 4, 5))
|
||||
@ -166,7 +166,7 @@ internal class TestDoubleTensorAlgebra {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEq() = DoubleTensorAlgebra.invoke {
|
||||
fun testEq() = DoubleTensorAlgebra {
|
||||
val tensor1 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor2 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
|
||||
val tensor3 = fromArray(intArrayOf(2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0, 5.0))
|
||||
|
Loading…
Reference in New Issue
Block a user