• Stars
    star
    550
  • Rank 80,860 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kotlin Alpha JetBrains incubator project Maven Central GitHub license

Multik

Multidimensional array library for Kotlin.

Modules

  • multik-core β€” contains ndarrays, methods called on them and [math], [stat] and [linalg] interfaces.
  • multik-default β€” implementation including multik-kotlin and multik-openblas for performance.
  • multik-kotlin β€” implementation of [math], [stat] and [linalg] interfaces on JVM.
  • multik-openblas β€” implementation of [math], [stat] and [linalg] interfaces in native code using OpenBLAS.

Using in your projects

Gradle

In your Gradle build script:

  1. Add the Maven Central Repository.
  2. Add the org.jetbrains.kotlinx:multik-core:$multik_version api dependency.
  3. Add an implementation dependency: org.jetbrains.kotlinx:multik-default:$multik_version, org.jetbrains.kotlinx:multik-kotlin:$multik_version or org.jetbrains.kotlinx:multik-openblas:$multik_version.

build.gradle:

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlinx:multik-core:0.2.2"
    implementation "org.jetbrains.kotlinx:multik-default:0.2.2"
}

build.gradle.kts:

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:multik-core:0.2.2")
    implementation("org.jetbrains.kotlinx:multik-default:0.2.2")
}

For a multiplatform project, set the dependency in a common block:

kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:multik-core:0.2.2")
            }
        }
    }
}

or in a platform-specific block:

kotlin {
    sourceSets {
        val jvmName by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:multik-core-jvm:0.2.2")
            }
        }
    }
}

Jupyter Notebook

Install Kotlin kernel for Jupyter or just visit to Datalore.

Import stable multik version into notebook:

%use multik

Support platforms

Platforms multik-core multik-kotlin multik-openblas multik-default
JS βœ… βœ… ❌ βœ…
linuxX64 βœ… βœ… βœ… βœ…
mingwX64 βœ… βœ… βœ… βœ…
macosX64 βœ… βœ… βœ… βœ…
macosArm64 βœ… βœ… βœ… βœ…
iosArm64 βœ… βœ… ❌ βœ…
iosX64 βœ… βœ… ❌ βœ…
iosSimulatorArm64 βœ… βœ… ❌ βœ…
JVM βœ… βœ… linuxX64 - βœ…
mingwX64 - βœ…
macosX64 - βœ…
macosArm64 - βœ…
androidArm64 - βœ…
androidArm32 - ❌
androidX86 - ❌
androidX64 - ❌

For Kotlin/JS, we use the new IR. We also use the new memory model in Kotlin/Native. Keep this in mind when using Multik in your multiplatform projects.

Note:

  • on ubuntu 18.04 and older multik-openblas doesn't work due to older versions of glibc.
  • multik-openblas for desktop targets (linuxX64, mingwX64, macosX64, macosArm64) is experimental and unstable. We will improve stability and perfomance as Kotlin/Native evolves.
  • JVM target multik-openblas for Android only supports arm64-v8a processors.

Quickstart

Visit Multik documentation for a detailed feature overview.

Creating arrays

val a = mk.ndarray(mk[1, 2, 3])
/* [1, 2, 3] */

val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]])
/*
[[1.5, 2.1, 3.0],
[4.0, 5.0, 6.0]]
*/

val c = mk.ndarray(mk[mk[mk[1.5f, 2f, 3f], mk[4f, 5f, 6f]], mk[mk[3f, 2f, 1f], mk[4f, 5f, 6f]]])
/*
[[[1.5, 2.0, 3.0],
[4.0, 5.0, 6.0]],

[[3.0, 2.0, 1.0],
[4.0, 5.0, 6.0]]]
*/


mk.zeros<Double>(3, 4) // create an array of zeros
/*
[[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]]
*/
mk.ndarray<Float, D2>(setOf(30f, 2f, 13f, 12f), intArrayOf(2, 2)) // create an array from a collection
/*
[[30.0, 2.0],
[13.0, 12.0]]
*/
val d = mk.ndarray(doubleArrayOf(1.0, 1.3, 3.0, 4.0, 9.5, 5.0), 2, 3) // create an array of shape(2, 3) from a primitive array
/*
[[1.0, 1.3, 3.0],
[4.0, 9.5, 5.0]]
*/
mk.d3array(2, 2, 3) { it * it } // create an array of 3 dimension
/*
[[[0, 1, 4],
[9, 16, 25]],

[[36, 49, 64],
[81, 100, 121]]]
*/

mk.d2arrayIndices(3, 3) { i, j -> ComplexFloat(i, j) }
/*
[[0.0+(0.0)i, 0.0+(1.0)i, 0.0+(2.0)i],
[1.0+(0.0)i, 1.0+(1.0)i, 1.0+(2.0)i],
[2.0+(0.0)i, 2.0+(1.0)i, 2.0+(2.0)i]]
 */

mk.arange<Long>(10, 25, 5) // creare an array with elements in the interval [10, 25) with step 5
/* [10, 15, 20] */

mk.linspace<Double>(0, 2, 9) // create an array of 9 elements in the interval [0, 2]
/* [0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0] */

val e = mk.identity<Double>(3) // create an identity array of shape (3, 3)
/*
[[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]]
*/

Array properties

a.shape // Array dimensions
a.size // Size of array
a.dim // object Dimension
a.dim.d // number of array dimensions
a.dtype // Data type of array elements

Arithmetic operations

val f = b - d // subtraction
/*
[[0.5, 0.8, 0.0],
[0.0, -4.5, 1.0]]
*/

d + f // addition
/*
[[1.5, 2.1, 3.0],
[4.0, 5.0, 6.0]]
*/

b / d // division
/*
[[1.5, 1.6153846153846154, 1.0],
[1.0, 0.5263157894736842, 1.2]]
*/

f * d // multiplication
/*
[[0.5, 1.04, 0.0],
[0.0, -42.75, 5.0]]
*/

Array mathematics

See documentation for other methods of mathematics, linear algebra, statistics.

a.sin() // element-wise sin, equivalent to mk.math.sin(a)
a.cos() // element-wise cos, equivalent to mk.math.cos(a)
b.log() // element-wise natural logarithm, equivalent to mk.math.log(b)
b.exp() // element-wise exp, equivalent to mk.math.exp(b)
d dot e // dot product, equivalent to mk.linalg.dot(d, e)

Aggregate functions

mk.math.sum(c) // array-wise sum
mk.math.min(c) // array-wise minimum elements
mk.math.maxD3(c, axis=0) // maximum value of an array along axis 0
mk.math.cumSum(b, axis=1) // cumulative sum of the elements
mk.stat.mean(a) // mean
mk.stat.median(b) // meadian

Copying arrays

val f = a.copy() // create a copy of the array and its data
val h = b.deepCopy() // create a copy of the array and copy the meaningful data

Operations of Iterable

c.filter { it < 3 } // select all elements less than 3
b.map { (it * it).toInt() } // return squares
c.groupNDArrayBy { it % 2 } // group elements by condition
c.sorted() // sort elements

Indexing/Slicing/Iterating

a[2] // select the element at the 2 index
b[1, 2] // select the element at row 1 column 2
b[1] // select row 1 
b[0..2, 1] // select elements at rows 0 and 1 in column 1
b[0..1..1] // select all elements at row 0

for (el in b) {
    print("$el, ") // 1.5, 2.1, 3.0, 4.0, 5.0, 6.0, 
}

// for n-dimensional
val q = b.asDNArray()
for (index in q.multiIndices) {
    print("${q[index]}, ") // 1.5, 2.1, 3.0, 4.0, 5.0, 6.0, 
}

Inplace

val a = mk.linspace<Float>(0, 1, 10)
/*
a = [0.0, 0.1111111111111111, 0.2222222222222222, 0.3333333333333333, 0.4444444444444444, 0.5555555555555556, 
0.6666666666666666, 0.7777777777777777, 0.8888888888888888, 1.0]
*/
val b = mk.linspace<Float>(8, 9, 10)
/*
b = [8.0, 8.11111111111111, 8.222222222222221, 8.333333333333334, 8.444444444444445, 8.555555555555555,
8.666666666666666, 8.777777777777779, 8.88888888888889, 9.0]
*/

a.inplace { 
    math { 
        (this - b) * b
         abs()
    }
}
// a = [64.0, 64.88888, 65.77778, 66.66666, 67.55556, 68.44444, 69.333336, 70.22222, 71.111115, 72.0]

Building

To build the entire project, you need to set up an environment for building multik-openblas:

  • JDK 1.8 or higher
  • JAVA_HOME environment - to search for jni files
  • Compilers gcc, g++, gfortran version 8 or higher. It is important that they are of the same version.

Run ./gradlew assemble to build all modules. If you don't need to build multik-openblas, just disable the cmake_build task and build the module you need.

Contributing

There is an opportunity to contribute to the project:

  1. Implement math, linalg, stat interfaces.
  2. Create your own engine successor from Engine, for example - JvmEngine.
  3. Use mk.addEngine and mk.setEngine to use your implementation.

More Repositories

1

anko

Pleasant Android application development
Kotlin
15,927
star
2

kotlinx.coroutines

Library support for Kotlin coroutines
Kotlin
12,203
star
3

kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Kotlin
5,375
star
4

dokka

API documentation engine for Kotlin
Kotlin
3,311
star
5

kotlin-examples

Various examples for Kotlin
3,177
star
6

KEEP

Kotlin Evolution and Enhancement Process
Markdown
3,109
star
7

kotlin-koans

Kotlin workshop
Kotlin
2,602
star
8

kotlinx-datetime

KotlinX multiplatform date/time library
Kotlin
2,379
star
9

kmp-production-sample

This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you can download it from the App Store and Google Play. It's been designed to demonstrate how KMM can be used in real production projects.
Kotlin
1,984
star
10

kotlindl

High-level Deep Learning Framework written in Kotlin and inspired by Keras
Kotlin
1,467
star
11

coroutines-examples

Examples for coroutines design in Kotlin
1,465
star
12

kotlinx-kover

Kotlin
1,324
star
13

kotlin-fullstack-sample

Kotlin Full-stack Application Example
Kotlin
1,218
star
14

kotlinx.collections.immutable

Immutable persistent collections for Kotlin
Kotlin
1,153
star
15

kotlin-jupyter

Kotlin kernel for Jupyter/IPython
Kotlin
1,094
star
16

kotlinx-cli

Pure Kotlin implementation of a generic CLI parser.
Kotlin
906
star
17

kmm-basic-sample

Example of Kotlin multiplatform project
Kotlin
887
star
18

dataframe

Structured data processing in Kotlin
Kotlin
831
star
19

kotlinx-io

Kotlin multiplatform I/O library
Kotlin
817
star
20

binary-compatibility-validator

Public API management tool
Kotlin
795
star
21

kotlinx-rpc

Add asynchronous RPC services to your multiplatform applications.
Kotlin
730
star
22

kotlinx-atomicfu

The idiomatic way to use atomic operations in Kotlin
Kotlin
716
star
23

kotlinconf-spinner

Kotlin
603
star
24

workshop

JetBrains Kotlin Workshop Material
Kotlin
594
star
25

kotlin-interactive-shell

Kotlin Language Interactive Shell
Java
591
star
26

kdoctor

Environment analysis tool
Kotlin
580
star
27

kandy

Kotlin plotting library.
Kotlin
579
star
28

kotlin-frontend-plugin

Gradle Kotlin (http://kotlinlang.org) plugin for frontend development
Kotlin
570
star
29

dukat

Converter of <any kind of declarations> to Kotlin external declarations
Kotlin
552
star
30

kotlin-wasm-examples

Examples with Kotlin/Wasm
519
star
31

kotlinx-benchmark

Kotlin multiplatform benchmarking toolkit
Kotlin
504
star
32

kotlin-spark-api

This projects gives Kotlin bindings and several extensions for Apache Spark. We are looking to have this as a part of Apache Spark 3.x
Kotlin
459
star
33

kotlin-by-example

The sources of Kotlin by Example.
396
star
34

kotlin-spec

Kotlin Language Specification:
Kotlin
358
star
35

kotlin-in-action

Code samples from the "Kotlin in Action" book
Kotlin
343
star
36

ts2kt

ts2kt is officially deprecated, please use https://github.com/Kotlin/dukat instead. // Converter of TypeScript definition files to Kotlin external declarations
Kotlin
320
star
37

kotlin-numpy

Kotlin bindings for NumPy
Kotlin
312
star
38

kotlin-style-guide

Work-in-progress notes for the Kotlin style guide
289
star
39

kotlinx-knit

Kotlin source code documentation management tool
Kotlin
287
star
40

anko-example

A small application built with Anko DSL
Kotlin
285
star
41

full-stack-web-jetbrains-night-sample

Full-stack demo application written with Kotlin MPP
Kotlin
271
star
42

KMP-App-Template

Kotlin Multiplatform app template with shared UI
Kotlin
269
star
43

kotlin-script-examples

Examples of Kotlin Scripts and usages of the Kotlin Scripting API
Kotlin
262
star
44

kotlinx-nodejs

Kotlin external declarations for using the Node.js API from Kotlin code targeting JavaScript
Kotlin
212
star
45

kotlin-eclipse

Kotlin Plugin for Eclipse
Kotlin
186
star
46

Storytale

Kotlin
165
star
47

kotlinx.reflect.lite

Lightweight library allowing to introspect basic stuff about Kotlin symbols
Kotlin
150
star
48

api-guidelines

Best practices to consider when writing an API for your library
144
star
49

kotlin-benchmarks

This is the project to verify and investigate performance issues in Kotlin and standard library.
Kotlin
136
star
50

kotlin-libs-publisher

Gradle plugin for publishing of Kotlin libs
Kotlin
119
star
51

kotlinx-browser

Kotlin browser API
Kotlin
110
star
52

kotlindl-app-sample

This repo demonstrates how to use KotlinDL for neural network inference on Android devices.
Kotlin
96
star
53

kotlin-koans-edu

Kotlin Koans for Educational Plugin and play.kotl.in
Kotlin
93
star
54

KMP-App-Template-Native

Kotlin Multiplatform app template with native UI
Kotlin
85
star
55

grammar-tools

Tokenization and parsing Kotlin code using the ANTLR Kotlin grammar
Kotlin
83
star
56

kmm-integration-sample

Kotlin
76
star
57

kmm-with-cocoapods-sample

This project represents the case when Cocoapods dependencies are added in Kotlin and there is no existing Xcode project
Kotlin
57
star
58

kotlin-koans-edu-obsolete

Obsolete: check https://github.com/Kotlin/kotlin-koans-edu for the latest version.
Kotlin
55
star
59

kotlin-native-calculator-sample

55
star
60

kotlinx.support

Extension and top-level functions to use JDK7/JDK8 features in Kotlin 1.0
Kotlin
54
star
61

js-externals

External declarations for Kotlin/JS
53
star
62

k2-performance-metrics

Measure Kotlin K2 compiler performance in your repository
Jupyter Notebook
45
star
63

kmp-native-wizard

A mostly-empty template to get started creating a Kotlin/Native project.
Kotlin
43
star
64

coroutines-workshop

Materials for a full-day workshop on Kotlin Coroutines
Kotlin
42
star
65

swift-export-sample

Kotlin to Swift technology preview
Swift
39
star
66

kotlin-playground-wp-plugin

WordPress plugin which allows to embed interactive Kotlin playground to any post via [kotlin] shortcode
PHP
35
star
67

io2019-serverside-demo

E2E Sample
Kotlin
31
star
68

kotlin-jupyter-libraries

Library descriptors for Kotlin kernel for Jupyter
29
star
69

kmm-with-cocoapods-multitarget-xcode-sample

This project is intended to demonstrate how to connect Kotlin library to Xcode project with several targets: iOS, macOS, tvOS, watchOS
Swift
23
star
70

xcode-compat

AppCode helper for Kotlin/Native and Xcode
Kotlin
21
star
71

kotlin-in-action-2e

Code samples for the second edition of "Kotlin in Action".
Kotlin
20
star
72

kotlin-wasm-compose-template

A template repository for Compose Multiplatform with Kotlin/Wasm target
Kotlin
20
star
73

kotlinx.dom

Kotlin
17
star
74

kotlin-wasm-benchmarks

Kotlin Multiplatform Collection of Benchmarks focused on Kotlin/Wasm performance
Kotlin
13
star
75

dokka-plugin-template

Dokka plugin quickstart template with pre-configured dependencies and publishing
Kotlin
12
star
76

kotlinx.team.infra

Kotlin
10
star
77

multiplatform-library-template

Kotlin
10
star
78

kotlin-js-inspection-pack-plugin

Adds useful inspections, intentions, and quick-fixes for working with Kotlin/JS projects.
Kotlin
10
star
79

obsolete-kotlin-swing

Experimental library providing some helper functions and extensions for creating Swing user interfaces.
Kotlin
8
star
80

kotlin-in-action-2e-jkid

Sample project accompanying the second edition of "Kotlin in Action". JSON serialization/deserialization library for Kotlin data classes
Kotlin
8
star
81

kotlin-wasm-browser-template

A template repository for Kotlin/Wasm on browser
HTML
8
star
82

kotlin-spark-shell

Kotlin Language support for Apache Spark
Kotlin
7
star
83

obsolete-kotlin-jdbc

Experimental library providing some helper functions and extensions for working with JDBC in Kotlin.
Kotlin
7
star
84

community-project-gradle-plugin

Kotlin
6
star
85

website-grammar-generator

Kotlin ANTLR grammar converter to XML for the Kotlin website or text file
Kotlin
6
star
86

kotlin-grammar-gpl2

Kotlin grammar ANTLR sources (under GPLv2)
ANTLR
6
star
87

web-site-samples

Examples repository for kotlinlang.org
4
star
88

kotlin-build-report-sample

Kotlin
4
star
89

kotlin-wasm-wasi-template

A template repository for Kotlin/Wasm with WASI
Kotlin
4
star
90

kotlin-jupyter-http-util

Ktor client and serialization for Kotlin Jupyter Notebooks
Kotlin
3
star
91

kotlin-cocoapods-spec

Ruby
3
star
92

analysis-api

Kotlin Analysis API Documentation
3
star
93

kotlin.github.io

Redirect to kotlinlang.org and favicon/title provider for kotlin.github.io/* websites.
HTML
2
star
94

kotlin-wasm-nodejs-template

A template repository for Kotlin/Wasm on Node.js
Kotlin
1
star
95

kmm-with-cocoapods-xcode-two-kotlin-libraries-sample

This project is intended to demonstrate the connection of two Kotlin libraries to existing Xcode project through Cocoapods
Kotlin
1
star