My guess would be to write funcgrad like this

1
2
3
let funcgrad (x : float[]) (f : Ref<float>) (g : Ref<float[]>)=
  f := f' (x.[0], x.[1])
  g := [| g1 (x.[0], x.[1]); g2 (x.[0], x.[1]) |]

Then you should be able to pass funcgrad as the delegate parameter to the C# function. But I haven't tried this so maybe not. I don't know why it would demand an "explicit lambda", but doing what Robert says would presumably satisfy that. Post the call site you are using for lbfgsminimize if you still have problems.

By on 8/14/2008 8:22 AM ()

Thanks for quick replies. Both gave me some valuable lessons even though I've not come up with a solution yet. So I'll try to give you some more information from where you might help me to locate the problem.

In C# I have a class lbfgs, among other things it include this delegate

1
public delegate void func (double[] x, ref double f, ref double[] g);

The delegate is then used like this in lbfgsminimize

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void lbfgsminimize(int n,
	        int m,
	        ref double[] x,
	        double epsg,
	        double epsf,
	        double epsx,
	        int maxits,
	        ref int info, func funcgrad)
   	        {
	        ...
			funcgrad(x, ref f, ref g)  //thus calculating f and g which gneverov gave me the basics for
			...
			}

So I figured I define funcgrad as gneverov proposed, which work just fine

1
2
3
let funcgrad (x : float[]) (f : Ref<float>) (g : Ref<float[]>)=
  f := f' (x.[0], x.[1])
  g := [| g1 (x.[0], x.[1]); g2 (x.[0], x.[1]) |

</code> and the minimizition as I think Rob proposed <code lang=fsharp> let x = ref [||] let info = ref 0 let eps = 0.000000001 lbfgsminimize(2,2,x,eps,eps,eps,100,info,(fun (x : float[]) (f : Ref<float>) (g : Ref<float[]>) -> (f := f' (x.[0], x.[1]) g := [| g1 (x.[0], x.[1]); g2 (x.[0], x.[1]) |))) </code> but also <code lang=fsharp> lbfgsminimize(2,2,x,eps,eps,eps,0,info,optimization.lbfgs.func(funcgrad)) </code> and lbfgsminimize(2,2,x,eps,eps,eps,0,info,funcgrad) but every one of these gives the same error message. error FS0191: This function value is being used to construct a delegate type whose signature includes a byref argument. You must use an explicit lambda expresion taking 3 arguments. Hopefully I'm making some simple mistake. But otherwise if someone has the time and interest in trying to help me out further I'll gladly supply more information, even the complete code if necessary. Fredrik

By on 8/14/2008 3:58 PM ()

This was compiling for me agaist a C# stub:

1
2
3
4
5
6
7
let x = ref [||]
let info = ref 0
do Testlbfgsminimize.Class1.lbfgsminimize(10, 10, x, 0.01, 0.01, 
                                          0.01, 100, info, 
                                          (fun x f g -> 
                                            f <- 1.0;
                                            g <- [| 1.0; 1.0|]))

It apears the values the delegate receives are mutable rather than reference cells, so you need to set them using <- rather than :=. Don't know why this is, it feels a little incosistent with the function parameters as these are converted into ref values. You can see the delegates parameters are mutable using the tooltips that appear in visual studio.

Cheers,
Rob

By on 8/14/2008 9:51 PM ()

Thank you Rob,

that did the trick!

and also thank you "gneverov" for supplying the structure for writing the gradient function which I hadn't figured out by my self!

This is truly a fine day!

Best regards

Fredrik

By on 8/15/2008 2:32 AM ()

Hmm, this is probably because the delegate your using has a slightly bizar design, it's quite unsual for them to contain reference parameters. I think the error message is trying to tell you need to use a lambda funtion rather than explicitly trying to create an instance of the delegate.

It's difficult to say with out access to library your using or a copy of the F# code your using, but I'd try something like:

1
2
3
let x = ref [||]
let info = ref 0
do lbfgsminimize(10, 10, x, 0.01, 0.01, 0.01, 100, info, (fun x f g -> ()))

Cheers,
Rob

By on 8/14/2008 12:46 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper