/* stable_rqc.c File to calculate the R factor of a structured Hankel matrix, recursively. The main focus is on memory savings - and not on speed. This is about a factor 0.6 to 0.9 of the speed of a Matlab rq factor. But it takes a lot less memory ! The file is used in conjunction with my_rqc, which is fast but not always numerically stable ... Syntax : R = stable_rqc(u,y,ii) Peter Van Overschee, June 8, 1994 */ #include #include "mex.h" /* Input Arguments */ #define u_IN prhs[0] #define y_IN prhs[1] #define ii_IN prhs[2] /* Output Arguments */ #define R_OUT plhs[0] #define max(A, B) ((A) > (B) ? (A) : (B)) #define min(A, B) ((A) < (B) ? (A) : (B)) #define my_abs(A) ((A) < (0) ? -(A) : (A)) /* Diagnostics Flag */ #define DIAG 0 static #ifdef __STDC__ void calcul_R( double u[], double y[], double R[], unsigned int m, unsigned int l, unsigned int nsamp, int ii ) #else calcul_R(u,y,R,m,l,nsamp,ii) double u[],y[],R[]; unsigned int m,l,nsamp; int ii; #endif { double *rr; double sumt,EPS; int k,k1,k2,k3,k4,ubasis,ybasis,mii,rrbasis1,rrbasis2,indR,Rbasis,n,j; double tol,t1,t2,piv,fra,cs,ss; /* Take the matlab epsilon */ EPS = 2e-16; /* There we go - follow the file stable_rqm.m */ ii=2*ii; n=(l+m)*ii; j=nsamp; tol = EPS*n; mii = m*ii; /* Allocate some space for a new row in R */ rr = (double *)calloc(n,sizeof(double)); for (k1 = 0; k1 < j-ii+1; k1++) { rrbasis1 = 0; rrbasis2 = mii; /* Make the matrix rr */ for (k2 = 0; k2 < ii; k2++) { ubasis = k1+k2; ybasis = ubasis; for (k4 = 0; k4 < m; k4++) { rr[rrbasis1] = u[ubasis]; rrbasis1++; ubasis += j; } for (k4 = 0; k4 < l; k4++) { rr[rrbasis2] = y[ybasis]; rrbasis2++; ybasis += j; } } /* Rotate this row into R */ for (k2 = 0; k2 < n; k2++) { indR = k2*n; piv = R[indR+k2]; if (my_abs(piv) < tol) { /* Check the current element of rr */ if (my_abs(rr[k2]) < tol) { /* Put rr[k2] = 0 and go on */ rr[k2] = 0; } else { /* Swap ! */ Rbasis = k2; for (k4 = 0; k4 < n; k4++) { sumt=R[Rbasis]; R[Rbasis]=rr[k4]; rr[k4]=sumt; Rbasis += n; } rr[k2] = 0; } } else { /* Now we have a decent pivot element */ /* Deetermine the rotation angle : */ fra = -rr[k2]/piv; cs = 1/sqrt(1+fra*fra); ss = cs*fra; /* Now swap and multiply */ Rbasis = indR + k2; for (k4 = k2; k4 < n; k4++) { t1 = R[Rbasis]; t2 = rr[k4]; R[Rbasis] = t1*cs - t2*ss; rr[k4] = t1*ss + t2*cs; Rbasis += n; } } rr[k2] = 0; } } free(rr); } #ifdef __STDC__ void mexFunction( int nlhs, Matrix *plhs[], int nrhs, Matrix *prhs[] ) #else mexFunction(nlhs, plhs, nrhs, prhs) int nlhs, nrhs; Matrix *plhs[], *prhs[]; #endif { double *R; double *u,*y; unsigned int l,m,nsamp_u,nsamp_y,du,n; double *iip; int ii; /* Check for proper number of arguments */ if (nrhs != 3) { mexErrMsgTxt("stable_rqc requires three input arguments."); } else if (nlhs > 1) { mexErrMsgTxt("stable_rqc requires maximum 1 output arguments."); } /* Check the dimensions of u */ m = mxGetN(u_IN); nsamp_u = mxGetM(u_IN); if (!mxIsNumeric(u_IN) || mxIsComplex(u_IN) || !mxIsFull(u_IN) || !mxIsDouble(u_IN)) { mexErrMsgTxt("my_rqc requires u to be a real vector."); } if (m > nsamp_u) { mexErrMsgTxt("Use u' to call my_rqc."); }; if (m == 0) { mexErrMsgTxt("No empty input sequence allowed."); } /* Check the dimensions of y */ l = mxGetN(y_IN); nsamp_y = mxGetM(y_IN); if (!mxIsNumeric(y_IN) || mxIsComplex(y_IN) || !mxIsFull(y_IN) || !mxIsDouble(y_IN)) { mexErrMsgTxt("my_rqc requires y to be a real vector."); } if (l > nsamp_y) { mexErrMsgTxt("Use y' to call my_rqc."); } if (l == 0) { mexErrMsgTxt("No empty output sequence allowed."); } if (nsamp_u != nsamp_y) { mexErrMsgTxt("u and y need to have equal length."); } /* Get the extra parameter ii */ iip=mxGetPr(ii_IN); ii=iip[0]; /* Create a matrix for the return argument */ n=2*ii*(m+l); R_OUT = mxCreateFull(n, n, REAL); /* Assign pointers to the various parameters */ R = mxGetPr(R_OUT); u = mxGetPr(u_IN); y = mxGetPr(y_IN); /* Do the actual computations in a subroutine */ calcul_R(u,y,R,m,l,nsamp_u,ii); return; }