| 1 |
|
|---|
| 2 | #define THISFUNCTION "VectorToSparse"
|
|---|
| 3 |
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 | #include <string.h> /* strcasecmp */
|
|---|
| 6 | #include <time.h> /* clock,time,difftime */
|
|---|
| 7 | #include "mex.h"
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | /* Input Arguments */
|
|---|
| 11 |
|
|---|
| 12 | #define IR_IN prhs[0]
|
|---|
| 13 | #define JC_IN prhs[1]
|
|---|
| 14 | #define PR_IN prhs[2]
|
|---|
| 15 | #define M_IN prhs[3]
|
|---|
| 16 | #define N_IN prhs[4]
|
|---|
| 17 |
|
|---|
| 18 | /* Output Arguments */
|
|---|
| 19 |
|
|---|
| 20 | #define A_OUT plhs[0]
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | void VectorToSparseUsage( void );
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | void mexFunction( int nlhs,
|
|---|
| 27 | mxArray *plhs[],
|
|---|
| 28 | int nrhs,
|
|---|
| 29 | const mxArray *prhs[] )
|
|---|
| 30 | {
|
|---|
| 31 | int min,nin;
|
|---|
| 32 | mwIndex *ir =NULL,*jc =NULL;
|
|---|
| 33 | double *ird=NULL,*jcd=NULL,*pr=NULL,*prd=NULL;
|
|---|
| 34 | int i,mrow;
|
|---|
| 35 |
|
|---|
| 36 | /* Check for proper number of arguments */
|
|---|
| 37 |
|
|---|
| 38 | if (nrhs == 0 && nlhs == 0) {
|
|---|
| 39 | VectorToSparseUsage();
|
|---|
| 40 | return;
|
|---|
| 41 | }
|
|---|
| 42 | else if (nrhs < 2 || nlhs != 1) {
|
|---|
| 43 | VectorToSparseUsage();
|
|---|
| 44 | mexErrMsgTxt(" ");
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /* Create matrices for the return arguments */
|
|---|
| 48 |
|
|---|
| 49 | if (!mxIsNumeric(IR_IN) || !mxIsNumeric(JC_IN)) {
|
|---|
| 50 | mexPrintf("%s -- Input matrices IR and JC must be numeric.\n",THISFUNCTION);
|
|---|
| 51 | mexErrMsgTxt(" ");
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | mrow = 0;
|
|---|
| 55 | ird = mxGetPr(IR_IN);
|
|---|
| 56 | for (i=0; i<mxGetM(IR_IN)*mxGetN(IR_IN); i++)
|
|---|
| 57 | if ((int)ird[i]+1 > mrow)
|
|---|
| 58 | mrow=(int)ird[i]+1;
|
|---|
| 59 |
|
|---|
| 60 | if (nrhs >= 4 && mxIsNumeric(M_IN) && !mxIsEmpty(M_IN))
|
|---|
| 61 | min = mxGetScalar(M_IN);
|
|---|
| 62 | else {
|
|---|
| 63 | min = mrow;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (mrow > min) {
|
|---|
| 67 | mexPrintf("%s -- Number of rows specified by M (%d) and IR (%d) is inconsistent.\n",
|
|---|
| 68 | THISFUNCTION,min,mrow);
|
|---|
| 69 | mexErrMsgTxt(" ");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (nrhs >= 5 && mxIsNumeric(N_IN) && !mxIsEmpty(N_IN))
|
|---|
| 73 | nin = mxGetScalar(N_IN);
|
|---|
| 74 | else
|
|---|
| 75 | nin = mxGetM(JC_IN)*mxGetN(JC_IN)-1;
|
|---|
| 76 |
|
|---|
| 77 | if (mxGetM(JC_IN)*mxGetN(JC_IN)-1 != nin) {
|
|---|
| 78 | mexPrintf("%s -- Number of columns specified by N (%d) and JC (%d) is inconsistent.\n",
|
|---|
| 79 | THISFUNCTION,nin,mxGetM(JC_IN)*mxGetN(JC_IN)-1);
|
|---|
| 80 | mexErrMsgTxt(" ");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | A_OUT = mxCreateSparse(min, nin, mxGetM(IR_IN)*mxGetN(IR_IN), mxREAL);
|
|---|
| 84 | if (mxGetM(IR_IN)*mxGetN(IR_IN)) {
|
|---|
| 85 | ird = mxGetPr(IR_IN);
|
|---|
| 86 | ir = mxGetIr(A_OUT);
|
|---|
| 87 | for (i=0; i<mxGetM(IR_IN)*mxGetN(IR_IN); i++)
|
|---|
| 88 | ir[i]=(mwIndex)ird[i];
|
|---|
| 89 | }
|
|---|
| 90 | if (mxGetM(JC_IN)*mxGetN(JC_IN)) {
|
|---|
| 91 | jcd = mxGetPr(JC_IN);
|
|---|
| 92 | jc = mxGetJc(A_OUT);
|
|---|
| 93 | for (i=0; i<mxGetM(JC_IN)*mxGetN(JC_IN); i++)
|
|---|
| 94 | jc[i]=(mwIndex)jcd[i];
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | if (nrhs >= 3 && mxIsNumeric(PR_IN) && !mxIsEmpty(PR_IN)) {
|
|---|
| 98 | if (mxGetM(PR_IN)*mxGetN(PR_IN) != mxGetM(IR_IN)*mxGetN(IR_IN)) {
|
|---|
| 99 | mexPrintf("%s -- Number of terms specified by IR (%d) and PR (%d) is inconsistent.\n",
|
|---|
| 100 | THISFUNCTION,mxGetM(IR_IN)*mxGetN(IR_IN),mxGetM(PR_IN)*mxGetN(PR_IN));
|
|---|
| 101 | mexErrMsgTxt(" ");
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (mxGetM(PR_IN)*mxGetN(PR_IN)) {
|
|---|
| 105 | prd = mxGetPr(PR_IN);
|
|---|
| 106 | pr = mxGetPr(A_OUT);
|
|---|
| 107 | for (i=0; i<mxGetM(PR_IN)*mxGetN(PR_IN); i++)
|
|---|
| 108 | pr[i]=prd[i];
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | else {
|
|---|
| 113 | mexPrintf("%s -- Populating sparse matrix terms with ones.\n",THISFUNCTION);
|
|---|
| 114 | mexWarnMsgTxt(" ");
|
|---|
| 115 |
|
|---|
| 116 | if (mxGetM(IR_IN)*mxGetN(IR_IN)) {
|
|---|
| 117 | pr = mxGetPr(A_OUT);
|
|---|
| 118 | for (i=0; i<mxGetM(IR_IN)*mxGetN(IR_IN); i++)
|
|---|
| 119 | pr[i]=1.;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | mexPrintf("%s -- Output matrix is of size %d by %d with %d non-zeroes.\n",
|
|---|
| 124 | THISFUNCTION,min,nin,mxGetM(IR_IN)*mxGetN(IR_IN));
|
|---|
| 125 |
|
|---|
| 126 | return;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void VectorToSparseUsage( void )
|
|---|
| 130 | {
|
|---|
| 131 |
|
|---|
| 132 | mexPrintf("\n");
|
|---|
| 133 | mexPrintf("Usage: [a]=VectorToSparse(ir,jc,pr,m,n);\n");
|
|---|
| 134 | mexPrintf("\n");
|
|---|
| 135 |
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|