KMP library for tensors #300

Merged
grinisrit merged 215 commits from feature/tensor-algebra into dev 2021-05-08 09:48:04 +03:00
2 changed files with 39 additions and 0 deletions
Showing only changes of commit df402086da - Show all commits

View File

@ -0,0 +1,35 @@
package space.kscience.kmath.tensors.core
import kotlin.math.abs
import kotlin.math.exp
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class TestDoubleAnalyticTensorAlgebra {
val shape = intArrayOf(2, 1, 3, 2)
val buffer = doubleArrayOf(27.1, 20.0, 19.84, 23.123, 0.0, 1.0, 3.23, 133.7, 25.3, 100.3, 11.0, 12.012)
val tensor = DoubleTensor(shape, buffer)
fun DoubleArray.fmap(transform: (Double) -> Double): DoubleArray {
return this.map(transform).toDoubleArray()
}
fun DoubleArray.deltaEqual(other: DoubleArray, delta: Double = 1e-5): Boolean {
for ((elem1, elem2) in this.asSequence().zip(other.asSequence())) {
if (abs(elem1 - elem2) > delta) {
return false
}
}
return true
}
@Test
fun testExp() = DoubleAnalyticTensorAlgebra {
tensor.exp().let {
assertTrue { shape contentEquals it.shape }
assertTrue { buffer.fmap(::exp).deltaEqual(it.buffer.array())}
}
}
}

View File

@ -79,4 +79,8 @@ class TestDoubleTensorAlgebra {
assertTrue(expected.buffer.array() contentEquals assignResult.buffer.array()) assertTrue(expected.buffer.array() contentEquals assignResult.buffer.array())
} }
@Test
fun testContentEqual() = DoubleTensorAlgebra {
//TODO()
}
} }