From 7ce0829597af279f54d368c4ef4ce643e7fa6f5a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 8 May 2021 14:20:16 +0300 Subject: [PATCH] 0.3.0-dev-8. Cleanup --- CHANGELOG.md | 1 + build.gradle.kts | 2 +- .../space/kscience/kmath/tensors/NeuralNetwork.kt | 8 ++++---- .../kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt | 2 +- .../kotlin/space/kscience/kmath/complex/Complex.kt | 9 +++++---- .../kotlin/space/kscience/kmath/complex/Quaternion.kt | 10 +++++----- .../kscience/kmath/stat/CommonsDistributionsTest.kt | 5 ++--- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5542b6563..286511327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - `contentEquals` from Buffer. It moved to the companion. - MSTExpression - Expression algebra builders +- Comples and Quaternion no longer are elements. ### Fixed - Ring inherits RingOperations, not GroupOperations diff --git a/build.gradle.kts b/build.gradle.kts index 760bf1aee..406a46810 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ allprojects { } group = "space.kscience" - version = "0.3.0-dev-7" + version = "0.3.0-dev-8" } subprojects { diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt index 874ac8034..5b3c2e1cd 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/NeuralNetwork.kt @@ -6,8 +6,8 @@ package space.kscience.kmath.tensors import space.kscience.kmath.operations.invoke -import space.kscience.kmath.tensors.core.DoubleTensor import space.kscience.kmath.tensors.core.BroadcastDoubleTensorAlgebra +import space.kscience.kmath.tensors.core.DoubleTensor import space.kscience.kmath.tensors.core.DoubleTensorAlgebra import space.kscience.kmath.tensors.core.toDoubleArray import kotlin.math.sqrt @@ -106,6 +106,7 @@ fun accuracy(yPred: DoubleTensor, yTrue: DoubleTensor): Double { } // neural network class +@OptIn(ExperimentalStdlibApi::class) class NeuralNetwork(private val layers: List) { private fun softMaxLoss(yPred: DoubleTensor, yTrue: DoubleTensor): DoubleTensor = BroadcastDoubleTensorAlgebra { @@ -120,7 +121,7 @@ class NeuralNetwork(private val layers: List) { (-onesForAnswers + softmaxValue) / (yPred.shape[0].toDouble()) } - @OptIn(ExperimentalStdlibApi::class) + private fun forward(x: DoubleTensor): List { var input = x @@ -133,7 +134,6 @@ class NeuralNetwork(private val layers: List) { } } - @OptIn(ExperimentalStdlibApi::class) private fun train(xTrain: DoubleTensor, yTrain: DoubleTensor) { val layerInputs = buildList { add(xTrain) @@ -180,7 +180,7 @@ fun main() { val features = 5 val sampleSize = 250 val trainSize = 180 - val testSize = sampleSize - trainSize + //val testSize = sampleSize - trainSize // take sample of features from normal distribution val x = randomNormal(intArrayOf(sampleSize, features), seed) * 2.5 diff --git a/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt b/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt index 42a0a4ba1..b42602988 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/tensors/OLSWithSVD.kt @@ -48,7 +48,7 @@ fun main() { // inverse Sigma matrix can be restored from singular values with diagonalEmbedding function - val sigma = diagonalEmbedding(singValues.map{ x -> if (abs(x) < 1e-3) 0.0 else 1.0/x }) + val sigma = diagonalEmbedding(singValues.map{ if (abs(it) < 1e-3) 0.0 else 1.0/it }) val alphaOLS = v dot sigma dot u.transpose() dot y println("Estimated alpha:\n" + diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt index 8d626c17d..8e28337f6 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt @@ -9,7 +9,10 @@ import space.kscience.kmath.memory.MemoryReader import space.kscience.kmath.memory.MemorySpec import space.kscience.kmath.memory.MemoryWriter import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.* +import space.kscience.kmath.operations.ExtendedField +import space.kscience.kmath.operations.Norm +import space.kscience.kmath.operations.NumbersAddOperations +import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.MemoryBuffer import space.kscience.kmath.structures.MutableBuffer @@ -180,12 +183,10 @@ public object ComplexField : ExtendedField, Norm, Num * @property im The imaginary part. */ @OptIn(UnstableKMathAPI::class) -public data class Complex(val re: Double, val im: Double) : FieldElement { +public data class Complex(val re: Double, val im: Double) { public constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble()) public constructor(re: Number) : this(re.toDouble(), 0.0) - public override val context: ComplexField get() = ComplexField - public override fun toString(): String = "($re + i*$im)" public companion object : MemorySpec { diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt index 382b5cc05..c59aabdcb 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt @@ -27,8 +27,10 @@ public val Quaternion.conjugate: Quaternion */ public val Quaternion.reciprocal: Quaternion get() { - val n = QuaternionField { norm(this@reciprocal) } - return conjugate / (n * n) + QuaternionField { + val n = norm(this@reciprocal) + return conjugate / (n * n) + } } /** @@ -198,7 +200,7 @@ public object QuaternionField : Field, Norm, @OptIn(UnstableKMathAPI::class) public data class Quaternion( val w: Double, val x: Double, val y: Double, val z: Double, -) : FieldElement { +) { public constructor(w: Number, x: Number, y: Number, z: Number) : this( w.toDouble(), x.toDouble(), @@ -219,8 +221,6 @@ public data class Quaternion( require(!z.isNaN()) { "x-component of quaternion is not-a-number" } } - public override val context: QuaternionField get() = QuaternionField - /** * Returns a string representation of this quaternion. */ diff --git a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt index 066fd69e6..19c01e099 100644 --- a/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt +++ b/kmath-stat/src/jvmTest/kotlin/space/kscience/kmath/stat/CommonsDistributionsTest.kt @@ -5,15 +5,14 @@ package space.kscience.kmath.stat -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import space.kscience.kmath.samplers.GaussianSampler internal class CommonsDistributionsTest { @Test - fun testNormalDistributionSuspend() = GlobalScope.launch { + fun testNormalDistributionSuspend() = runBlocking { val distribution = GaussianSampler(7.0, 2.0) val generator = RandomGenerator.default(1) val sample = distribution.sample(generator).nextBuffer(1000)