static void UmatExtract_f(svdObj_f *svd) { vsip_mview_f* B=svd->B; vsip_mview_f *U=svd->L; vsip_index i; vsip_length m = vsip_mgetcollength_f(B); vsip_length n = vsip_mgetrowlength_f(B); vsip_mview_f *Bs=svd->Bs; vsip_mview_f *Us=svd->Ls; vsip_vview_f *v; vsip_scalar_f t; v = col_sv_f(B,svd->bs,0); if (m > n){ i=n-1; col_sv_f(msv_f(B,Bs,i,i),v,0); t=vsip_vget_f(v,0); vsip_vput_f(v,0,1.0); houseProd_f(v,msv_f(U,Us,i,i)); vsip_vput_f(v,0,t); } for(i=n-2; i>0; i--){ col_sv_f(msv_f(B,Bs,i,i),v,0); t=vsip_vget_f(v,0); vsip_vput_f(v,0,1.0); houseProd_f(v,msv_f(U,Us,i,i)); vsip_vput_f(v,0,t); } col_sv_f(msv_f(B,Bs,0,0),v,0); t=vsip_vget_f(v,0); vsip_vput_f(v,0,1.0); houseProd_f(v,msv_f(U,Us,0,0)); vsip_vput_f(v,0,t); }
int main(int argc, char** argv){ int retval = 0; int i, Navg; param_obj param; kw_obj kw; ts_obj ts; vsip_mview_f *dtaIn, *gramOut; if(retval += param_read(argv[1],¶m)){ printf("Failed to read parameter file\n"); exit(-1); } param_log(¶m); kw_init(&kw, ¶m); ts_init(&ts, ¶m); /* simulate time series and beamform */ Navg = (int) param.Navg; dtaIn = ts_instance(&ts); gramOut = kw_instance(&kw); kw_zero(&kw); for(i=0; i<Navg; i++){ ts_zero(&ts); ts_sim_noise(&ts); ts_sim_nb(&ts); komega(&kw,dtaIn); } /* beamform done. Gram should be in gramOut */ /*************/ /* Massage gram data and save to file for plotting */ for(i=0; i<vsip_mgetrowlength_f(gramOut); i++) {/* move zero to middle */ vsip_vview_f *v = vsip_mcolview_f(gramOut, i); vsip_vfreqswap_f(v); vsip_vdestroy_f(v); } {/* massage the data for plot*/ vsip_scalar_f max = vsip_mmaxval_f(gramOut, NULL),min; vsip_scalar_f avg = vsip_mmeanval_f(gramOut); vsip_mclip_f(gramOut,0.0, max, avg/100000.0, max, gramOut); vsip_mlog10_f(gramOut,gramOut); min = -vsip_mminval_f(gramOut, NULL); vsip_smadd_f(min, gramOut, gramOut); max = vsip_mmaxval_f(gramOut, NULL); vsip_smmul_f(1.0/max, gramOut, gramOut); } { /* output data and plot with octave */ FILE *fptr = fopen("gramOut","w"); size_t size = vsip_mgetrowlength_f(gramOut) * vsip_mgetcollength_f(gramOut); vsip_scalar_f *out = (vsip_scalar_f*)malloc(size * sizeof(vsip_scalar_f)); vsip_mcopyto_user_f(gramOut, VSIP_COL, out); fwrite(out,size,sizeof(vsip_scalar_f),fptr); fclose(fptr); free(out); } /* cleanup */ kw_fin(&kw); ts_fin(&ts); param_free(¶m); return retval; }
void VU_mfillz_f(vsip_mview_f* Z) { vsip_length m = vsip_mgetcollength_f(Z); vsip_length n = vsip_mgetrowlength_f(Z); vsip_stride i,j; for(i=0; i<m; i++) for(j=0; j<n; j++) vsip_mput_f(Z,i,j,(vsip_scalar_f)1.0); return; }
void VU_mfill_f(vsip_mview_f *X, vsip_scalar_f a) { vsip_length RL = vsip_mgetrowlength_f(X); vsip_length CL = vsip_mgetcollength_f(X); vsip_length row,col; for(row=0; row<CL; row++) for(col=0; col<RL; col++) vsip_mput_f(X,row,col,a); return; }
svdObj_f *svd_f(vsip_mview_f *A) { svdObj_f *svd = svdInit_f(vsip_mgetcollength_f(A),vsip_mgetrowlength_f(A)); if(!svd){ printf("malloc failure for SVD"); exit(0);} vsip_mcopy_f_f(A,svd->B); svdBidiag_f(svd); svdIteration_f(svd); svdSort_f(svd); return svd; }
void VU_mprintgram_f(vsip_mview_f* M,char* fname) { vsip_length RL = vsip_mgetrowlength_f(M); vsip_length CL = vsip_mgetcollength_f(M); FILE *of = fopen(fname,"w"); vsip_length row,col; for(row = 0; row<CL; row++) for(col=0; col<RL; col++) fprintf(of,"%ld %ld %3.0f\n", row,col,vsip_mget_f(M,row,col)); fclose(of); return; }
PyObject *mcopyToListByCol_f(vsip_mview_f *v){ vsip_length M = vsip_mgetcollength_f(v); vsip_length N = vsip_mgetrowlength_f(v); PyObject *retval = PyList_New(M); vsip_index i,j; for(j=0; j<N; j++){ PyObject *col = PyList_New(M); for(i=0; i<M; i++){ double x = (double)vsip_mget_f(v,i,j); PyList_SetItem(col,i,PyFloat_FromDouble(x)); } PyList_SetItem(retval,j,col); } return retval; }
void VU_mprint_f(vsip_mview_f *X) { vsip_length RL = vsip_mgetrowlength_f(X); vsip_length CL = vsip_mgetcollength_f(X); vsip_length row,col; vsip_scalar_f x; printf("[\n"); for(row=0; row<CL; row++) { for(col=0; col<RL; col++) { x=vsip_mget_f(X,row,col); printf("%6.4f %s",x,((col==(RL-1)) ? ";" : " ")); } printf("\n"); } printf("];\n"); return; }
static void bidiag_f(svdObj_f *svd) { vsip_mview_f *B = svd->B; vsip_mview_f *Bs = svd->Bs; vsip_length m = vsip_mgetcollength_f(B); vsip_length n = vsip_mgetrowlength_f(B); vsip_vview_f *x=col_sv_f(B,svd->bs,0); vsip_vview_f *v=vclone_f(x,svd->t); vsip_vview_f *vs = svd->ts; vsip_index i,j; for(i=0; i<n-1; i++){ vsip_vputlength_f(v,m-i); vsip_vcopy_f_f(col_sv_f(msv_f(B,Bs,i,i),x,0),v); houseVector_f(v); vsip_svmul_f(1.0/vsip_vget_f(v,0),v,v); houseProd_f(v,Bs); vsip_vcopy_f_f(vsv_f(v,vs,1),vsv_f(x,x,1)); if(i < n-2){ j = i+1; vsip_vputlength_f(v,n-j); vsip_vcopy_f_f(row_sv_f(msv_f(B,Bs,i,j),x,0),v); houseVector_f(v); vsip_svmul_f(1.0/vsip_vget_f(v,0),v,v); prodHouse_f(Bs,v); vsip_vcopy_f_f(vsv_f(v,vs,1),vsv_f(x,x,1)); } } if(m > n){ i=n-1; vsip_vputlength_f(v,m-i); vsip_vcopy_f_f(col_sv_f(msv_f(B,Bs,i,i),x,0),v); houseVector_f(v); vsip_svmul_f(1.0/vsip_vget_f(v,0),v,v); houseProd_f(v,Bs); vsip_vcopy_f_f(vsv_f(v,vs,1),vsv_f(x,x,1)); } }
int main(){vsip_init((void*)0); { int solretval=0; vsip_scalar_vi i,j; vsip_mview_f *A = vsip_mcreate_f(M, N,VSIP_COL,0); vsip_mview_f *X = vsip_mcreate_f(M,NB,VSIP_ROW,0); /* Nullify the data-space */ for (i=0; i < vsip_mgetcollength_f(A); i++) for(j=0; j < vsip_mgetrowlength_f(A); j++) vsip_mput_f(A,i,j,(double)0); for (i=0; i < vsip_mgetcollength_f(X); i++) for(j=0; j < vsip_mgetrowlength_f(X); j++) vsip_mput_f(X,i,j,(double)0); /* Initialise matrix A */ for (i=0; i<M; i++) for (j = 0; j < N; j++) if(i == j) vsip_mput_f(A,i,j, (double)(M+1)); else vsip_mput_f(A,i,j, -1.0); { /* store data */ /* vsip_mput_f(A,0,0,1); vsip_mput_f(A,0,1,2); vsip_mput_f(A,0,2,1); */ /* vsip_mput_f(A,1,0,3); vsip_mput_f(A,1,1,-1); vsip_mput_f(A,1,2,0); */ /* vsip_mput_f(A,2,0,2); vsip_mput_f(A,2,1,1); vsip_mput_f(A,2,2,-1); */ /* vsip_mput_f(A,3,0,1); vsip_mput_f(A,3,1,2); vsip_mput_f(A,3,2,2); */ /* vsip_mput_f(X,0,0,1); */ /* vsip_mput_f(X,1,0,2); */ /* vsip_mput_f(X,2,0,2); */ /* vsip_mput_f(X,3,0,1); */ } {int i,j; printf("matrix\n A = [\n"); for(i=0; i<M; i++) { for(j=0; j< N; j++) printf("%9.2f%s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i==M-1) ? printf(";]\n") : printf(";\n") ; } } { int j, k; vsip_vview_f *y = NULL; vsip_vview_f *x; vsip_length L = NB; vsip_length p = M; for(k=0; k<L; k++) { x = vsip_mcolview_f(X,k); for (j=0; j<p; j++) { y = vsip_mrowview_f(A,j); vsip_vput_f(x,j,(double)(k+1)*(vsip_vsumval_f(y))); /* vsip_vput_f(x,j,(vsip_vsumval_f(y)));*/ vsip_vdestroy_f(y); } vsip_vdestroy_f(x); } } {int i,j; printf("rhs matrix\n B = [\n"); for(i=0; i<NN; i++) { for(j=0; j<NB; j++) printf("%9.2f%s",vsip_mget_f(X,i,j),(j == NB-1) ? "":","); (i==NN-1) ? printf(";]\n") : printf(";\n") ; } } {vsip_qr_f* qrAop = vsip_qrd_create_f(M,N, QOPT); if(qrAop == NULL) exit(1); {int i,j; if(QOPT == VSIP_QRD_SAVEQ1) { printf("qrd returns %i\n",vsip_qrd_f(qrAop,A)); printf("matrix A after factorisation: skinny Q explicitly\n Q1 = [\n"); for(i= 0; i< M ; i++) { for(j=0; j< N; j++) printf("%8.4f %s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i == M - 1) ? printf("]\n") : printf(";\n"); } } else if(QOPT == VSIP_QRD_SAVEQ || QOPT == VSIP_QRD_NOSAVEQ) { printf("qrd returns %i\n",vsip_qrd_f(qrAop,A)); printf("matrix A after fact.: R and "); (QOPT == VSIP_QRD_SAVEQ) ? printf("full Q implicitly\n Q/R = [\n") : printf("Q not saved -- ignore LT portion. \n R = [\n"); for(i= 0; i<M ; i++) { for(j=0; j< N; j++) printf("%9.5f %s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i == M - 1) ? printf("]\n") : printf(";\n"); } } } if( QPROB == VSIP_LLS) { if (QOPT == VSIP_QRD_SAVEQ1 || QOPT == VSIP_QRD_SAVEQ) { if((solretval=vsip_qrsol_f(qrAop, QPROB, X))) { printf("Warning -- Least Squares soln returns %i -- Check\n", solretval); printf("Upper triang. mat. R, possibly singular\n"); } else printf("Least Squares soln returns %i\n", solretval); } else { printf("Least Squares systems cannot be solved by the NOSAVEQ option --exiting\n"); exit(1); } } else { if((solretval=vsip_qrsol_f(qrAop,QPROB, X))) { printf("Warning -- Covariance soln returns %i -- Check\n",solretval); printf("Upper triang. mat. R, possibly singular\n"); } else printf("Covariance soln returns %i\n",solretval); } vsip_qrd_destroy_f(qrAop); } {int i,j; printf("Soln Matrix\n"); for(i=0; i<N; i++) { for(j=0; j<NB; j++) printf("%9.5f%s",vsip_mget_f(X,i,j),(j == NB-1) ? "":","); printf(";\n"); } } vsip_malldestroy_f(X); vsip_malldestroy_f(A); } vsip_finalize((void*)0); return 1; }
int main() { vsip_init((void*)0); { int i,j; vsip_mview_f *A = vsip_mcreate_f(M,N,VSIP_COL,0); vsip_mview_f *X = vsip_mcreate_f( (NN>= M)?NN:M, (NB>=M)?NB:M, VSIP_ROW,0); /* put the appropriate row, col lengths of X */ X = vsip_mputcollength_f(X,NN); X = vsip_mputrowlength_f(X,NB); /* Initialise matrix A */ for (i=0; i<M; i++) for (j = 0; j < N; j++) if(i == j) vsip_mput_f(A,i,j, (double)(M+1)); else vsip_mput_f(A,i,j, -1.0); { int i,j; printf("matrix\n A = [\n"); for(i=0; i<M; i++) { for(j=0; j< N; j++) printf("%9.2f%s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i == M-1)? printf("]\n") : printf(";\n"); } } { int j, k; vsip_vview_f *y = NULL; vsip_vview_f *x; vsip_length L = NB; for(k=0; k<L; k++) { x = vsip_mcolview_f(X,k); for (j=0; j<vsip_vgetlength_f(x); j++) { y = vsip_mrowview_f(A,j); vsip_vput_f(x,j,(double)(k+1)*(vsip_vsumval_f(y))); vsip_vdestroy_f(y); } vsip_vdestroy_f(x); } } { int i,j; printf("rhs matrix\n C = [\n"); for(i=0; i<NN; i++) { for(j=0; j<NB; j++) printf("%9.2f%s",vsip_mget_f(X,i,j),(j == NB-1) ? "":","); (i == NN - 1) ? printf("]\n") : printf(";\n"); } } { vsip_qr_f* qrAop = vsip_qrd_create_f(M,N, QOPT); if(qrAop == NULL) exit(1); { int i,j; if(QOPT == VSIP_QRD_SAVEQ1) { printf("qrd returns %i\n",vsip_qrd_f(qrAop,A)); printf("matrix A after factorisation: skinny Q explicitly\n Q1 = [\n"); for(i= 0; i< M ; i++) { for(j=0; j< N; j++) printf("%9.5f%s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i == M - 1) ? printf("]\n") : printf(";\n"); } } else if(QOPT == VSIP_QRD_SAVEQ) { printf("qrd returns %i\n",vsip_qrd_f(qrAop,A)); printf("matrix A after factorisation: R + full Q implicitly\n Q/R = [\n"); for(i= 0; i<M ; i++) { for(j=0; j< N; j++) printf("%9.2f%s",vsip_mget_f(A,i,j),(j == N-1) ? "":","); (i == M-1)? printf("]\n") : printf(";\n"); } } else if(QOPT == VSIP_QRD_NOSAVEQ) { printf("Q is not saved with this option. \n"); printf("Product with Q is invalid, exiting\n"); vsip_qrd_destroy_f(qrAop); vsip_malldestroy_f(X); vsip_malldestroy_f(A); exit(1); } if (opQ == VSIP_MAT_TRANS || opQ == VSIP_MAT_HERM) { if(apQ == VSIP_MAT_RSIDE) /* C * Q^t */ { printf(" This is a product of type C <- C * Q^t \n"); if(vsip_qrdprodq_f(qrAop,opQ,apQ,X)) { printf("Size not conformal or invalid operation by Q: -- exiting\n"); vsip_qrd_destroy_f(qrAop); vsip_malldestroy_f(X); vsip_malldestroy_f(A); exit(1); } else { X = vsip_mputrowlength_f(X,M); } } else if(apQ == VSIP_MAT_LSIDE) /* Q^t * C */ { if(QOPT == 1) printf(" This is a product of type C <- Q^t * C \n"); if(QOPT == 2) printf(" This is a product of type C <- Q_1^t * C \n"); if(vsip_qrdprodq_f(qrAop,opQ,apQ,X)) { printf("Size not conformal or invalid operation by Q: -- exiting\n"); vsip_qrd_destroy_f(qrAop); vsip_malldestroy_f(X); vsip_malldestroy_f(A); exit(1); } else { if(QOPT == 2) X = vsip_mputcollength_f(X,N); } } } else if (opQ == VSIP_MAT_NTRANS) { if(apQ == VSIP_MAT_RSIDE) /* C * Q */ { printf(" This is a product of type C <- C * Q \n"); if(vsip_qrdprodq_f(qrAop,opQ,apQ,X)) { printf("Size not conformal or invalid operation by Q: -- exiting\n"); vsip_qrd_destroy_f(qrAop); vsip_malldestroy_f(X); vsip_malldestroy_f(A); exit(1); } else { X = vsip_mputrowlength_f(X,N); } } else if(apQ == VSIP_MAT_LSIDE) /* Q * C */ { if(QOPT == 1) printf(" This is a product of type C <- Q * C \n"); if(QOPT == 2) printf(" This is a product of type C <- Q_1 * C \n"); if(vsip_qrdprodq_f(qrAop,opQ,apQ,X)) { printf("Size not conformal or invalid operation by Q: -- exiting\n"); vsip_qrd_destroy_f(qrAop); vsip_malldestroy_f(X); vsip_malldestroy_f(A); exit(1); } else { X = vsip_mputcollength_f(X,M); } } } } vsip_qrd_destroy_f(qrAop); } { int i,j; printf("Soln Matrix\n C = [\n"); for(i=0; i< vsip_mgetcollength_f(X); i++) { for(j=0; j< vsip_mgetrowlength_f(X); j++) printf("%8.4f%s",vsip_mget_f(X,i,j), (j == vsip_mgetrowlength_f(X)-1) ? "":","); (i == vsip_mgetcollength_f(X)-1) ? printf("]\n") : printf(";\n"); } } vsip_malldestroy_f(X); vsip_malldestroy_f(A); } vsip_finalize((void*)0); return 1; }