GSL Library usage Kotlin Native

Submitted by Dickens A S on Wed, 12/11/2019 - 13:52

GSL library can be used for advanced math operations and vector, stats, sort, permute, fitting, matrix, fourier transform, blas integration

This code demonstration a simple matrix addition and diagonal addition in kotlin using GSL

package plot

import platform.posix.*
import kotlinx.cinterop.*
import gsl.*

fun main() {

    var m1 = gsl_matrix_float_alloc(4,4)
    var m2 = gsl_matrix_float_alloc(4,4)
    
    gsl_matrix_float_set_zero(m1)
    gsl_matrix_float_set_zero(m2)
    
    gsl_matrix_float_set(m1, 0, 0, 1.0f)
    gsl_matrix_float_set(m2, 0, 0, 6.0f)
    
    gsl_matrix_float_add(m1,m2)
    
    gsl_matrix_float_add_diagonal(m2, 3.0)
    
    for(i in 0 until 4) {
        for(j in 0 until 4) {
            print("${gsl_matrix_float_get(m2,i.toULong(),j.toULong())} ")
        }
        println()
    }
    
    gsl_matrix_float_free(m1)
    gsl_matrix_float_free(m2)
    
    
}

Output

9.0 0.0 0.0 0.0 
0.0 3.0 0.0 0.0 
0.0 0.0 3.0 0.0 
0.0 0.0 0.0 3.0 

 

GitHubhttps://github.com/dickensas/kotlin-gradle-templates/tree/master/gsl-matrix

install MSYS2 with below library

pacman -S mingw-w64-x86_64-gsl

 

Add new comment