/* solve_lsc.c File to calculate B and D Syntax : slp = solve_lsc(P,Q,Bp,Dp,cu,cy,n,i) Peter Van Overschee, June 7, 1994 */ #include #include "mex.h" /* Input Arguments */ /* Output Arguments */ #define slp_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)) static #ifdef __STDC__ void solve_fun( double P[], double Q[], double Bp[], double Dp[], int m, int l, int n, int ii, double slp[] ) #else solve_fun(P,Q,Bp,Dp,m,l,n,ii,slp) double P[],Q[],Bp[],Dp[],slp[]; int m,l,n,ii; #endif { double *R,*rr,*qq; int kc,kr,k1,k2,k3,k4,indR,ind3,nn2; int nn,basisn,basisl,basism,mii,d_basis,basis_kc,tel; double sumt,EPS,a,b,cs,ss,fra,RHS; /* Take the matlab epsilon */ EPS = 2e-16; /* There we go - follow the file solve_ls.m */ nn = m*(n+l)+1; R = (double *)calloc(nn*nn,sizeof(double)); rr = (double *)calloc(nn,sizeof(double)); mii = m*ii; d_basis = m*n; /* Outer Loop */ for (kc = 0; kc < 2*mii; kc++) { basis_kc = kc*mii; for (kr = 0; kr < (n+l); kr++) { /* Form a row out of R */ for (k1 = 0; k1 < m; k1++) { /* The B part */ for (k2 = 0; k2 < n; k2 ++) { basisn = kr + k2*(l+n); basism = basis_kc + k1; sumt = 0; for (k4 = 0; k4 < ii; k4 ++) { sumt += Bp[basisn]*Q[basism]; basisn += n*(l+n); basism += m; } rr[k1*n+k2] = sumt; } /* The D part */ for (k2 = 0; k2 < l; k2 ++) { basisl = kr + k2*(l+n); basism = basis_kc + k1; sumt = 0; for (k4 = 0; k4 < ii; k4 ++) { sumt += Dp[basisl]*Q[basism]; basisl += l*(l+n); basism += m; } rr[d_basis+k1*l+k2] = sumt; } } rr[nn-1] = P[kc*(l+n)+kr]; /* Rotate this row into R */ for (k3 = 0; k3 < nn; k3++) { /* Determine the rotation angle : */ ind3 = k3*nn; if (my_abs(R[ind3+k3]) < EPS) { cs = 0; ss = -1; } else { fra = -rr[k3]/R[ind3+k3]; cs = 1/sqrt(1+fra*fra); ss = cs*fra; } /* Update rr(k3:nn) and R(k3,k3:nn) */ indR = ind3 + k3; for (k4 = k3; k4 < nn; k4++) { a = R[indR]; b = rr[k4]; R[indR] = a*cs - b*ss; rr[k4] = a*ss + b*cs; indR += nn; } } } } /* for (k4=0; k4= 0; k1--) { /* Get the right hand side */ RHS = R[nn2+k1]; indR = (k1+1)*nn+k1; for (k2 = k1+1; k2 < nn-1; k2 ++) { RHS -= slp[k2]*R[indR]; indR += nn; } slp[k1] = RHS/R[k1*nn+k1]; } free(R); 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 *P,*Q,*Bp,*Dp,*slp; double *point; int l,m,n,ii; /* Check for proper number of arguments */ if (nrhs != 8) { mexErrMsgTxt("solve_lsc requires eight input arguments."); } else if (nlhs > 1) { mexErrMsgTxt("my_rqc requires maximum one output argument."); } /* Check the dimensions of u */ /* All dimensions are given as parameters */ /* We DON'T check these ! */ /* Since it is highly unlikely this will be */ /* Called from somewhere else then from n4sid.m */ /* Get the parameters */ point=mxGetPr(prhs[4]); m=point[0]; point=mxGetPr(prhs[5]); l=point[0]; point=mxGetPr(prhs[6]); n=point[0]; point=mxGetPr(prhs[7]); ii=point[0]; /* Create a matrix for the return argument */ slp_OUT = mxCreateFull((n+l)*m, 1, REAL); /* Assign pointers to the various parameters */ slp = mxGetPr(slp_OUT); P = mxGetPr(prhs[0]); Q = mxGetPr(prhs[1]); Bp = mxGetPr(prhs[2]); Dp = mxGetPr(prhs[3]); /* Do the actual computations in a subroutine */ solve_fun(P,Q,Bp,Dp,m,l,n,ii,slp); return; }