just sharing/archiving.

fortran wrapper: 
    
    
    ! lapdog.so
    ! f95 -shared -o lapdog.so types.o constants.o utils.o lapack.o linalg.o 
lapdog.o -llapack
    module lapdog
      use linalg, only: solve ! https://github.com/certik/fortran-utils
      use iso_c_binding, only: c_int, c_double
      implicit none
    contains
      subroutine solver(n,a,b,x) bind(c)
        integer(c_int), intent(in)  :: n
        real(c_double), intent(in)  :: a(n,n)
        real(c_double), intent(in)  :: b(n)
        real(c_double), intent(out) :: x(n)
        x = solve(a,b)
      end subroutine solver
    end module lapdog
    

nim test program: 
    
    
    # test.nim
    # nim c test.nim
    const
      N = 3
    
    type
      m1 = array[1..N, float64]
      m2 = array[1..N, m1]
    
    proc solver(n: ptr int; a: m2; b: m1; x: var m1)
      {. dynlib: "./lapdog.so", importc .}
    
    when isMainModule:
    # solve for x, y, z where:
    # 1) 1x + 1y − 1z =  4
    # 2) 1x − 2y + 3z = −6
    # 3) 2x + 3y + 1z =  7
      
      var
        n: int = N
        A: m2
        B: m1
        X: m1
      
      A[1][1] = 1
      A[1][2] = 1
      A[1][3] = 2
      A[2][1] = 1
      A[2][2] = -2
      A[2][3] = 3
      A[3][1] = -1
      A[3][2] = 3
      A[3][3] = 1
      B[1] = 4
      B[2] = -6
      B[3] = 7
    
    solver(n.addr(),A,B,X)
    
    for i in 1..N:
      echo X[i]
    

Reply via email to