You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributesSimilarity5.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "mex.h"
  2. void attributesSimilarity5(mxLogical* nodeAtts,
  3. mwSize numNodes, mwSize numAttrs, double threshold,
  4. double* idxNodesToKeep, mwSize numNodesToKeep,
  5. mwIndex* ir_out, mwIndex* jc_out, double* outSimAtts)
  6. {
  7. double total=0, common=0, affinity=0;
  8. int nnz = 0;
  9. int prevCol = -1;
  10. int currCol, currRow, i, j, k;
  11. int a;
  12. bool a1, a2;
  13. // printf("attributesSimilarity5: nnz = %d\n", nnz_in);
  14. printf("start attributesSimilarity5 ... \n");
  15. for (j=0; j<numNodesToKeep; j++){
  16. currCol = (int)idxNodesToKeep[j]-1;
  17. for (k=prevCol+1; k<=currCol; k++){
  18. jc_out[k] = nnz;
  19. }
  20. for (i=0; i<numNodesToKeep; i++) {
  21. currRow = (int)idxNodesToKeep[i]-1;
  22. affinity = 0;
  23. if (currCol == currRow){
  24. affinity = 1;
  25. } else {
  26. total = 0;
  27. common = 0;
  28. for (a=0; a<numAttrs; a++){
  29. a1 = (bool)nodeAtts[currCol*numAttrs+a];
  30. a2 = (bool)nodeAtts[currRow*numAttrs+a];
  31. if (a1 || a2) {total++;}
  32. if (a1 && a2) {common++;}
  33. }
  34. if (total > 0) affinity=common/total;
  35. }
  36. if (affinity > threshold) {
  37. // printf("attributesSimilarity5 [j=%d,i=%d][col=%d,row=%d]\n",j,i,currCol,currRow);
  38. outSimAtts[nnz] = affinity;
  39. ir_out[nnz] = currRow;
  40. nnz++;
  41. if (nnz % 10000000 == 0)
  42. printf("attributesSimilarity5 nnz=%d M\n", nnz/1000000);
  43. }
  44. }
  45. prevCol = currCol;
  46. }
  47. for (k=prevCol+1; k<=numNodes; k++){
  48. jc_out[k] = nnz;
  49. }
  50. printf("end attributesSimilarity5 => nnz=%d\n", nnz);
  51. // mxDestroyArray(va1_P);
  52. // mxDestroyArray(va2_P);
  53. // mxDestroyArray(vaC_P);
  54. // mxDestroyArray(vaC_P);
  55. }
  56. /* The gateway function
  57. //this function accepts four parameters:
  58. //1. an attributes matrix
  59. //2. an array with idxNodesToKeep
  60. //3. min threshold
  61. */
  62. void mexFunction( int nlhs, mxArray *plhs[],
  63. int nrhs, const mxArray *prhs[])
  64. {
  65. /* variable declarations here */
  66. mwSize numNodes, numAttrs;
  67. mxLogical* nodeAtts;
  68. double* idxNodesToKeep;
  69. double threshold;
  70. mwSize numNodesToKeep, estimateNNZ;
  71. double percentSparse = 0.25; //0.33;
  72. // ouput
  73. mwIndex* ir_out;
  74. mwIndex* jc_out;
  75. double* outSimAtts;
  76. printf("nlhs = %d, nrhs = %d\n", nlhs, nrhs);
  77. /* Check for the correct number of outputs */
  78. if(nlhs != 1){
  79. mexErrMsgIdAndTxt( "MATLAB:attributesSimilarity:wrongrhs",
  80. "Wrong number of output arguments.");
  81. }
  82. /* Check for the correct number of inputs */
  83. if(nrhs != 4){
  84. mexErrMsgIdAndTxt( "MATLAB:attributesSimilarity:wrongrhs",
  85. "Wrong number of input arguments.");
  86. }
  87. nodeAtts = mxGetLogicals(prhs[0]); /* pointer to first input */
  88. numAttrs = mxGetM(prhs[0]); // rows
  89. numNodes = mxGetN(prhs[0]); // cols
  90. idxNodesToKeep = mxGetPr(prhs[1]); /* pointer to second input matrix */
  91. numNodesToKeep = mxGetM(prhs[1]);
  92. threshold = mxGetScalar(prhs[2]); /* pointer to thrid input matrix */
  93. percentSparse = mxGetScalar(prhs[3]); /* pointer to fourth input matrix */
  94. // *** estimateNNZ as part of population
  95. estimateNNZ = numNodesToKeep*numNodesToKeep*percentSparse;
  96. printf("nodeAtts dimensions: numNodes=%d, numAttrs=%d\n", numNodes, numAttrs);
  97. printf("params: threshold = %f, percentSparse = %f, numNodesToKeep= %d, estNNZ = %d\n", threshold, percentSparse, numNodesToKeep, estimateNNZ);
  98. // prepare output: nxn
  99. // allocate memeory according to estimateNNZ
  100. plhs[0] = mxCreateSparse(numNodes, numNodes, estimateNNZ, mxREAL);
  101. outSimAtts = mxGetPr(plhs[0]);
  102. ir_out = mxGetIr(plhs[0]);
  103. jc_out = mxGetJc(plhs[0]);
  104. // calculate the affinity
  105. attributesSimilarity5(nodeAtts, numNodes, numAttrs, threshold, idxNodesToKeep, numNodesToKeep, ir_out, jc_out, outSimAtts);
  106. }