I am trying to call Mathematica 10 from F#. Following the C# sample code found in the Mathematica directory, I have written the following F# code:
type TestClass() =
member this.AddNumsWithWM (m : int) (n : int) : int =
let ml = MathLinkFactory.CreateKernelLink()
ml.WaitAndDiscardAnswer()
ml.PutFunction("EvaluatePacket", 1)
ml.PutFunction("testFunc", 2) // IF BUILT-IN FUNCTION LIKE Plus, THEN NO ERROR
ml.Put(m)
ml.Put(n)
ml.EndPacket()
ml.WaitForAnswer() |> ignore
let res = ml.GetInteger() // THE ERROR OCCURS HERE FOR CUSTOM FUNCTION
ml.Close()
res
The Mathematica function testFunc
is simply:
testFunc[m_Integer, n_Integer] := m + 2 * n;
But when I call the function AddNumsWithWM 2 3
, I get the following error at the line indicated above:
Wolfram.NETLink.MathLinkException was unhandled
Message: An unhandled exception of type 'Wolfram.NETLink.MathLinkException' occurred in Wolfram.NETLink.dll
Additional information: Error code: 3.
I don't understand, because the sample C#, from which this has been translated literally, works. Any idea why I would get the above error?
Thanks in advance for your help.
EDIT
I have verified something else. If I use a built-in function like Plus
instead of my custom function testFunc
, the code works.
So, then, I guess my question would be, Why does the code not work for a custom function? I would be very grateful for pointers on what I need to do differently in order to call a custom function.
I have edited the question accordingly.