3

In trying to translate the following C# code to F#, I'm struggling with the 'using' keyword. The following snippet is from the ILNumerics library. How to translate the following?

ILRetArray<double> ObjFuncBFGS2D(ILInArray<double> X) {
    using (ILScope.Enter(X)) {
        return X[0] * X[0] + X[1] * X[1] + 2; 
    }
} 

On a side note, what libraries do F# people tend to use for optimization? I've been using NLoptNet but with very strange convergence issues with routines that converge properly in Matlab and Julia and Python. So either the problem is with my F# translations (granted this is more likely) or the optimization libraries. This is what I'm hoping to pin down. Frankly I'm a bit surprised by the lack of numerical optimization material related to F# on the internet.

  • F# have `using` (and `use`) – Sehnsucht Aug 20 '16 at 20:11
  • 4
    If I remember rightly, `use/using` is the least of your problems. ILNumerics makes use of a lot of implicit conversions between `ILArray`, `ILInArray` and `ILRetArray` which don't create much of a hindrance in C# but require explicit conversions in F#. You will probably need to define a bunch of helper functions to convert between these in order to get anything done. – TheInnerLight Aug 20 '16 at 20:32
  • @TheInnerLight any recommendations re constrained/unconstrained optimization libraries in F#? – professor bigglesworth Aug 20 '16 at 22:52
  • 1
    I think F# is relatively new (it was added to VS2010, but really became "mainstream" at F#3.0, which was VS2012). So that's only four years out in the wild, hence for certain specific use cases there is indeed little available. That said, this is true for some other environments as well, R for example got out of the box multi-core support last year when MS purchased it. Pandas is fairly young too.. For optimization specifically, the libraries won't be written in F#, but in C++ (or C, and/or Fortran). The advantage of F# is that it allows you to wrap those for a much better user experience. – s952163 Aug 21 '16 at 01:07
  • Now to comment on the more specific parts, 1. if you can give examples where NLoptNet behaves strange in F# compared to C#/python/matlab that can be investigated. 2. [Accord.NET](http://accord-framework.net/docs/html/N_Accord_Math_Optimization.htm/) provides some optimization routines. There is also Extreme Optimization (which is proprietary like ILNumerics). I assume Math.Net lacked these features? – s952163 Aug 21 '16 at 01:12
  • Sorry for the serial comments... finally you can also drop out to `R` via the RProvider. But in the end anything with a .NET wrapper should be available. – s952163 Aug 21 '16 at 01:18

1 Answers1

8

The MSDN documentation on resource management in F# is relevant here. To auto-dispose, the usual ways in F# are:

  • use, which replaces let and disposes once the binding goes out of scope
  • using, a function that takes the resource to dispose when finished and a function to execute before disposal, which takes the disposable object as input.

The code with use might look like this:

let ObjFuncBFGS2D (X : ILInArray<double>) =
    use myEnteredScope = ILScope.Enter(X)
    X.[0] * X.[0] + X.[1] * X.[1] + 2

Or, with using, like this:

let ObjFuncBFGS2D (X : ILInArray<double>) =
    using (ILScope.Enter(X)) <| fun _ ->
        X.[0] * X.[0] + X.[1] * X.[1] + 2

I don't use ILNumerics and can't syntax check this, but I hope the idea is clear.

Vandroiy
  • 6,163
  • 1
  • 19
  • 28