7
$\begingroup$

I'm performing derivatives in Mathematica and I want to use them in a C code. For that, I'm using CForm as for example in this expression:

f = (1 + Exp[-(Sqrt[x*x + M*M] - mu)/T])

CForm[D[f, M]]

what I get is:

-((Power(E,(mu - Sqrt(Power(M,2) + Power(x,2)))/T)*M)/
 (T*Sqrt(Power(M,2) + Power(x,2))))

How can I get Mathematica to print pow() instead of Power, sqrt instead of Sqrt and most important, to print exp() instead of Power(E,##). Thanks in advance!

$\endgroup$
  • 1
    $\begingroup$ I would also like to add to this: why does CForm print C functions in this way? $\endgroup$ – QuantumDot May 2 '16 at 22:15
4
$\begingroup$
ClearAll[foo]
foo = RawBoxes[Replace[ToBoxes@#, InterpretationBox[a_, b_, c___] :>
      With[{aa = StringReplace[a, 
            { "Sqrt" -> "sqrt", "Power(E," -> "exp(", "Power" -> "pow"}]}, aa], 
      {0, Infinity}]] &;

foo@CForm[D[f, M]]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ This can also be used e.g. to replace Power(E by np.exp( to export expressions to python's numpy etc. $\endgroup$ – Andre Holzner Jun 12 '18 at 7:41
2
$\begingroup$

I would unprotect Power and use Format:

Unprotect[Power];
Format[Power[E, a_], CForm] := exp[a]
Format[Power[a_, 1/2], CForm] := sqrt[a]
Format[Power[a_, b_], CForm] := pow[a, b]
Protect[Power];

Then:

D[f, M] //CForm

-((exp((mu - sqrt(pow(M,2) + pow(x,2)))/T)*M)/(T*sqrt(pow(M,2) + pow(x,2))))

$\endgroup$
1
$\begingroup$

I've you're compiling with C, then you can #include the mdefs.h that shipped with your version of Mathematica.

You can find it by using:

FileNames["*",FileNameJoin[{$InstallationDirectory,"SystemFiles","IncludeFiles","C"}]]

{C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\dllexport.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\extern.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\mdefs.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframCompileLibrary.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframImageLibrary.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframIOLibraryFunctions.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframLibrary.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframRawArrayLibrary.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframRTL.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframSparseLibrary.h,C:\Program Files\Wolfram Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\WolframStreamsLibrary.h}

On my system, it is the 3rd entry in the preceding list:

mdefs = FileNames["*",FileNameJoin[{$InstallationDirectory,"SystemFiles","IncludeFiles","C"}]][[3]]

"C:\Program Files\Wolfram \ Research\Mathematica\11.2\SystemFiles\IncludeFiles\C\mdefs.h"

You can look at the file with:

FilePrint[mdefs]

Output:

/*************************************************************************

                        Mathematica source file

        Copyright 1986 through 1999 by Wolfram Research Inc.


*************************************************************************/

/* C language definitions for use with Mathematica output */
#define Power(x, y) (pow((double)(x), (double)(y)))
#define Sqrt(x)     (sqrt((double)(x)))

#define Abs(x)      (fabs((double)(x)))

#define Exp(x)      (exp((double)(x)))
#define Log(x)      (log((double)(x)))
#define Sin(x)      (sin((double)(x)))
#define Cos(x)      (cos((double)(x)))
#define Tan(x)      (tan((double)(x)))

#define ArcSin(x)       (asin((double)(x)))
#define ArcCos(x)       (acos((double)(x)))
#define ArcTan(x)       (atan((double)(x)))

#define Sinh(x)          (sinh((double)(x)))
#define Cosh(x)          (cosh((double)(x)))
#define Tanh(x)          (tanh((double)(x)))


#define E       2.71828182845904523536029
#define Pi      3.14159265358979323846264
#define Degree      0.01745329251994329576924

/** Could add definitions for Random(), SeedRandom(), etc. **/

These #define statements will allow you to using the direct output of CForm.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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