0
$\begingroup$

I am trying to use MMA generated function and call it form C. The function is (MMA env):

w0 = 9.;

ManNorm[L1_, mf_, \[Lambda]_, mg_, \[Phi]_, si_, thk_, x_] :=ClebschGordan[{1/2, si}, {L1, mf}, {5/2, si + mf}] (I^(mf - 2 mg)) Exp[-I (mf - mg) \[Phi]] BesselJ[-mf + mg, 2*Pi*Sin[thk]*x/0.729]*WignerD[{L1, mf, \[Lambda]}, thk]*Exp[-(x/0.729/w0)^2];
b2sub[L_, M_, Si_, \[Phi]_, th_, thk_, nf_, polar_, x_] = 2 Abs[Exp[I M \[Phi]r] Sum[
   Sum[Sum[WignerD[{1/2, Si, sip}, 
       th] WignerD[{5/2, Si + M, si + mf}, 
       th] (polar ManNorm[2, mf, 1, L + 1, \[Phi], si, thk, 
          x] - (1 - polar) ManNorm[2, mf, -1, L - 1, \[Phi], si, 
          thk, x]) nf, {mf, -2, 2, 1}], {si, -1/2, 1/2, 
     1}], {sip, -1/2, 1/2, 1}]] /. {\[Phi]r -> 0};

Export["b2sub.c", Compile[{x, \[Phi]1, th1, polar1}, b2sub[0, 1, 1/2, \[Phi]1, th1, 0.065, 110, polar1, x]]]

This is the code generated by MMA

#include "math.h"
#include <stdio.h>
#include <stdlib.h>

#include "/Applications/Mathematica.app/Contents/SystemFiles/IncludeFiles/C/WolframRTL.h"

static WolframLibraryData libData=0;

static WolframCompileLibrary_Functions funStructCompile;

static void * E0 = 0;


static mint I0_0;

static mint I0_1;

static mint I0_2;

static mint I0_3;

static mreal R0_6;

static mbool initialize = 1;

#include "b2sub.h"

DLLEXPORT int Initialize_b2sub(WolframLibraryData libData)
{
if( initialize)
{
funStructCompile = libData->compileLibraryFunctions;
I0_0 = (mint) 0;
I0_3 = (mint) 110;
R0_6 = (mreal) 0.065;
I0_2 = (mint) 2;
I0_1 = (mint) 1;
{
E0 = funStructCompile->getExpressionFunctionPointer(libData,"Hold[b2sub]");
}
if( E0 == 0)
{
return LIBRARY_FUNCTION_ERROR;
}
initialize = 0;
}
return 0;
}

DLLEXPORT void Uninitialize_b2sub(WolframLibraryData libData)
{
if( !initialize)
{
initialize = 1;
}
}

DLLEXPORT int b2sub(WolframLibraryData libData, mreal A1, mreal A2, mreal A3, mreal A4, mreal *Res)
{
mreal R0_0;
mreal R0_1;
mreal R0_2;
mreal R0_3;
mreal R0_4;
mreal R0_5;
int err = 0;
R0_0 = A1;
R0_1 = A2;
R0_2 = A3;
R0_3 = A4;
R0_4 = (mreal) I0_2;
R0_5 = 1 / R0_4;
{
int S0[9];
void * S1[9];
S0[0] = 2;
S1[0] = (void*) (&I0_0);
S0[1] = 2;
S1[1] = (void*) (&I0_1);
S0[2] = 3;
S1[2] = (void*) (&R0_5);
S0[3] = 3;
S1[3] = (void*) (&R0_1);
S0[4] = 3;
S1[4] = (void*) (&R0_2);
S0[5] = 3;
S1[5] = (void*) (&R0_6);
S0[6] = 2;
S1[6] = (void*) (&I0_3);
S0[7] = 3;
S1[7] = (void*) (&R0_3);
S0[8] = 3;
S1[8] = (void*) (&R0_0);
err = funStructCompile->evaluateFunctionExpression(libData, E0, 0, 0, 9, S0, S1, 3, 0, (void*) (&R0_4));
if( err)
{
goto error_label;
}
}
*Res = R0_4;
error_label:
funStructCompile->WolframLibraryData_cleanUp(libData, 1);
return err;
}

int main()
{
mreal *Res=0;

b2sub(libData, 1, 2, 2, 0., Res);
printf("halo pips!\n");

return 0;
}

And the header "b2sub.h"

#include "/Applications/Mathematica.app/Contents/SystemFiles/IncludeFiles/C/WolframLibrary.h"

EXTERN_C DLLEXPORT int Initialize_b2sub(WolframLibraryData libData);

EXTERN_C DLLEXPORT void Uninitialize_b2sub(WolframLibraryData libData);

EXTERN_C DLLEXPORT int b2sub(WolframLibraryData libData, mreal A1, mreal A2, mreal A3, mreal A4, mreal *Res);

I have added main function to it because I want to eventually manipulate it in c and supply the set of new 4 parameters A1,A2,A3 and A4 back in MMA. I have also added the line "static WolframLibraryData libData=0;" in the source code. I actually need some help with that as well. I have read the documentation on LibraryData and found some manuals on how to extract a function from C-code with its help. However, how do I call it in C? What goes into libData?

I work from terminal, gcc. The latest result was that the code did compile, but execution produces segmentation fault. Is there a way to fix it?

Thanks a lot!

$\endgroup$
  • 2
    $\begingroup$ Please include the steps you used to generate this code. $\endgroup$ – Szabolcs Nov 1 '17 at 7:51
  • $\begingroup$ Please give a complete example, and include it in the original post, not a comment. Make sure nothing required to reproduce the problem is missing (such as certain definitions). $\endgroup$ – Szabolcs Nov 1 '17 at 15:51
  • $\begingroup$ Aren't they defined in MMA libraries? Isn't it C though? I have never used C++, sorry. I know they are similar... To be honest, I don't see my function at all! It is supposed to be the sum of many terms containing Bessel functions. I don't see it. Is it hidden somewhere? $\endgroup$ – MsTais Nov 1 '17 at 18:16
  • 1
    $\begingroup$ There's also your Res pointer, which again is set to 0 (null pointer), but then you pass it to the b2sub function which dereferences it. You should have mreal Res, not mreal *Res, and pass &Res, not Res. How familiar are you with C programming? $\endgroup$ – Szabolcs Nov 1 '17 at 19:16
  • 1
    $\begingroup$ Thanks! It's fine. I guess it is not worth a trouble. I wrote it myself using gsl library... $\endgroup$ – MsTais Nov 3 '17 at 0:29

Your Answer

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

Browse other questions tagged or ask your own question.