├── .gitignore ├── LICENSE ├── README.md ├── build.gradle.kts ├── docs ├── c.list ├── cfg │ └── buildprofiles.xml ├── images │ ├── Kodee_Assets_Digital_Kodee-naughty.svg │ ├── kotlin_logo.svg │ ├── kotlin_notebook_installation.png │ ├── kotlin_notebook_installation_dark.png │ ├── overview-architecture.png │ └── overview-architecture_dark.png ├── mk.tree ├── project.ihp ├── topics │ ├── FAQ.md │ ├── additional-resources-and-links.md │ ├── apiDocs │ │ ├── IO-operations.md │ │ ├── NDArray Class.md │ │ ├── NDArray.md │ │ ├── all.md │ │ ├── any.md │ │ ├── arange.md │ │ ├── arithmetic-operations.md │ │ ├── array-creation.md │ │ ├── array-objects.md │ │ ├── array-operations.md │ │ ├── asSequence.md │ │ ├── associate.md │ │ ├── average.md │ │ ├── chunked.md │ │ ├── comparison-operations.md │ │ ├── constants.md │ │ ├── contains.md │ │ ├── correlation-analysis.md │ │ ├── count.md │ │ ├── d1array.md │ │ ├── d2array.md │ │ ├── d2arrayIndices.md │ │ ├── d3array.md │ │ ├── d3arrayIndices.md │ │ ├── d4array.md │ │ ├── d4arrayIndices.md │ │ ├── descriptive-statistics.md │ │ ├── dimension.md │ │ ├── distinct.md │ │ ├── dnarray.md │ │ ├── documentation.md │ │ ├── dot.md │ │ ├── drop.md │ │ ├── eigenvalues-and-eigenvectors.md │ │ ├── filter.md │ │ ├── find.md │ │ ├── first.md │ │ ├── flatMap.md │ │ ├── fold.md │ │ ├── forEach.md │ │ ├── groupNDArray.md │ │ ├── hypothesis-testing.md │ │ ├── identity.md │ │ ├── indexOf.md │ │ ├── indexing-routines.md │ │ ├── intersect.md │ │ ├── iterating.md │ │ ├── joinTo.md │ │ ├── last.md │ │ ├── linear-algebra.md │ │ ├── linspace.md │ │ ├── logical-operations.md │ │ ├── map.md │ │ ├── mathematical.md │ │ ├── matrix-norms.md │ │ ├── matrix-operations.md │ │ ├── max.md │ │ ├── maximum.md │ │ ├── meshgrid.md │ │ ├── min.md │ │ ├── minimum.md │ │ ├── ndarrayOf.md │ │ ├── onEach.md │ │ ├── ones.md │ │ ├── partition.md │ │ ├── probability-distributions.md │ │ ├── rand.md │ │ ├── random-number-generation.md │ │ ├── reduce.md │ │ ├── reversed.md │ │ ├── scalars.md │ │ ├── scan.md │ │ ├── solving-systems-of-equations.md │ │ ├── sorted.md │ │ ├── statistics.md │ │ ├── sum.md │ │ ├── toArray.md │ │ ├── toCollection.md │ │ ├── toList.md │ │ ├── toNDArray.md │ │ ├── toPrimitiveArray.md │ │ ├── toType.md │ │ ├── type.md │ │ ├── universal-operations.md │ │ ├── windowed.md │ │ └── zeros.md │ ├── community-and-contribution-guidelines.md │ ├── examples.md │ ├── gettingStarted │ │ ├── getting-started.topic │ │ ├── installation.md │ │ ├── quickstart.md │ │ └── supported-platforms.md │ ├── overview.md │ ├── project-repository.md │ ├── release-notes-and-updates.md │ ├── userGuide │ │ ├── about-multik.md │ │ ├── basic.md │ │ ├── complex-numbers.md │ │ ├── copies-and-views.md │ │ ├── creating-multidimensional-arrays.md │ │ ├── engines-of-multik.md │ │ ├── indexing-and-slicing.md │ │ ├── input-and-output.md │ │ ├── iterating-over-arrays.md │ │ ├── multik-on-JVM.md │ │ ├── multik-on-JavaScript.md │ │ ├── multik-on-WASM.md │ │ ├── multik-on-desktop.md │ │ ├── multik-on-different-platforms.md │ │ ├── multik-on-mobile.md │ │ ├── performance-and-optimization.md │ │ ├── shape-manipulation.md │ │ ├── type-casting.md │ │ └── user-guide.topic │ └── welcome.topic └── v.list ├── gradle.properties ├── gradle ├── maven-metadata.gradle ├── multik_jni-cmake.gradle ├── publish-mpp-root-module-in-platform.gradle ├── publish.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── multik-core ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ ├── api │ │ ├── Annotation.kt │ │ ├── Engine.kt │ │ ├── EngineFactory.kt │ │ ├── Multik.kt │ │ ├── constructors.kt │ │ ├── createAlignedNDArray.kt │ │ ├── linalg │ │ │ ├── LinAlg.kt │ │ │ ├── LinAlgEx.kt │ │ │ ├── NormClassification.kt │ │ │ ├── _linalg.kt │ │ │ ├── dot.kt │ │ │ ├── eigvals.kt │ │ │ ├── inverse.kt │ │ │ ├── norm.kt │ │ │ ├── plu.kt │ │ │ ├── qr.kt │ │ │ ├── solve.kt │ │ │ └── svd.kt │ │ ├── math │ │ │ ├── Math.kt │ │ │ ├── MathEx.kt │ │ │ ├── _math.kt │ │ │ ├── cos.kt │ │ │ ├── exp.kt │ │ │ ├── log.kt │ │ │ └── sin.kt │ │ ├── primitiveArrays.kt │ │ ├── random.kt │ │ └── stat │ │ │ └── Statistics.kt │ │ └── ndarray │ │ ├── complex │ │ ├── Complex.kt │ │ ├── ComplexArrayIterators.kt │ │ ├── ComplexArrays.kt │ │ ├── ComplexDouble.kt │ │ ├── ComplexFloat.kt │ │ ├── Iterators.kt │ │ ├── _Complex.kt │ │ ├── _ComplexArrays.kt │ │ └── _NDArray.kt │ │ ├── data │ │ ├── DataType.kt │ │ ├── Dimensions.kt │ │ ├── Internals.kt │ │ ├── MemoryView.kt │ │ ├── MultiArrays.kt │ │ ├── MultiIndexProgression.kt │ │ ├── MutableMultiArrays.kt │ │ ├── NDArray.kt │ │ ├── NDArrayIterator.kt │ │ ├── ScalarsGettersAndSetters.kt │ │ ├── Slice.kt │ │ └── ViewGettersAndSetters.kt │ │ └── operations │ │ ├── Inplace.kt │ │ ├── IteratingNDArray.kt │ │ ├── Transformation.kt │ │ ├── _Arithmetic.kt │ │ └── _ArithmeticNDArray.kt │ ├── commonTest │ └── kotlin │ │ ├── org │ │ └── jetbrains │ │ │ └── kotlinx │ │ │ └── multik │ │ │ ├── creation │ │ │ ├── create1DArray.kt │ │ │ ├── create2DArray.kt │ │ │ ├── create3DArray.kt │ │ │ ├── create4DArray.kt │ │ │ └── createNDArray.kt │ │ │ ├── iterable │ │ │ └── IterableNDArrayTest.kt │ │ │ ├── ndarray │ │ │ ├── complex │ │ │ │ ├── ComplexMultiArrayTest.kt │ │ │ │ └── CreateComplexTest.kt │ │ │ ├── data │ │ │ │ ├── ConcatenateTest.kt │ │ │ │ ├── InternalsTest.kt │ │ │ │ ├── MultiIndexProgressionTest.kt │ │ │ │ └── SliceTest.kt │ │ │ └── operation │ │ │ │ └── TransformationTest.kt │ │ │ └── utils.kt │ │ └── samples │ │ ├── creation.kt │ │ └── docs │ │ ├── arithmetic.kt │ │ ├── creation.kt │ │ ├── indexingAndIterating.kt │ │ ├── overview.kt │ │ └── stdOperations.kt │ ├── jsMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ ├── api │ │ └── Engine.kt │ │ └── ndarray │ │ ├── complex │ │ └── ComplexDouble.js.kt │ │ └── data │ │ └── DataType.js.kt │ ├── jvmMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ ├── api │ │ ├── Engine.kt │ │ └── io │ │ │ ├── csv.kt │ │ │ ├── io.kt │ │ │ └── npy.kt │ │ └── ndarray │ │ ├── complex │ │ └── ComplexDouble.jvm.kt │ │ ├── data │ │ └── DataType.jvm.kt │ │ └── operations │ │ └── JVMIteratingNDArray.kt │ ├── jvmTest │ ├── kotlin │ │ └── org │ │ │ └── jetbrains │ │ │ └── kotlinx │ │ │ └── multik │ │ │ └── io │ │ │ ├── UtilsTestsIO.kt │ │ │ ├── csv.kt │ │ │ └── npy.kt │ └── resources │ │ └── data │ │ ├── csv │ │ ├── a1d.csv │ │ ├── a2d.csv │ │ └── a_complex.csv │ │ └── npy │ │ ├── a1d.npy │ │ ├── a4d.npy │ │ └── arrays.npz │ ├── nativeMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ ├── api │ │ └── Engine.kt │ │ └── ndarray │ │ ├── complex │ │ └── ComplexDouble.native.kt │ │ └── data │ │ └── DataType.native.kt │ └── wasmJsMain │ └── kotlin │ └── org │ └── jetbrains │ └── kotlinx │ └── multik │ ├── api │ └── Engine.kt │ └── ndarray │ ├── complex │ └── ComplexDouble.wasmJs.kt │ └── data │ └── DataType.wasmJs.kt ├── multik-default ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ └── default │ │ ├── DefaultEngine.kt │ │ ├── DefaultEngineFactory.kt │ │ ├── linalg │ │ ├── DefaultLinAlg.kt │ │ └── DefaultLinAlgEx.kt │ │ ├── math │ │ ├── DefaultMath.kt │ │ └── DefaultMathEx.kt │ │ └── stat │ │ └── DefaultStatistics.kt │ ├── iosMain │ └── kotlin │ │ └── org.jetbrains.kotlinx.multik.default │ │ ├── DefaultEngineFactory.kt │ │ ├── DefaultIosEngine.kt │ │ ├── linalg │ │ ├── DefaultLinAlg.kt │ │ └── DefaultLinAlgEx.kt │ │ ├── math │ │ ├── DefaultMath.kt │ │ └── DefaultMathEx.kt │ │ └── stat │ │ └── DefaultStatistics.kt │ ├── jsMain │ └── kotlin │ │ └── org.jetbrains.kotlinx.multik.default │ │ ├── DefaultEngineFactory.kt │ │ ├── DefaultJsEngine.kt │ │ ├── linalg │ │ ├── DefaultLinAlg.kt │ │ └── DefaultLinAlgEx.kt │ │ ├── math │ │ ├── DefaultMath.kt │ │ └── DefaultMathEx.kt │ │ └── stat │ │ └── DefaultStatistics.kt │ ├── jvmMain │ ├── kotlin │ │ └── org │ │ │ └── jetbrains │ │ │ └── kotlinx │ │ │ └── multik │ │ │ └── default │ │ │ ├── DefaultEngineFactory.kt │ │ │ ├── DefaultJvmEngine.kt │ │ │ ├── linalg │ │ │ ├── DefaultLinAlg.kt │ │ │ └── DefaultLinAlgEx.kt │ │ │ ├── math │ │ │ ├── DefaultMath.kt │ │ │ └── DefaultMathEx.kt │ │ │ └── stat │ │ │ └── DefaultStatistics.kt │ └── resources │ │ └── META-INF │ │ └── services │ │ └── org.jetbrains.kotlinx.multik.api.Engine │ ├── nativeMain │ └── kotlin │ │ └── org.jetbrains.kotlinx.multik.default │ │ ├── DefaultEngineFactory.kt │ │ ├── DefaultNativeEngine.kt │ │ ├── linalg │ │ ├── DefaultLinAlg.kt │ │ └── DefaultLinAlgEx.kt │ │ ├── math │ │ ├── DefaultMath.kt │ │ └── DefaultMathEx.kt │ │ └── stat │ │ └── DefaultStatistics.kt │ └── wasmJsMain │ └── kotlin │ └── org │ └── jetbrains │ └── kotlinx │ └── multik │ └── default │ ├── DefaultEngineFactory.kt │ ├── DefaultWasmEngine.kt │ ├── linalg │ ├── DefaultLinAlg.kt │ └── DefaultLinAlgEx.kt │ ├── math │ ├── DefaultMath.kt │ └── DefaultMathEx.kt │ └── stat │ └── DefaultStatistics.kt ├── multik-kotlin ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ └── kotlin │ │ ├── KEEngine.kt │ │ ├── linalg │ │ ├── KELinAlg.kt │ │ ├── LinAlgEx.kt │ │ ├── dot.kt │ │ ├── eigen.kt │ │ ├── gramShmidt.kt │ │ ├── hessenbergForm.kt │ │ ├── norm.kt │ │ ├── pluDecomposition.kt │ │ ├── qrFactorization.kt │ │ ├── solve.kt │ │ └── utils.kt │ │ ├── math │ │ ├── KEMath.kt │ │ └── KEMathEx.kt │ │ └── stat │ │ └── KEStatistics.kt │ ├── commonTest │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik_kotlin │ │ ├── linAlg │ │ └── KELinAlgTest.kt │ │ ├── math │ │ └── KEMathTest.kt │ │ └── statistics │ │ └── KEStatisticsTest.kt │ ├── jsMain │ └── kotlin │ │ └── org.jetbrains.kotlinx.multik │ │ └── kotlin │ │ └── KEEngine.kt │ ├── jvmMain │ └── resources │ │ └── META-INF │ │ └── services │ │ └── org.jetbrains.kotlinx.multik.api.Engine │ ├── nativeMain │ └── kotlin │ │ └── org.jetbrains.kotlinx.multik │ │ └── kotlin │ │ └── KEEngine.kt │ └── wasmJsMain │ └── kotlin │ └── org │ └── jetbrains │ └── kotlinx │ └── multik │ └── kotlin │ └── KEEngine.kt ├── multik-openblas ├── build.gradle.kts ├── cinterop │ └── libmultik.def ├── multik_jni │ ├── CMakeLists.txt │ └── src │ │ └── main │ │ ├── cpp │ │ ├── ComplexDouble.cpp │ │ ├── JniStat.cpp │ │ ├── jni_JniMath.cpp │ │ ├── jni_Linalg.cpp │ │ ├── mk_linalg.cpp │ │ ├── mk_math.cpp │ │ └── mk_stat.cpp │ │ └── headers │ │ ├── ComplexDouble.h │ │ ├── JniStat.h │ │ ├── jni_JniLinAlg.h │ │ ├── jni_JniMath.h │ │ ├── mk_linalg.h │ │ ├── mk_math.h │ │ └── mk_stat.h └── src │ ├── commonMain │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ └── openblas │ │ ├── Loader.kt │ │ ├── NativeEngine.kt │ │ ├── linalg │ │ ├── JniLinAlg.kt │ │ ├── NativeLinAlg.kt │ │ └── NativeLinAlgEx.kt │ │ ├── math │ │ ├── JniMath.kt │ │ ├── NativeMath.kt │ │ └── NativeMathEx.kt │ │ └── stat │ │ ├── JniStat.kt │ │ └── NativeStatistics.kt │ ├── commonTest │ └── kotlin │ │ └── org │ │ └── jetbrains │ │ └── kotlinx │ │ └── multik │ │ └── openblas │ │ ├── DataStructure.kt │ │ ├── MathTestUtils.kt │ │ ├── linalg │ │ └── NativeLinAlgTest.kt │ │ ├── math │ │ └── NativeMathTest.kt │ │ └── stat │ │ └── NativeStatisticsTest.kt │ ├── jvmMain │ ├── kotlin │ │ └── org │ │ │ └── jetbrains │ │ │ └── kotlinx │ │ │ └── multik │ │ │ └── openblas │ │ │ ├── JvmNativeEngine.kt │ │ │ ├── Loader.kt │ │ │ ├── linalg │ │ │ └── JniLinAlg.kt │ │ │ ├── math │ │ │ └── JniMath.kt │ │ │ └── stat │ │ │ └── JniStat.kt │ └── resources │ │ └── META-INF │ │ └── services │ │ └── org.jetbrains.kotlinx.multik.api.Engine │ └── nativeMain │ └── kotlin │ └── org.jetbrains.kotlinx.multik │ └── openblas │ ├── Loader.kt │ ├── NativeEngine.kt │ ├── linalg │ └── JniLinAlg.kt │ ├── math │ └── JniMath.kt │ └── stat │ └── JniStat.kt └── settings.gradle.kts /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### C++ template 3 | # Prerequisites 4 | *.d 5 | 6 | # Compiled Object files 7 | *.slo 8 | *.lo 9 | *.o 10 | *.obj 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Compiled Dynamic libraries 17 | *.so 18 | *.dylib 19 | *.dll 20 | 21 | # Fortran module files 22 | *.mod 23 | *.smod 24 | 25 | # Compiled Static libraries 26 | *.lai 27 | *.la 28 | *.a 29 | *.lib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | 36 | ### CMake template 37 | CMakeLists.txt.user 38 | CMakeCache.txt 39 | CMakeFiles 40 | CMakeScripts 41 | Testing 42 | Makefile 43 | cmake_install.cmake 44 | install_manifest.txt 45 | compile_commands.json 46 | CTestTestfile.cmake 47 | _deps 48 | 49 | ### Kotlin template 50 | .kotlin 51 | 52 | # Compiled class file 53 | *.class 54 | 55 | # Log file 56 | *.log 57 | 58 | # BlueJ files 59 | *.ctxt 60 | 61 | # Package Files # 62 | *.war 63 | *.nar 64 | *.ear 65 | *.zip 66 | *.tar.gz 67 | *.rar 68 | 69 | # Kotlin/JS 70 | kotlin-js-store/ 71 | 72 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 73 | hs_err_pid* 74 | 75 | ### JetBrains template 76 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 77 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 78 | 79 | # User-specific stuff 80 | .idea/**/workspace.xml 81 | .idea/**/tasks.xml 82 | .idea/**/usage.statistics.xml 83 | .idea/**/dictionaries 84 | .idea/**/shelf 85 | 86 | # Generated files 87 | .idea/**/contentModel.xml 88 | 89 | # Sensitive or high-churn files 90 | .idea/**/dataSources/ 91 | .idea/**/dataSources.ids 92 | .idea/**/dataSources.local.xml 93 | .idea/**/sqlDataSources.xml 94 | .idea/**/dynamic.xml 95 | .idea/**/uiDesigner.xml 96 | .idea/**/dbnavigator.xml 97 | 98 | # Gradle 99 | .gradle/ 100 | .idea/**/gradle.xml 101 | .idea/**/libraries 102 | 103 | # Gradle and Maven with auto-import 104 | # When using Gradle or Maven with auto-import, you should exclude module files, 105 | # since they will be recreated, and may cause churn. Uncomment if using 106 | # auto-import. 107 | # .idea/artifacts 108 | # .idea/compiler.xml 109 | # .idea/jarRepositories.xml 110 | # .idea/modules.xml 111 | # .idea/*.iml 112 | # .idea/modules 113 | # *.iml 114 | # *.ipr 115 | 116 | # CMake 117 | cmake-build-*/ 118 | 119 | # Mongo Explorer plugin 120 | .idea/**/mongoSettings.xml 121 | 122 | # File-based project format 123 | *.iws 124 | 125 | # IntelliJ 126 | .idea/ 127 | build/ 128 | out/ 129 | 130 | # mpeltonen/sbt-idea plugin 131 | .idea_modules/ 132 | 133 | .DS_Store 134 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import java.time.Duration 2 | 3 | buildscript { 4 | repositories { 5 | mavenCentral() 6 | } 7 | } 8 | 9 | plugins { 10 | val kotlin_version: String by System.getProperties() 11 | val nexus_version: String by System.getProperties() 12 | 13 | kotlin("multiplatform") version kotlin_version apply false 14 | id("io.github.gradle-nexus.publish-plugin") version nexus_version 15 | } 16 | 17 | val kotlin_version: String by System.getProperties() 18 | val multik_version: String by project 19 | val unpublished = listOf("multik") 20 | 21 | allprojects { 22 | repositories { 23 | mavenCentral() 24 | } 25 | 26 | group = "org.jetbrains.kotlinx" 27 | version = multik_version 28 | } 29 | 30 | configure(subprojects.filter { it.name !in unpublished }) { 31 | apply("$rootDir/gradle/publish.gradle") 32 | } 33 | 34 | tasks.withType().configureEach { 35 | args.add("--ignore-engines") 36 | } 37 | 38 | val sonatypeUser: String = System.getenv("SONATYPE_USER") ?: "" 39 | val sonatypePassword: String = System.getenv("SONATYPE_PASSWORD") ?: "" 40 | 41 | nexusPublishing { 42 | packageGroup.set(project.group.toString()) 43 | this.repositories { 44 | sonatype { 45 | username.set(sonatypeUser) 46 | password.set(sonatypePassword) 47 | repositoryDescription.set("kotlinx.multik staging repository, version: $version") 48 | } 49 | } 50 | 51 | transitionCheckOptions { 52 | maxRetries.set(100) 53 | delayBetween.set(Duration.ofSeconds(5)) 54 | } 55 | } -------------------------------------------------------------------------------- /docs/c.list: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/cfg/buildprofiles.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | kotlin_logo.svg 7 | https://kotlin.github.io/multik/welcome.html 8 | true 9 | https://github.com/Kotlin/multik/edit/develop/docs/ 10 | true 11 | 12 | https://kotlin.github.io/multik/ 13 | false 14 | webmaster@jetbrains.com 15 | https://www.jetbrains.com/search/json/ 16 | https://kotlin.github.io/multik/ 17 | 18 | https://kotlinlang.org/assets/images/favicon.svg?v2 19 | true 20 | 21 | 22 | 23 | false 24 | 25 | 26 |
27 | 28 | 29 | 30 | Slack Community 31 | GitHub 32 | Code of Conduct 33 | 2000–2024 JetBrains s.r.o. 34 |
35 | 36 |
37 | -------------------------------------------------------------------------------- /docs/images/kotlin_logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/images/kotlin_notebook_installation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/images/kotlin_notebook_installation.png -------------------------------------------------------------------------------- /docs/images/kotlin_notebook_installation_dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/images/kotlin_notebook_installation_dark.png -------------------------------------------------------------------------------- /docs/images/overview-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/images/overview-architecture.png -------------------------------------------------------------------------------- /docs/images/overview-architecture_dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/images/overview-architecture_dark.png -------------------------------------------------------------------------------- /docs/project.ihp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /docs/topics/FAQ.md: -------------------------------------------------------------------------------- 1 | # FAQ 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/additional-resources-and-links.md: -------------------------------------------------------------------------------- 1 | # Additional Resources and Links 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/IO-operations.md: -------------------------------------------------------------------------------- 1 | # IO operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/NDArray Class.md: -------------------------------------------------------------------------------- 1 | # NDArray 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/NDArray.md: -------------------------------------------------------------------------------- 1 | # ndarray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/all.md: -------------------------------------------------------------------------------- 1 | # all 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/any.md: -------------------------------------------------------------------------------- 1 | # any 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/arange.md: -------------------------------------------------------------------------------- 1 | # arange 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/arithmetic-operations.md: -------------------------------------------------------------------------------- 1 | # Arithmetic Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/array-creation.md: -------------------------------------------------------------------------------- 1 | # Array creation 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/array-objects.md: -------------------------------------------------------------------------------- 1 | # Array objects 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/array-operations.md: -------------------------------------------------------------------------------- 1 | # Array Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/asSequence.md: -------------------------------------------------------------------------------- 1 | # asSequence 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/associate.md: -------------------------------------------------------------------------------- 1 | # associate 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/average.md: -------------------------------------------------------------------------------- 1 | # average 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/chunked.md: -------------------------------------------------------------------------------- 1 | # chunked 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/comparison-operations.md: -------------------------------------------------------------------------------- 1 | # Comparison Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/constants.md: -------------------------------------------------------------------------------- 1 | # Constants 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/contains.md: -------------------------------------------------------------------------------- 1 | # contains 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/correlation-analysis.md: -------------------------------------------------------------------------------- 1 | # Correlation Analysis 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/count.md: -------------------------------------------------------------------------------- 1 | # count 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d1array.md: -------------------------------------------------------------------------------- 1 | # d1array 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d2array.md: -------------------------------------------------------------------------------- 1 | # d2array 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d2arrayIndices.md: -------------------------------------------------------------------------------- 1 | # d2arrayIndices 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d3array.md: -------------------------------------------------------------------------------- 1 | # d3array 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d3arrayIndices.md: -------------------------------------------------------------------------------- 1 | # d3arrayIndices 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d4array.md: -------------------------------------------------------------------------------- 1 | # d4array 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/d4arrayIndices.md: -------------------------------------------------------------------------------- 1 | # d4arrayIndices 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/descriptive-statistics.md: -------------------------------------------------------------------------------- 1 | # Descriptive Statistics 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/dimension.md: -------------------------------------------------------------------------------- 1 | # Dimension 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/distinct.md: -------------------------------------------------------------------------------- 1 | # distinct 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/dnarray.md: -------------------------------------------------------------------------------- 1 | # dnarray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/documentation.md: -------------------------------------------------------------------------------- 1 | # Documentation 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/dot.md: -------------------------------------------------------------------------------- 1 | # dot 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/drop.md: -------------------------------------------------------------------------------- 1 | # drop 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/eigenvalues-and-eigenvectors.md: -------------------------------------------------------------------------------- 1 | # Eigenvalues and Eigenvectors 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/filter.md: -------------------------------------------------------------------------------- 1 | # filter 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/find.md: -------------------------------------------------------------------------------- 1 | # find 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/first.md: -------------------------------------------------------------------------------- 1 | # first 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/flatMap.md: -------------------------------------------------------------------------------- 1 | # flatMap 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/fold.md: -------------------------------------------------------------------------------- 1 | # fold 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/forEach.md: -------------------------------------------------------------------------------- 1 | # forEach 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/groupNDArray.md: -------------------------------------------------------------------------------- 1 | # groupNDArray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/hypothesis-testing.md: -------------------------------------------------------------------------------- 1 | # Hypothesis Testing 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/identity.md: -------------------------------------------------------------------------------- 1 | # identity 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/indexOf.md: -------------------------------------------------------------------------------- 1 | # indexOf 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/indexing-routines.md: -------------------------------------------------------------------------------- 1 | # Indexing routines 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/intersect.md: -------------------------------------------------------------------------------- 1 | # intersect 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/iterating.md: -------------------------------------------------------------------------------- 1 | # Iterating 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/joinTo.md: -------------------------------------------------------------------------------- 1 | # joinTo 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/last.md: -------------------------------------------------------------------------------- 1 | # last 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/linear-algebra.md: -------------------------------------------------------------------------------- 1 | # Linear Algebra 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/linspace.md: -------------------------------------------------------------------------------- 1 | # linspace 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/logical-operations.md: -------------------------------------------------------------------------------- 1 | # Logical Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/map.md: -------------------------------------------------------------------------------- 1 | # map 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/mathematical.md: -------------------------------------------------------------------------------- 1 | # Mathematical 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/matrix-norms.md: -------------------------------------------------------------------------------- 1 | # Matrix Norms 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/matrix-operations.md: -------------------------------------------------------------------------------- 1 | # Matrix Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/max.md: -------------------------------------------------------------------------------- 1 | # max 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/maximum.md: -------------------------------------------------------------------------------- 1 | # maximum 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/meshgrid.md: -------------------------------------------------------------------------------- 1 | # meshgrid 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/min.md: -------------------------------------------------------------------------------- 1 | # min 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/minimum.md: -------------------------------------------------------------------------------- 1 | # minimum 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/ndarrayOf.md: -------------------------------------------------------------------------------- 1 | # ndarrayOf 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/onEach.md: -------------------------------------------------------------------------------- 1 | # onEach 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/ones.md: -------------------------------------------------------------------------------- 1 | # ones 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/partition.md: -------------------------------------------------------------------------------- 1 | # partition 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/probability-distributions.md: -------------------------------------------------------------------------------- 1 | # Probability Distributions 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/rand.md: -------------------------------------------------------------------------------- 1 | # rand 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/random-number-generation.md: -------------------------------------------------------------------------------- 1 | # Random Number Generation 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/reduce.md: -------------------------------------------------------------------------------- 1 | # reduce 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/reversed.md: -------------------------------------------------------------------------------- 1 | # reversed 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/scalars.md: -------------------------------------------------------------------------------- 1 | # Scalars 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/scan.md: -------------------------------------------------------------------------------- 1 | # scan 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/solving-systems-of-equations.md: -------------------------------------------------------------------------------- 1 | # Solving Systems of Equations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/sorted.md: -------------------------------------------------------------------------------- 1 | # sorted 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/statistics.md: -------------------------------------------------------------------------------- 1 | # Statistics 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/sum.md: -------------------------------------------------------------------------------- 1 | # sum 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toArray.md: -------------------------------------------------------------------------------- 1 | # toArray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toCollection.md: -------------------------------------------------------------------------------- 1 | # toCollection 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toList.md: -------------------------------------------------------------------------------- 1 | # toList 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toNDArray.md: -------------------------------------------------------------------------------- 1 | # toNDArray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toPrimitiveArray.md: -------------------------------------------------------------------------------- 1 | # toPrimitiveArray 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/toType.md: -------------------------------------------------------------------------------- 1 | # toType 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/type.md: -------------------------------------------------------------------------------- 1 | # Type 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/universal-operations.md: -------------------------------------------------------------------------------- 1 | # Universal Operations 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/apiDocs/windowed.md: -------------------------------------------------------------------------------- 1 | # windowed 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/apiDocs/zeros.md: -------------------------------------------------------------------------------- 1 | # zeros 2 | 3 | Start typing here... -------------------------------------------------------------------------------- /docs/topics/community-and-contribution-guidelines.md: -------------------------------------------------------------------------------- 1 | # Community and Contribution Guidelines 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/examples.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/gettingStarted/getting-started.topic: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | 9 | Embark on your journey with Multik. 10 | Get to grips with the essential features through a comprehensive Quickstart guide, understand how 11 | to set up Multik through detailed Installation instructions, and explore the various Supported Platforms. 12 | Kickstart your path to multidimensional array computations in Kotlin. 13 | 14 | 15 | 16 | Kickstart your journey with Multik — quickstart guide, installation instructions, and supported platforms. 17 | 18 | 19 | 20 | Begin your exploration with Multik's quickstart, installation, and supported platforms. 21 | 22 | 23 | 24 |
  • 25 | 26 |
  • 27 |
  • 28 | 29 |
  • 30 |
  • 31 | 32 |
  • 33 |
    34 | 35 | Kodee 36 | 37 |
    -------------------------------------------------------------------------------- /docs/topics/project-repository.md: -------------------------------------------------------------------------------- 1 | # Project Repository 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/release-notes-and-updates.md: -------------------------------------------------------------------------------- 1 | # Release Notes and Updates 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/about-multik.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/about-multik.md -------------------------------------------------------------------------------- /docs/topics/userGuide/basic.md: -------------------------------------------------------------------------------- 1 | # Basics of Multik 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/complex-numbers.md: -------------------------------------------------------------------------------- 1 | # Complex Numbers 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/copies-and-views.md: -------------------------------------------------------------------------------- 1 | # Copies and Views 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/creating-multidimensional-arrays.md: -------------------------------------------------------------------------------- 1 | # Creating Multidimensional Arrays 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/engines-of-multik.md: -------------------------------------------------------------------------------- 1 | # Engines of Multik 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/indexing-and-slicing.md: -------------------------------------------------------------------------------- 1 | # Indexing and Slicing 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/input-and-output.md: -------------------------------------------------------------------------------- 1 | # Input and Output 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/iterating-over-arrays.md: -------------------------------------------------------------------------------- 1 | # Iterating over Arrays 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-JVM.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/multik-on-JVM.md -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-JavaScript.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/multik-on-JavaScript.md -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-WASM.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/multik-on-WASM.md -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-desktop.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/multik-on-desktop.md -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-different-platforms.md: -------------------------------------------------------------------------------- 1 | # Multik on Different Platforms 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/multik-on-mobile.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/docs/topics/userGuide/multik-on-mobile.md -------------------------------------------------------------------------------- /docs/topics/userGuide/performance-and-optimization.md: -------------------------------------------------------------------------------- 1 | # Performance and Optimization 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/shape-manipulation.md: -------------------------------------------------------------------------------- 1 | # Shape manipulation 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/type-casting.md: -------------------------------------------------------------------------------- 1 | # Type Casting 2 | 3 | [//]: # (TODO) 4 | TODO 5 | TODO 6 | TODO -------------------------------------------------------------------------------- /docs/topics/userGuide/user-guide.topic: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 |

    Start typing here...

    9 |
    -------------------------------------------------------------------------------- /docs/v.list: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | multik_version=0.2.3 3 | 4 | # Kotlin 5 | systemProp.kotlin_version=1.9.22 6 | group=org.jetbrains.kotlinx 7 | 8 | # Dependencies 9 | systemProp.nexus_version=1.3.0 10 | systemProp.dokka_version=1.8.20 11 | systemProp.korro_version=0.1.5 12 | openblas_version=0.3.23 13 | common_csv_version=1.10.0 14 | 15 | # Gradle 16 | org.gradle.parallel=true 17 | org.gradle.caching=true 18 | 19 | # MPP 20 | kotlin.mpp.stability.nowarn=true 21 | kotlin.mpp.enableCInteropCommonization=true 22 | 23 | # Native 24 | kotlin.native.cacheKind.linuxX64=static 25 | kotlin.native.cacheKind.iosArm64=static 26 | 27 | # Settings 28 | kotlin.incremental.multiplatform=true 29 | kotlin.native.ignoreDisabledTargets=true 30 | 31 | # JS IR backend sometimes crashes with out-of-memory 32 | # Check KT-37187 33 | org.gradle.jvmargs=-Xmx3g 34 | 35 | # NodeJS 36 | nodeJsVersion = 21.0.0-v8-canary202309143a48826a08 37 | nodeDownloadUrl = https://nodejs.org/download/v8-canary 38 | -------------------------------------------------------------------------------- /gradle/maven-metadata.gradle: -------------------------------------------------------------------------------- 1 | def pomConfig = { 2 | licenses { 3 | license { 4 | name "The Apache Software License, Version 2.0" 5 | url "https://www.apache.org/licenses/LICENSE-2.0.txt" 6 | distribution "repo" 7 | } 8 | } 9 | developers { 10 | developer { 11 | id "JetBrains" 12 | name "Jetbrains Team" 13 | organization "JetBrains" 14 | organizationUrl "https://www.jetbrains.com" 15 | } 16 | } 17 | 18 | scm { 19 | url "https://github.com/Kotlin/multik" 20 | } 21 | } 22 | 23 | project.ext.configureMavenCentralMetadata = { pom -> 24 | def root = asNode() 25 | root.appendNode('name', project.name) 26 | root.appendNode('description', 'Multidimensional array library for Kotlin.') 27 | root.appendNode('url', 'https://github.com/Kotlin/multik') 28 | root.children().last() + pomConfig 29 | } -------------------------------------------------------------------------------- /gradle/multik_jni-cmake.gradle: -------------------------------------------------------------------------------- 1 | def cmake_path = "${rootDir}/multik-openblas/multik_jni" 2 | def path_to_build = layout.buildDirectory.dir("cmake-build").get().asFile.absolutePath 3 | 4 | String getTargetOSAndArch() { 5 | String hostOs = System.getProperty('os.name').toLowerCase() 6 | String hostArch = System.getProperty('os.arch').toLowerCase() 7 | if (hostOs == "mac os x" && hostArch == "x86_64") 8 | return 'macosX64' 9 | else if (hostOs == "mac os x" && hostArch == "aarch64") 10 | return 'macosArm64' 11 | else if (hostOs == "linux") 12 | return 'linuxX64' 13 | else if (hostOs.startsWith("windows")) 14 | return 'mingwX64' 15 | else 16 | throw GradleException(""" 17 | Failed to detect platform. Please use the following platforms: 18 | macosX64 19 | macosArm64 20 | linuxX64 21 | mingwX64 (Windows X64 with mingw) 22 | Or specify explicitly, for example `export androidArm64` 23 | """) 24 | } 25 | 26 | def targetOS = System.getenv("TARGET_OS") ?: getTargetOSAndArch() 27 | 28 | def cmake_c_compiler = System.getenv("CMAKE_C_COMPILER") ?: "gcc" 29 | def cmake_cxx_compiler = System.getenv("CMAKE_CXX_COMPILER") ?: "g++" 30 | def lib_gcc_path = System.getenv("GCC_LIB_Path") ?: "" 31 | 32 | static boolean isWindows() { 33 | return System.getProperty('os.name').toLowerCase().contains('windows') 34 | } 35 | 36 | tasks.register('createBuildDir') { 37 | doLast { 38 | mkdir(layout.buildDirectory.get().asFile) 39 | mkdir(layout.buildDirectory.dir("cmake-build").get().asFile) 40 | } 41 | } 42 | 43 | tasks.register('config_cmake') { 44 | dependsOn createBuildDir 45 | String gen = isWindows() ? "-G CodeBlocks - MinGW Makefiles" : "" 46 | doLast { 47 | exec { 48 | commandLine "cmake", 49 | "-DCMAKE_BUILD_TYPE=Release", 50 | "-DCMAKE_C_COMPILER=$cmake_c_compiler", 51 | "-DCMAKE_CXX_COMPILER=$cmake_cxx_compiler", 52 | "-DGCC_LIB_PATH=$lib_gcc_path", 53 | "-DTARGET_OS=$targetOS", 54 | gen, 55 | "-S", cmake_path, 56 | "-B", path_to_build 57 | } 58 | } 59 | } 60 | 61 | tasks.register('build_cmake') { 62 | dependsOn config_cmake 63 | String jProc = Runtime.getRuntime().availableProcessors().toString() 64 | doLast { 65 | exec { 66 | commandLine("cmake", "--build", path_to_build, "--target", "multik_jni-$targetOS", "--", "-j", "$jProc") 67 | } 68 | copy { 69 | from(path_to_build) 70 | include("*.dylib", "*.so", "*.dll") 71 | into(layout.buildDirectory.dir("libs").get().asFile) 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /gradle/publish-mpp-root-module-in-platform.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | /** 6 | * took from https://github.com/Kotlin/kotlinx.serialization/blob/master/gradle/publish-mpp-root-module-in-platform.gradle 7 | */ 8 | 9 | project.ext.publishPlatformArtifactsInRootModule = { MavenPublication platformPublication -> 10 | afterEvaluate { 11 | XmlProvider platformXml = null 12 | 13 | platformPublication.pom.withXml { platformXml = it } 14 | 15 | publishing.publications.kotlinMultiplatform { 16 | pom.withXml { 17 | Node root = asNode() 18 | // Remove the original content and add the content from the platform POM: 19 | root.children().toList().each { root.remove(it as Node) } 20 | platformXml.asNode().children().each { root.append(it as Node) } 21 | 22 | // Adjust the self artifact ID, as it should match the root module's coordinates: 23 | ((root.get("artifactId") as NodeList).get(0) as Node).setValue(artifactId) 24 | 25 | // Set packaging to POM to indicate that there's no artifact: 26 | root.appendNode("packaging", "pom") 27 | 28 | // Remove the original platform dependencies and add a single dependency on the platform module: 29 | Node dependencies = (root.get("dependencies") as NodeList).get(0) as Node 30 | dependencies.children().toList().each { dependencies.remove(it as Node) } 31 | Node singleDependency = dependencies.appendNode("dependency") 32 | singleDependency.appendNode("groupId", platformPublication.groupId) 33 | singleDependency.appendNode("artifactId", platformPublication.artifactId) 34 | singleDependency.appendNode("version", platformPublication.version) 35 | singleDependency.appendNode("scope", "compile") 36 | } 37 | } 38 | 39 | tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication"}.configureEach { 40 | dependsOn(tasks["generatePomFileFor${platformPublication.name.capitalize()}Publication"]) 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /gradle/publish.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | 6 | apply plugin: 'maven-publish' 7 | apply from: project.rootProject.file('gradle/maven-metadata.gradle') 8 | 9 | def sonatypeUser = System.getenv('SONATYPE_USER') 10 | def sonatypePassword = System.getenv('SONATYPE_PASSWORD') 11 | 12 | task javadocJar(type: Jar) { 13 | archiveClassifier = 'javadoc' 14 | } 15 | 16 | publishing { 17 | def variantName = "${project.name}" 18 | publications.all { 19 | def type = it.name 20 | switch (type) { 21 | case 'kotlinMultiplatform': 22 | it.artifactId = variantName 23 | apply from: "$rootDir/gradle/publish-mpp-root-module-in-platform.gradle" 24 | break 25 | case 'metadata': 26 | case 'jvm': 27 | case 'js': 28 | it.artifactId = "$variantName-$type" 29 | break 30 | } 31 | pom.withXml(configureMavenCentralMetadata) 32 | groupId project.group 33 | version project.version 34 | if (name != "kotlinMultiplatform") { 35 | artifact javadocJar 36 | } 37 | } 38 | repositories { 39 | maven { 40 | url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 41 | credentials { 42 | username = sonatypeUser 43 | password = sonatypePassword 44 | } 45 | } 46 | } 47 | } 48 | 49 | def signingKey = System.getenv("SIGN_KEY_ID") 50 | def signingKeyPassphrase = System.getenv("SIGN_KEY_PASSPHRASE") 51 | def signingPrivateKey = System.getenv("SIGN_KEY_PRIVATE") 52 | 53 | 54 | if (signingKey != null) { 55 | apply plugin: 'signing' 56 | signing { 57 | useInMemoryPgpKeys(signingKey, signingPrivateKey, signingKeyPassphrase) 58 | sign publishing.publications 59 | } 60 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Apr 09 14:05:51 MSK 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/Annotation.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.api 2 | 3 | /** 4 | * Marks declarations that are still **experimental** in multik, which means that the design of the 5 | * corresponding declarations has open issues which may (or may not) lead to their changes in the future. 6 | * Roughly speaking, there is a chance that those declarations will be deprecated in the near future or 7 | * the semantics of their behavior may change in some way that may break some code. 8 | */ 9 | @MustBeDocumented 10 | @Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION, AnnotationTarget.TYPEALIAS) 11 | @RequiresOptIn(level = RequiresOptIn.Level.WARNING) 12 | public annotation class ExperimentalMultikApi -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/EngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api 6 | 7 | public interface EngineFactory { 8 | public fun getEngine(type: EngineType? = null): Engine 9 | } -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/Multik.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api 6 | 7 | import org.jetbrains.kotlinx.multik.api.Multik.engine 8 | import org.jetbrains.kotlinx.multik.api.Multik.engines 9 | import org.jetbrains.kotlinx.multik.api.Multik.linalg 10 | import org.jetbrains.kotlinx.multik.api.Multik.math 11 | import org.jetbrains.kotlinx.multik.api.Multik.stat 12 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 13 | import org.jetbrains.kotlinx.multik.api.math.Math 14 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 15 | 16 | /** 17 | * Abbreviated name for [Multik]. 18 | */ 19 | public typealias mk = Multik 20 | 21 | 22 | /** 23 | * The basic object through which calls all ndarray functions. Gives access to ndarray creation and interfaces [Math], 24 | * [LinAlg] and [Statistics]. 25 | * Calling [Multik] will load the engine. The default is "DEFAULT". 26 | * If no engine is found, then an exception is thrown only when you call an implementation that requires the engine. 27 | * 28 | * Note: Through [Multik], you can set your own interface implementation. 29 | * 30 | * @property engine currently used engine. 31 | * @property engines list of engines. 32 | * @property math returns the [Math] implementation of the corresponding engine. 33 | * @property linalg returns the [LinAlg] implementation of the corresponding engine. 34 | * @property stat returns the [Statistics] implementation of the corresponding engine. 35 | */ 36 | public object Multik { 37 | public val engine: String? get() = Engine.getDefaultEngine() 38 | 39 | private val _engines: MutableMap = mutableMapOf( 40 | "DEFAULT" to DefaultEngineType, 41 | "KOTLIN" to KEEngineType, 42 | "NATIVE" to NativeEngineType 43 | ) 44 | 45 | public val engines: Map 46 | get() = _engines 47 | 48 | public val math: Math get() = Engine.getMath() 49 | public val linalg: LinAlg get() = Engine.getLinAlg() 50 | public val stat: Statistics get() = Engine.getStatistics() 51 | 52 | /** 53 | * Adds engine to [engines]. 54 | */ 55 | public fun addEngine(type: EngineType) { 56 | if (!_engines.containsKey(type.name)) { 57 | _engines[type.name] = type 58 | } 59 | } 60 | 61 | /** 62 | * Sets the engine of type [type] as the current implementation. 63 | */ 64 | public fun setEngine(type: EngineType) { 65 | if (type.name in engines) 66 | Engine.setDefaultEngine(type) 67 | } 68 | 69 | /** 70 | * Returns a list of [elements]. Sugar for easy array creation. 71 | */ 72 | public operator fun get(vararg elements: T): List = elements.toList() 73 | } 74 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/LinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 8 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 9 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 10 | 11 | 12 | /** 13 | * Linear Algebra methods interface. 14 | */ 15 | public interface LinAlg { 16 | 17 | /** 18 | * instance of [LinAlgEx] 19 | */ 20 | public val linAlgEx: LinAlgEx 21 | 22 | /** 23 | * Raise a square matrix to power [n]. 24 | */ 25 | public fun pow(mat: MultiArray, n: Int): NDArray 26 | 27 | } -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/NormClassification.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | /** 8 | * Matrix norm types. 9 | * 10 | * @property lapackCode value for lapack 11 | */ 12 | public enum class Norm(public val lapackCode: Char) { 13 | /** 14 | * max(abs(A(i,j))) 15 | */ 16 | Max('M'), 17 | 18 | /** 19 | * denotes the one norm of a matrix (maximum column sum) 20 | */ 21 | N1('1'), 22 | 23 | /** 24 | * denotes the infinity norm of a matrix (maximum row sum) 25 | */ 26 | Inf('I'), 27 | 28 | /** 29 | * denotes the Frobenius norm of a matrix (square root of sum of squares) 30 | */ 31 | Fro('F') 32 | } -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/_linalg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.mk 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D1 10 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 11 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 12 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 13 | import kotlin.jvm.JvmName 14 | 15 | /** 16 | * Returns the matrix product of two numeric matrices. 17 | * 18 | * same as [LinAlg.dot] 19 | */ 20 | @JvmName("dotDefMMNumber") 21 | public infix fun MultiArray.dot(b: MultiArray): NDArray = mk.linalg.linAlgEx.dotMM(this, b) 22 | 23 | /** 24 | * Returns the matrix product of two complex matrices. 25 | * 26 | * same as [LinAlg.dot] 27 | */ 28 | @JvmName("dotDefMMComplex") 29 | public infix fun MultiArray.dot(b: MultiArray): NDArray = mk.linalg.linAlgEx.dotMMComplex(this, b) 30 | 31 | /** 32 | * Returns the matrix product of a numeric matrix and a numeric vector. 33 | * 34 | * same as [LinAlg.dot] 35 | */ 36 | @JvmName("dotDefMVNumber") 37 | public infix fun MultiArray.dot(b: MultiArray): NDArray = mk.linalg.linAlgEx.dotMV(this, b) 38 | 39 | /** 40 | * Returns the matrix product of a complex matrix and a complex vector. 41 | * 42 | * same as [LinAlg.dot] 43 | */ 44 | @JvmName("dotDefMVComplex") 45 | public infix fun MultiArray.dot(b: MultiArray): NDArray = mk.linalg.linAlgEx.dotMVComplex(this, b) 46 | 47 | /** 48 | * Returns the product of two numeric vectors. 49 | * 50 | * same as [LinAlg.dot] 51 | */ 52 | @JvmName("dotDefVVNumber") 53 | public infix fun MultiArray.dot(b: MultiArray): T = mk.linalg.linAlgEx.dotVV(this, b) 54 | 55 | /** 56 | * Returns the product of two complex vectors. 57 | * 58 | * same as [LinAlg.dot] 59 | */ 60 | @JvmName("dotDefVVComplex") 61 | public infix fun MultiArray.dot(b: MultiArray): T = mk.linalg.linAlgEx.dotVVComplex(this, b) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/dot.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.data.D1 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Dot products of two number matrices. 16 | */ 17 | @JvmName("dotMMNumber") 18 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.dotMM(a, b) 19 | 20 | /** 21 | * Dot products of two complex matrices. 22 | */ 23 | @JvmName("dotMMComplex") 24 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.dotMMComplex(a, b) 25 | 26 | /** 27 | * Dot products of number matrix and number vector. 28 | */ 29 | @JvmName("dotMVNumber") 30 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.dotMV(a, b) 31 | 32 | /** 33 | * Dot products of complex matrix and complex vector. 34 | */ 35 | @JvmName("dotMVComplex") 36 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.dotMVComplex(a, b) 37 | 38 | /** 39 | * Dot products of two number vectors. Scalar product. 40 | */ 41 | @JvmName("dotVVNumber") 42 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): T = this.linAlgEx.dotVV(a, b) 43 | 44 | /** 45 | * Dot products of two complex vectors. Scalar product. 46 | */ 47 | @JvmName("dotVVComplex") 48 | public fun LinAlg.dot(a: MultiArray, b: MultiArray): T = this.linAlgEx.dotVVComplex(a, b) 49 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/eigvals.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 9 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 10 | import org.jetbrains.kotlinx.multik.ndarray.data.D1Array 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.D2Array 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import kotlin.jvm.JvmName 15 | 16 | /** 17 | * Calculates the eigenvalues and eigenvectors of a float matrix 18 | * @return a pair of a vector of eigenvalues and a matrix of eigenvectors 19 | */ 20 | @JvmName("eigF") 21 | public fun LinAlg.eig(mat: MultiArray): Pair, D2Array> = 22 | this.linAlgEx.eigF(mat) 23 | 24 | /** 25 | * Calculates the eigenvalues and eigenvectors of a numeric matrix 26 | * @return a pair of a vector of eigenvalues and a matrix of eigenvectors 27 | */ 28 | @JvmName("eig") 29 | public fun LinAlg.eig(mat: MultiArray): Pair, D2Array> = 30 | this.linAlgEx.eig(mat) 31 | 32 | /** 33 | * Calculates the eigenvalues and eigenvectors of a complex matrix 34 | * @return a pair of a vector of eigenvalues and a matrix of eigenvectors 35 | */ 36 | @JvmName("eigC") 37 | public fun LinAlg.eig(mat: MultiArray): Pair, D2Array> = 38 | this.linAlgEx.eigC(mat) 39 | 40 | /** 41 | * Calculates the eigenvalues of a float matrix 42 | * @return [ComplexFloat] vector 43 | */ 44 | @JvmName("eigValsF") 45 | public fun LinAlg.eigVals(mat: MultiArray): D1Array = this.linAlgEx.eigValsF(mat) 46 | 47 | /** 48 | * Calculates the eigenvalues of a numeric matrix. 49 | * @return [ComplexDouble] vector 50 | */ 51 | @JvmName("eigVals") 52 | public fun LinAlg.eigVals(mat: MultiArray): D1Array = this.linAlgEx.eigVals(mat) 53 | 54 | /** 55 | * Calculates the eigenvalues of a float matrix 56 | * @return complex vector 57 | */ 58 | @JvmName("eigValsC") 59 | public fun LinAlg.eigVals(mat: MultiArray): D1Array = this.linAlgEx.eigValsC(mat) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/inverse.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 9 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 10 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 11 | import kotlin.jvm.JvmName 12 | 13 | /** 14 | * Returns inverse float matrix 15 | */ 16 | @JvmName("invF") 17 | public fun LinAlg.inv(mat: MultiArray): NDArray = this.linAlgEx.invF(mat) 18 | 19 | /** 20 | * Returns inverse of a double matrix from numeric matrix 21 | */ 22 | @JvmName("invD") 23 | public fun LinAlg.inv(mat: MultiArray): NDArray = this.linAlgEx.inv(mat) 24 | 25 | /** 26 | * Returns inverse complex matrix 27 | */ 28 | @JvmName("invC") 29 | public fun LinAlg.inv(mat: MultiArray): NDArray = this.linAlgEx.invC(mat) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/norm.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.mk 8 | import org.jetbrains.kotlinx.multik.api.zeros 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D1 10 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 11 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 12 | import org.jetbrains.kotlinx.multik.ndarray.operations.stack 13 | import kotlin.jvm.JvmName 14 | 15 | /** 16 | * Returns norm of float vector 17 | */ 18 | @JvmName("normFV") 19 | public fun LinAlg.norm(mat: MultiArray, norm: Norm = Norm.Fro): Float = 20 | this.linAlgEx.normF(mk.stack(mat, mk.zeros(mat.size)), norm) 21 | 22 | /** 23 | * Returns norm of float matrix 24 | */ 25 | @JvmName("normF") 26 | public fun LinAlg.norm(mat: MultiArray, norm: Norm = Norm.Fro): Float = this.linAlgEx.normF(mat, norm) 27 | 28 | /** 29 | * Returns norm of double vector 30 | */ 31 | @JvmName("normDV") 32 | public fun LinAlg.norm(mat: MultiArray, norm: Norm = Norm.Fro): Double = 33 | this.linAlgEx.norm(mk.stack(mat, mk.zeros(mat.size)), norm) 34 | 35 | /** 36 | * Returns norm of double matrix 37 | */ 38 | @JvmName("normD") 39 | public fun LinAlg.norm(mat: MultiArray, norm: Norm = Norm.Fro): Double = this.linAlgEx.norm(mat, norm) 40 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/plu.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D2Array 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import kotlin.jvm.JvmName 12 | 13 | /** 14 | * Returns PLU decomposition of the float matrix 15 | */ 16 | @JvmName("pluF") 17 | public fun LinAlg.plu(mat: MultiArray): Triple, D2Array, D2Array> = this.linAlgEx.pluF(mat) 18 | 19 | /** 20 | * Returns PLU decomposition of the numeric matrix 21 | */ 22 | @JvmName("pluD") 23 | public fun LinAlg.plu(mat: MultiArray): Triple, D2Array, D2Array> = this.linAlgEx.plu(mat) 24 | 25 | /** 26 | * Returns PLU decomposition of the complex matrix 27 | */ 28 | @JvmName("pluC") 29 | public fun LinAlg.plu(mat: MultiArray): Triple, D2Array, D2Array> = this.linAlgEx.pluC(mat) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/qr.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D2Array 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import kotlin.jvm.JvmName 12 | 13 | /** 14 | * Returns QR decomposition of the float matrix 15 | */ 16 | @JvmName("qrF") 17 | public fun LinAlg.qr(mat: MultiArray): Pair, D2Array> = this.linAlgEx.qrF(mat) 18 | 19 | /** 20 | * Returns QR decomposition of the numeric matrix 21 | */ 22 | @JvmName("qrD") 23 | public fun LinAlg.qr(mat: MultiArray): Pair, D2Array> = this.linAlgEx.qr(mat) 24 | 25 | /** 26 | * Returns QR decomposition of the complex matrix 27 | */ 28 | @JvmName("qrC") 29 | public fun LinAlg.qr(mat: MultiArray): Pair, D2Array> = this.linAlgEx.qrC(mat) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/solve.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 8 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 9 | import org.jetbrains.kotlinx.multik.ndarray.data.Dim2 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Solves a linear matrix equation, or system of linear scalar equations. 16 | */ 17 | @JvmName("solveF") 18 | public fun LinAlg.solve(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.solveF(a, b) 19 | /** 20 | * Solves a linear matrix equation, or system of linear scalar equations. 21 | */ 22 | @JvmName("solveD") 23 | public fun LinAlg.solve(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.solve(a, b) 24 | /** 25 | * Solves a linear matrix equation, or system of linear scalar equations. 26 | */ 27 | @JvmName("solveC") 28 | public fun LinAlg.solve(a: MultiArray, b: MultiArray): NDArray = this.linAlgEx.solveC(a, b) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/linalg/svd.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.ExperimentalMultikApi 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.Complex 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D1Array 10 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2Array 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import kotlin.jvm.JvmName 14 | 15 | /** 16 | * Returns SVD decomposition of the float matrix 17 | */ 18 | @ExperimentalMultikApi 19 | @JvmName("svdF") 20 | public fun LinAlg.svd(mat: MultiArray): Triple, D1Array, D2Array> = this.linAlgEx.svdF(mat) 21 | 22 | /** 23 | * Returns SVD decomposition of the numeric matrix 24 | */ 25 | @ExperimentalMultikApi 26 | @JvmName("svdD") 27 | public fun LinAlg.svd(mat: MultiArray): Triple, D1Array, D2Array> = this.linAlgEx.svd(mat) 28 | 29 | /** 30 | * Returns SVD decomposition of the complex matrix 31 | */ 32 | @ExperimentalMultikApi 33 | @JvmName("svdC") 34 | public fun LinAlg.svd(mat: MultiArray): Triple, D1Array, D2Array> = this.linAlgEx.svdC(mat) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/math/cos.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.math 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 9 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Returns a ndarray of Double from the given ndarray to each element of which a cos function has been applied. 16 | */ 17 | @JvmName("cos") 18 | public fun Math.cos(a: MultiArray): NDArray = this.mathEx.cos(a) 19 | 20 | /** 21 | * Returns a ndarray of Float from the given ndarray to each element of which a cos function has been applied. 22 | */ 23 | @JvmName("cosFloat") 24 | public fun Math.cos(a: MultiArray): NDArray = this.mathEx.cosF(a) 25 | 26 | /** 27 | * Returns a ndarray of [ComplexFloat] from the given ndarray to each element of which a cos function has been applied. 28 | */ 29 | @JvmName("cosComplexFloat") 30 | public fun Math.cos(a: MultiArray): NDArray = this.mathEx.cosCF(a) 31 | 32 | /** 33 | * Returns a ndarray of [ComplexDouble] from the given ndarray to each element of which a cos function has been applied. 34 | */ 35 | @JvmName("cosComplexDouble") 36 | public fun Math.cos(a: MultiArray): NDArray = this.mathEx.cosCD(a) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/math/exp.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.math 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 9 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Returns a ndarray of Double from the given ndarray to each element of which an exp function has been applied. 16 | */ 17 | @JvmName("exp") 18 | public fun Math.exp(a: MultiArray): NDArray = this.mathEx.exp(a) 19 | 20 | /** 21 | * Returns a ndarray of Float from the given ndarray to each element of which an exp function has been applied. 22 | */ 23 | @JvmName("expFloat") 24 | public fun Math.exp(a: MultiArray): NDArray = this.mathEx.expF(a) 25 | 26 | /** 27 | * Returns a ndarray of [ComplexFloat] from the given ndarray to each element of which an exp function has been applied. 28 | */ 29 | @JvmName("expComplexFloat") 30 | public fun Math.exp(a: MultiArray): NDArray = this.mathEx.expCF(a) 31 | 32 | /** 33 | * Returns a ndarray of [ComplexDouble] from the given ndarray to each element of which an exp function has been applied. 34 | */ 35 | @JvmName("expComplexDouble") 36 | public fun Math.exp(a: MultiArray): NDArray = this.mathEx.expCD(a) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/math/log.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.math 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 9 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Returns a ndarray of Double from the given ndarray to each element of which a log function has been applied. 16 | */ 17 | @JvmName("log") 18 | public fun Math.log(a: MultiArray): NDArray = this.mathEx.log(a) 19 | 20 | /** 21 | * Returns a ndarray of Float from the given ndarray to each element of which a log function has been applied. 22 | */ 23 | @JvmName("logFloat") 24 | public fun Math.log(a: MultiArray): NDArray = this.mathEx.logF(a) 25 | 26 | /** 27 | * Returns a ndarray of [ComplexFloat] from the given ndarray to each element of which a log function has been applied. 28 | */ 29 | @JvmName("logComplexFloat") 30 | public fun Math.log(a: MultiArray): NDArray = this.mathEx.logCF(a) 31 | 32 | /** 33 | * Returns a ndarray of [ComplexDouble] from the given ndarray to each element of which a log function has been applied. 34 | */ 35 | @JvmName("logComplexDouble") 36 | public fun Math.log(a: MultiArray): NDArray = this.mathEx.logCD(a) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/api/math/sin.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api.math 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 9 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | import kotlin.jvm.JvmName 13 | 14 | /** 15 | * Returns an ndarray of Double from the given ndarray to each element of which a sin function has been applied. 16 | */ 17 | @JvmName("sin") 18 | public fun Math.sin(a: MultiArray): NDArray = this.mathEx.sin(a) 19 | 20 | /** 21 | * Returns an ndarray of Float from the given ndarray to each element of which a sin function has been applied. 22 | */ 23 | @JvmName("sinFloat") 24 | public fun Math.sin(a: MultiArray): NDArray = this.mathEx.sinF(a) 25 | 26 | /** 27 | * Returns an ndarray of [ComplexFloat] from the given ndarray to each element of which a sin function has been applied. 28 | */ 29 | @JvmName("sinComplexFloat") 30 | public fun Math.sin(a: MultiArray): NDArray = this.mathEx.sinCF(a) 31 | 32 | /** 33 | * Returns an ndarray of [ComplexDouble] from the given ndarray to each element of which a sin function has been applied. 34 | */ 35 | @JvmName("sinComplexDouble") 36 | public fun Math.sin(a: MultiArray): NDArray = this.mathEx.sinCD(a) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/Complex.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.complex 6 | 7 | /** 8 | * A sealed interface representing a superclass for complex numbers. 9 | */ 10 | public sealed interface Complex { 11 | public companion object { 12 | 13 | /** 14 | * Returns a [ComplexFloat] with the given real part. 15 | * 16 | * @param re the real part of the complex number 17 | * @return a [ComplexFloat] number with the given real part and 0f imaginary part 18 | */ 19 | public fun r(re: Float): ComplexFloat = ComplexFloat(re, 0f) 20 | 21 | /** 22 | * Returns a [ComplexDouble] with the given real part. 23 | * 24 | * @param re the real part of the complex number 25 | * @return a [ComplexDouble] number with the given real part and 0.0 imaginary part 26 | */ 27 | public fun r(re: Double): ComplexDouble = ComplexDouble(re, 0.0) 28 | 29 | /** 30 | * Returns the [ComplexFloat] number representation of the given imaginary part. 31 | * 32 | * @param im the imaginary part of the complex number 33 | * @return a [ComplexFloat] number with the 0f real part and given imaginary part 34 | */ 35 | public fun i(im: Float): ComplexFloat = ComplexFloat(0f, im) 36 | 37 | /** 38 | * Returns the [ComplexDouble] number representation of the given imaginary part. 39 | * 40 | * @param im the imaginary part of the complex number. 41 | * @return a [ComplexDouble] number with the 0.0 real part and given imaginary part 42 | */ 43 | public fun i(im: Double): ComplexDouble = ComplexDouble(0.0, im) 44 | 45 | /** 46 | * Converts a complex float to a long value. 47 | * 48 | * This method takes in a real and imaginary float value and returns a long equivalent. The real 49 | * value is converted to raw bits and left shifted by 32 bits. The imaginary value is also 50 | * converted to raw bits and ANDed with the hexadecimal value 0xFFFFFFFFL to get the last 32 bits 51 | * of the long. The two 32-bit values are then ORed to get the final long value. 52 | * 53 | * @param re the real value of the complex number as a float 54 | * @param im the imaginary value of the complex number as a float 55 | * 56 | * @return the long equivalent of the complex number given by the real and imaginary values 57 | */ 58 | internal fun convertComplexFloatToLong(re: Float, im: Float): Long = 59 | (re.toRawBits().toLong() shl 32) or (im.toRawBits().toLong() and 0xFFFFFFFFL) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/ComplexArrayIterators.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.complex 6 | 7 | private class ArrayComplexFloatIterator(private val array: ComplexFloatArray) : ComplexFloatIterator() { 8 | private var index = 0 9 | override fun hasNext() = index < array.size 10 | override fun nextComplexFloat() = try { array[index++] } catch (e: IndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) } 11 | } 12 | 13 | private class ArrayComplexDoubleIterator(private val array: ComplexDoubleArray) : ComplexDoubleIterator() { 14 | private var index = 0 15 | override fun hasNext() = index < array.size 16 | override fun nextComplexDouble() = try { array[index++] } catch (e: IndexOutOfBoundsException) { index -= 1; throw NoSuchElementException(e.message) } 17 | } 18 | 19 | public fun iterator(array: ComplexFloatArray): ComplexFloatIterator = ArrayComplexFloatIterator(array) 20 | public fun iterator(array: ComplexDoubleArray): ComplexDoubleIterator = ArrayComplexDoubleIterator(array) -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/Iterators.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.complex 6 | 7 | public abstract class ComplexFloatIterator : Iterator { 8 | final override fun next(): ComplexFloat = nextComplexFloat() 9 | 10 | public abstract fun nextComplexFloat(): ComplexFloat 11 | } 12 | 13 | public abstract class ComplexDoubleIterator : Iterator { 14 | final override fun next(): ComplexDouble = nextComplexDouble() 15 | 16 | public abstract fun nextComplexDouble(): ComplexDouble 17 | } -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/_NDArray.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.complex 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 4 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 5 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 6 | import org.jetbrains.kotlinx.multik.ndarray.operations.map 7 | import kotlin.jvm.JvmName 8 | 9 | /** 10 | * Transforms this [NDArray] of [ComplexFloat] to an [NDArray] of the real part of complex numbers. 11 | * Dimensions are preserved. 12 | * 13 | * @param D dimension. 14 | * @return [NDArray] of real portion of [ComplexFloat] 15 | */ 16 | @get:JvmName("reFloat") 17 | public val MultiArray.re: NDArray 18 | get() = this.map { it.re } 19 | 20 | /** 21 | * Transforms this [NDArray] of [ComplexDouble] to an [NDArray] of the real part of complex numbers. 22 | * Dimensions are preserved. 23 | * 24 | * @param D dimension. 25 | * @return [NDArray] of real portion of [ComplexDouble] 26 | */ 27 | @get:JvmName("reDouble") 28 | public val MultiArray.re: NDArray 29 | get() = this.map { it.re } 30 | 31 | /** 32 | * Transforms this [NDArray] of [ComplexFloat] to an [NDArray] of the imaginary part of complex numbers. 33 | * Dimensions are preserved. 34 | * 35 | * @param D dimension. 36 | * @return [NDArray] of imaginary portion of [ComplexFloat] 37 | */ 38 | @get:JvmName("imFloat") 39 | public val MultiArray.im: NDArray 40 | get() = this.map { it.im } 41 | 42 | /** 43 | * Transforms this [NDArray] of [ComplexDouble] to an [NDArray] of the imaginary part of complex numbers. 44 | * Dimensions are preserved. 45 | * 46 | * @param D dimension. 47 | * @return [NDArray] of imaginary portion of [ComplexDouble] 48 | */ 49 | @get:JvmName("imDouble") 50 | public val MultiArray.im: NDArray 51 | get() = this.map { it.im } 52 | 53 | /** 54 | * Transforms this [MultiArray] of [ComplexDouble] to an [NDArray] of the conjugated value. 55 | * Dimensions are preserved. 56 | * 57 | * @param D dimension. 58 | * @return [NDArray] of conjugated [ComplexDouble] 59 | */ 60 | @JvmName("conjDouble") 61 | public fun MultiArray.conj(): MultiArray = this.map { it.conjugate() } 62 | 63 | /** 64 | * Transforms this [MultiArray] of [ComplexFloat] to an [NDArray] of the conjugated value. 65 | * Dimensions are preserved. 66 | * 67 | * @param D dimension. 68 | * @return [NDArray] of conjugated [ComplexFloat] 69 | */ 70 | @JvmName("conjFloat") 71 | public fun MultiArray.conj(): MultiArray = this.map { it.conjugate() } 72 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/DataType.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.data 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 9 | import org.jetbrains.kotlinx.multik.ndarray.data.DataType.* 10 | import kotlin.reflect.KClass 11 | 12 | @PublishedApi 13 | internal expect inline fun dataTypeOf(type: KClass): DataType 14 | 15 | /** 16 | * Describes the type of elements stored in a [NDArray]. 17 | * 18 | * @param nativeCode an integer value of the type. Required to define the type in JNI. 19 | * @param itemSize size of one ndarray element in bytes. 20 | * @param clazz [KClass] type. 21 | * 22 | * @property ByteDataType byte. 23 | * @property ShortDataType short. 24 | * @property IntDataType int. 25 | * @property LongDataType long. 26 | * @property FloatDataType float. 27 | * @property DoubleDataType double. 28 | * @property ComplexFloatDataType complex float. 29 | * @property ComplexDoubleDataType complex double. 30 | */ 31 | public enum class DataType(public val nativeCode: Int, public val itemSize: Int, public val clazz: KClass) { 32 | ByteDataType(1, 1, Byte::class), 33 | ShortDataType(2, 2, Short::class), 34 | IntDataType(3, 4, Int::class), 35 | LongDataType(4, 8, Long::class), 36 | FloatDataType(5, 4, Float::class), 37 | DoubleDataType(6, 8, Double::class), 38 | ComplexFloatDataType(7, 8, ComplexFloat::class), 39 | ComplexDoubleDataType(8, 16, ComplexDouble::class); 40 | 41 | public fun isNumber(): Boolean = when (nativeCode) { 42 | 1, 2, 3, 4, 5, 6 -> true 43 | else -> false 44 | } 45 | 46 | public fun isComplex(): Boolean = !isNumber() 47 | 48 | public companion object { 49 | 50 | /** 51 | * Returns [DataType] by [nativeCode]. 52 | */ 53 | public fun of(i: Int): DataType { 54 | return when (i) { 55 | 1 -> ByteDataType 56 | 2 -> ShortDataType 57 | 3 -> IntDataType 58 | 4 -> LongDataType 59 | 5 -> FloatDataType 60 | 6 -> DoubleDataType 61 | 7 -> ComplexFloatDataType 62 | 8 -> ComplexDoubleDataType 63 | else -> throw IllegalStateException("One of the primitive types indexes was expected, got $i") 64 | } 65 | } 66 | 67 | /** 68 | * Returns [DataType] by class of [element]. 69 | */ 70 | public inline fun of(element: T): DataType { 71 | element ?: throw IllegalStateException("Element is null cannot find type") 72 | return dataTypeOf(element!!::class) 73 | } 74 | 75 | 76 | /** 77 | * Returns [DataType] by [KClass] of [type]. [T] is `reified` type. 78 | */ 79 | public inline fun ofKClass(type: KClass): DataType = dataTypeOf(type) 80 | } 81 | 82 | override fun toString(): String { 83 | return "DataType(nativeCode=$nativeCode, itemSize=$itemSize, class=$clazz)" 84 | } 85 | } -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/MutableMultiArrays.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.data 6 | 7 | /** 8 | * A generic ndarray. Methods in this interface support write access to the ndarray. 9 | */ 10 | public interface MutableMultiArray : MultiArray { 11 | public override val data: MemoryView 12 | 13 | override fun copy(): MutableMultiArray 14 | 15 | override fun deepCopy(): MutableMultiArray 16 | 17 | // Reshape 18 | 19 | override fun reshape(dim1: Int): MutableMultiArray 20 | 21 | override fun reshape(dim1: Int, dim2: Int): MutableMultiArray 22 | 23 | override fun reshape(dim1: Int, dim2: Int, dim3: Int): MutableMultiArray 24 | 25 | override fun reshape(dim1: Int, dim2: Int, dim3: Int, dim4: Int): MutableMultiArray 26 | 27 | override fun reshape(dim1: Int, dim2: Int, dim3: Int, dim4: Int, vararg dims: Int): MutableMultiArray 28 | 29 | override fun transpose(vararg axes: Int): MutableMultiArray 30 | 31 | override fun squeeze(vararg axes: Int): MutableMultiArray 32 | 33 | override fun unsqueeze(vararg axes: Int): MutableMultiArray 34 | } 35 | -------------------------------------------------------------------------------- /multik-core/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/NDArrayIterator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.data 6 | 7 | /** 8 | * Iterator over multidimensional arrays. Iterated taking into account the [offset], [strides] and [shape]. 9 | */ 10 | public class NDArrayIterator( 11 | private val data: MemoryView, 12 | private val offset: Int = 0, 13 | private val strides: IntArray, 14 | private val shape: IntArray 15 | ) : Iterator { 16 | private val index = IntArray(shape.size) 17 | 18 | override fun hasNext(): Boolean { 19 | for (i in shape.indices) { 20 | if (index[i] >= shape[i]) 21 | return false 22 | } 23 | return true 24 | } 25 | 26 | override fun next(): T { 27 | var p = offset 28 | for (i in shape.indices) { 29 | p += strides[i] * index[i] 30 | } 31 | 32 | for (i in shape.size - 1 downTo 0) { 33 | val t = index[i] + 1 34 | if (t >= shape[i] && i != 0) { 35 | index[i] = 0 36 | } else { 37 | index[i] = t 38 | break 39 | } 40 | } 41 | 42 | return data[p] 43 | } 44 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/CreateComplexTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.complex 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | 6 | class CreateComplexTest { 7 | 8 | @Test 9 | fun test_easy_complex_creation() { 10 | assertEquals(Complex.i(1.12f), 1.12f.i) 11 | assertEquals(Complex.i(3.33), 3.33.i) 12 | assertEquals(ComplexFloat(1, 1), 1 + 1f.i) 13 | assertEquals(ComplexDouble(3, 7), 3 + 7.0.i) 14 | } 15 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/ConcatenateTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.zeros 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 6 | import org.jetbrains.kotlinx.multik.ndarray.operations.asSequence 7 | import org.jetbrains.kotlinx.multik.ndarray.operations.plus 8 | import kotlin.test.Test 9 | import kotlin.test.assertContentEquals 10 | import kotlin.test.assertFalse 11 | 12 | class ConcatenateTest { 13 | @Test 14 | fun `concatenate_should_concatenate_for_simple_array`() { 15 | 16 | val arr1 = mk.zeros(1) + 1.0 17 | val arr2 = mk.zeros(2) + 2.0 18 | val arr3 = mk.zeros(3) + 3.0 19 | val arr4 = mk.zeros(4) + 4.0 20 | 21 | val result = arr1.cat(listOf(arr2, arr3, arr4), 0) 22 | 23 | assertContentEquals( 24 | doubleArrayOf( 25 | 1.0, 26 | 2.0, 2.0, 27 | 3.0, 3.0, 3.0, 28 | 4.0, 4.0, 4.0, 4.0 29 | ), result.data.getDoubleArray() 30 | ) 31 | 32 | } 33 | 34 | @Test 35 | fun `concatenate_should_concatenate_for_non_consistent_array`() { 36 | 37 | val arr1 = mk.zeros(1) + 1.0 38 | val arr2 = mk.zeros(2) + 2.0 39 | val arr3 = mk.zeros(3) + 3.0 40 | val arr4 = mk.zeros(10) + 4.0 41 | val arr5 = arr4[Slice(2, 5, 1)] 42 | 43 | assertFalse(arr5.consistent) 44 | val result = arr1.cat(listOf(arr2, arr3, arr5), 0) 45 | 46 | assertContentEquals( 47 | doubleArrayOf( 48 | 1.0, 49 | 2.0, 2.0, 50 | 3.0, 3.0, 3.0, 51 | 4.0, 4.0, 4.0, 4.0 52 | ), result.data.getDoubleArray() 53 | ) 54 | 55 | } 56 | 57 | @Test 58 | fun `concatenate_should_concatenate_for_complex`() { 59 | 60 | val arr1 = mk.zeros(1) + ComplexDouble(1.0, 0.0) 61 | val arr2 = mk.zeros(2) + ComplexDouble(2.0, 0.0) 62 | val arr3 = mk.zeros(3) + ComplexDouble(3.0, 0.0) 63 | val arr4 = mk.zeros(4) + ComplexDouble(4.0, 0.0) 64 | 65 | 66 | val result = arr1.cat(listOf(arr2, arr3, arr4), 0) 67 | 68 | val realResult = result.asSequence().map { it.re }.toList().toDoubleArray() 69 | 70 | assertContentEquals( 71 | doubleArrayOf( 72 | 1.0, 73 | 2.0, 2.0, 74 | 3.0, 3.0, 3.0, 75 | 4.0, 4.0, 4.0, 4.0 76 | ), 77 | realResult 78 | ) 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/InternalsTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.zeros 5 | import kotlin.test.Test 6 | import kotlin.test.assertTrue 7 | import kotlin.test.fail 8 | 9 | class InternalsTest { 10 | 11 | @Test 12 | fun `require_equal_shape_throws_exception_for_unequal_shape`() { 13 | val left = mk.zeros(0, 1, 2, 3) 14 | val right = mk.zeros(0, 1, 2, 4) 15 | expectUnEqualShape(left, right) 16 | } 17 | 18 | @Test 19 | fun `require_equal_shape_throws_exception_for_different_no_of_dim`() { 20 | val left = mk.zeros(0, 1, 2, 3) 21 | val right = mk.zeros(0, 1, 2) 22 | expectUnEqualShape(left, right) 23 | } 24 | 25 | @Test 26 | fun `require_equal_shape_succeeds_for_arrays_with_equal_shapes`() { 27 | val left = mk.zeros(0, 1, 2, 3) 28 | val right = mk.zeros(0, 1, 2, 3) 29 | requireEqualShape(left.shape, right.shape) 30 | } 31 | 32 | @Test 33 | fun `require_equal_shape_succeeds_empty_arrays`() { 34 | val left = mk.zeros(0) 35 | val right = mk.zeros(0) 36 | assertTrue(left.isEmpty()) 37 | assertTrue(right.isEmpty()) 38 | requireEqualShape(left.shape, right.shape) 39 | } 40 | 41 | private fun expectUnEqualShape(left: NDArray, right: NDArray) { 42 | try { 43 | requireEqualShape(left.shape, right.shape) 44 | fail("Exception expected") 45 | } catch (e: IllegalArgumentException) { } 46 | } 47 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/SliceTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.data 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import kotlin.test.Test 9 | import kotlin.test.assertEquals 10 | import kotlin.test.assertSame 11 | import kotlin.test.assertTrue 12 | 13 | class SliceTest { 14 | 15 | @Test 16 | fun testSlice1D() { 17 | val a = mk.ndarrayOf(1, 2, 3, 4) 18 | 19 | 20 | assertEquals(mk.ndarrayOf(2, 3), a[1 until 3]) 21 | assertTrue(a[3 until 1].isEmpty()) 22 | assertEquals(mk.ndarrayOf(1, 3), a[sl.bounds..2]) 23 | } 24 | 25 | @Test 26 | fun testSlice2D() { 27 | val a = mk.d2array(3, 3) { it } 28 | 29 | assertEquals(mk.ndarray(mk[mk[3, 4, 5], mk[6, 7, 8]]), a[1 until 3]) 30 | assertEquals(mk.ndarray(mk[mk[2], mk[5]]), a[0 until 2, 2 until 3]) 31 | assertEquals(mk.ndarrayOf(3), a[1, 0 until 1]) 32 | assertEquals(mk.ndarrayOf(5), a[1 until 2, 2]) 33 | } 34 | 35 | @Test 36 | fun testSlice3D() { 37 | val a = mk.d3array(3, 3, 3) { it } 38 | 39 | assertEquals(mk.d3array(2, 3, 3) { it + 9 }, a[1 until 3]) 40 | assertEquals( 41 | mk.ndarray(mk[mk[mk[3, 4, 5], mk[6, 7, 8]], mk[mk[12, 13, 14], mk[15, 16, 17]]]), 42 | a[0 until 2, 1 until 3, sl.bounds] 43 | ) 44 | assertEquals(mk.ndarrayOf(15, 16), a[1, 2, 0 until 2]) 45 | assertEquals(mk.ndarrayOf(11, 14), a[1, 0 until 2, 2]) 46 | assertEquals(mk.ndarrayOf(5, 14), a[0 until 2, 1, 2]) 47 | assertEquals(mk.ndarray(mk[mk[4, 7], mk[13, 16]]), a[0 until 2, 1 until 3, 1]) 48 | assertEquals(mk.ndarray(mk[mk[4, 5], mk[13, 14]]), a[0 until 2, 1, 1 until 3]) 49 | assertEquals(mk.ndarray(mk[mk[10, 11], mk[13, 14]]), a[1, 0 until 2, 1 until 3]) 50 | } 51 | 52 | @Test 53 | fun testSlice4D() { 54 | val a = mk.d4array(2, 2, 2, 2) { it } 55 | assertEquals(a, a[sl.bounds]) 56 | assertEquals(mk.ndarray(mk[mk[mk[mk[5]]]]), a[0 until 1, 1 until 2, 0 until 1, 1 until 2]) 57 | assertEquals(mk.ndarray(mk[mk[mk[5]]]), a[0 until 1, 1, 0 until 1, (1 until 2)..1]) 58 | assertEquals(mk.ndarray(mk[mk[7]]), a[0 until 1, 1, 1, 1 until 2]) 59 | assertEquals(mk.ndarrayOf(6), a[0 until 1, 1, 1, 0]) 60 | } 61 | 62 | @Test 63 | fun testBase() { 64 | val a = mk.ndarrayOf(0, 1, 2, 3, 4, 5) 65 | val b = a[1 until 5] 66 | val c = a[1 until 3] 67 | assertSame(null, a.base) 68 | assertSame(a, b.base) 69 | assertSame(a, c.base) 70 | 71 | val a2 = a.reshape(3, 2) 72 | val b2 = b.reshape(4, 1) 73 | assertSame(a, a2.base) 74 | assertSame(null, b2.base) 75 | 76 | val d1 = b2.squeeze() 77 | val d2 = d1.unsqueeze() 78 | assertSame(b2, d1.base) 79 | assertSame(b2, d2.base) 80 | 81 | val e = b2.transpose() 82 | assertSame(b2, e.base) 83 | 84 | val f = a2[1] 85 | assertSame(a, f.base) 86 | 87 | val x = b2.deepCopy() 88 | val y = b2.copy() 89 | assertSame(null, x.base) 90 | assertSame(null, y.base) 91 | 92 | val z = a2.asDNArray() 93 | assertSame(a, z.base) 94 | } 95 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/ndarray/operation/TransformationTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.operation 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.operations.clip 6 | import org.jetbrains.kotlinx.multik.ndarray.operations.toList 7 | import kotlin.random.Random 8 | import kotlin.test.Test 9 | import kotlin.test.assertContentEquals 10 | import kotlin.test.assertEquals 11 | 12 | class TransformationTest { 13 | 14 | @Test 15 | fun clip1DIntDataType() { 16 | val a = mk.ndarray(mk[1, 2, 3, 4, 5]) 17 | val result = a.clip(2, 4) 18 | assertContentEquals(listOf(2, 2, 3, 4, 4), result.toList()) 19 | } 20 | 21 | @Test 22 | fun clip1DShortDataType() { 23 | val a = mk.ndarray(mk[1, 2, 3, 4, 5]) 24 | val result = a.clip(2, 4) 25 | assertContentEquals(listOf(2, 2, 3, 4, 4), result.toList()) 26 | } 27 | 28 | @Test 29 | fun clip1DLongDataType() { 30 | val a = mk.ndarray(mk[1, 2, 3, 4, 5]) 31 | val result = a.clip(2, 4) 32 | assertContentEquals(listOf(2, 2, 3, 4, 4), result.toList()) 33 | } 34 | 35 | @Test 36 | fun clip2dFloatDataType() { 37 | val absoluteTolerance = 0.01f 38 | val a = mk.ndarray(mk[ mk[1f, 2f, 3f, 4f, 5f], mk[6f, 7f, 8f, 9f, 10f]]) 39 | val min = 3.5f 40 | val max = 7.1f 41 | val expected = listOf(3.5f, 3.5f, 3.5f, 4f, 5f, 6f, 7f, 7.1f, 7.1f, 7.1f) 42 | val result = a.clip(min, max).toList() 43 | for (i in expected.indices){ // run assert with absolute tolerance because of floating number is not stable when we test for js 44 | assertEquals(expected[i], result[i], absoluteTolerance = absoluteTolerance) 45 | } 46 | } 47 | 48 | @Test 49 | fun `clip_3d_byte_data_type_and_min_is_max`() { 50 | val inputArray = ByteArray(60) { it.toByte() } 51 | val a = mk.ndarray(inputArray, 2, 5, 6) 52 | val min = 1.toByte() 53 | val expected = ByteArray(60) { min } 54 | assertContentEquals(expected.toList(), a.clip(min, min).toList()) 55 | } 56 | 57 | @Test 58 | fun clip4dDoubleDataType(){ 59 | val inputArray = DoubleArray(60) { Random.nextDouble() } 60 | val min = 7.0 61 | val max = 42.0 62 | val expected = inputArray.copyOf().map { if (it < min) min else if (it > max) max else it } 63 | val a = mk.ndarray(inputArray, 2, 5, 3, 2) 64 | assertContentEquals(expected.toList(), a.clip(min,max).toList()) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/utils.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDoubleArray 4 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloatArray 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.joinToString 6 | import kotlin.test.assertTrue 7 | 8 | infix fun ByteArray.shouldBe(expected: ByteArray) { 9 | assertTrue( 10 | "Expected <${ 11 | expected.joinToString(prefix = "[", postfix = "]") 12 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 13 | ) { this.contentEquals(expected) } 14 | } 15 | 16 | infix fun ShortArray.shouldBe(expected: ShortArray) { 17 | assertTrue( 18 | "Expected <${ 19 | expected.joinToString(prefix = "[", postfix = "]") 20 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 21 | ) { this.contentEquals(expected) } 22 | } 23 | 24 | infix fun IntArray.shouldBe(expected: IntArray) { 25 | assertTrue( 26 | "Expected <${ 27 | expected.joinToString(prefix = "[", postfix = "]") 28 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 29 | ) { this.contentEquals(expected) } 30 | } 31 | 32 | infix fun LongArray.shouldBe(expected: LongArray) { 33 | assertTrue( 34 | "Expected <${ 35 | expected.joinToString(prefix = "[", postfix = "]") 36 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 37 | ) { this.contentEquals(expected) } 38 | } 39 | 40 | infix fun FloatArray.shouldBe(expected: FloatArray) { 41 | assertTrue( 42 | "Expected <${ 43 | expected.joinToString(prefix = "[", postfix = "]") 44 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 45 | ) { this.contentEquals(expected) } 46 | } 47 | 48 | infix fun DoubleArray.shouldBe(expected: DoubleArray) { 49 | assertTrue( 50 | "Expected <${ 51 | expected.joinToString(prefix = "[", postfix = "]") 52 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 53 | ) { this.contentEquals(expected) } 54 | } 55 | 56 | infix fun ComplexFloatArray.shouldBe(expected: ComplexFloatArray) { 57 | assertTrue( 58 | "Expected <${ 59 | expected.joinToString(prefix = "[", postfix = "]") 60 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 61 | ) { this == expected } 62 | } 63 | 64 | infix fun ComplexDoubleArray.shouldBe(expected: ComplexDoubleArray) { 65 | assertTrue( 66 | "Expected <${ 67 | expected.joinToString(prefix = "[", postfix = "]") 68 | }>, actual <${this.joinToString(prefix = "[", postfix = "]")}>." 69 | ) { this == expected } 70 | } 71 | -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/samples/docs/arithmetic.kt: -------------------------------------------------------------------------------- 1 | package samples.docs 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.operations.* 6 | import kotlin.test.Test 7 | import kotlin.test.assertEquals 8 | 9 | class ArithmeticOperations { 10 | @Test 11 | fun arith_with_scalars() { 12 | // SampleStart 13 | val a = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 14 | println(3.3 + a) 15 | /* 16 | [[4.8, 5.4, 6.3], 17 | [7.3, 8.3, 9.3]] 18 | */ 19 | 20 | println(a * 2.0) 21 | /* 22 | [[3.0, 4.2, 6.0], 23 | [8.0, 10.0, 12.0]] 24 | */ 25 | // SampleEnd 26 | assertEquals(listOf(4.8, 5.4, 6.3, 7.3, 8.3, 9.3), (3.3 + a).toList()) 27 | assertEquals(listOf(3.0, 4.2, 6.0, 8.0, 10.0, 12.0), (a * 2.0).toList()) 28 | } 29 | 30 | @Test 31 | fun div_with_ndarrays() { 32 | // SampleStart 33 | val a = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 34 | val b = mk.ndarray(mk[mk[1.0, 1.3, 3.0], mk[4.0, 9.5, 5.0]]) 35 | a / b // division 36 | /* 37 | [[1.5, 1.6153846153846154, 1.0], 38 | [1.0, 0.5263157894736842, 1.2]] 39 | */ 40 | // SampleEnd 41 | 42 | val actual = a / b 43 | assertEquals(true, intArrayOf(2, 3).contentEquals(actual.shape)) 44 | assertEquals(listOf(1.5, 1.6153846153846154, 1.0, 1.0, 0.5263157894736842, 1.2), actual.toList()) 45 | } 46 | 47 | @Test 48 | fun mul_with_ndarrays() { 49 | // SampleStart 50 | val a = mk.ndarray(mk[mk[0.5, 0.8, 0.0], mk[0.0, -4.5, 1.0]]) 51 | val b = mk.ndarray(mk[mk[1.0, 1.3, 3.0], mk[4.0, 9.5, 5.0]]) 52 | a * b // multiplication 53 | /* 54 | [[0.5, 1.04, 0.0], 55 | [0.0, -42.75, 5.0]] 56 | */ 57 | // SampleEnd 58 | 59 | val actual = a * b 60 | assertEquals(true, intArrayOf(2, 3).contentEquals(actual.shape)) 61 | assertEquals(listOf(0.5, 1.04, 0.0, 0.0, -42.75, 5.0), actual.toList()) 62 | } 63 | 64 | @Test 65 | fun inplace_arith_ops() { 66 | // SampleStart 67 | val a = mk.ndarray(mk[mk[1, 2], mk[3, 4]]) 68 | val b = mk.ndarray(mk[mk[4, 0], mk[7, 5]]) 69 | 70 | a += b 71 | println(a) 72 | /* 73 | [[5, 2], 74 | [10, 9]] 75 | */ 76 | 77 | a *= 3 78 | println(a) 79 | /* 80 | [[15, 6], 81 | [30, 27]] 82 | */ 83 | // SampleEnd 84 | 85 | assertEquals(listOf(15, 6, 30, 27), a.toList()) 86 | } 87 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/samples/docs/indexingAndIterating.kt: -------------------------------------------------------------------------------- 1 | package samples.docs 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.data.get 6 | import org.jetbrains.kotlinx.multik.ndarray.data.rangeTo 7 | import kotlin.test.Test 8 | import kotlin.test.assertEquals 9 | 10 | class IndexingAndIterating { 11 | @Test 12 | fun simple_indexing() { 13 | // SampleStart 14 | val a = mk.ndarray(mk[1, 2, 3]) 15 | a[2] // select the element at index 2 16 | 17 | val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 18 | b[1, 2] // select the element at row 1 column 2 19 | // SampleEnd 20 | assertEquals(3, a[2]) 21 | assertEquals(6.0, b[1, 2]) 22 | } 23 | 24 | @Test 25 | fun slice_1() { 26 | // SampleStart 27 | val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 28 | // select elements at rows 0 and 1 in column 1 29 | b[0..<2, 1] // [2.1, 5.0] 30 | // SampleEnd 31 | assertEquals(mk.ndarray(mk[2.1, 5.0]), b[0..<2, 1]) 32 | } 33 | 34 | @Test 35 | fun slice_2() { 36 | // SampleStart 37 | val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 38 | // select row 1 39 | b[1] // [4.0, 5.0, 6.0] 40 | b[1, 0..2..1] // [4.0, 5.0, 6.0] 41 | // SampleEnd 42 | 43 | assertEquals(b[1], b[1, 0..2..1]) 44 | } 45 | 46 | @Test 47 | fun iterating() { 48 | // SampleStart 49 | val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 50 | for (el in b) { 51 | print("$el, ") // 1.5, 2.1, 3.0, 4.0, 5.0, 6.0, 52 | } 53 | // SampleEnd 54 | } 55 | 56 | @Test 57 | fun iterating_multiIndices() { 58 | // SampleStart 59 | val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) 60 | for (index in b.multiIndices) { 61 | print("${b[index]}, ") // 1.5, 2.1, 3.0, 4.0, 5.0, 6.0, 62 | } 63 | // SampleEnd 64 | } 65 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/samples/docs/overview.kt: -------------------------------------------------------------------------------- 1 | package samples.docs 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.operations.times 6 | import kotlin.test.Test 7 | 8 | class Overview { 9 | @Test 10 | fun create_kotlin_matrix() { 11 | // SampleStart 12 | val a = listOf(listOf(1, 2, 3), listOf(4, 5, 6)) 13 | val b = listOf(listOf(7, 8, 9), listOf(10, 11, 12)) 14 | val c = MutableList(2) { MutableList(3) { 0 } } 15 | for (i in a.indices) { 16 | for (j in a.first().indices) { 17 | c[i][j] = a[i][j] * b[i][j] 18 | } 19 | } 20 | println(c) //[[7, 16, 27], [40, 55, 72]] 21 | // SampleEnd 22 | } 23 | 24 | @Test 25 | fun create_multik_matrix() { 26 | // SampleStart 27 | val a = mk.ndarray(mk[mk[1, 2, 3], mk[4, 5, 6]]) 28 | val b = mk.ndarray(mk[mk[7, 8, 9], mk[10, 11, 12]]) 29 | val c = a * b 30 | println(c) 31 | /* 32 | [[7, 16, 27], 33 | [40, 55, 72]] 34 | */ 35 | // SampleEnd 36 | } 37 | } -------------------------------------------------------------------------------- /multik-core/src/commonTest/kotlin/samples/docs/stdOperations.kt: -------------------------------------------------------------------------------- 1 | package samples.docs 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.operations.filter 6 | import org.jetbrains.kotlinx.multik.ndarray.operations.map 7 | import org.jetbrains.kotlinx.multik.ndarray.operations.reduce 8 | import kotlin.test.Test 9 | import kotlin.test.assertEquals 10 | 11 | class StandardOperations { 12 | @Test 13 | fun small_example_collection_operations() { 14 | // SampleStart 15 | val a = mk.ndarray(mk[1, 2, 3, 4, 5]) 16 | val b = a.filter { it > 2 } 17 | println(b) // [3, 4, 5] 18 | val c = a.map { it * 2 } 19 | println(c) // [2, 4, 6, 8, 10] 20 | val d = a.reduce { acc, value -> acc + value } 21 | println(d) // 15 22 | // SampleEnd 23 | assertEquals(mk.ndarray(mk[3, 4, 5]), b) 24 | assertEquals(mk.ndarray(mk[2, 4, 6, 8, 10]), c) 25 | assertEquals(15, d) 26 | } 27 | } -------------------------------------------------------------------------------- /multik-core/src/jsMain/kotlin/org/jetbrains/kotlinx/multik/api/Engine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api 6 | 7 | /** 8 | * Engine Provider for JS. 9 | */ 10 | public actual fun enginesProvider(): Map = enginesStore 11 | 12 | /** 13 | * Saves and initialize engine. 14 | */ 15 | public val enginesStore: MutableMap = mutableMapOf() 16 | -------------------------------------------------------------------------------- /multik-core/src/jsMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/ComplexDouble.js.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.complex 2 | 3 | /** 4 | * Creates a [ComplexDouble] with the given real and imaginary values in floating-point format. 5 | * 6 | * @param re the real value of the complex number in double format. 7 | * @param im the imaginary value of the complex number in double format. 8 | */ 9 | public actual fun ComplexDouble(re: Double, im: Double): ComplexDouble = JsComplexDouble(re, im) 10 | 11 | /** 12 | * Creates a [ComplexDouble] with the given real and imaginary values in number format. 13 | * 14 | * @param re the real value of the complex number in number format. 15 | * @param im the imaginary value of the complex number in number format. 16 | */ 17 | public actual fun ComplexDouble(re: Number, im: Number): ComplexDouble = ComplexDouble(re.toDouble(), im.toDouble()) 18 | 19 | /** 20 | * Represents a complex number with double precision. 21 | * 22 | * @property re The real part of the complex number. 23 | * @property im The imaginary part of the complex number. 24 | */ 25 | public class JsComplexDouble internal constructor( 26 | public override val re: Double, public override val im: Double 27 | ) : ComplexDouble { 28 | 29 | override fun eq(other: Any): Boolean = equals(other) 30 | 31 | override fun hash(): Int = hashCode() 32 | 33 | override fun equals(other: Any?): Boolean = when { 34 | this === other -> true 35 | other is ComplexDouble -> re == other.re && im == other.im 36 | else -> false 37 | } 38 | 39 | override fun hashCode(): Int = 31 * re.toBits().hashCode() + im.toBits().hashCode() 40 | 41 | override fun toString(): String = "$re+($im)i" 42 | } 43 | -------------------------------------------------------------------------------- /multik-core/src/jsMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/DataType.js.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 4 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.JsComplexDouble 6 | import org.jetbrains.kotlinx.multik.ndarray.data.DataType.* 7 | import kotlin.reflect.KClass 8 | 9 | @PublishedApi 10 | @Suppress("NOTHING_TO_INLINE") 11 | internal actual inline fun dataTypeOf(type: KClass): DataType = 12 | when (type) { 13 | Byte::class -> ByteDataType 14 | Short::class -> ShortDataType 15 | Int::class -> IntDataType 16 | Long::class -> LongDataType 17 | Float::class -> FloatDataType 18 | Double::class -> DoubleDataType 19 | ComplexFloat::class -> ComplexFloatDataType 20 | ComplexDouble::class, JsComplexDouble::class -> ComplexDoubleDataType 21 | else -> throw IllegalStateException("One of the primitive types was expected, got ${type.simpleName}") 22 | } -------------------------------------------------------------------------------- /multik-core/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/api/Engine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.api 2 | 3 | import java.util.* 4 | import java.util.concurrent.ConcurrentHashMap 5 | 6 | /** 7 | * Engine Provider for JVM. 8 | */ 9 | public actual fun enginesProvider(): Map { 10 | val engineList = ServiceLoader.load(Engine::class.java).toList() 11 | 12 | if (engineList.isEmpty()) { 13 | error( 14 | """Fail to find engine. Consider to add one of the following dependencies: 15 | - multik-default 16 | - multik-kotlin 17 | - multik-openblas""" 18 | ) 19 | } 20 | 21 | return ConcurrentHashMap(engineList.associateBy { it.type }) 22 | } 23 | -------------------------------------------------------------------------------- /multik-core/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/api/io/io.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.api.io 2 | 3 | import org.jetbrains.kotlinx.multik.api.Multik 4 | import org.jetbrains.kotlinx.multik.ndarray.data.* 5 | import java.io.File 6 | import java.nio.file.Path 7 | import kotlin.io.path.Path 8 | import kotlin.io.path.extension 9 | import kotlin.io.path.notExists 10 | 11 | @PublishedApi 12 | internal enum class FileFormats(val extension: String) { 13 | NPY("npy"), 14 | NPZ("npz"), 15 | CSV("csv"), 16 | } 17 | 18 | public inline fun Multik.read(fileName: String): NDArray = 19 | this.read(Path(fileName)) 20 | 21 | public inline fun Multik.read(file: File): NDArray = this.read(file.path) 22 | 23 | public inline fun Multik.read(path: Path): NDArray = 24 | this.read(path, DataType.ofKClass(T::class), dimensionClassOf()) 25 | 26 | public fun Multik.read(path: Path, dtype: DataType, dim: D): NDArray { 27 | if (path.notExists()) throw NoSuchFileException(path.toFile()) 28 | return when (path.extension) { 29 | FileFormats.NPY.extension -> { 30 | if (dtype.isComplex()) throw Exception("NPY format only supports Number types") 31 | this.readNPY(path, dtype, dim) 32 | } 33 | 34 | FileFormats.CSV.extension -> { 35 | if (dim.d > 2) throw Exception("CSV format only supports 1 and 2 dimensions") 36 | this.readRaw(path.toFile(), dtype, dim as Dim2) as NDArray 37 | } 38 | 39 | else -> throw Exception("Format ${path.extension} does not support reading ndarrays. If it is `npz` format, try `mk.readNPZ`") 40 | } 41 | } 42 | 43 | public fun Multik.write(fileName: String, ndarray: NDArray<*, *>): Unit = 44 | this.write(Path(fileName), ndarray) 45 | 46 | public fun Multik.write(file: File, ndarray: NDArray<*, *>): Unit = 47 | this.write(file.toPath(), ndarray) 48 | 49 | public fun Multik.write(path: Path, ndarray: NDArray<*, *>): Unit = 50 | when (path.extension) { 51 | FileFormats.NPY.extension -> { 52 | require(ndarray.dtype != DataType.ComplexFloatDataType || ndarray.dtype != DataType.ComplexFloatDataType) { 53 | "NPY format does not support complex numbers." 54 | } 55 | this.writeNPY(path, ndarray as NDArray) 56 | } 57 | 58 | FileFormats.CSV.extension -> { 59 | require(ndarray.dim.d < 2) { "Expected array of dimension less than 2, but got array of dimension ${ndarray.dim.d}." } 60 | this.writeCSV(path.toFile(), ndarray as NDArray<*, out Dim2>) 61 | } 62 | 63 | else -> throw Exception("Unknown format `${path.extension}`. Please use one of the supported formats: `npy`, `csv`.") 64 | } 65 | -------------------------------------------------------------------------------- /multik-core/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/DataType.jvm.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 4 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble32 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble64 6 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 7 | import org.jetbrains.kotlinx.multik.ndarray.data.DataType.* 8 | import kotlin.reflect.KClass 9 | 10 | @PublishedApi 11 | @Suppress("NOTHING_TO_INLINE") 12 | internal actual inline fun dataTypeOf(type: KClass): DataType = 13 | when (type) { 14 | Byte::class -> ByteDataType 15 | Short::class -> ShortDataType 16 | Int::class -> IntDataType 17 | Long::class -> LongDataType 18 | Float::class -> FloatDataType 19 | Double::class -> DoubleDataType 20 | ComplexFloat::class -> ComplexFloatDataType 21 | ComplexDouble::class, ComplexDouble64::class, ComplexDouble32::class -> ComplexDoubleDataType 22 | else -> throw IllegalStateException("One of the primitive types was expected, got ${type.simpleName}") 23 | } -------------------------------------------------------------------------------- /multik-core/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/operations/JVMIteratingNDArray.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.ndarray.operations 6 | 7 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 8 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 9 | 10 | 11 | /** 12 | * Returns a [SortedSet][java.util.SortedSet] of all elements. 13 | */ 14 | public fun MultiArray.toSortedSet(): java.util.SortedSet { 15 | return toCollection(java.util.TreeSet()) 16 | } 17 | 18 | /** 19 | * Returns a [SortedSet][java.util.SortedSet] of all elements. 20 | * 21 | * Elements in the set returned are sorted according to the given [comparator]. 22 | */ 23 | public fun MultiArray.toSortedSet(comparator: Comparator): java.util.SortedSet { 24 | return toCollection(java.util.TreeSet(comparator)) 25 | } 26 | -------------------------------------------------------------------------------- /multik-core/src/jvmTest/kotlin/org/jetbrains/kotlinx/multik/io/UtilsTestsIO.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.io 2 | 3 | class UtilsTestsIO 4 | 5 | internal fun testResource(resourcePath: String): String = 6 | UtilsTestsIO::class.java.classLoader.getResource(resourcePath)!!.path 7 | 8 | internal fun testCsv(csvName: String) = testResource("data/csv/$csvName.csv") 9 | 10 | internal fun testNpy(npyName: String) = testResource("data/npy/$npyName.npy") 11 | 12 | internal fun testNpz(npzName: String) = testResource("data/npy/$npzName.npz") -------------------------------------------------------------------------------- /multik-core/src/jvmTest/kotlin/org/jetbrains/kotlinx/multik/io/csv.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.io 2 | 3 | import org.jetbrains.kotlinx.multik.api.io.read 4 | import org.jetbrains.kotlinx.multik.api.io.write 5 | import org.jetbrains.kotlinx.multik.api.mk 6 | import org.jetbrains.kotlinx.multik.api.ndarray 7 | import org.jetbrains.kotlinx.multik.ndarray.complex.complexDoubleArrayOf 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.i 9 | import org.jetbrains.kotlinx.multik.ndarray.complex.plus 10 | import org.jetbrains.kotlinx.multik.ndarray.data.D1 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import kotlin.io.path.Path 13 | import kotlin.io.path.deleteExisting 14 | import kotlin.io.path.exists 15 | import kotlin.test.Test 16 | import kotlin.test.assertEquals 17 | import kotlin.test.assertTrue 18 | 19 | 20 | class CSVTest { 21 | 22 | @Test 23 | fun `read 1-d array`() { 24 | println(mk.read(testCsv("a1d"))) 25 | } 26 | 27 | @Test 28 | fun `read 2-d array`() { 29 | println(mk.read(testCsv("a2d"))) 30 | } 31 | 32 | @Test 33 | fun `read array with complex numbers`() { 34 | // TODO(issue #161) 35 | } 36 | 37 | @Test 38 | fun `write simple array`() { 39 | val a = mk.ndarray(intArrayOf(1, 2, 3, 7)) 40 | val path = Path("src/jvmTest/resources/data/csv/testWrite1dArray.csv") 41 | mk.write(path, a) 42 | assertTrue(path.exists()) 43 | assertEquals(a, mk.read(path)) 44 | path.deleteExisting() 45 | } 46 | 47 | @Test 48 | fun `write array with complex numbers`() { 49 | val a = mk.ndarray(complexDoubleArrayOf(1.0 + 2.0.i, 3.0 + 4.0.i, 5.0 + 6.0.i)) 50 | val path = Path("src/jvmTest/resources/data/csv/testWrite1dArray.csv") 51 | mk.write(path, a) 52 | assertTrue(path.exists()) 53 | assertEquals(a, mk.read(path)) 54 | path.deleteExisting() 55 | } 56 | } -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/csv/a1d.csv: -------------------------------------------------------------------------------- 1 | 1.000000000000000000e+00 2 | 2.000000000000000000e+00 3 | 3.000000000000000000e+00 4 | -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/csv/a2d.csv: -------------------------------------------------------------------------------- 1 | 1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00 2 | 4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00 3 | -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/csv/a_complex.csv: -------------------------------------------------------------------------------- 1 | (1.000000000000000000e+00+2.000000000000000000e+00j) 2 | (3.000000000000000000e+00+4.000000000000000000e+00j) 3 | (5.000000000000000000e+00+6.000000000000000000e+00j) 4 | -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/npy/a1d.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/multik-core/src/jvmTest/resources/data/npy/a1d.npy -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/npy/a4d.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/multik-core/src/jvmTest/resources/data/npy/a4d.npy -------------------------------------------------------------------------------- /multik-core/src/jvmTest/resources/data/npy/arrays.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kotlin/multik/24c4181616fd66ddd5f0f8747fd608c46b5bfcc4/multik-core/src/jvmTest/resources/data/npy/arrays.npz -------------------------------------------------------------------------------- /multik-core/src/nativeMain/kotlin/org/jetbrains/kotlinx/multik/api/Engine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.api 6 | import kotlin.concurrent.AtomicReference 7 | 8 | /** 9 | * Engine Provider for Kotlin/Native targets. 10 | */ 11 | public actual fun enginesProvider(): Map = engines.value 12 | 13 | /** 14 | * Saves and initialize engine. 15 | */ 16 | public val engines: AtomicReference> by lazy { 17 | AtomicReference(mutableMapOf()) 18 | } 19 | -------------------------------------------------------------------------------- /multik-core/src/nativeMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/ComplexDouble.native.kt: -------------------------------------------------------------------------------- 1 | @file:OptIn(ExperimentalForeignApi::class) 2 | 3 | package org.jetbrains.kotlinx.multik.ndarray.complex 4 | 5 | import kotlinx.cinterop.ExperimentalForeignApi 6 | import kotlinx.cinterop.Vector128 7 | import kotlinx.cinterop.vectorOf 8 | 9 | /** 10 | * Creates a [ComplexDouble] with the given real and imaginary values in floating-point format. 11 | * 12 | * @param re the real value of the complex number in double format. 13 | * @param im the imaginary value of the complex number in double format. 14 | */ 15 | public actual fun ComplexDouble(re: Double, im: Double): ComplexDouble { 16 | val reBits = re.toBits() 17 | val reHigherBits = (reBits shr 32).toInt() 18 | val reLowerBits = (reBits and 0xFFFFFFFFL).toInt() 19 | 20 | val imBits = im.toBits() 21 | val imHigherBits = (imBits shr 32).toInt() 22 | val imLowerBits = (imBits and 0xFFFFFFFFL).toInt() 23 | 24 | return NativeComplexDouble(vectorOf(reLowerBits, reHigherBits, imLowerBits, imHigherBits)) 25 | } 26 | 27 | /** 28 | * Creates a [ComplexDouble] with the given real and imaginary values in number format. 29 | * 30 | * @param re the real value of the complex number in number format. 31 | * @param im the imaginary value of the complex number in number format. 32 | */ 33 | public actual fun ComplexDouble(re: Number, im: Number): ComplexDouble = ComplexDouble(re.toDouble(), im.toDouble()) 34 | 35 | /** 36 | * Represents a complex number with double precision. 37 | * 38 | * @property re The real part of the complex number. 39 | * @property im The imaginary part of the complex number. 40 | */ 41 | public value class NativeComplexDouble internal constructor(private val vector: Vector128) : ComplexDouble { 42 | public override val re: Double 43 | get() = vector.getDoubleAt(0) 44 | 45 | public override val im: Double 46 | get() = vector.getDoubleAt(1) 47 | 48 | override fun eq(other: Any): Boolean = 49 | when { 50 | other is NativeComplexDouble && vector == other.vector -> true 51 | other is NativeComplexDouble -> re == other.re && im == other.im 52 | else -> false 53 | } 54 | 55 | override fun hash(): Int = 31 * vector.hashCode() 56 | 57 | override fun toString(): String = "$re+($im)i" 58 | } 59 | -------------------------------------------------------------------------------- /multik-core/src/nativeMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/DataType.native.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 4 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.NativeComplexDouble 6 | import org.jetbrains.kotlinx.multik.ndarray.data.DataType.* 7 | import kotlin.reflect.KClass 8 | 9 | @PublishedApi 10 | internal actual inline fun dataTypeOf(type: KClass): DataType = 11 | when (type) { 12 | Byte::class -> ByteDataType 13 | Short::class -> ShortDataType 14 | Int::class -> IntDataType 15 | Long::class -> LongDataType 16 | Float::class -> FloatDataType 17 | Double::class -> DoubleDataType 18 | ComplexFloat::class -> ComplexFloatDataType 19 | ComplexDouble::class, NativeComplexDouble::class -> ComplexDoubleDataType 20 | else -> throw IllegalStateException("One of the primitive types was expected, got ${type.simpleName}") 21 | } -------------------------------------------------------------------------------- /multik-core/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/api/Engine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.api 2 | 3 | /** 4 | * Engine Provider for WASM. 5 | */ 6 | public actual fun enginesProvider(): Map = enginesStore 7 | 8 | /** 9 | * Saves and initialize engine. 10 | */ 11 | public val enginesStore: MutableMap = mutableMapOf() -------------------------------------------------------------------------------- /multik-core/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/complex/ComplexDouble.wasmJs.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.complex 2 | 3 | 4 | /** 5 | * Creates a [ComplexDouble] with the given real and imaginary values in floating-point format. 6 | * 7 | * @param re the real value of the complex number in double format. 8 | * @param im the imaginary value of the complex number in double format. 9 | */ 10 | public actual fun ComplexDouble(re: Double, im: Double): ComplexDouble = WasmComplexDouble(re, im) 11 | 12 | /** 13 | * Creates a [ComplexDouble] with the given real and imaginary values in number format. 14 | * 15 | * @param re the real value of the complex number in number format. 16 | * @param im the imaginary value of the complex number in number format. 17 | */ 18 | public actual fun ComplexDouble(re: Number, im: Number): ComplexDouble = ComplexDouble(re.toDouble(), im.toDouble()) 19 | 20 | /** 21 | * Represents a complex number with double precision. 22 | * 23 | * @property re The real part of the complex number. 24 | * @property im The imaginary part of the complex number. 25 | */ 26 | public class WasmComplexDouble internal constructor( 27 | public override val re: Double, public override val im: Double 28 | ) : ComplexDouble { 29 | 30 | override fun eq(other: Any): Boolean = equals(other) 31 | 32 | override fun hash(): Int = hashCode() 33 | 34 | override fun equals(other: Any?): Boolean = when { 35 | this === other -> true 36 | other is ComplexDouble -> re == other.re && im == other.im 37 | else -> false 38 | } 39 | 40 | override fun hashCode(): Int = 31 * re.toBits().hashCode() + im.toBits().hashCode() 41 | 42 | override fun toString(): String = "$re+($im)i" 43 | } 44 | -------------------------------------------------------------------------------- /multik-core/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/ndarray/data/DataType.wasmJs.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.ndarray.data 2 | 3 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 4 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 5 | import org.jetbrains.kotlinx.multik.ndarray.complex.WasmComplexDouble 6 | import org.jetbrains.kotlinx.multik.ndarray.data.DataType.* 7 | import kotlin.reflect.KClass 8 | 9 | @PublishedApi 10 | @Suppress("NOTHING_TO_INLINE") 11 | internal actual inline fun dataTypeOf(type: KClass): DataType = 12 | when (type) { 13 | Byte::class -> ByteDataType 14 | Short::class -> ShortDataType 15 | Int::class -> IntDataType 16 | Long::class -> LongDataType 17 | Float::class -> FloatDataType 18 | Double::class -> DoubleDataType 19 | ComplexFloat::class -> ComplexFloatDataType 20 | ComplexDouble::class, WasmComplexDouble::class -> ComplexDoubleDataType 21 | else -> throw IllegalStateException("One of the primitive types was expected, got ${type.simpleName}") 22 | } -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.DefaultEngineType 8 | import org.jetbrains.kotlinx.multik.api.Engine 9 | import org.jetbrains.kotlinx.multik.api.EngineType 10 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 11 | import org.jetbrains.kotlinx.multik.api.math.Math 12 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 13 | import org.jetbrains.kotlinx.multik.default.linalg.DefaultLinAlg 14 | import org.jetbrains.kotlinx.multik.default.math.DefaultMath 15 | import org.jetbrains.kotlinx.multik.default.stat.DefaultStatistics 16 | 17 | public class DefaultEngine : Engine() { 18 | override val name: String 19 | get() = type.name 20 | 21 | override val type: EngineType 22 | get() = DefaultEngineType 23 | 24 | override fun getMath(): Math { 25 | return DefaultMath 26 | } 27 | 28 | override fun getLinAlg(): LinAlg { 29 | return DefaultLinAlg 30 | } 31 | 32 | override fun getStatistics(): Statistics { 33 | return DefaultStatistics 34 | } 35 | } -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.EngineFactory 8 | 9 | internal expect object DefaultEngineFactory: EngineFactory -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 9 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 10 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 11 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 12 | 13 | public expect object DefaultLinAlg : LinAlg { 14 | 15 | override val linAlgEx: LinAlgEx 16 | 17 | override fun pow(mat: MultiArray, n: Int): NDArray 18 | } -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/math/DefaultMath.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.math.Math 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.ndarray.data.* 10 | 11 | public expect object DefaultMath : Math { 12 | 13 | override val mathEx: MathEx 14 | 15 | override fun argMax(a: MultiArray): Int 16 | override fun argMax(a: MultiArray, axis: Int): NDArray 17 | override fun argMaxD2(a: MultiArray, axis: Int): NDArray 18 | override fun argMaxD3(a: MultiArray, axis: Int): NDArray 19 | override fun argMaxD4(a: MultiArray, axis: Int): NDArray 20 | override fun argMaxDN(a: MultiArray, axis: Int): NDArray 21 | 22 | override fun argMin(a: MultiArray): Int 23 | override fun argMin(a: MultiArray, axis: Int): NDArray 24 | override fun argMinD2(a: MultiArray, axis: Int): NDArray 25 | override fun argMinD3(a: MultiArray, axis: Int): NDArray 26 | override fun argMinD4(a: MultiArray, axis: Int): NDArray 27 | override fun argMinDN(a: MultiArray, axis: Int): NDArray 28 | 29 | override fun max(a: MultiArray): T 30 | override fun max(a: MultiArray, axis: Int): NDArray 31 | override fun maxD2(a: MultiArray, axis: Int): NDArray 32 | override fun maxD3(a: MultiArray, axis: Int): NDArray 33 | override fun maxD4(a: MultiArray, axis: Int): NDArray 34 | override fun maxDN(a: MultiArray, axis: Int): NDArray 35 | 36 | override fun min(a: MultiArray): T 37 | override fun min(a: MultiArray, axis: Int): NDArray 38 | override fun minD2(a: MultiArray, axis: Int): NDArray 39 | override fun minD3(a: MultiArray, axis: Int): NDArray 40 | override fun minD4(a: MultiArray, axis: Int): NDArray 41 | override fun minDN(a: MultiArray, axis: Int): NDArray 42 | 43 | override fun sum(a: MultiArray): T 44 | override fun sum(a: MultiArray, axis: Int): NDArray 45 | override fun sumD2(a: MultiArray, axis: Int): NDArray 46 | override fun sumD3(a: MultiArray, axis: Int): NDArray 47 | override fun sumD4(a: MultiArray, axis: Int): NDArray 48 | override fun sumDN(a: MultiArray, axis: Int): NDArray 49 | 50 | override fun cumSum(a: MultiArray): D1Array 51 | override fun cumSum(a: MultiArray, axis: Int): NDArray 52 | } -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.math.MathEx 8 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 9 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 10 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 11 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 12 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 13 | 14 | public expect object DefaultMathEx : MathEx { 15 | 16 | override fun exp(a: MultiArray): NDArray 17 | override fun expF(a: MultiArray): NDArray 18 | override fun expCF(a: MultiArray): NDArray 19 | override fun expCD(a: MultiArray): NDArray 20 | 21 | override fun log(a: MultiArray): NDArray 22 | override fun logF(a: MultiArray): NDArray 23 | override fun logCF(a: MultiArray): NDArray 24 | override fun logCD(a: MultiArray): NDArray 25 | 26 | override fun sin(a: MultiArray): NDArray 27 | override fun sinF(a: MultiArray): NDArray 28 | override fun sinCF(a: MultiArray): NDArray 29 | override fun sinCD(a: MultiArray): NDArray 30 | 31 | override fun cos(a: MultiArray): NDArray 32 | override fun cosF(a: MultiArray): NDArray 33 | override fun cosCF(a: MultiArray): NDArray 34 | override fun cosCD(a: MultiArray): NDArray 35 | } -------------------------------------------------------------------------------- /multik-default/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 8 | import org.jetbrains.kotlinx.multik.ndarray.data.* 9 | 10 | public expect object DefaultStatistics : Statistics { 11 | 12 | override fun median(a: MultiArray): Double? 13 | 14 | override fun average(a: MultiArray, weights: MultiArray?): Double 15 | 16 | override fun mean(a: MultiArray): Double 17 | 18 | override fun mean(a: MultiArray, axis: Int): NDArray 19 | 20 | override fun meanD2(a: MultiArray, axis: Int): NDArray 21 | 22 | override fun meanD3(a: MultiArray, axis: Int): NDArray 23 | 24 | override fun meanD4(a: MultiArray, axis: Int): NDArray 25 | 26 | override fun meanDN(a: MultiArray, axis: Int): NDArray 27 | } -------------------------------------------------------------------------------- /multik-default/src/iosMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import org.jetbrains.kotlinx.multik.kotlin.KEEngine 9 | 10 | internal actual object DefaultEngineFactory : EngineFactory { 11 | override fun getEngine(type: EngineType?): Engine = 12 | when (type) { 13 | null, KEEngineType, DefaultEngineType -> KEEngine() 14 | NativeEngineType -> error("Don't exist native engine for iOS targets") 15 | } 16 | } -------------------------------------------------------------------------------- /multik-default/src/iosMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultIosEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.DefaultEngineType 8 | import org.jetbrains.kotlinx.multik.api.engines 9 | 10 | @ExperimentalStdlibApi 11 | @Suppress("unused", "DEPRECATION") 12 | @EagerInitialization 13 | private val InitHook = EngineInitializer 14 | 15 | private object EngineInitializer { 16 | init { 17 | engines.value[DefaultEngineType] = DefaultEngine() 18 | } 19 | } -------------------------------------------------------------------------------- /multik-default/src/iosMain/kotlin/org.jetbrains.kotlinx.multik.default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | public actual object DefaultLinAlg : LinAlg { 16 | 17 | private val ktLinAlg = DefaultEngineFactory.getEngine(KEEngineType).getLinAlg() 18 | 19 | actual override val linAlgEx: LinAlgEx 20 | get() = DefaultLinAlgEx 21 | 22 | actual override fun pow(mat: MultiArray, n: Int): NDArray = ktLinAlg.pow(mat, n) 23 | } -------------------------------------------------------------------------------- /multik-default/src/iosMain/kotlin/org.jetbrains.kotlinx.multik.default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 11 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 12 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | public actual object DefaultMathEx : MathEx { 17 | 18 | private val ktMathEx = DefaultEngineFactory.getEngine(KEEngineType).getMath().mathEx 19 | 20 | actual override fun exp(a: MultiArray): NDArray = ktMathEx.exp(a) 21 | actual override fun expF(a: MultiArray): NDArray = ktMathEx.expF(a) 22 | actual override fun expCF(a: MultiArray): NDArray = ktMathEx.expCF(a) 23 | actual override fun expCD(a: MultiArray): NDArray = ktMathEx.expCD(a) 24 | 25 | actual override fun log(a: MultiArray): NDArray = ktMathEx.log(a) 26 | actual override fun logF(a: MultiArray): NDArray = ktMathEx.logF(a) 27 | actual override fun logCF(a: MultiArray): NDArray = ktMathEx.logCF(a) 28 | actual override fun logCD(a: MultiArray): NDArray = ktMathEx.logCD(a) 29 | 30 | actual override fun sin(a: MultiArray): NDArray = ktMathEx.sin(a) 31 | actual override fun sinF(a: MultiArray): NDArray = ktMathEx.sinF(a) 32 | actual override fun sinCF(a: MultiArray): NDArray = ktMathEx.sinCF(a) 33 | actual override fun sinCD(a: MultiArray): NDArray = ktMathEx.sinCD(a) 34 | 35 | actual override fun cos(a: MultiArray): NDArray = ktMathEx.cos(a) 36 | actual override fun cosF(a: MultiArray): NDArray = ktMathEx.cosF(a) 37 | actual override fun cosCF(a: MultiArray): NDArray = ktMathEx.cosCF(a) 38 | actual override fun cosCD(a: MultiArray): NDArray = ktMathEx.cosCD(a) 39 | } -------------------------------------------------------------------------------- /multik-default/src/iosMain/kotlin/org.jetbrains.kotlinx.multik.default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.data.* 11 | 12 | public actual object DefaultStatistics : Statistics { 13 | 14 | private val ktStat = DefaultEngineFactory.getEngine(KEEngineType).getStatistics() 15 | 16 | actual override fun median(a: MultiArray): Double? = ktStat.median(a) 17 | 18 | actual override fun average(a: MultiArray, weights: MultiArray?): Double = 19 | ktStat.average(a, weights) 20 | 21 | actual override fun mean(a: MultiArray): Double = ktStat.mean(a) 22 | 23 | actual override fun mean(a: MultiArray, axis: Int): NDArray = 24 | ktStat.mean(a, axis) 25 | 26 | actual override fun meanD2(a: MultiArray, axis: Int): NDArray = mean(a, axis) 27 | 28 | actual override fun meanD3(a: MultiArray, axis: Int): NDArray = mean(a, axis) 29 | 30 | actual override fun meanD4(a: MultiArray, axis: Int): NDArray = mean(a, axis) 31 | 32 | actual override fun meanDN(a: MultiArray, axis: Int): NDArray = mean(a, axis) 33 | } -------------------------------------------------------------------------------- /multik-default/src/jsMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import org.jetbrains.kotlinx.multik.kotlin.KEEngine 9 | 10 | internal actual object DefaultEngineFactory : EngineFactory { 11 | override fun getEngine(type: EngineType?): Engine = 12 | when (type) { 13 | null, KEEngineType, DefaultEngineType -> KEEngine() 14 | NativeEngineType -> error("Don't exist native engine for iOS targets") 15 | } 16 | } -------------------------------------------------------------------------------- /multik-default/src/jsMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultJsEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.DefaultEngineType 8 | import org.jetbrains.kotlinx.multik.api.enginesStore 9 | 10 | @ExperimentalJsExport 11 | @ExperimentalStdlibApi 12 | @Suppress("unused", "DEPRECATION") 13 | @EagerInitialization 14 | @JsExport 15 | public val initializer: dynamic = EngineInitializer 16 | 17 | public object EngineInitializer { 18 | init { 19 | enginesStore[DefaultEngineType] = DefaultEngine() 20 | } 21 | } -------------------------------------------------------------------------------- /multik-default/src/jsMain/kotlin/org.jetbrains.kotlinx.multik.default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | public actual object DefaultLinAlg : LinAlg { 16 | 17 | private val ktLinAlg = DefaultEngineFactory.getEngine(KEEngineType).getLinAlg() 18 | 19 | actual override val linAlgEx: LinAlgEx 20 | get() = DefaultLinAlgEx 21 | 22 | actual override fun pow(mat: MultiArray, n: Int): NDArray = ktLinAlg.pow(mat, n) 23 | } -------------------------------------------------------------------------------- /multik-default/src/jsMain/kotlin/org.jetbrains.kotlinx.multik.default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 11 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 12 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | public actual object DefaultMathEx : MathEx { 17 | 18 | private val ktMathEx = DefaultEngineFactory.getEngine(KEEngineType).getMath().mathEx 19 | 20 | actual override fun exp(a: MultiArray): NDArray = ktMathEx.exp(a) 21 | actual override fun expF(a: MultiArray): NDArray = ktMathEx.expF(a) 22 | actual override fun expCF(a: MultiArray): NDArray = ktMathEx.expCF(a) 23 | actual override fun expCD(a: MultiArray): NDArray = ktMathEx.expCD(a) 24 | 25 | actual override fun log(a: MultiArray): NDArray = ktMathEx.log(a) 26 | actual override fun logF(a: MultiArray): NDArray = ktMathEx.logF(a) 27 | actual override fun logCF(a: MultiArray): NDArray = ktMathEx.logCF(a) 28 | actual override fun logCD(a: MultiArray): NDArray = ktMathEx.logCD(a) 29 | 30 | actual override fun sin(a: MultiArray): NDArray = ktMathEx.sin(a) 31 | actual override fun sinF(a: MultiArray): NDArray = ktMathEx.sinF(a) 32 | actual override fun sinCF(a: MultiArray): NDArray = ktMathEx.sinCF(a) 33 | actual override fun sinCD(a: MultiArray): NDArray = ktMathEx.sinCD(a) 34 | 35 | actual override fun cos(a: MultiArray): NDArray = ktMathEx.cos(a) 36 | actual override fun cosF(a: MultiArray): NDArray = ktMathEx.cosF(a) 37 | actual override fun cosCF(a: MultiArray): NDArray = ktMathEx.cosCF(a) 38 | actual override fun cosCD(a: MultiArray): NDArray = ktMathEx.cosCD(a) 39 | } -------------------------------------------------------------------------------- /multik-default/src/jsMain/kotlin/org.jetbrains.kotlinx.multik.default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.data.* 11 | 12 | public actual object DefaultStatistics : Statistics { 13 | 14 | private val ktStat = DefaultEngineFactory.getEngine(KEEngineType).getStatistics() 15 | 16 | actual override fun median(a: MultiArray): Double? = ktStat.median(a) 17 | 18 | actual override fun average(a: MultiArray, weights: MultiArray?): Double = 19 | ktStat.average(a, weights) 20 | 21 | actual override fun mean(a: MultiArray): Double = ktStat.mean(a) 22 | 23 | actual override fun mean(a: MultiArray, axis: Int): NDArray = 24 | ktStat.mean(a, axis) 25 | 26 | actual override fun meanD2(a: MultiArray, axis: Int): NDArray = mean(a, axis) 27 | 28 | actual override fun meanD3(a: MultiArray, axis: Int): NDArray = mean(a, axis) 29 | 30 | actual override fun meanD4(a: MultiArray, axis: Int): NDArray = mean(a, axis) 31 | 32 | actual override fun meanDN(a: MultiArray, axis: Int): NDArray = mean(a, axis) 33 | } -------------------------------------------------------------------------------- /multik-default/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import org.jetbrains.kotlinx.multik.kotlin.KEEngine 9 | import org.jetbrains.kotlinx.multik.openblas.JvmNativeEngine 10 | 11 | internal actual object DefaultEngineFactory : EngineFactory { 12 | override fun getEngine(type: EngineType?): Engine = 13 | when (type) { 14 | null -> error("Pass NativeEngineType of KEEngineType") 15 | KEEngineType -> KEEngine() 16 | NativeEngineType -> { 17 | if (supportNative) 18 | JvmNativeEngine() 19 | else 20 | KEEngine() 21 | } 22 | DefaultEngineType -> error("Default Engine doesn't link to Default Engine") 23 | } 24 | 25 | private val supportNative: Boolean by lazy { 26 | when ("$os-$arch") { 27 | "macos-X64", "macos-Arm64", "linux-X64", "mingw-X64", "android-Arm64" -> true 28 | else -> false 29 | } 30 | } 31 | 32 | private val os: String 33 | get() { 34 | val osProperty: String = System.getProperty("os.name").lowercase() 35 | return when { 36 | osProperty.contains("mac") -> "macos" 37 | System.getProperty("java.vm.name").contains("Dalvik") -> "android" 38 | osProperty.contains("nux") -> "linux" 39 | osProperty.contains("win") -> "mingw" 40 | else -> "Unknown os" 41 | } 42 | } 43 | 44 | private val arch: String 45 | get() = when (val arch: String = System.getProperty("os.arch").lowercase()) { 46 | "amd64", "x86_64", "x86-64", "x64" -> "X64" 47 | "arm64", "aarch64", "armv8" -> "Arm64" 48 | else -> "Unknown arch" 49 | } 50 | } -------------------------------------------------------------------------------- /multik-default/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultJvmEngine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.default 2 | 3 | -------------------------------------------------------------------------------- /multik-default/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | public actual object DefaultLinAlg : LinAlg { 16 | 17 | private val natLinAlg = DefaultEngineFactory.getEngine(NativeEngineType).getLinAlg() 18 | actual override val linAlgEx: LinAlgEx 19 | get() = DefaultLinAlgEx 20 | 21 | actual override fun pow(mat: MultiArray, n: Int): NDArray = natLinAlg.pow(mat, n) 22 | } -------------------------------------------------------------------------------- /multik-default/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 11 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 12 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | public actual object DefaultMathEx : MathEx { 17 | 18 | private val natMathEx = DefaultEngineFactory.getEngine(NativeEngineType).getMath().mathEx 19 | 20 | actual override fun exp(a: MultiArray): NDArray = natMathEx.exp(a) 21 | actual override fun expF(a: MultiArray): NDArray = natMathEx.expF(a) 22 | actual override fun expCF(a: MultiArray): NDArray = natMathEx.expCF(a) 23 | actual override fun expCD(a: MultiArray): NDArray = natMathEx.expCD(a) 24 | 25 | actual override fun log(a: MultiArray): NDArray = natMathEx.log(a) 26 | actual override fun logF(a: MultiArray): NDArray = natMathEx.logF(a) 27 | actual override fun logCF(a: MultiArray): NDArray = natMathEx.logCF(a) 28 | actual override fun logCD(a: MultiArray): NDArray = natMathEx.logCD(a) 29 | 30 | actual override fun sin(a: MultiArray): NDArray = natMathEx.sin(a) 31 | actual override fun sinF(a: MultiArray): NDArray = natMathEx.sinF(a) 32 | actual override fun sinCF(a: MultiArray): NDArray = natMathEx.sinCF(a) 33 | actual override fun sinCD(a: MultiArray): NDArray = natMathEx.sinCD(a) 34 | 35 | actual override fun cos(a: MultiArray): NDArray = natMathEx.cos(a) 36 | actual override fun cosF(a: MultiArray): NDArray = natMathEx.cosF(a) 37 | actual override fun cosCF(a: MultiArray): NDArray = natMathEx.cosCF(a) 38 | actual override fun cosCD(a: MultiArray): NDArray = natMathEx.cosCD(a) 39 | } -------------------------------------------------------------------------------- /multik-default/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 9 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.* 12 | 13 | public actual object DefaultStatistics : Statistics { 14 | 15 | private val ktStat = DefaultEngineFactory.getEngine(KEEngineType).getStatistics() 16 | private val natStat = DefaultEngineFactory.getEngine(NativeEngineType).getStatistics() 17 | 18 | actual override fun median(a: MultiArray): Double? = natStat.median(a) 19 | 20 | actual override fun average(a: MultiArray, weights: MultiArray?): Double = 21 | ktStat.average(a, weights) 22 | 23 | actual override fun mean(a: MultiArray): Double = ktStat.mean(a) 24 | 25 | actual override fun mean(a: MultiArray, axis: Int): NDArray = 26 | ktStat.mean(a, axis) 27 | 28 | actual override fun meanD2(a: MultiArray, axis: Int): NDArray = mean(a, axis) 29 | 30 | actual override fun meanD3(a: MultiArray, axis: Int): NDArray = mean(a, axis) 31 | 32 | actual override fun meanD4(a: MultiArray, axis: Int): NDArray = mean(a, axis) 33 | 34 | actual override fun meanDN(a: MultiArray, axis: Int): NDArray = mean(a, axis) 35 | } -------------------------------------------------------------------------------- /multik-default/src/jvmMain/resources/META-INF/services/org.jetbrains.kotlinx.multik.api.Engine: -------------------------------------------------------------------------------- 1 | org.jetbrains.kotlinx.multik.default.DefaultEngine -------------------------------------------------------------------------------- /multik-default/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import org.jetbrains.kotlinx.multik.kotlin.KEEngine 9 | import org.jetbrains.kotlinx.multik.openblas.NativeEngine 10 | 11 | internal actual object DefaultEngineFactory : EngineFactory { 12 | override fun getEngine(type: EngineType?): Engine = 13 | when (type) { 14 | null -> error("Pass NativeEngineType of KEEngineType") 15 | KEEngineType -> KEEngine() 16 | NativeEngineType -> NativeEngine() 17 | DefaultEngineType -> error("Default Engine doesn't link to Default Engine") 18 | } 19 | } -------------------------------------------------------------------------------- /multik-default/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik.default/DefaultNativeEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.DefaultEngineType 8 | import org.jetbrains.kotlinx.multik.api.engines 9 | 10 | @ExperimentalStdlibApi 11 | @Suppress("unused", "DEPRECATION") 12 | @EagerInitialization 13 | private val InitHook = EngineInitializer 14 | 15 | private object EngineInitializer { 16 | init { 17 | engines.value[DefaultEngineType] = DefaultEngine() 18 | } 19 | } -------------------------------------------------------------------------------- /multik-default/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik.default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | public actual object DefaultLinAlg : LinAlg { 16 | 17 | private val natLinAlg = DefaultEngineFactory.getEngine(NativeEngineType).getLinAlg() 18 | 19 | actual override val linAlgEx: LinAlgEx 20 | get() = DefaultLinAlgEx 21 | 22 | actual override fun pow(mat: MultiArray, n: Int): NDArray = natLinAlg.pow(mat, n) 23 | } -------------------------------------------------------------------------------- /multik-default/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik.default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 11 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 12 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | public actual object DefaultMathEx : MathEx { 17 | 18 | private val natMath = DefaultEngineFactory.getEngine(NativeEngineType).getMath().mathEx 19 | 20 | actual override fun exp(a: MultiArray): NDArray = natMath.exp(a) 21 | actual override fun expF(a: MultiArray): NDArray = natMath.expF(a) 22 | actual override fun expCF(a: MultiArray): NDArray = natMath.expCF(a) 23 | actual override fun expCD(a: MultiArray): NDArray = natMath.expCD(a) 24 | 25 | actual override fun log(a: MultiArray): NDArray = natMath.log(a) 26 | actual override fun logF(a: MultiArray): NDArray = natMath.logF(a) 27 | actual override fun logCF(a: MultiArray): NDArray = natMath.logCF(a) 28 | actual override fun logCD(a: MultiArray): NDArray = natMath.logCD(a) 29 | 30 | actual override fun sin(a: MultiArray): NDArray = natMath.sin(a) 31 | actual override fun sinF(a: MultiArray): NDArray = natMath.sinF(a) 32 | actual override fun sinCF(a: MultiArray): NDArray = natMath.sinCF(a) 33 | actual override fun sinCD(a: MultiArray): NDArray = natMath.sinCD(a) 34 | 35 | actual override fun cos(a: MultiArray): NDArray = natMath.cos(a) 36 | actual override fun cosF(a: MultiArray): NDArray = natMath.cosF(a) 37 | actual override fun cosCF(a: MultiArray): NDArray = natMath.cosCF(a) 38 | actual override fun cosCD(a: MultiArray): NDArray = natMath.cosCD(a) 39 | } -------------------------------------------------------------------------------- /multik-default/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik.default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 9 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.* 12 | 13 | public actual object DefaultStatistics : Statistics { 14 | 15 | private val ktStat = DefaultEngineFactory.getEngine(KEEngineType).getStatistics() 16 | private val natStat = DefaultEngineFactory.getEngine(NativeEngineType).getStatistics() 17 | 18 | actual override fun median(a: MultiArray): Double? = natStat.median(a) 19 | 20 | actual override fun average(a: MultiArray, weights: MultiArray?): Double = 21 | ktStat.average(a, weights) 22 | 23 | actual override fun mean(a: MultiArray): Double = ktStat.mean(a) 24 | 25 | actual override fun mean(a: MultiArray, axis: Int): NDArray = 26 | ktStat.mean(a, axis) 27 | 28 | actual override fun meanD2(a: MultiArray, axis: Int): NDArray = mean(a, axis) 29 | 30 | actual override fun meanD3(a: MultiArray, axis: Int): NDArray = mean(a, axis) 31 | 32 | actual override fun meanD4(a: MultiArray, axis: Int): NDArray = mean(a, axis) 33 | 34 | actual override fun meanDN(a: MultiArray, axis: Int): NDArray = mean(a, axis) 35 | } -------------------------------------------------------------------------------- /multik-default/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultEngineFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.* 8 | import org.jetbrains.kotlinx.multik.kotlin.KEEngine 9 | 10 | internal actual object DefaultEngineFactory : EngineFactory { 11 | override fun getEngine(type: EngineType?): Engine = 12 | when (type) { 13 | null, KEEngineType, DefaultEngineType -> KEEngine() 14 | NativeEngineType -> error("Don't exist native engine for iOS targets") 15 | } 16 | } -------------------------------------------------------------------------------- /multik-default/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/default/DefaultWasmEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default 6 | 7 | import org.jetbrains.kotlinx.multik.api.DefaultEngineType 8 | import org.jetbrains.kotlinx.multik.api.enginesStore 9 | 10 | @ExperimentalJsExport 11 | @ExperimentalStdlibApi 12 | @Suppress("unused", "DEPRECATION") 13 | @EagerInitialization 14 | public val initializer: EngineInitializer = EngineInitializer 15 | 16 | public object EngineInitializer { 17 | init { 18 | enginesStore[DefaultEngineType] = DefaultEngine() 19 | } 20 | } -------------------------------------------------------------------------------- /multik-default/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/default/linalg/DefaultLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | public actual object DefaultLinAlg : LinAlg { 16 | 17 | private val ktLinAlg = DefaultEngineFactory.getEngine(KEEngineType).getLinAlg() 18 | 19 | actual override val linAlgEx: LinAlgEx 20 | get() = DefaultLinAlgEx 21 | 22 | actual override fun pow(mat: MultiArray, n: Int): NDArray = ktLinAlg.pow(mat, n) 23 | } -------------------------------------------------------------------------------- /multik-default/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/default/math/DefaultMathEx.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.math 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.math.MathEx 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 11 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 12 | import org.jetbrains.kotlinx.multik.ndarray.data.Dimension 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | public actual object DefaultMathEx : MathEx { 17 | 18 | private val ktMathEx = DefaultEngineFactory.getEngine(KEEngineType).getMath().mathEx 19 | 20 | actual override fun exp(a: MultiArray): NDArray = ktMathEx.exp(a) 21 | actual override fun expF(a: MultiArray): NDArray = ktMathEx.expF(a) 22 | actual override fun expCF(a: MultiArray): NDArray = ktMathEx.expCF(a) 23 | actual override fun expCD(a: MultiArray): NDArray = ktMathEx.expCD(a) 24 | 25 | actual override fun log(a: MultiArray): NDArray = ktMathEx.log(a) 26 | actual override fun logF(a: MultiArray): NDArray = ktMathEx.logF(a) 27 | actual override fun logCF(a: MultiArray): NDArray = ktMathEx.logCF(a) 28 | actual override fun logCD(a: MultiArray): NDArray = ktMathEx.logCD(a) 29 | 30 | actual override fun sin(a: MultiArray): NDArray = ktMathEx.sin(a) 31 | actual override fun sinF(a: MultiArray): NDArray = ktMathEx.sinF(a) 32 | actual override fun sinCF(a: MultiArray): NDArray = ktMathEx.sinCF(a) 33 | actual override fun sinCD(a: MultiArray): NDArray = ktMathEx.sinCD(a) 34 | 35 | actual override fun cos(a: MultiArray): NDArray = ktMathEx.cos(a) 36 | actual override fun cosF(a: MultiArray): NDArray = ktMathEx.cosF(a) 37 | actual override fun cosCF(a: MultiArray): NDArray = ktMathEx.cosCF(a) 38 | actual override fun cosCD(a: MultiArray): NDArray = ktMathEx.cosCD(a) 39 | } -------------------------------------------------------------------------------- /multik-default/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/default/stat/DefaultStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.default.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 9 | import org.jetbrains.kotlinx.multik.default.DefaultEngineFactory 10 | import org.jetbrains.kotlinx.multik.ndarray.data.* 11 | 12 | public actual object DefaultStatistics : Statistics { 13 | 14 | private val ktStat = DefaultEngineFactory.getEngine(KEEngineType).getStatistics() 15 | 16 | actual override fun median(a: MultiArray): Double? = ktStat.median(a) 17 | 18 | actual override fun average(a: MultiArray, weights: MultiArray?): Double = 19 | ktStat.average(a, weights) 20 | 21 | actual override fun mean(a: MultiArray): Double = ktStat.mean(a) 22 | 23 | actual override fun mean(a: MultiArray, axis: Int): NDArray = 24 | ktStat.mean(a, axis) 25 | 26 | actual override fun meanD2(a: MultiArray, axis: Int): NDArray = mean(a, axis) 27 | 28 | actual override fun meanD3(a: MultiArray, axis: Int): NDArray = mean(a, axis) 29 | 30 | actual override fun meanD4(a: MultiArray, axis: Int): NDArray = mean(a, axis) 31 | 32 | actual override fun meanDN(a: MultiArray, axis: Int): NDArray = mean(a, axis) 33 | } -------------------------------------------------------------------------------- /multik-kotlin/build.gradle.kts: -------------------------------------------------------------------------------- 1 | @file:OptIn(ExperimentalWasmDsl::class) 2 | 3 | import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl 4 | 5 | plugins { 6 | kotlin("multiplatform") 7 | } 8 | 9 | 10 | kotlin { 11 | jvm { 12 | compilations.all { 13 | kotlinOptions.jvmTarget = "1.8" 14 | } 15 | testRuns["test"].executionTask.configure { 16 | useJUnit() 17 | } 18 | } 19 | mingwX64() 20 | linuxX64() 21 | macosX64 { 22 | binaries { 23 | framework { 24 | baseName = "multik-kotlin" 25 | } 26 | } 27 | } 28 | macosArm64 { 29 | binaries { 30 | framework { 31 | baseName = "multik-kotlin" 32 | } 33 | } 34 | } 35 | iosArm64 { 36 | binaries { 37 | framework { 38 | baseName = "multik-kotlin" 39 | } 40 | } 41 | } 42 | iosSimulatorArm64 { 43 | binaries { 44 | framework { 45 | baseName = "multik-kotlin" 46 | } 47 | } 48 | } 49 | iosX64 { 50 | binaries { 51 | framework { 52 | baseName = "multik-kotlin" 53 | } 54 | } 55 | } 56 | 57 | wasmJs { 58 | browser { 59 | testTask { 60 | enabled = false 61 | } 62 | } 63 | nodejs { 64 | testTask { 65 | enabled = false 66 | } 67 | } 68 | d8() 69 | } 70 | 71 | js(IR) { 72 | val timeoutMs = "1000000" 73 | browser { 74 | testTask { 75 | useMocha { 76 | timeout = timeoutMs 77 | } 78 | } 79 | } 80 | nodejs { 81 | testTask { 82 | useMocha { 83 | timeout = timeoutMs 84 | } 85 | } 86 | } 87 | } 88 | 89 | targets.withType { 90 | binaries.getTest("debug").apply { 91 | debuggable = false 92 | optimized = true 93 | } 94 | } 95 | 96 | sourceSets { 97 | val commonMain by getting { 98 | dependencies { 99 | api(project(":multik-core")) 100 | } 101 | } 102 | val commonTest by getting { 103 | dependencies { 104 | api(project(":multik-core")) 105 | implementation(kotlin("test")) 106 | } 107 | } 108 | val jvmMain by getting { 109 | dependencies { 110 | implementation(kotlin("reflect")) 111 | } 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /multik-kotlin/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/kotlin/KEEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.kotlin 6 | 7 | import org.jetbrains.kotlinx.multik.api.Engine 8 | import org.jetbrains.kotlinx.multik.api.EngineType 9 | import org.jetbrains.kotlinx.multik.api.KEEngineType 10 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 11 | import org.jetbrains.kotlinx.multik.api.math.Math 12 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 13 | import org.jetbrains.kotlinx.multik.kotlin.linalg.KELinAlg 14 | import org.jetbrains.kotlinx.multik.kotlin.math.KEMath 15 | import org.jetbrains.kotlinx.multik.kotlin.stat.KEStatistics 16 | 17 | 18 | public class KEEngine : Engine() { 19 | 20 | override val name: String 21 | get() = type.name 22 | 23 | override val type: EngineType 24 | get() = KEEngineType 25 | 26 | override fun getMath(): Math { 27 | return KEMath 28 | } 29 | 30 | override fun getLinAlg(): LinAlg { 31 | return KELinAlg 32 | } 33 | 34 | override fun getStatistics(): Statistics { 35 | return KEStatistics 36 | } 37 | } -------------------------------------------------------------------------------- /multik-kotlin/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/kotlin/linalg/KELinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.kotlin.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.identity 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.api.linalg.dot 11 | import org.jetbrains.kotlinx.multik.api.mk 12 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 13 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 14 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 15 | 16 | internal object KELinAlg : LinAlg { 17 | 18 | override val linAlgEx: LinAlgEx 19 | get() = KELinAlgEx 20 | 21 | override fun pow(mat: MultiArray, n: Int): NDArray { 22 | if (n == 0) return mk.identity(mat.shape[0], mat.dtype) 23 | 24 | return if (n % 2 == 0) { 25 | val tmp = pow(mat, n / 2) 26 | dot(tmp, tmp) 27 | } else { 28 | dot(mat, pow(mat, n - 1)) 29 | } 30 | } 31 | 32 | // override fun norm(mat: MultiArray, p: Int): Double { 33 | // require(p > 0) { "Power $p must be positive" } 34 | // 35 | // return when (mat.dtype) { 36 | // DataType.DoubleDataType -> { 37 | // norm(mat.data.getDoubleArray(), mat.offset, mat.strides, mat.shape[0], mat.shape[1], p, mat.consistent) 38 | // } 39 | // DataType.FloatDataType -> { 40 | // norm(mat.data.getFloatArray(), mat.offset, mat.strides, mat.shape[0], mat.shape[1], p, mat.consistent) 41 | // } 42 | // else -> { 43 | // norm(mat.data, mat.offset, mat.strides, mat.shape[0], mat.shape[1], p, mat.consistent) 44 | // } 45 | // } 46 | // } 47 | } 48 | -------------------------------------------------------------------------------- /multik-kotlin/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/kotlin/linalg/utils.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.kotlin.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.mk 8 | import org.jetbrains.kotlinx.multik.api.zeros 9 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexDouble 10 | import org.jetbrains.kotlinx.multik.ndarray.complex.ComplexFloat 11 | import org.jetbrains.kotlinx.multik.ndarray.data.* 12 | import kotlin.jvm.JvmName 13 | import kotlin.math.cos 14 | import kotlin.math.sin 15 | import kotlin.math.sqrt 16 | 17 | internal fun requireSquare(shape: IntArray) { 18 | require(shape[0] == shape[1]) { "Square matrix expected, shape=(${shape[0]}, ${shape[1]}) given" } 19 | } 20 | 21 | // return hermitian transposed copy of matrix 22 | @JvmName("conjTransposeFloat") 23 | internal fun NDArray.conjTranspose(): D2Array { 24 | val ans = mk.zeros(this.shape[1], this.shape[0]) 25 | for (i in 0 until ans.shape[0]) { 26 | for (j in 0 until ans.shape[1]) { 27 | ans[i, j] = this[j, i].conjugate() 28 | } 29 | } 30 | return ans 31 | } 32 | 33 | @JvmName("conjTransposeDouble") 34 | internal fun NDArray.conjTranspose(): D2Array { 35 | val ans = mk.zeros(this.shape[1], this.shape[0]) 36 | for (i in 0 until ans.shape[0]) { 37 | for (j in 0 until ans.shape[1]) { 38 | ans[i, j] = this[j, i].conjugate() 39 | } 40 | } 41 | return ans 42 | } 43 | 44 | internal fun requireDotShape(aShape: IntArray, bShape: IntArray) = require(aShape[1] == bShape[0]) { 45 | "Shapes mismatch: shapes " + 46 | "${aShape.joinToString(prefix = "(", postfix = ")")} and " + 47 | "${bShape.joinToString(prefix = "(", postfix = ")")} not aligned: " + 48 | "${aShape[1]} (dim 1) != ${bShape[0]} (dim 0)" 49 | } 50 | 51 | // computes some square root of complex number 52 | // guarantee csqrt(a) * csqrt(a) = a 53 | fun csqrt(a: ComplexFloat): ComplexFloat { 54 | val arg = a.angle() 55 | val absval = a.abs() 56 | return ComplexFloat(sqrt(absval) * cos(arg / 2), sqrt(absval) * sin(arg / 2)) 57 | } 58 | 59 | fun csqrt(a: ComplexDouble): ComplexDouble { 60 | val arg = a.angle() 61 | val absval = a.abs() 62 | return ComplexDouble(sqrt(absval) * cos(arg / 2), sqrt(absval) * sin(arg / 2)) 63 | } 64 | 65 | 66 | /** 67 | * swap lines for plu decomposition 68 | * @param swap `a[i, j] = a[i + rowPerm[ i ], j].also { a[i + rowPerm[ i ], j] = a[i, j] }` 69 | */ 70 | internal inline fun swapLines( 71 | rowPerm: MultiArray, 72 | from1: Int = 0, to1: Int = rowPerm.size, from2: Int = 0, to2: Int, swap: (Int, Int) -> Unit 73 | ) { 74 | for (i in from1 until to1) { 75 | if (rowPerm[i] != 0) { 76 | for (j in from2 until to2) { 77 | swap(i, j) 78 | } 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /multik-kotlin/src/commonTest/kotlin/org/jetbrains/kotlinx/multik_kotlin/statistics/KEStatisticsTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik_kotlin.statistics 6 | 7 | import org.jetbrains.kotlinx.multik.api.arange 8 | import org.jetbrains.kotlinx.multik.api.mk 9 | import org.jetbrains.kotlinx.multik.api.ndarray 10 | import org.jetbrains.kotlinx.multik.kotlin.stat.KEStatistics 11 | import kotlin.test.Test 12 | import kotlin.test.assertEquals 13 | 14 | class KEStatisticsTest { 15 | 16 | @Test 17 | fun test_median() { 18 | val a = mk.ndarray(mk[mk[10, 7, 4], mk[3, 2, 1]]) 19 | assertEquals(3.5, mk.stat.median(a)) 20 | } 21 | 22 | @Test 23 | fun test_simple_average() { 24 | val a = mk.arange(1, 11, 1) 25 | assertEquals(mk.stat.mean(a), mk.stat.average(a)) 26 | } 27 | 28 | @Test 29 | fun test_average_with_weights() { 30 | val a = mk.arange(1, 11, 1) 31 | val weights = mk.arange(10, 0, -1) 32 | assertEquals(4.0, mk.stat.average(a, weights)) 33 | } 34 | 35 | @Test 36 | fun test_of_mean_function_on_a_3d_ndarray() { 37 | val ndarray = mk.ndarray(mk[mk[mk[0, 3], mk[1, 4]], mk[mk[2, 5], mk[6, 8]], mk[mk[7, 9], mk[10, 11]]]) 38 | 39 | val expectedWith0Axis = mk.ndarray(mk[mk[3.0, 5.666666666666667], mk[5.666666666666667, 7.666666666666667]]) 40 | val expectedWith1Axis = mk.ndarray(mk[mk[0.5, 3.5], mk[4.0, 6.5], mk[8.5, 10.0]]) 41 | val expectedWith2Axis = mk.ndarray(mk[mk[1.5, 2.5], mk[3.5, 7.0], mk[8.0, 10.5]]) 42 | 43 | assertEquals(expectedWith0Axis, mk.stat.mean(ndarray, axis = 0)) 44 | assertEquals(expectedWith1Axis, mk.stat.mean(ndarray, axis = 1)) 45 | assertEquals(expectedWith2Axis, mk.stat.mean(ndarray, axis = 2)) 46 | } 47 | 48 | @Test 49 | fun test_of_mean_function_on_a_flat_ndarray() { 50 | val ndarray = mk.ndarray(mk[1, 2, 3, 4]) 51 | assertEquals(2.5, KEStatistics.mean(ndarray)) 52 | } 53 | 54 | @Test 55 | fun test_of_mean_function_on_a_2d_ndarray() { 56 | val ndarray = mk.ndarray(mk[mk[1, 2], mk[3, 4]]) 57 | assertEquals(2.5, KEStatistics.mean(ndarray)) 58 | } 59 | } -------------------------------------------------------------------------------- /multik-kotlin/src/jsMain/kotlin/org.jetbrains.kotlinx.multik/kotlin/KEEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.kotlin 6 | 7 | import org.jetbrains.kotlinx.multik.api.KEEngineType 8 | import org.jetbrains.kotlinx.multik.api.enginesStore 9 | 10 | @ExperimentalJsExport 11 | @ExperimentalStdlibApi 12 | @Suppress("unused", "DEPRECATION") 13 | @EagerInitialization 14 | @JsExport 15 | public val initializer: dynamic = EngineInitializer 16 | 17 | public object EngineInitializer { 18 | init { 19 | enginesStore[KEEngineType] = KEEngine() 20 | } 21 | } -------------------------------------------------------------------------------- /multik-kotlin/src/jvmMain/resources/META-INF/services/org.jetbrains.kotlinx.multik.api.Engine: -------------------------------------------------------------------------------- 1 | org.jetbrains.kotlinx.multik.kotlin.KEEngine -------------------------------------------------------------------------------- /multik-kotlin/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik/kotlin/KEEngine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.kotlin 2 | 3 | import org.jetbrains.kotlinx.multik.api.KEEngineType 4 | import org.jetbrains.kotlinx.multik.api.engines 5 | 6 | 7 | @ExperimentalStdlibApi 8 | @Suppress("unused", "DEPRECATION") 9 | @EagerInitialization 10 | private val InitHook = EngineInitializer 11 | 12 | private object EngineInitializer { 13 | init { 14 | engines.value[KEEngineType] = KEEngine() 15 | } 16 | } -------------------------------------------------------------------------------- /multik-kotlin/src/wasmJsMain/kotlin/org/jetbrains/kotlinx/multik/kotlin/KEEngine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.kotlin 2 | 3 | import org.jetbrains.kotlinx.multik.api.KEEngineType 4 | import org.jetbrains.kotlinx.multik.api.enginesStore 5 | 6 | 7 | @ExperimentalStdlibApi 8 | @Suppress("unused", "DEPRECATION") 9 | @EagerInitialization 10 | public val initializer: EngineInitializer = EngineInitializer 11 | 12 | public object EngineInitializer { 13 | init { 14 | enginesStore[KEEngineType] = KEEngine() 15 | } 16 | } -------------------------------------------------------------------------------- /multik-openblas/cinterop/libmultik.def: -------------------------------------------------------------------------------- 1 | package = org.jetbrains.kotlinx.multik.cinterop 2 | 3 | staticLibraries = libopenblas.a 4 | libraryPaths = build/cmake-build/openblas-install/lib 5 | -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/cpp/ComplexDouble.cpp: -------------------------------------------------------------------------------- 1 | #include "jni.h" 2 | 3 | jobject newComplexDouble(JNIEnv *env, double re, double im) { 4 | jclass cls = env->FindClass("org/jetbrains/kotlinx/multik/ndarray/complex/ComplexDouble_jvmKt"); 5 | if (cls == nullptr) { 6 | // TODO(exception handling) 7 | } 8 | 9 | jmethodID methodID = env->GetStaticMethodID(cls, "ComplexDouble", "(DD)Lorg/jetbrains/kotlinx/multik/ndarray/complex/ComplexDouble;"); 10 | if (methodID == nullptr) { 11 | // TODO(exception handling) 12 | } 13 | 14 | jobject ret = env->CallStaticObjectMethod(cls, methodID, re, im); 15 | return ret; 16 | } 17 | -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/cpp/JniStat.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | #include "JniStat.h" 6 | #include "mk_stat.h" 7 | 8 | /* 9 | * Class: org_jetbrains_kotlinx_multik_openblas_stat_JniStat 10 | * Method: median 11 | * Signature: (Ljava/lang/Object;II)D 12 | */ 13 | JNIEXPORT jdouble JNICALL Java_org_jetbrains_kotlinx_multik_openblas_stat_JniStat_median 14 | (JNIEnv *env, jobject jobj, jobject jarr, jint size, jint type) { 15 | 16 | void *varr = env->GetPrimitiveArrayCritical((jarray)jarr, nullptr); 17 | 18 | double ret = array_median(varr, size, type); 19 | 20 | env->ReleasePrimitiveArrayCritical((jarray)jarr, varr, 0); 21 | 22 | return ret; 23 | } -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/cpp/mk_stat.cpp: -------------------------------------------------------------------------------- 1 | #include "mk_stat.h" 2 | #include "algorithm" 3 | 4 | double array_median(const void *arr, int size, int type) { 5 | double ret; 6 | int mid = size / 2; 7 | switch (type) { 8 | case 1: { 9 | auto *sorted = (int8_t *)arr; 10 | std::sort(sorted, sorted + size); 11 | if (size % 2 != 0) { 12 | ret = sorted[mid]; 13 | } else { 14 | ret = (static_cast(sorted[mid - 1]) + static_cast(sorted[mid])) / 2.0; 15 | } 16 | break; 17 | } 18 | case 2: { 19 | auto *sorted = (int16_t *)arr; 20 | std::sort(sorted, sorted + size); 21 | if (size % 2 != 0) { 22 | ret = sorted[mid]; 23 | } else { 24 | ret = (static_cast(sorted[mid - 1]) + static_cast(sorted[mid])) / 2.0; 25 | } 26 | break; 27 | } 28 | case 3: { 29 | auto *sorted = (int32_t *)arr; 30 | std::sort(sorted, sorted + size); 31 | if (size % 2 != 0) { 32 | ret = sorted[mid]; 33 | } else { 34 | ret = (static_cast(sorted[mid - 1]) + static_cast(sorted[mid])) / 2.0; 35 | } 36 | break; 37 | } 38 | case 4: { 39 | auto *sorted = (int64_t *)arr; 40 | std::sort(sorted, sorted + size); 41 | if (size % 2 != 0) { 42 | ret = sorted[mid]; 43 | } else { 44 | ret = (static_cast(sorted[mid - 1]) + static_cast(sorted[mid])) / 2.0; 45 | } 46 | break; 47 | } 48 | case 5: { 49 | auto *sorted = (float *)arr; 50 | std::sort(sorted, sorted + size); 51 | if (size % 2 != 0) { 52 | ret = sorted[mid]; 53 | } else { 54 | ret = (static_cast(sorted[mid - 1]) + static_cast(sorted[mid])) / 2.0; 55 | } 56 | break; 57 | } 58 | case 6: { 59 | auto *sorted = (double *)arr; 60 | std::sort(sorted, sorted + size); 61 | if (size % 2 != 0) { 62 | ret = sorted[mid]; 63 | } else { 64 | ret = (sorted[mid - 1] + sorted[mid]) / 2.0; 65 | } 66 | break; 67 | } 68 | default:break; 69 | } 70 | return ret; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/headers/ComplexDouble.h: -------------------------------------------------------------------------------- 1 | #ifndef MULTIK_MULTIK_OPENBLAS_MULTIK_JNI_SRC_MAIN_HEADERS_COMPLEXDOUBLE_H_ 2 | #define MULTIK_MULTIK_OPENBLAS_MULTIK_JNI_SRC_MAIN_HEADERS_COMPLEXDOUBLE_H_ 3 | 4 | #include "jni.h" 5 | 6 | jobject newComplexDouble(JNIEnv *, double, double); 7 | 8 | #endif //MULTIK_MULTIK_OPENBLAS_MULTIK_JNI_SRC_MAIN_HEADERS_COMPLEXDOUBLE_H_ 9 | -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/headers/JniStat.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class org_jetbrains_kotlinx_multik_openblas_stat_JniStat */ 4 | 5 | #ifndef _Included_org_jetbrains_kotlinx_multik_openblas_stat_JniStat 6 | #define _Included_org_jetbrains_kotlinx_multik_openblas_stat_JniStat 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: org_jetbrains_kotlinx_multik_openblas_stat_JniStat 12 | * Method: median 13 | * Signature: (Ljava/lang/Object;II)D 14 | */ 15 | JNIEXPORT jdouble JNICALL Java_org_jetbrains_kotlinx_multik_openblas_stat_JniStat_median 16 | (JNIEnv *, jobject, jobject, jint, jint); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | #endif 22 | -------------------------------------------------------------------------------- /multik-openblas/multik_jni/src/main/headers/mk_stat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | #include 6 | 7 | #ifndef MULTIK_JNI_MULTIK_JNI_SRC_MAIN_HEADERS_MK_STAT_H_ 8 | #define MULTIK_JNI_MULTIK_JNI_SRC_MAIN_HEADERS_MK_STAT_H_ 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | double array_median(const void *arr, int size, int type); 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | #endif //MULTIK_JNI_MULTIK_JNI_SRC_MAIN_HEADERS_MK_STAT_H_ 19 | -------------------------------------------------------------------------------- /multik-openblas/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/openblas/Loader.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas 6 | 7 | internal expect fun libLoader(name: String): Loader 8 | 9 | internal interface Loader { 10 | val isLoaded: Boolean 11 | 12 | fun load(): Boolean 13 | 14 | fun manualLoad(javaPath: String? = null): Boolean 15 | } 16 | -------------------------------------------------------------------------------- /multik-openblas/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/openblas/NativeEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas 6 | 7 | import org.jetbrains.kotlinx.multik.api.Engine 8 | import org.jetbrains.kotlinx.multik.api.EngineType 9 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 10 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 11 | import org.jetbrains.kotlinx.multik.api.math.Math 12 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 13 | import org.jetbrains.kotlinx.multik.openblas.linalg.NativeLinAlg 14 | import org.jetbrains.kotlinx.multik.openblas.math.NativeMath 15 | import org.jetbrains.kotlinx.multik.openblas.stat.NativeStatistics 16 | 17 | 18 | public open class NativeEngine : Engine() { 19 | 20 | override val name: String 21 | get() = type.name 22 | 23 | override val type: EngineType 24 | get() = NativeEngineType 25 | 26 | override fun getMath(): Math { 27 | return NativeMath 28 | } 29 | 30 | override fun getLinAlg(): LinAlg { 31 | return NativeLinAlg 32 | } 33 | 34 | override fun getStatistics(): Statistics { 35 | return NativeStatistics 36 | } 37 | } -------------------------------------------------------------------------------- /multik-openblas/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/openblas/linalg/NativeLinAlg.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas.linalg 6 | 7 | import org.jetbrains.kotlinx.multik.api.identity 8 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 9 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlgEx 10 | import org.jetbrains.kotlinx.multik.api.mk 11 | import org.jetbrains.kotlinx.multik.ndarray.data.D2 12 | import org.jetbrains.kotlinx.multik.ndarray.data.MultiArray 13 | import org.jetbrains.kotlinx.multik.ndarray.data.NDArray 14 | 15 | internal object NativeLinAlg : LinAlg { 16 | 17 | override val linAlgEx: LinAlgEx 18 | get() = NativeLinAlgEx 19 | 20 | override fun pow(mat: MultiArray, n: Int): NDArray { 21 | requireSquare(mat.shape) 22 | if (n == 0) return mk.identity(mat.shape[0], mat.dtype) 23 | return if (n % 2 == 0) { 24 | val tmp = pow(mat, n / 2) 25 | NativeLinAlgEx.dotMM(tmp, tmp) 26 | } else { 27 | NativeLinAlgEx.dotMM(mat, pow(mat, n - 1)) 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /multik-openblas/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/openblas/stat/JniStat.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas.stat 6 | 7 | 8 | internal expect object JniStat { 9 | fun median(arr: Any, size: Int, dtype: Int): Double 10 | } -------------------------------------------------------------------------------- /multik-openblas/src/commonMain/kotlin/org/jetbrains/kotlinx/multik/openblas/stat/NativeStatistics.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas.stat 6 | 7 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 8 | import org.jetbrains.kotlinx.multik.ndarray.data.* 9 | import org.jetbrains.kotlinx.multik.ndarray.operations.first 10 | import org.jetbrains.kotlinx.multik.ndarray.operations.times 11 | import org.jetbrains.kotlinx.multik.openblas.math.NativeMath 12 | 13 | internal object NativeStatistics : Statistics { 14 | 15 | override fun median(a: MultiArray): Double? { 16 | val size = a.size 17 | return when { 18 | size == 1 -> a.first().toDouble() 19 | size > 1 -> JniStat.median(a.deepCopy().data.data, size, a.dtype.nativeCode) 20 | else -> null 21 | } 22 | } 23 | 24 | override fun average(a: MultiArray, weights: MultiArray?): Double { 25 | if (weights == null) return mean(a) 26 | return NativeMath.sum(a * weights).toDouble() / NativeMath.sum(weights).toDouble() 27 | } 28 | 29 | override fun mean(a: MultiArray): Double { 30 | val ret = NativeMath.sum(a) 31 | return ret.toDouble() / a.size 32 | } 33 | 34 | override fun mean(a: MultiArray, axis: Int): NDArray { 35 | TODO("Not yet implemented") 36 | } 37 | 38 | override fun meanD2(a: MultiArray, axis: Int): NDArray { 39 | TODO("Not yet implemented") 40 | } 41 | 42 | override fun meanD3(a: MultiArray, axis: Int): NDArray { 43 | TODO("Not yet implemented") 44 | } 45 | 46 | override fun meanD4(a: MultiArray, axis: Int): NDArray { 47 | TODO("Not yet implemented") 48 | } 49 | 50 | override fun meanDN(a: MultiArray, axis: Int): NDArray { 51 | TODO("Not yet implemented") 52 | } 53 | } -------------------------------------------------------------------------------- /multik-openblas/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/openblas/math/NativeMathTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.openblas.math 2 | 3 | import org.jetbrains.kotlinx.multik.api.mk 4 | import org.jetbrains.kotlinx.multik.api.ndarray 5 | import org.jetbrains.kotlinx.multik.ndarray.data.D2Array 6 | import org.jetbrains.kotlinx.multik.openblas.DataStructure 7 | import org.jetbrains.kotlinx.multik.openblas.assertFloatingNDArray 8 | import org.jetbrains.kotlinx.multik.openblas.assertFloatingNumber 9 | import org.jetbrains.kotlinx.multik.openblas.libLoader 10 | import kotlin.test.BeforeTest 11 | import kotlin.test.Test 12 | import kotlin.test.assertEquals 13 | 14 | class NativeMathTest { 15 | 16 | private lateinit var data: DataStructure 17 | private lateinit var ndarray: D2Array 18 | 19 | @BeforeTest 20 | fun load() { 21 | libLoader("multik_jni").manualLoad() 22 | 23 | data = DataStructure(42) 24 | ndarray = data.getFloatM(2) 25 | } 26 | 27 | @Test 28 | fun argMaxTest() = assertEquals(3, NativeMath.argMax(ndarray)) 29 | 30 | @Test 31 | fun argMinTest() = assertEquals(0, NativeMath.argMin(ndarray)) 32 | 33 | @Test 34 | fun expTest() { 35 | val expected = mk.ndarray( 36 | mk[mk[1.2539709297612778, 1.499692498450485], 37 | mk[2.47182503414346, 2.647835581718662]] 38 | ) 39 | assertFloatingNDArray(expected, NativeMathEx.exp(ndarray)) 40 | } 41 | 42 | @Test 43 | fun logTest() { 44 | val expected = mk.ndarray( 45 | mk[mk[-1.4858262962985072, -0.9032262301885379], 46 | mk[-0.0998681176145879, -0.026608338154056003]] 47 | ) 48 | assertFloatingNDArray(expected, NativeMathEx.log(ndarray)) 49 | } 50 | 51 | @Test 52 | fun sinTest() { 53 | val expected = 54 | mk.ndarray(mk[mk[0.22438827641771292, 0.39425779276225403], mk[0.7863984442713585, 0.826995590204087]]) 55 | assertFloatingNDArray(expected, NativeMathEx.sin(ndarray)) 56 | } 57 | 58 | @Test 59 | fun cosTest() { 60 | val expected = 61 | mk.ndarray(mk[mk[0.9744998211422555, 0.9189998872939189], mk[0.6177195859349023, 0.5622084077839763]]) 62 | assertFloatingNDArray(expected, NativeMathEx.cos(ndarray)) 63 | } 64 | 65 | @Test 66 | fun maxTest() = assertEquals(0.97374254f, mk.math.max(ndarray)) 67 | 68 | @Test 69 | fun minTest() = assertEquals(0.22631526f, mk.math.min(ndarray)) 70 | 71 | @Test 72 | fun sumTest() = assertFloatingNumber(2.5102746f, mk.math.sum(ndarray)) 73 | } -------------------------------------------------------------------------------- /multik-openblas/src/commonTest/kotlin/org/jetbrains/kotlinx/multik/openblas/stat/NativeStatisticsTest.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.openblas.stat 2 | 3 | import org.jetbrains.kotlinx.multik.api.arange 4 | import org.jetbrains.kotlinx.multik.api.mk 5 | import org.jetbrains.kotlinx.multik.api.ndarray 6 | import org.jetbrains.kotlinx.multik.openblas.libLoader 7 | import kotlin.test.BeforeTest 8 | import kotlin.test.Test 9 | import kotlin.test.assertEquals 10 | 11 | class NativeStatisticsTest { 12 | 13 | @BeforeTest 14 | fun load() { 15 | libLoader("multik_jni").manualLoad() 16 | } 17 | 18 | @Test 19 | fun test_median() { 20 | val a = mk.ndarray(mk[mk[10, 7, 4], mk[3, 2, 1]]) 21 | assertEquals(3.5, mk.stat.median(a)) 22 | } 23 | 24 | @Test 25 | fun test_simple_average() { 26 | val a = mk.arange(1, 11, 1) 27 | assertEquals(mk.stat.mean(a), mk.stat.average(a)) 28 | } 29 | 30 | @Test 31 | fun test_average_with_weights() { 32 | val a = mk.arange(1, 11, 1) 33 | val weights = mk.arange(10, 0, -1) 34 | assertEquals(4.0, mk.stat.average(a, weights)) 35 | } 36 | 37 | @Test 38 | fun test_of_mean_function_on_a_flat_ndarray() { 39 | val ndarray = mk.ndarray(mk[1, 2, 3, 4]) 40 | assertEquals(2.5, NativeStatistics.mean(ndarray)) 41 | } 42 | 43 | @Test 44 | fun test_of_mean_function_on_a_2d_ndarray() { 45 | val ndarray = mk.ndarray(mk[mk[1, 2], mk[3, 4]]) 46 | assertEquals(2.5, NativeStatistics.mean(ndarray)) 47 | } 48 | } -------------------------------------------------------------------------------- /multik-openblas/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/openblas/JvmNativeEngine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas 6 | 7 | import org.jetbrains.kotlinx.multik.api.linalg.LinAlg 8 | import org.jetbrains.kotlinx.multik.api.math.Math 9 | import org.jetbrains.kotlinx.multik.api.stat.Statistics 10 | import org.jetbrains.kotlinx.multik.openblas.linalg.NativeLinAlg 11 | import org.jetbrains.kotlinx.multik.openblas.math.NativeMath 12 | import org.jetbrains.kotlinx.multik.openblas.stat.NativeStatistics 13 | 14 | public class JvmNativeEngine: NativeEngine() { 15 | private val loader: Loader by lazy { libLoader("multik_jni") } 16 | 17 | override fun getMath(): Math { 18 | if(!loader.isLoaded) loader.load() 19 | return NativeMath 20 | } 21 | 22 | override fun getLinAlg(): LinAlg { 23 | if(!loader.isLoaded) loader.load() 24 | return NativeLinAlg 25 | } 26 | 27 | override fun getStatistics(): Statistics { 28 | if(!loader.isLoaded) loader.load() 29 | return NativeStatistics 30 | } 31 | } -------------------------------------------------------------------------------- /multik-openblas/src/jvmMain/kotlin/org/jetbrains/kotlinx/multik/openblas/stat/JniStat.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas.stat 6 | 7 | internal actual object JniStat { 8 | actual external fun median(arr: Any, size: Int, dtype: Int): Double 9 | } -------------------------------------------------------------------------------- /multik-openblas/src/jvmMain/resources/META-INF/services/org.jetbrains.kotlinx.multik.api.Engine: -------------------------------------------------------------------------------- 1 | org.jetbrains.kotlinx.multik.openblas.JvmNativeEngine -------------------------------------------------------------------------------- /multik-openblas/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik/openblas/Loader.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.openblas 2 | 3 | internal actual fun libLoader(name: String): Loader = NativeLoader() 4 | 5 | internal class NativeLoader(override val isLoaded: Boolean = true) : Loader { 6 | override fun load(): Boolean = true 7 | 8 | override fun manualLoad(javaPath: String?): Boolean = true 9 | } -------------------------------------------------------------------------------- /multik-openblas/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik/openblas/NativeEngine.kt: -------------------------------------------------------------------------------- 1 | package org.jetbrains.kotlinx.multik.openblas 2 | 3 | import org.jetbrains.kotlinx.multik.api.NativeEngineType 4 | import org.jetbrains.kotlinx.multik.api.engines 5 | 6 | @ExperimentalStdlibApi 7 | @Suppress("unused", "DEPRECATION") 8 | @EagerInitialization 9 | private val InitHook = EngineInitializer 10 | 11 | private object EngineInitializer { 12 | init { 13 | engines.value[NativeEngineType] = NativeEngine() 14 | } 15 | } -------------------------------------------------------------------------------- /multik-openblas/src/nativeMain/kotlin/org.jetbrains.kotlinx.multik/openblas/stat/JniStat.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 | */ 4 | 5 | package org.jetbrains.kotlinx.multik.openblas.stat 6 | 7 | import kotlinx.cinterop.ExperimentalForeignApi 8 | import kotlinx.cinterop.addressOf 9 | import kotlinx.cinterop.usePinned 10 | import org.jetbrains.kotlinx.multik.cinterop.array_median 11 | 12 | @OptIn(ExperimentalForeignApi::class) 13 | internal actual object JniStat { 14 | actual fun median(arr: Any, size: Int, dtype: Int): Double = when (arr) { 15 | is DoubleArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 16 | is FloatArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 17 | is IntArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 18 | is LongArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 19 | is ByteArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 20 | is ShortArray -> arr.usePinned { array_median(it.addressOf(0), size, dtype) } 21 | else -> throw Exception("Only primitive arrays are supported for Kotlin/Native `median`") 22 | } 23 | } -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | } 5 | } 6 | 7 | rootProject.name = "multik" 8 | include( 9 | ":multik-core", 10 | ":multik-default", 11 | ":multik-kotlin", 12 | ":multik-openblas", 13 | ) 14 | --------------------------------------------------------------------------------