# Converting built-in Mathematica symbol to C [closed]

I am trying to convert my Mathematica code to C since it can sometimes takes the Mathematica code quite a while to execute since the arrival times lists are hundreds of thousands of counts long, and am looking to reduce computational time, but I am a little rusty on my C. I see that there is "CForm" but I don't really understand it, or how it might be used. (For reference, I am using a Windows computer, and use MobaXterm.)

When I originally wrote my Mathematica code, I used an example data set to make sure that I could get everything running smoothly, however I used BinLists and BinCounts when writing the code, and am not entirely sure how to convert these built-in Mathematica symbols to C code.

The sample data I used to originally make the Mathematica code was:

dat = {0,1,3,9,11,13,14,15,19,20};


I wanted to have each bin be a constant width, currently set at 5, so the number of bins was found using:

numBins = (Max[dat] - Min[dat])/width


To get my intervals, I wrote:

intervals = Table[0,2*numBins];
intervals[] = min;
For[i = 2, i < 2*numBins, i+=2, intervals[[i]] = intervals[[i-1]] + width; intervals[[i+1]] = intervals[[i]]]
intervals[[2*numBins]] = intervals[[2*numBins - 1]] + width;


which gets me:

{0, 5, 5, 10, 10, 15, 15, 20}


which is exactly what I'm looking for. Now, I know that for the bins, I should get:

0-5, 5-10, 10-15, 15-20 (intervals)

0    9     11     19    (elements in bins)
1          13     20
3          14
15

3    1     4      2     (number of elements in each bin)


What I originally wrote, which gets me what I want, was:

intvalOG = Table[0, numBins + 1];
intvalOG[] = min;
For[i = 2, i < numBins + 1, i++, intvalOG[[i]] = intvalOG[[i - 1]] + width]
intvalOG[[numBins + 1]] = intvalOG[[numBins]] + \[CapitalDelta]t;

intvalINC = Table[intvalOG[[i]] + 1, {i, 2, numBins + 1}];
intval = Prepend[intvalINC, min];

elements = BinLists[dat, {intval}]
gn = BinCounts[dat, {intval}]


This gives me a list where gn contains the values:

{3, 1, 4, 2}


Now, I have everything coded in C up to when I try to get my elements and gn arrays, and I am stumped in how to convert those over from Mathematica to C. I have a minmax function, that finds the min and max values of my data,

double minmax(const double *arr, size_t length){
size_t i;
min = arr;
for(i = 1; i < length; i++){
if(min > arr[i]){min = arr[i];}
if(maxOG < arr[i]){maxOG = arr[i];}}
return 0;}


and all the Tables were converted straight to for loops, and indices were changed a bit to account for starting at 0 rather than with 1:

int main(){
minmax(dat,datLength);
numBins = ceil((maxOG-min)/width);

double intervals;
intervals = min;

for(int j = 1; j < 2*numBins; j+=2){
intervals[j] = intervals[j-1] + width;
intervals[j+1] = intervals[j];}

double tn;
int count = 0;
for(int j = 0; j < 2*numBins; j+=2){
tn[count] = (intervals[j]+intervals[j+1])/2;
count++;}

double intvalOG;
intvalOG = min;
for(int j = 1; j < numBins + 1; j++){
intvalOG[j] = intvalOG[j-1] + width;}


My attempt at rewritting it looked like:

double gn;
int count = 0;

for(int i =0; i < datLength; i++){
for(int j = 0; j < 5; j++){
if(dat[i] <= intvalOG[j]){
count++;}
gn[j] = count;}
count = 0;}


but all this ended up printing out was:

{0.000000 0.000000 0.000000 0.000000 1.000000 }


so clearly I'm doing something wrong here, I'm guessing with the count.

tl;dr: Any advice for how to convert BinLists and BinCounts into C code would be appreciated. If there is a more appropriate stackexchange to post this to, please feel free to comment.

## closed as off-topic by Szabolcs, yode, C. E., Kuba♦Nov 20 '18 at 12:00

• The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

• For questions regarding writing C code, Stack Overflow may work better. – user202729 Nov 20 '18 at 5:57
• I'm voting to close this question as off-topic because it is about how to implement histogramming in C, i.e. unrelated to Mathematica. – Szabolcs Nov 20 '18 at 9:09
• Okay! I was wondering if there was a better place to put this as stated in the original problem, so thank you for informing me. – Jomy Blue Nov 20 '18 at 14:29
• Take a look at this, maybe you will find it helpful: web.archive.org/web/20140420075011/https://… – Szabolcs Nov 20 '18 at 16:33
• There is also this: mathematica.stackexchange.com/a/96395/12 A BinCounts alternative implemented in C++, and usable from Mathematica. – Szabolcs Nov 20 '18 at 16:38

dat = {0, 1, 3, 9, 11, 13, 14, 15, 19, 20};