CForm not getting exp(), pow, log() functions
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]]
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))))
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
.