1
$\begingroup$

In this tutorial some MathLink concepts are introduced. At page 33 in the last lines the author is talking about sending to MathLink a function definition like F[x_] := x^2.

In C/C++ (Unix) I want to to do exactly like that! Which function should I use to put a definition? MLPutFunction? MLPutSymbol?

$\endgroup$
9
  • $\begingroup$ The simplest way is to define the function in the .tm file. $\endgroup$
    – Szabolcs
    May 10, 2013 at 23:33
  • $\begingroup$ You mean templates? But aren't they used to call a C prog from Mathematica? Here I want the opposite $\endgroup$
    – Andry
    May 11, 2013 at 7:51
  • $\begingroup$ Would you please show me an example? $\endgroup$
    – Andry
    May 11, 2013 at 8:11
  • $\begingroup$ Yes, template files are used when calling C functions from Mathematica. You should make it clear in your question that you need the opposite. $\endgroup$
    – Szabolcs
    May 11, 2013 at 13:27
  • 1
    $\begingroup$ Looking at your other MathLink questions: why don't you just drive your C program from Mathematica? It would let you write the Mathematica part directly in Mathematica and would save you a lot of trouble. Your C program can still call back to Mathematica when it needs to. I suspect your design is needlessly complicated. Also regarding your other question: you can just send commands as strings when that is actually simpler and the whole command is known beforehand. $\endgroup$
    – Szabolcs
    May 11, 2013 at 13:55

1 Answer 1

3
$\begingroup$

Section 2.3.1 of the document you linked details how you can send things to the kernel.

The main point here is that you can send a function definition like you send any other Mathematica input. I doesn't matter that it's a function definition. It's just an expression like any other. If you send as an expression, remember its full form: SetDelayed[F[Pattern[x, Blank[]]], Power[x, 2]]

You can send things to the kernel for evaluation in two ways: either as a string or as an expression. Sending as a string is going to be far easier if you know beforehand what you want to send.

There are two ways to send something as string (shown in section 2.3.1 again):

MLPutFunction(link, "EvaluatePacket", 1);
MLPutFunction(link, "ToExpression", 1);
MLPutString(link, "...");
MLEndPacket(link);

or more directly:

MLPutFunction(link, "EnterTextPacket", 1);
MLPutString(link, "...");
MLEndPacket(link);
$\endgroup$
1
  • $\begingroup$ I was reading that document last night so many times that I didn't recognize that section... thankyou for your concern and your answer! $\endgroup$
    – Andry
    May 11, 2013 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.