Message Boards Message Boards

Compile a function and produce C code version of it?

Posted 5 years ago

I have a function that works well in Mathematica. I am trying to figure out how to compile this function and produce the C code for the entire function. I have read the documentation and can see how to do it with simple functions but I cannot figure out how to include a function that has several lines that are dependent on the results from the previous lines. For example, my Mathematica code is:

{xcorrs1, xcorrs2} = 
 CanonicalWarpingCorrespondence[s1, s2, 
  Automatic, {"SlantedBand", band}, 
  Method -> {"MatchingInterval" -> Automatic}, 
  DistanceFunction -> distfunc]; d1 = Part[s1, xcorrs1]; d2 = 
 Part[s2, xcorrs2]; dist = 
 CanonicalWarpingDistance[s1, s2, Automatic, {"SlantedBand", band}, 
  Method -> {"MatchingInterval" -> Automatic}, 
  DistanceFunction -> distfunc]; outtopp = {dist, d1, d2}

I will provide the following variables to the function: distfunc that will be the Distance measure to use (string), s1 that will be a single-dimensional array of numerical values, s2 that will be a single-dimensional array of numerical values and band that will be an integer representing the desired band width to use for the canonical time warping

The function will return a string that will consist of the concatenated output from dist, d1 and d2.

Ultimately I want to incorporate this into a dll to use with vb.net in Visual Studio but at the moment I just need the C code. Any suggestions would be greatly appreciated.

Sample input is in the attached file.

The output using the example input should be the following string: {7160.47, {41., 47.903, 33., -5., -12.677, -20., -11., -18.876, -11., -20., -28.646, -21., \ -15., -23.738, -15., -21., -25.161, -25.161, -25.161, -25.161, -16., \ -1., 2.826, 7., 7., 7., 7., 12.736, 12.736, 12.736, 12.736, 12.736, 12.736, 12.736, 12.736, 12.736, 9., 9., 9., 14., 14., 14., 14., 11.417, 6., -14., -24.368, -22., -19., -28.863, -19., -21., \ -33.117, -23., -24., -35.696, -24., -24., -26.076, -14., 6., 6., 6., 6., 6., 6., 3.654, 6., -14., -9.964, -8., 24., 28.462, 24., -8., -14.461, -19.}, {9., 12.309, 2., 2., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 11., 25.179, 22., 34., 40.08, 26., -3., -7.628, -13., -6., -10.917, -6., -13., -22.032, -17., \ -17., -25.736, -17., -17., -21.37, -13., -4., -8.272, -4., -13., \ -21.37, -17., -17., -17.535, -9., 7., 15.273, 15.273, 15.273, 15.273, 15.273, 15.273, 16., 16., 16., 16., 16., 16., 15.273, 7., -9., -17.535, -17., -17., -21.37, -13., -4., -4.568, 0., 0., 0., -2., -6.551, -6., -13., -22.032, -17.}}

This is the notebook where I am currently running the function.

Attachments:
POSTED BY: Jamie Dixson
2 Replies

Jamie,

You can easily turn your code into a multiline function:

myfunction[s1_, s2_, distfunc_, band_] := 
 Module[{xcorrs1, xcorrs2, d1, d2, dist}, {xcorrs1, xcorrs2} = 
   CanonicalWarpingCorrespondence[s1, s2, 
    Automatic, {"SlantedBand", band}, 
    Method -> {"MatchingInterval" -> Automatic}, 
    DistanceFunction -> distfunc]; d1 = Part[s1, xcorrs1]; 
  d2 = Part[s2, xcorrs2]; 
  dist = CanonicalWarpingDistance[s1, s2, 
    Automatic, {"SlantedBand", band}, 
    Method -> {"MatchingInterval" -> Automatic}, 
    DistanceFunction -> distfunc]; {dist, d1, d2}]

However, this will not help you compile it. The compiler will not support functions such as CanonicalWarpingDistance and CanonicalWarpingCorrespondence. In Version 12 they announced that they will enable compiling of functions like this, however, I have no idea when they will add support for those two functions. At the Wolfram conference 2018 they announced that you will be able to do this. I would ask technical support for a timetable (and a V12 release date). I assume that you would need to have your C code either link to a MMA library or communicate with a running kernel to do built-in functions like that.

One other option is to recode those two routines using elemental functions and then compile it with the new compiler when it is released. The existing compiler is very limited.

Regards,

Neil

POSTED BY: Neil Singer
Posted 5 years ago

Thank you Neil. I was afraid that might be the case. At one point I was successful in compiling the function after turning it into a module similar to what you have shown but when I tried to compile it, there was an error that said that it was successfully compiled but that it would produce some error if I tried to run it outside of Mathematica. I gave up on trying to compile as a module at that point. I will check with tech support on the release date for V12 and if CWC and CWD will be include in the initial release or when they will be added. Thank you again.

POSTED BY: Jamie Dixson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract