[kotlin/intellij] Add https://github.com/mipt-npm/kmath - how?

I’m taking the first steps to move my project from Python to Kotlin. Besides the new language I have to cope with a new IDE.

I’d like to add KMath to my project to be able to do linear algebra. I found it on GitHub - SciProgCentre/kmath: Kotlin mathematics extensions library and wonder how I could add it to my project.

The project for now is a Kotlin/JVM project.

Paul

There is actually a description in the README: GitHub - SciProgCentre/kmath: Kotlin mathematics extensions library. On JVM you should probably replace api("scientifik:kmath-core:${kmathVersion}") with api("scientifik:kmath-core-jvm:${kmathVersion}") to be sure. Could you please open an issue in the repository if something is not clear in the documentaion? I know, we need to work on it more.

By the way, it would make sense to register in Kotlin Slack Sign-up (registration is manual so you will need to wait for a few days), then go to #mathematics channel and then people there could help you with migrating from python in on-line regime.

I saw the readme, thank you darksnake. But where am I supposed to enter these lines? I use IntelliJ IDEA, which until now has done everything for me. A quick grep hasn’t revealed anything like that in any file.

If you’re using the intellij build system you need to define a maven library

That said it’s worth it learning to use gradle. It’s the main build tool for kotlin and some features like multiplatform require gradle. Also it’s much easier to share code if your using a gradle/maven instead of the build system of an IDE.

I recommend to create a project using gradle template and then follow instructions here: https://kotlinlang.org/docs/reference/using-gradle.html.

For people with Python background, gralde build could look like very complicated and unnecessary thing, but believe me, it is really worth it. It allows to reproducible build, run and deploy programs without relying on user-established “environment”. For beginner it is one of largest stepping stones in kotlin ecosystem, but it should be done. For now, you can just copy-paste configuration lines or I can help you with the config (ping me in slack if you register).

Thanks for your advice. My hope was to avoid diving into those build tools by using an IDE. I’ll go into this. A Question though: Does using maven / gradle mean, I’m on the console then or do they play well with Intellij IDEA?

I almost never use the console, but I’m almost always using gradle.
This means they are working very well with intellij.
I think the one exception is adding gradle to an existing project. If i’m right, there is no wizard for that.

I personally always create a new project and in the new-project window select gradle and copy over the files. I’m very very certain there is an easier way to do this, but I like it that the gradle-file is already filled in.

Having said it, when it’s ready it’s way way way more easy to add one line of code to get a library then having it to do yourself.

Both have good integration with IDEA. IDEA just inports maven or gradle build files and then you work with them as with internal build, but in a more stable way. You should go for gradle since maven is more of a legacy variant. You can import library from IDEA by manually providing artifact coordinates, but the common recommendation is still to use build system since it is an important part of the ecosystem. Fill free to ask any questions here or in slack.

You just create build file, right click it and say “import gradle build”. That’s all.

I’m very lazy :stuck_out_tongue: .
I don’t like to search across the web to find out how the file should look:

  • Which plugins should be applied
  • how the buildscript/sourcesets are implemented
  • Which dependencies should be provided in which dependency-block
  • where to provide groupName and groupVersion…

When you create a nw project, everything is clear and you don’t have to know anything about it…

I managed to write a working gradle build file and creating an IDEA project by importing the gradle build script worked, too.

Now for the adding of kmath: I added ‘maven(“https://dl.bintray.com/mipt-npm/scientifik”)’ to the repositories and ‘api(“scientifik:kmath-core:${kmathVersion}”)’ to the dependenvies. But where does the ‘kmathVersion’ come from?

You have to provide it manually based on the version you want to use. The latest published released version is “0.1.3” (JFrog Distribution: Get your software to where it needs to be -- ASAP!).

Thanks, works.

Shouldn’t I be able to import ‘Matrix’ now? My

‘import scientifik.kmath.structures.Matrix’

at the top of the file where I plan to use kmath causes an ‘Unresolved reference: scientifik’.

It should work. Use IDEA autocomplete if needed. You should not forget to sync idea with gradle build if automatic sync is not turned on. I am not sure, but maybe Matrix is a typealias in current version.

But the error message points to scientifik and not to Matrix. Shouldn’t scientifik be found somewhere on my disk and CLASSPATH point to it? But when I search thru all the folders on my disk I only find

~/.gradle/caches/modules-2/file-2.1/scientifik

and

~/.gradle/caches/modules-2/metadata-2.69/descriptors/scientifik

Is that valid?

You should not manage classpath manually in maven, gradle. It is possible that you used the wrong build, or you forgot to sync IDEA index with the build. We plan to separate example module into a stand-alone project so you could just copy the config. For not you can just paste your build file here or in the slack.

build.gradle.kts:

import org.gradle.jvm.tasks.Jar

plugins {
    kotlin("jvm") version "1.3.61" 
    id("org.jetbrains.dokka") version "0.10.0"
    `maven-publish`
}

group = "org.mobotiKs"
version = "0.0.1"

repositories {
    jcenter()

    maven("https://dl.bintray.com/mipt-npm/scientifik")
}


val kmathVersion = "0.1.3"


dependencies {
    implementation( kotlin("stdlib"))
    testImplementation("junit:junit:4.12")

    api("scientifik:kmath-core:${kmathVersion}")
}
    
tasks.dokka {
    outputFormat = "html"
    outputDirectory = "$buildDir/javadoc"
}


val dokkaJar by tasks.creating(Jar::class) { 
    group = JavaBasePlugin.DOCUMENTATION_GROUP
    description = "Assembles Kotlin docs with Dokka"
    classifier = "javadoc"
    from(tasks.dokka) 
}


publishing {
    publications {
        create<MavenPublication>("default") { 
            from( components["java"])
            artifact( dokkaJar)
        }
    }
    repositories {
        maven {
            url = uri("$buildDir/repository") 
        }
    }

It should work on gradle 6. If you have older gradle, replace kmath-core with kmath-core-jvm or turn on gradle metadata. I will try to provide stand-alone examples ASAP.

It’s gradle 6.1.1-1.