Convert C # using a keyword in F #

When trying to translate the following C # code to F #, I struggle 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 the other hand, which libraries F # people tend to use for optimization? I use NLoptNet, but with very strange convergence issues with routines that converge correctly in Matlab, Julia, and Python. So the problem is with my F # translations (if this is more likely) or with the optimization libraries. This is what I hope to attach. Honestly, I'm a little surprised at the lack of numerical optimization material related to F # on the Internet.

+4
source share
1 answer

The MSDN documentation for resource management in F # is important here . To automatically delete the usual methods in F #:

  • usethat replaces letand is placed after the binding goes out of scope
  • using, a function that transfers a resource to its disposal when it is finished, and a function that is performed before deletion, which takes a one-time object as input.

Code with usemay look like this:

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

Or, s using, like this:

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

I do not use ILNumerics and cannot syntax check this, but I hope the idea is clear.

+8
source

Source: https://habr.com/ru/post/1651899/


All Articles