/* function [L] = myluinc(A,milu,no_steps) Simplified incomplete LU factorisation method, which preserves the sparsity pattern of A in the factorisation. milu is a flag which if set will implement modified ilu, in which discarded fill-in entries are subtracted from the diagonal to preserve the row sum. Written by George Beckett Thursday 31st May 2001. */ #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], \ int nrhs, const mxArray *prhs[]){ int no_rows, no_cols, no_nonzeros, milu=0, no_steps; int *pAi=NULL, *pAj=NULL, *pSrci=NULL, *pSrcj=NULL; int pivot_row, current_row, current_col; int diag_index, sub_index, rj_index, ij_index, ii_index; int k; double *pAr=NULL, *pSrcr=NULL; double d, e; /* Validate no. inputs and outputs. */ if(!nrhs || (nrhs>3)) mexErrMsgTxt("Invalid no. inputs."); else if(nlhs>1) mexErrMsgTxt("Invalid no. outputs."); /* Check that A is sparse, real, and has correct dimensions. */ if(mxIsComplex(prhs[0])) mexErrMsgTxt("Input A must be a real matrix."); if(!mxIsSparse(prhs[0])) mexErrMsgTxt("Input A must be a sparse matrix."); no_rows = mxGetM(prhs[0]); no_cols = mxGetN(prhs[0]); no_nonzeros = mxGetNzmax(prhs[0]); pSrci = mxGetIr(prhs[0]); pSrcj = mxGetJc(prhs[0]); pSrcr = mxGetPr(prhs[0]); if(no_rows>no_cols) mexErrMsgTxt("In input A, no. rows is greater than no. columns."); /* Check if the user has specified an optional second argument, i.e. milu. Default value is 0. */ if(nrhs>1){ if(!mxIsEmpty(prhs[1])){ if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || (mxGetN(prhs[1])*mxGetM(prhs[1]) != 1)) mexErrMsgTxt("Input milu must be scalar."); milu = (int) mxGetScalar(prhs[1]); } } /* Check to see if the user has specified an optional third argument. Default is norows-1. */ no_steps = no_rows-1; if(nrhs==3){ if(!mxIsEmpty(prhs[2])){ if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || (mxGetN(prhs[2])*mxGetM(prhs[2]) != 1)) mexErrMsgTxt("Input no_steps must be scalar."); no_steps = (int) mxGetScalar(prhs[2]); } } /* Create sparse output matrix, setup pointers, and copy input matrix into output. */ plhs[0] = mxCreateSparse(no_rows, no_cols, no_nonzeros, mxREAL); pAi = mxGetIr(plhs[0]); pAj = mxGetJc(plhs[0]); pAr = mxGetPr(plhs[0]); for(k=0; k