Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
The simplest way is to define the function in the .tm file. –  Szabolcs May 10 '13 at 23:33
    
You mean templates? But aren't they used to call a C prog from Mathematica? Here I want the opposite –  Andry May 11 '13 at 7:51
    
Would you please show me an example? –  Andry May 11 '13 at 8:11
    
Yes, template files are used when calling C functions from Mathematica. You should make it clear in your question that you need the opposite. –  Szabolcs May 11 '13 at 13:27
1  
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. –  Szabolcs May 11 '13 at 13:55
show 4 more comments

1 Answer

up vote 3 down vote accepted

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);
share|improve this answer
    
I was reading that document last night so many times that I didn't recognize that section... thankyou for your concern and your answer! –  Andry May 11 '13 at 15:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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