From 7f3dd95e7284b2ff6c30b865f6e422da25caa929 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 17 Jul 2019 14:21:37 +0300 Subject: [PATCH] Moved to scientifik plugin. Removed depricated Vector --- build.gradle.kts | 18 ++- buildSrc/build.gradle.kts | 20 --- buildSrc/settings.gradle.kts | 0 buildSrc/src/main/kotlin/Versions.kt | 9 -- .../src/main/kotlin/dokka-publish.gradle.kts | 75 ----------- buildSrc/src/main/kotlin/js-test.gradle.kts | 44 ------- .../main/kotlin/npm-multiplatform.gradle.kts | 103 --------------- .../src/main/kotlin/npm-publish.gradle.kts | 120 ------------------ examples/build.gradle.kts | 17 +-- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew | 2 +- gradlew.bat | 2 +- kmath-core/build.gradle.kts | 2 +- .../kmath/linear/LUPDecomposition.kt | 2 +- .../scientifik/kmath/linear/LinearAlgrebra.kt | 16 +-- .../scientifik/kmath/linear/RealVector.kt | 48 +++++++ .../kotlin/scientifik/kmath/linear/Vector.kt | 71 ----------- .../scientifik/kmath/linear/MatrixTest.kt | 10 +- kmath-coroutines/build.gradle.kts | 16 +-- .../kotlin/scientifik/kmath/chains/Chain.kt | 37 ++++-- kmath-histograms/build.gradle.kts | 2 +- .../kmath/histogram/RealHistogram.kt | 6 +- .../histogram/MultivariateHistogramTest.kt | 10 +- .../kmath/histogram/UnivariateHistogram.kt | 4 +- kmath-koma/build.gradle.kts | 2 +- kmath-memory/build.gradle.kts | 2 +- kmath-prob/build.gradle.kts | 10 +- settings.gradle.kts | 7 +- 28 files changed, 136 insertions(+), 521 deletions(-) delete mode 100644 buildSrc/build.gradle.kts delete mode 100644 buildSrc/settings.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/Versions.kt delete mode 100644 buildSrc/src/main/kotlin/dokka-publish.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/js-test.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/npm-multiplatform.gradle.kts delete mode 100644 buildSrc/src/main/kotlin/npm-publish.gradle.kts create mode 100644 kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/RealVector.kt delete mode 100644 kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/Vector.kt diff --git a/build.gradle.kts b/build.gradle.kts index edc4bed7b..3584b790a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,9 +1,18 @@ -val kmathVersion by extra("0.1.3") +plugins { + id("scientifik.mpp") version "0.1.3" apply false + id("scientifik.publish") version "0.1.3" apply false + id("kotlinx-atomicfu") version "0.12.9" apply false +} + +val kmathVersion by extra("0.1.4-dev-1") + +val bintrayRepo by extra("scientifik") +val githubProject by extra("kmath") allprojects { repositories { jcenter() - maven("https://kotlin.bintray.com/kotlinx") + maven("https://dl.bintray.com/kotlin/kotlinx") } group = "scientifik" @@ -11,8 +20,7 @@ allprojects { } subprojects { - apply(plugin = "dokka-publish") if (name.startsWith("kmath")) { - apply(plugin = "npm-publish") + apply(plugin = "scientifik.publish") } -} +} \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts deleted file mode 100644 index 318189162..000000000 --- a/buildSrc/build.gradle.kts +++ /dev/null @@ -1,20 +0,0 @@ -plugins { - `kotlin-dsl` -} - -repositories { - gradlePluginPortal() - jcenter() -} - -val kotlinVersion = "1.3.31" - -// Add plugins used in buildSrc as dependencies, also we should specify version only here -dependencies { - implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") - implementation("org.jfrog.buildinfo:build-info-extractor-gradle:4.9.6") - implementation("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4") - implementation("org.jetbrains.dokka:dokka-gradle-plugin:0.9.18") - implementation("com.moowork.gradle:gradle-node-plugin:1.3.1") - implementation("org.openjfx:javafx-plugin:0.0.7") -} diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts deleted file mode 100644 index e69de29bb..000000000 diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt deleted file mode 100644 index 883af1203..000000000 --- a/buildSrc/src/main/kotlin/Versions.kt +++ /dev/null @@ -1,9 +0,0 @@ -// Instead of defining runtime properties and use them dynamically -// define version in buildSrc and have autocompletion and compile-time check -// Also dependencies itself can be moved here -object Versions { - val ioVersion = "0.1.8" - val coroutinesVersion = "1.2.1" - val atomicfuVersion = "0.12.6" - val serializationVersion = "0.11.0" -} diff --git a/buildSrc/src/main/kotlin/dokka-publish.gradle.kts b/buildSrc/src/main/kotlin/dokka-publish.gradle.kts deleted file mode 100644 index b7b48fb68..000000000 --- a/buildSrc/src/main/kotlin/dokka-publish.gradle.kts +++ /dev/null @@ -1,75 +0,0 @@ -import org.jetbrains.dokka.gradle.DokkaTask -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension - -plugins { - id("org.jetbrains.dokka") - `maven-publish` -} - -afterEvaluate { - - extensions.findByType()?.apply{ - val dokka by tasks.getting(DokkaTask::class) { - outputFormat = "html" - outputDirectory = "$buildDir/javadoc" - jdkVersion = 8 - - kotlinTasks { - // dokka fails to retrieve sources from MPP-tasks so we only define the jvm task - listOf(tasks.getByPath("compileKotlinJvm")) - } - sourceRoot { - // assuming only single source dir - path = sourceSets["commonMain"].kotlin.srcDirs.first().toString() - platforms = listOf("Common") - } - // although the JVM sources are now taken from the task, - // we still define the jvm source root to get the JVM marker in the generated html - sourceRoot { - // assuming only single source dir - path = sourceSets["jvmMain"].kotlin.srcDirs.first().toString() - platforms = listOf("JVM") - } - } - - val kdocJar by tasks.registering(Jar::class) { - group = JavaBasePlugin.DOCUMENTATION_GROUP - dependsOn(dokka) - archiveClassifier.set("javadoc") - from("$buildDir/javadoc") - } - - configure { - - targets.all { - val publication = publications.findByName(name) as MavenPublication - - // Patch publications with fake javadoc - publication.artifact(kdocJar.get()) - } - } - } - - - extensions.findByType()?.apply{ - val dokka by tasks.getting(DokkaTask::class) { - outputFormat = "html" - outputDirectory = "$buildDir/javadoc" - jdkVersion = 8 - } - - val kdocJar by tasks.registering(Jar::class) { - group = JavaBasePlugin.DOCUMENTATION_GROUP - dependsOn(dokka) - archiveClassifier.set("javadoc") - from("$buildDir/javadoc") - } - - configure { - publications.filterIsInstance().forEach { publication -> - publication.artifact(kdocJar.get()) - } - } - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/js-test.gradle.kts b/buildSrc/src/main/kotlin/js-test.gradle.kts deleted file mode 100644 index 61759a28a..000000000 --- a/buildSrc/src/main/kotlin/js-test.gradle.kts +++ /dev/null @@ -1,44 +0,0 @@ -import com.moowork.gradle.node.npm.NpmTask -import com.moowork.gradle.node.task.NodeTask -import org.gradle.kotlin.dsl.* -import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile - -plugins { - id("com.moowork.node") - kotlin("multiplatform") -} - -node { - nodeModulesDir = file("$buildDir/node_modules") -} - -val compileKotlinJs by tasks.getting(Kotlin2JsCompile::class) -val compileTestKotlinJs by tasks.getting(Kotlin2JsCompile::class) - -val populateNodeModules by tasks.registering(Copy::class) { - dependsOn(compileKotlinJs) - from(compileKotlinJs.destinationDir) - - kotlin.js().compilations["test"].runtimeDependencyFiles.forEach { - if (it.exists() && !it.isDirectory) { - from(zipTree(it.absolutePath).matching { include("*.js") }) - } - } - - into("$buildDir/node_modules") -} - -val installMocha by tasks.registering(NpmTask::class) { - setWorkingDir(buildDir) - setArgs(listOf("install", "mocha")) -} - -val runMocha by tasks.registering(NodeTask::class) { - dependsOn(compileTestKotlinJs, populateNodeModules, installMocha) - setScript(file("$buildDir/node_modules/mocha/bin/mocha")) - setArgs(listOf(compileTestKotlinJs.outputFile)) -} - -tasks["jsTest"].dependsOn(runMocha) - - diff --git a/buildSrc/src/main/kotlin/npm-multiplatform.gradle.kts b/buildSrc/src/main/kotlin/npm-multiplatform.gradle.kts deleted file mode 100644 index 7cf92afaf..000000000 --- a/buildSrc/src/main/kotlin/npm-multiplatform.gradle.kts +++ /dev/null @@ -1,103 +0,0 @@ -import com.jfrog.bintray.gradle.tasks.BintrayUploadTask -import org.gradle.api.publish.maven.internal.artifact.FileBasedMavenArtifact -import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask - -plugins { - kotlin("multiplatform") - `maven-publish` -} - - -kotlin { - jvm { - compilations.all { - kotlinOptions { - jvmTarget = "1.8" - } - } - } - - js { - compilations.all { - kotlinOptions { - metaInfo = true - sourceMap = true - sourceMapEmbedSources = "always" - moduleKind = "commonjs" - } - } - - compilations.named("main") { - kotlinOptions { - main = "call" - } - } - } - - sourceSets { - val commonMain by getting { - dependencies { - api(kotlin("stdlib")) - } - } - val commonTest by getting { - dependencies { - implementation(kotlin("test-common")) - implementation(kotlin("test-annotations-common")) - } - } - val jvmMain by getting { - dependencies { - api(kotlin("stdlib-jdk8")) - } - } - val jvmTest by getting { - dependencies { - implementation(kotlin("test")) - implementation(kotlin("test-junit")) - } - } - val jsMain by getting { - dependencies { - api(kotlin("stdlib-js")) - } - } - val jsTest by getting { - dependencies { - implementation(kotlin("test-js")) - } - } - } - - targets.all { - sourceSets.all { - languageSettings.progressiveMode = true - languageSettings.enableLanguageFeature("InlineClasses") - } - } - - // Apply JS test configuration - val runJsTests by ext(false) - - if (runJsTests) { - apply(plugin = "js-test") - } - -} - -//workaround for bintray and artifactory -project.tasks.filter { it is ArtifactoryTask || it is BintrayUploadTask }.forEach { - it.doFirst { - project.configure { - publications.filterIsInstance() - .forEach { publication -> - val moduleFile = project.buildDir.resolve("publications/${publication.name}/module.json") - if (moduleFile.exists()) { - publication.artifact(object : FileBasedMavenArtifact(moduleFile) { - override fun getDefaultExtension() = "module" - }) - } - } - } - } -} diff --git a/buildSrc/src/main/kotlin/npm-publish.gradle.kts b/buildSrc/src/main/kotlin/npm-publish.gradle.kts deleted file mode 100644 index ee55b996f..000000000 --- a/buildSrc/src/main/kotlin/npm-publish.gradle.kts +++ /dev/null @@ -1,120 +0,0 @@ -@file:Suppress("UnstableApiUsage") - -import com.jfrog.bintray.gradle.tasks.BintrayUploadTask -import groovy.lang.GroovyObject -import org.gradle.api.publish.maven.internal.artifact.FileBasedMavenArtifact -import org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig -import org.jfrog.gradle.plugin.artifactory.dsl.ResolverConfig - -// Old bintray.gradle script converted to real Gradle plugin (precompiled script plugin) -// It now has own dependencies and support type safe accessors -// Syntax is pretty close to what we had in Groovy -// (excluding Property.set and bintray dynamic configs) - -plugins { - `maven-publish` - id("com.jfrog.bintray") - id("com.jfrog.artifactory") -} - -val vcs = "https://github.com/mipt-npm/kmath" -val bintrayRepo = "https://bintray.com/mipt-npm/scientifik" - -// Configure publishing -publishing { - repositories { - maven(bintrayRepo) - } - - // Process each publication we have in this project - publications.filterIsInstance().forEach { publication -> - - // use type safe pom config GSL instead of old dynamic - publication.pom { - name.set(project.name) - description.set(project.description) - url.set(vcs) - - licenses { - license { - name.set("The Apache Software License, Version 2.0") - url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") - distribution.set("repo") - } - } - developers { - developer { - id.set("MIPT-NPM") - name.set("MIPT nuclear physics methods laboratory") - organization.set("MIPT") - organizationUrl.set("http://npm.mipt.ru") - } - - } - scm { - url.set(vcs) - } - } - } - -} - -bintray { - user = findProperty("bintrayUser") as? String ?: System.getenv("BINTRAY_USER") - key = findProperty("bintrayApiKey") as? String? ?: System.getenv("BINTRAY_API_KEY") - publish = true - override = true // for multi-platform Kotlin/Native publishing - - // We have to use delegateClosureOf because bintray supports only dynamic groovy syntax - // this is a problem of this plugin - pkg.apply { - userOrg = "mipt-npm" - repo = "scientifik" - name = project.name - issueTrackerUrl = "$vcs/issues" - setLicenses("Apache-2.0") - vcsUrl = vcs - version.apply { - name = project.version.toString() - vcsTag = project.version.toString() - released = java.util.Date().toString() - } - } - - //workaround bintray bug - afterEvaluate { - setPublications(*publishing.publications.names.toTypedArray()) - } - - tasks { - bintrayUpload { - dependsOn(publishToMavenLocal) - } - } -} - -artifactory { - val artifactoryUser: String? by project - val artifactoryPassword: String? by project - val artifactoryContextUrl = "http://npm.mipt.ru:8081/artifactory" - - setContextUrl(artifactoryContextUrl)//The base Artifactory URL if not overridden by the publisher/resolver - publish(delegateClosureOf { - repository(delegateClosureOf { - setProperty("repoKey", "gradle-dev-local") - setProperty("username", artifactoryUser) - setProperty("password", artifactoryPassword) - }) - - defaults(delegateClosureOf { - invokeMethod("publications", arrayOf("jvm", "js", "kotlinMultiplatform", "metadata")) - }) - }) - resolve(delegateClosureOf { - repository(delegateClosureOf { - setProperty("repoKey", "gradle-dev") - setProperty("username", artifactoryUser) - setProperty("password", artifactoryPassword) - }) - }) -} diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 7f6bec31f..98d8607e2 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -1,12 +1,11 @@ -import org.jetbrains.gradle.benchmarks.JvmBenchmarkTarget import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { java kotlin("jvm") - kotlin("plugin.allopen") version "1.3.31" - id("org.jetbrains.gradle.benchmarks.plugin") version "0.1.7-dev-24" + kotlin("plugin.allopen") version "1.3.41" + id("kotlinx.benchmark") version "0.2.0-dev-2" } configure { @@ -16,7 +15,8 @@ configure { repositories { maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("http://dl.bintray.com/kyonifer/maven") - maven("https://dl.bintray.com/orangy/maven") + maven ("https://dl.bintray.com/orangy/maven") + mavenCentral() } @@ -30,9 +30,9 @@ dependencies { implementation(project(":kmath-commons")) implementation(project(":kmath-koma")) implementation("com.kyonifer:koma-core-ejml:0.12") - implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.1.5") + implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:${Scientifik.ioVersion}") - implementation("org.jetbrains.gradle.benchmarks:runtime:0.1.7-dev-24") + implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-2") "benchmarksCompile"(sourceSets.main.get().compileClasspath) @@ -43,10 +43,7 @@ benchmark { // Setup configurations targets { // This one matches sourceSet name above - register("benchmarks") { - this as JvmBenchmarkTarget - jmhVersion = "1.21" - } + register("benchmarks") } configurations { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5f1b1201a..4b7e1f3d3 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index b0d6d0ab5..8e25e6c19 100755 --- a/gradlew +++ b/gradlew @@ -7,7 +7,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/gradlew.bat b/gradlew.bat index 9991c5032..9618d8d96 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -5,7 +5,7 @@ @rem you may not use this file except in compliance with the License. @rem You may obtain a copy of the License at @rem -@rem http://www.apache.org/licenses/LICENSE-2.0 +@rem https://www.apache.org/licenses/LICENSE-2.0 @rem @rem Unless required by applicable law or agreed to in writing, software @rem distributed under the License is distributed on an "AS IS" BASIS, diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index 25507968c..34941064b 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - `npm-multiplatform` + id("scientifik.mpp") } kotlin.sourceSets { diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUPDecomposition.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUPDecomposition.kt index 910ed4551..87e0ef027 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUPDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUPDecomposition.kt @@ -183,7 +183,7 @@ fun LUPDecomposition.solve(type: KClass, matrix: Matrix): Mat elementContext.run { // Apply permutations to b - val bp = create { i, j -> zero } + val bp = create { _, _ -> zero } for (row in 0 until pivot.size) { val bpRow = bp.row(row) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt index 7e631f5fc..0456ffebb 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt @@ -3,10 +3,12 @@ package scientifik.kmath.linear import scientifik.kmath.operations.Field import scientifik.kmath.operations.Norm import scientifik.kmath.operations.RealField +import scientifik.kmath.structures.Buffer import scientifik.kmath.structures.Matrix import scientifik.kmath.structures.VirtualBuffer import scientifik.kmath.structures.asSequence +typealias Point = Buffer /** * A group of methods to resolve equation A dot X = B, where A and B are matrices or vectors @@ -17,20 +19,6 @@ interface LinearSolver { fun inverse(a: Matrix): Matrix } -/** - * Convert vector to array (copying content of array) - */ -fun Array.toVector(field: Field) = Vector.generic(size, field) { this[it] } - -fun DoubleArray.toVector() = Vector.real(this.size) { this[it] } -fun List.toVector() = Vector.real(this.size) { this[it] } - -object VectorL2Norm : Norm, Double> { - override fun norm(arg: Point): Double = - kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() }) -} - -typealias RealVector = Vector typealias RealMatrix = Matrix /** diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/RealVector.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/RealVector.kt new file mode 100644 index 000000000..d3cf07e79 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/RealVector.kt @@ -0,0 +1,48 @@ +package scientifik.kmath.linear + +import scientifik.kmath.operations.Field +import scientifik.kmath.operations.Norm +import scientifik.kmath.operations.RealField +import scientifik.kmath.operations.SpaceElement +import scientifik.kmath.structures.Buffer +import scientifik.kmath.structures.DoubleBuffer +import scientifik.kmath.structures.asBuffer +import scientifik.kmath.structures.asSequence + +fun DoubleArray.asVector() = RealVector(this.asBuffer()) +fun List.asVector() = RealVector(this.asBuffer()) + + +object VectorL2Norm : Norm, Double> { + override fun norm(arg: Point): Double = + kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() }) +} + +inline class RealVector(val point: Point) : + SpaceElement, RealVector, VectorSpace>, Point { + override val context: VectorSpace get() = space(point.size) + + override fun unwrap(): Point = point + + override fun Point.wrap(): RealVector = RealVector(this) + + override val size: Int get() = point.size + + override fun get(index: Int): Double = point[index] + + override fun iterator(): Iterator = point.iterator() + + companion object { + + private val spaceCache = HashMap>() + + inline operator fun invoke(dim:Int, initalizer: (Int)-> Double) = RealVector(DoubleBuffer(dim, initalizer)) + + operator fun invoke(vararg values: Double) = values.asVector() + + fun space(dim: Int) = + spaceCache.getOrPut(dim) { + BufferVectorSpace(dim, RealField) { size, init -> Buffer.real(size, init) } + } + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/Vector.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/Vector.kt deleted file mode 100644 index 18a0021c3..000000000 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/Vector.kt +++ /dev/null @@ -1,71 +0,0 @@ -package scientifik.kmath.linear - -import scientifik.kmath.operations.RealField -import scientifik.kmath.operations.Space -import scientifik.kmath.operations.SpaceElement -import scientifik.kmath.structures.Buffer -import scientifik.kmath.structures.asSequence -import kotlin.jvm.JvmName - -typealias Point = Buffer - - -fun > BufferVectorSpace.produceElement(initializer: (Int) -> T): Vector = - BufferVector(this, produce(initializer)) - -@JvmName("produceRealElement") -fun BufferVectorSpace.produceElement(initializer: (Int) -> Double): Vector = - BufferVector(this, produce(initializer)) - -/** - * A point coupled to the linear space - */ -@Deprecated("Use VectorContext instead") -interface Vector> : SpaceElement, Vector, VectorSpace>, Point { - override val size: Int get() = context.size - - override operator fun plus(b: Point): Vector = context.add(this, b).wrap() - override operator fun minus(b: Point): Vector = context.add(this, context.multiply(b, -1.0)).wrap() - override operator fun times(k: Number): Vector = context.multiply(this, k.toDouble()).wrap() - override operator fun div(k: Number): Vector = context.multiply(this, 1.0 / k.toDouble()).wrap() - - companion object { - /** - * Create vector with custom field - */ - fun > generic(size: Int, field: S, initializer: (Int) -> T): Vector = - VectorSpace.buffered(size, field).produceElement(initializer) - - fun real(size: Int, initializer: (Int) -> Double): Vector = - VectorSpace.real(size).produceElement(initializer) - - fun ofReal(vararg elements: Double): Vector = - VectorSpace.real(elements.size).produceElement { elements[it] } - - } -} - -@Deprecated("Use VectorContext instead") -data class BufferVector>(override val context: VectorSpace, val buffer: Buffer) : - Vector { - - init { - if (context.size != buffer.size) { - error("Array dimension mismatch") - } - } - - override fun get(index: Int): T { - return buffer[index] - } - - override fun unwrap(): Point = this - - override fun Point.wrap(): Vector = BufferVector(context, this) - - override fun iterator(): Iterator = (0 until size).map { buffer[it] }.iterator() - - override fun toString(): String = - this.asSequence().joinToString(prefix = "[", postfix = "]", separator = ", ") { it.toString() } -} - diff --git a/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt index 5370ae960..de13c9888 100644 --- a/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/scientifik/kmath/linear/MatrixTest.kt @@ -8,15 +8,15 @@ class MatrixTest { @Test fun testSum() { - val vector1 = Vector.real(5) { it.toDouble() } - val vector2 = Vector.real(5) { 5 - it.toDouble() } + val vector1 = RealVector(5) { it.toDouble() } + val vector2 = RealVector(5) { 5 - it.toDouble() } val sum = vector1 + vector2 assertEquals(5.0, sum[2]) } @Test fun testVectorToMatrix() { - val vector = Vector.real(5) { it.toDouble() } + val vector = RealVector(5) { it.toDouble() } val matrix = vector.asMatrix() assertEquals(4.0, matrix[4, 0]) } @@ -31,8 +31,8 @@ class MatrixTest { @Test fun testDot() { - val vector1 = Vector.real(5) { it.toDouble() } - val vector2 = Vector.real(5) { 5 - it.toDouble() } + val vector1 = RealVector(5) { it.toDouble() } + val vector2 = RealVector(5) { 5 - it.toDouble() } val matrix1 = vector1.asMatrix() val matrix2 = vector2.asMatrix().transpose() diff --git a/kmath-coroutines/build.gradle.kts b/kmath-coroutines/build.gradle.kts index 6ffaf4df6..f54263072 100644 --- a/kmath-coroutines/build.gradle.kts +++ b/kmath-coroutines/build.gradle.kts @@ -1,26 +1,26 @@ plugins { - `npm-multiplatform` - id("kotlinx-atomicfu") version Versions.atomicfuVersion + id("scientifik.mpp") + id("kotlinx-atomicfu") } kotlin.sourceSets { commonMain { dependencies { api(project(":kmath-core")) - api("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Versions.coroutinesVersion}") - compileOnly("org.jetbrains.kotlinx:atomicfu-common:${Versions.atomicfuVersion}") + api("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Scientifik.coroutinesVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu-common:${Scientifik.atomicfuVersion}") } } jvmMain { dependencies { - api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutinesVersion}") - compileOnly("org.jetbrains.kotlinx:atomicfu:${Versions.atomicfuVersion}") + api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Scientifik.coroutinesVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu:${Scientifik.atomicfuVersion}") } } jsMain { dependencies { - api("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:${Versions.coroutinesVersion}") - compileOnly("org.jetbrains.kotlinx:atomicfu-js:${Versions.atomicfuVersion}") + api("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:${Scientifik.coroutinesVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu-js:${Scientifik.atomicfuVersion}") } } } diff --git a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt index 317cdcea0..e3f141b44 100644 --- a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt @@ -20,6 +20,8 @@ import kotlinx.atomicfu.atomic import kotlinx.atomicfu.updateAndGet import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock /** @@ -64,16 +66,22 @@ class SimpleChain(private val gen: suspend () -> R) : Chain { */ class MarkovChain(private val seed: suspend () -> R, private val gen: suspend (R) -> R) : Chain { - constructor(seedValue: R, gen: suspend (R) -> R) : this({ seedValue }, gen) + //constructor(seedValue: R, gen: suspend (R) -> R) : this({ seedValue }, gen) - private val value = atomic(null) + private val mutex = Mutex() + + private var value: R? = null override suspend fun next(): R { - return value.updateAndGet { prev -> gen(prev ?: seed()) }!! + mutex.withLock { + val newValue = gen(value ?: seed()) + value = newValue + return newValue + } } override fun fork(): Chain { - return MarkovChain(seed = { value.value ?: seed() }, gen = gen) + return MarkovChain(seed = { value ?: seed() }, gen = gen) } } @@ -89,17 +97,22 @@ class StatefulChain( private val gen: suspend S.(R) -> R ) : Chain { - constructor(state: S, seedValue: R, forkState: ((S) -> S), gen: suspend S.(R) -> R) : this( - state, - { seedValue }, - forkState, - gen - ) +// constructor(state: S, seedValue: R, forkState: ((S) -> S), gen: suspend S.(R) -> R) : this( +// state, +// { seedValue }, +// forkState, +// gen +// ) + private val mutex = Mutex() - private val atomicValue = atomic(null) + private var value: R? = null override suspend fun next(): R { - return atomicValue.updateAndGet { prev -> state.gen(prev ?: state.seed()) }!! + mutex.withLock { + val newValue = state.gen(value ?: state.seed()) + value = newValue + return newValue + } } override fun fork(): Chain { diff --git a/kmath-histograms/build.gradle.kts b/kmath-histograms/build.gradle.kts index 7eaa5e174..39aa833ad 100644 --- a/kmath-histograms/build.gradle.kts +++ b/kmath-histograms/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - `npm-multiplatform` + id("scientifik.mpp") } kotlin.sourceSets.commonMain { diff --git a/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/RealHistogram.kt b/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/RealHistogram.kt index 75296ba27..a2f0fc516 100644 --- a/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/RealHistogram.kt +++ b/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/RealHistogram.kt @@ -1,7 +1,7 @@ package scientifik.kmath.histogram import scientifik.kmath.linear.Point -import scientifik.kmath.linear.toVector +import scientifik.kmath.linear.asVector import scientifik.kmath.operations.SpaceOperations import scientifik.kmath.structures.* import kotlin.math.floor @@ -132,8 +132,8 @@ class RealHistogram( */ fun fromRanges(vararg ranges: ClosedFloatingPointRange): RealHistogram { return RealHistogram( - ranges.map { it.start }.toVector(), - ranges.map { it.endInclusive }.toVector() + ranges.map { it.start }.asVector(), + ranges.map { it.endInclusive }.asVector() ) } diff --git a/kmath-histograms/src/commonTest/kotlin/scietifik/kmath/histogram/MultivariateHistogramTest.kt b/kmath-histograms/src/commonTest/kotlin/scietifik/kmath/histogram/MultivariateHistogramTest.kt index 678b8eba7..4dc4dfc74 100644 --- a/kmath-histograms/src/commonTest/kotlin/scietifik/kmath/histogram/MultivariateHistogramTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/scietifik/kmath/histogram/MultivariateHistogramTest.kt @@ -3,7 +3,7 @@ package scietifik.kmath.histogram import scientifik.kmath.histogram.RealHistogram import scientifik.kmath.histogram.fill import scientifik.kmath.histogram.put -import scientifik.kmath.linear.Vector +import scientifik.kmath.linear.RealVector import kotlin.random.Random import kotlin.test.Test import kotlin.test.assertEquals @@ -19,9 +19,9 @@ class MultivariateHistogramTest { ) histogram.put(0.55, 0.55) val bin = histogram.find { it.value.toInt() > 0 }!! - assertTrue { bin.contains(Vector.ofReal(0.55, 0.55)) } - assertTrue { bin.contains(Vector.ofReal(0.6, 0.5)) } - assertFalse { bin.contains(Vector.ofReal(-0.55, 0.55)) } + assertTrue { bin.contains(RealVector(0.55, 0.55)) } + assertTrue { bin.contains(RealVector(0.6, 0.5)) } + assertFalse { bin.contains(RealVector(-0.55, 0.55)) } } @Test @@ -39,7 +39,7 @@ class MultivariateHistogramTest { histogram.fill { repeat(n) { - yield(Vector.ofReal(nextDouble(), nextDouble(), nextDouble())) + yield(RealVector(nextDouble(), nextDouble(), nextDouble())) } } assertEquals(n, histogram.sumBy { it.value.toInt() }) diff --git a/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt b/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt index 53c2da641..90b9aff5e 100644 --- a/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt @@ -1,7 +1,7 @@ package scientifik.kmath.histogram import scientifik.kmath.linear.RealVector -import scientifik.kmath.linear.toVector +import scientifik.kmath.linear.asVector import scientifik.kmath.structures.Buffer import java.util.* import kotlin.math.floor @@ -12,7 +12,7 @@ class UnivariateBin(val position: Double, val size: Double, val counter: LongCou //TODO add weighting override val value: Number get() = counter.sum() - override val center: RealVector get() = doubleArrayOf(position).toVector() + override val center: RealVector get() = doubleArrayOf(position).asVector() operator fun contains(value: Double): Boolean = value in (position - size / 2)..(position + size / 2) diff --git a/kmath-koma/build.gradle.kts b/kmath-koma/build.gradle.kts index 9b07fdcf2..26955bca7 100644 --- a/kmath-koma/build.gradle.kts +++ b/kmath-koma/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - `npm-multiplatform` + id("scientifik.mpp") } repositories { diff --git a/kmath-memory/build.gradle.kts b/kmath-memory/build.gradle.kts index 3fb56989c..1f34a4f17 100644 --- a/kmath-memory/build.gradle.kts +++ b/kmath-memory/build.gradle.kts @@ -1,3 +1,3 @@ plugins { - `npm-multiplatform` + id("scientifik.mpp") } diff --git a/kmath-prob/build.gradle.kts b/kmath-prob/build.gradle.kts index 972b9bba0..3294469ab 100644 --- a/kmath-prob/build.gradle.kts +++ b/kmath-prob/build.gradle.kts @@ -1,25 +1,25 @@ plugins { - `npm-multiplatform` - id("kotlinx-atomicfu") version Versions.atomicfuVersion + id("scientifik.mpp") + id("kotlinx-atomicfu") } kotlin.sourceSets { commonMain { dependencies { api(project(":kmath-coroutines")) - compileOnly("org.jetbrains.kotlinx:atomicfu-common:${Versions.atomicfuVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu-common:${Scientifik.atomicfuVersion}") } } jvmMain { dependencies { // https://mvnrepository.com/artifact/org.apache.commons/commons-rng-simple //api("org.apache.commons:commons-rng-sampling:1.2") - compileOnly("org.jetbrains.kotlinx:atomicfu:${Versions.atomicfuVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu:${Scientifik.atomicfuVersion}") } } jsMain { dependencies { - compileOnly("org.jetbrains.kotlinx:atomicfu-js:${Versions.atomicfuVersion}") + compileOnly("org.jetbrains.kotlinx:atomicfu-js:${Scientifik.atomicfuVersion}") } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 004b432fd..b45091a0b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,9 +1,11 @@ pluginManagement { repositories { + mavenLocal() jcenter() gradlePluginPortal() maven("https://dl.bintray.com/kotlin/kotlin-eap") - maven("https://dl.bintray.com/orangy/maven") + maven("https://dl.bintray.com/mipt-npm/scientifik") + maven("https://dl.bintray.com/kotlin/kotlinx") } resolutionStrategy { eachPlugin { @@ -11,7 +13,8 @@ pluginManagement { "kotlinx-atomicfu" -> useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${requested.version}") "kotlin-multiplatform" -> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") "kotlin2js" -> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") - //"org.jetbrains.kotlin.frontend" -> useModule("org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.45") + "org.jetbrains.kotlin.frontend" -> useModule("org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.45") + "scientifik.mpp", "scientifik.publish" -> useModule("scientifik:gradle-tools:${requested.version}") } } }