/* * The mex function runs a clustering coefficients problem. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int mrows, ncols; int n,nz; /* sparse matrix */ mwIndex *ia, *ja; /* output data */ double *ccfs; if (nrhs != 1) { mexErrMsgTxt("1 inputs required."); } /* The first input must be a sparse matrix. */ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if (mrows != ncols || !mxIsSparse(prhs[0])) { mexErrMsgTxt("Input must be a noncomplex square sparse matrix."); } n = mrows; /* Get the sparse matrix */ /* recall that we've transposed the matrix */ ja = mxGetIr(prhs[0]); ia = mxGetJc(prhs[0]); nz = ia[n]; plhs[0] = mxCreateDoubleMatrix(n,1,mxREAL); ccfs = mxGetPr(plhs[0]); #ifdef _DEBUG mexPrintf("clustering_coefficients..."); #endif clustering_coefficients(n, ja, ia, ccfs); #ifdef _DEBUG mexPrintf("done\n"); #endif #ifdef _DEBUG mexPrintf("return\n"); #endif }
/* * The mex function runs a clustering coefficients problem. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwIndex mrows, ncols; mwIndex n,nz; /* sparse matrix */ mwIndex *ia, *ja; double *a; /* output data */ double *ccfs; /* used to switch between algorithm types */ int weight_type; /* = 0 if unweighted, = 1 if a vector of weights, = 2 if given by the matrix */ int undirected; /* * The current calling pattern is * clustering_coefficients_mex(A,undirected,weight) * where weight = 0 to use the unweighted version * weight = 'matrix' to use the values in the matrix * weight = vector to use a vector of weights */ const mxArray* arg_matrix; const mxArray* arg_undirected; const mxArray* arg_weight; int required_arguments = 3; if (nrhs != required_arguments) { mexErrMsgIdAndTxt("matlab_bgl:invalidMexArgument", "the function requires %i arguments, not %i\n", required_arguments, nrhs); } arg_matrix = prhs[0]; arg_undirected = prhs[1]; arg_weight = prhs[2]; undirected = (int)load_scalar_arg(arg_undirected, 1); if (mxGetNumberOfElements(arg_weight) == 1) { /* make sure it is valid */ if ((int)mxGetScalar(arg_weight) != 0) { mexErrMsgIdAndTxt("matlab_bgl:invalidParameter", "unknown weight option %g\n", mxGetScalar(arg_weight)); } weight_type = 0; a = NULL; } else if (mxIsChar(arg_weight)) { weight_type = 2; a = mxGetPr(arg_matrix); } else if (mxIsDouble(arg_weight)) { weight_type = 1; a = mxGetPr(arg_weight); } else { mexErrMsgIdAndTxt("matlab_bgl:invalidParameter", "unrecognized weight option"); return; } /* The first input must be a sparse matrix. */ mrows = mxGetM(arg_matrix); ncols = mxGetN(arg_matrix); if (mrows != ncols || !mxIsSparse(arg_matrix) || ((!mxIsDouble(arg_matrix) || mxIsComplex(arg_matrix)) && (weight_type == 2)) ) { mexErrMsgTxt("Input must be a square sparse matrix."); } n = mrows; /* recall that we've transposed the matrix */ ja = mxGetIr(prhs[0]); ia = mxGetJc(prhs[0]); nz = ia[n]; /* check the reweighting array to make sure it is acceptable */ if (weight_type == 1 && mxGetNumberOfElements(arg_weight) < nz) { mexErrMsgIdAndTxt("matlab_bgl:invalidParameter", "the weight array must have length >= nnz(A)"); } plhs[0] = mxCreateDoubleMatrix(n,1,mxREAL); ccfs = mxGetPr(plhs[0]); #ifdef _DEBUG mexPrintf("clustering_coefficients..."); #endif if (weight_type == 0) { clustering_coefficients(n, ja, ia, ccfs, !undirected); } else if (undirected) { weighted_clustering_coefficients(n, ja, ia, a, ccfs); } else { directed_clustering_coefficients(n, ja, ia, a, ccfs); } #ifdef _DEBUG mexPrintf("done\n"); #endif #ifdef _DEBUG mexPrintf("return\n"); #endif }