#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <limits>
#include <utility>
#include <assert.h>
#ifdef _CHAR16T //for matlab 2010a
#define CHAR16_T
#endif
#include "mex.h"
#include "matrix.h"
usingnamespace std;
some code
.
.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *pr, *pi;
int *pr2, *pi2;
mwIndex *ir, *jc;
mwIndex *ir2, *jc2;
/* Right hand side: input of the function */
if( nrhs!=3 ||
!mxIsNumeric(prhs[0]) ||
!mxIsNumeric(prhs[1]) ||
mxIsEmpty(prhs[0]) ||
mxIsEmpty(prhs[1]))
{
mexErrMsgTxt("Expects three real matrices, two vectors of the same size and one 2 x k or empty matrix");
}
const mxArray *d = prhs[0];
const mxArray *p = prhs[1];
const mxArray *match0 = prhs[2];
mwSize n = mxGetN(d);
int numW = ceil( double(n/2) );
int numU = n-numW;
mexPrintf("n = %d. numW = %d,numU = %d \n",n,numW,numU);
if (n != mxGetN(p)){
mexErrMsgTxt("The vector of distances and predecessors must have the same size");
}
more code ...
}
then I decide to check in the internet and create a small program to try a solution. The program worked fine. I tried using cygwin. It was too tedious to do a mexfunction for that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
usingnamespace std;
int main()
{
int n = 131;
int m = ceil(nextafter(double(n/2),n));
printf("%d /2 =%d",n,m);
return 0;
}
But when I change line 46 of my mexfunction with line 10 of my trial program and run it in matlab 2010a. I have this answer:
error C3861: 'nextafter': identifier not found
So, somehow math.h does not define nextafter, I know that math.h for C is different that (cmath) math.h of C++. I try to use #include <cmath.h> but then it said that could not find the file.
I really would appreciate any help with this issue.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <limits>
#include <utility>
#include <assert.h>
#ifdef _CHAR16T //for matlab 2010a
#define CHAR16_T
#endif
#include "mex.h"
#include "matrix.h"
#define PAD 0.49999 /* A hair below 1/2, to avoid roundoff errors */
usingnamespace std;
some code
.
.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *pr, *pi;
int *pr2, *pi2;
mwIndex *ir, *jc;
mwIndex *ir2, *jc2;
/* Right hand side: input of the function */
if( nrhs!=3 ||
!mxIsNumeric(prhs[0]) ||
!mxIsNumeric(prhs[1]) ||
mxIsEmpty(prhs[0]) ||
mxIsEmpty(prhs[1]))
{
mexErrMsgTxt("Expects three real matrices, two vectors of the same size and one 2 x k or empty matrix");
}
const mxArray *d = prhs[0];
const mxArray *p = prhs[1];
const mxArray *match0 = prhs[2];
mwSize n = mxGetN(d);
int numW = ceil( double(n/2) + PAD );
int numU = n-numW;
mexPrintf("n = %d. numW = %d,numU = %d \n",n,numW,numU);
if (n != mxGetN(p)){
mexErrMsgTxt("The vector of distances and predecessors must have the same size");
}
more code ...
}
The CPU is capable of performing over 4 billion computations a second but struggles with rounding.
Is nextafter( ) a function you defined, or is it part of another header module within the standard library? I can't see nextafter( ) defined anywhere else other than main( ).
But is not defined in C++. What I think, which is only an intuition, is that matlab compiler reads <math.h> as <cmath> in C++ and not as <math.h> in C. In cmath nextafter is not defined. That is why I got the error. But I do not know how to make it work using the nextafter function.
I never worked on matlab, so cannot comment. nextafter is included in c99 specification, so if your matlab version doesn't comply with c99, it probably will not be having that function.
you said you have cygwin, try that if it works there?
Matlab compiler is Visual studio 2010 express with SDK 7.1. I do not know how to compile a mexfunction in cygwin. In my trial example(see second code in my first post) nextafter works just fine in cygwin.
Now, when I used a constant to round the number instead of nextafter (my second post) somehow I got a segmentation violation from matlab and close the program :(
Thanks! Now the mexfile compile. But still keep throwing a segmentation violation from matlab and close the program. I am going to check the logic and the code. But thanks very much!!