A thought about Math libraries in Nim. I tried GSL library mentioned above, it 
works well.

The one thing is, as it turned out, the C-wrappers needs to be written 
manually, as usage and concepts in Nim and C are quite different. So you can't 
just auto-import or auto-generate these things.

As example, the GSL library provides Normal Distribution Function:
    
    
    proc gsl_cdf_gaussian_P*(x: cdouble; sigma: cdouble): cdouble {.cdecl, 
importc, dynlib: libgsl.}
    
    
    Run

But in order to be usable in Nim it should be rewritten into Nim like code. The 
good side is that it's easy to do, you just wrap it in a little bit of high 
level interfacing code.
    
    
    type Normal* = object
      mu:    float
      sigma: float
    
    proc cdf*(self: Normal): proc (x: float): float =
      return proc (x: float): float =
        gsl_cdf_gaussian_P(x - self.mu, self.sigma)
    
    test "Normal.cdf":
      assert Normal(mu: 0.0, sigma: 1.0).cdf()(0) =~ 0.5
    
    
    Run

I think a possible option to keep Nim Math ecosystem consistent is to mimic 
Julia math libraries, function names, usage patterns etc. As Nim and Julia are 
conceptually are very similar languages.

Reply via email to