tags:

views:

61

answers:

1

I'm trying to figure out how to use Mathematica from C via Mathlink.

If I just want to compute the value of a built-in function, I can do

        MLPutFunction( lp, "RiemannSiegelZ", 1L);
        MLPutDouble(lp, val);

and all is fine.

Now, if I want the value of the derivative, things get worse:

MLPutFunction( lp, "RiemannSiegelZ'", 1L);
MLPutDouble(lp, val);

does not work. I tried to insert the "D" function, but I couldn't seem to get it working. The best way I could find this far is this

char query[128];
sprintf(query, "N[RiemannSiegelZ'[%.20lf]]", val);
MLPutFunction(lp, "ToExpression", 1);
MLPutString(lp, query);

it does work, but it's extremely slow, probably because I'm asking Mathematica to parse the expression, instead of just calling the function ... Is there a better way?

+2  A: 

The full form of f'[x] in Mathematica is Derivative[1][f][x]. You need to use the lower-level MLPutNext for this, e.g.

MLPutNext(lp, MLTKFUNC);       // Derivative[1][Sin][_]
MLPutArgCount(lp, 1);          //                    1.23456
MLPutNext(lp, MLTKFUNC);       // Derivative[1][_]
MLPutArgCount(lp, 1);          //               Sin
MLPutNext(lp, MLTKFUNC);       // Derivative[_]
MLPutArgCount(lp, 1);          //            1
MLPutSymbol(lp, "Derivative");
MLPutInteger(lp, 1);
MLPutSymbol(lp, "Sin");
MLPutDouble(lp, 1.23456);
KennyTM
Awesome. It works, and it runs about 1000 times faster than "ToExpression". Thanks!