static int maximum_var() { int i,k; // char x[LLENGTH]; // double eps,a; i=matrix_load(word[1],&rr,&m,&k,&rlab,&clab,&lr,&lc,&type,expr); if (i<0) return(-1); if (k!=m) { sprintf(sbuf,"\n%s not a square matrix!",word[1]); sur_print(sbuf); WAIT; return(-1); } if(!mat_symmetric(rr,m)) { sprintf(sbuf,"\nMatrix %s is not symmetric!",word[1]); sur_print(sbuf); WAIT; return(-1); } rr11=(double *)muste_malloc(m*m*sizeof(double)); if (rr11==NULL) { not_mem(); return(-1); } p=(int *)muste_malloc(m*sizeof(int)); if (p==NULL) { not_mem(); return(-1); } p_max=(int *)muste_malloc(m*sizeof(int)); if (p_max==NULL) { not_mem(); return(-1); } var1=(int *)muste_malloc(m*sizeof(int)); if (var1==NULL) { not_mem(); return(-1); } tr2=(double *)muste_malloc(m*sizeof(double)); if (tr2==NULL) { not_mem(); return(-1); } max_var(); return(1); }
static int load_X(char *nimi) { int i; i=matrix_load(nimi,&X,&mX,&nX,&rlabX,&clabX,&lrX,&lcX,&typeX,exprX); return(i); }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: %s infile outfile {o|m} [res_pic.png] [intermid_pic.png\n"); return 2; } int err; int is_in_fmc = (strstr(argv[1], ".mtx") != NULL); int is_out_fmc = (strstr(argv[2], ".mtx") != NULL); int type = (argv[3][0] == 'o') ? ALG_ORIG : ALG_MULT; TMatrix_DCSR _A, *A = &_A; TMatrix_DCSR _B, *B = &_B; TWGraph _gr, *gr = &_gr; if (is_in_fmc) err = matrix_load_fmc(A, argv[1]); else err = matrix_load(A, argv[1]); if (err != ERROR_NO_ERROR) PRINT_ERROR_MESSAGE_AND_EXIT(err); int size = A->size; int *xadj, *adjncy; int *perm, *invp; if (!perm || !invp) PRINT_ERROR_MESSAGE_AND_EXIT(ERROR_MEMORY_ALLOCATION); switch (type) { case ALG_ORIG: do_orig(A, &xadj, &adjncy); break; case ALG_MULT: do_mult(A, &xadj, &adjncy); break; } TWGraph igr = { NULL, adjncy, xadj, NULL, size, xadj[size] }; tnd_perm(&igr, &perm, &invp); SAFE(build_graph(gr, A)); SAFE(graph_reorder(gr, perm, invp)); SAFE(build_matrix(gr, B, 1)); if (argc > 4) matrix_portrait(B, argv[4], 0, 0, NULL); if (is_out_fmc) matrix_save_fmc(B, argv[2]); else matrix_save(B, argv[2]); free(xadj); free(adjncy); if (argc > 5) { switch (type) { case ALG_ORIG: do_orig(B, &xadj, &adjncy); break; case ALG_MULT: do_mult(B, &xadj, &adjncy); break; } igr = (TWGraph){ NULL, adjncy, xadj, NULL, size, 0 }; graph_portrait(&igr, argv[5]); } return 0; }
void muste_linco(char *argv) { int i; // RS 5.5.2014 Variable init // SURVO_DATA d; A=NULL; rlab=NULL; clab=NULL; rdim=cdim=lr=lc=type=0; expr[0]=EOS; matname[0]=EOS; pros=0; var=NULL; y=NULL; outvar=NULL; lag=NULL; act=0; // if (argc==1) return; s_init(argv); if (g<3) { sur_print("\nUsage: LINCO <SURVO_data>,<matrix_of_coefficients>"); WAIT; return; } i=data_open2(word[1],&d,1,0,0); if (i<0) return; /* tilaa uusille muuttujille */ i=spec_init(r1+r-1); if (i<0) return; pros=0; act='A'; i=spfind("ACT"); if (i>=0) act=*spb[i]; i=conditions(&d); if (i<0) return; /* permitted only once */ i=matparam(); if (i<0) return; i=matrix_load(matname,&A,&rdim,&cdim,&rlab,&clab,&lr,&lc,&type,expr); if (i<0) return; i=varaa_tilat(); if (i<0) return; i=find_variables(); if (i<0) { if (etu==2) { sprintf(tut_info,"___@%d@LINCO@Error in LINCO@",-i); s_end(argv[1]); return; } return; } linear_combinations(); data_close(&d); s_end(argv); }
/* mvarp.c 7.7.1995/SM (23.7.1995) */ static int maximum_var_perm() /* exhaustive solution */ { int i,k; // char *p; char x[LLENGTH]; // double eps,a; i=matrix_load(word[1],&rr,&m,&k,&rlab,&clab,&lr,&lc,&type,expr); if (i<0) return(-1); if (k!=m) { sprintf(sbuf,"\n%s not a square matrix!",word[1]); sur_print(sbuf); WAIT; return(-1); } if(!mat_symmetric(rr,m)) { sprintf(sbuf,"\nMatrix %s is not symmetric!",word[1]); sur_print(sbuf); WAIT; return(-1); } rr11=(double *)muste_malloc(m*m*sizeof(double)); if (rr11==NULL) { not_mem(); return(-1); } rr22=(double *)muste_malloc(m*m*sizeof(double)); if (rr22==NULL) { not_mem(); return(-1); } perm1=(int *)muste_malloc(m*sizeof(int)); if (perm1==NULL) { not_mem(); return(-1); } perm2=(int *)muste_malloc(m*sizeof(int)); if (perm2==NULL) { not_mem(); return(-1); } perm_max=(int *)muste_malloc(m*sizeof(int)); if (perm_max==NULL) { not_mem(); return(-1); } strcpy(x,etmpd); strcat(x,"SURVO.TMP"); f=muste_fopen(x,"wt"); max_varp(rr,m); return(1); }
/**************** main(argc,argv) int argc; char *argv[]; *******************/ void muste_facta(char *argv) { int i,j,h; unsigned int ui; double da,db; char x[LLENGTH]; double sumlogsii; // extern double cdf_chi2(); char acc[32]; // if (argc==1) return; s_init(argv); if (g<3) { init_remarks(); rem_pr("Usage: FACTA <corr.matrix>,k,L "); rem_pr(" k=number of factors "); rem_pr(" L=first line for the results "); rem_pr(" Factor matrix saved as FACT.M "); rem_pr(" "); rem_pr(" METHOD=ULS Unweighted Least Squares "); rem_pr(" METHOD=GLS Generalized Least Squares "); rem_pr(" METHOD=ML Maximum Likelihood (default) "); rem_pr(" Test statistics by N=<number of observations>"); rem_pr("More information by FACTA? "); wait_remarks(2); s_end(argv); return; } results_line=0; if (g>3) { results_line=edline2(word[3],1); if (results_line==0) return; } i=sp_init(r1+r-1); if (i<0) return; i=matrix_load(word[1],&E,&p,&n,&rlab,&clab,&lr,&lc,&type,expr); if (i<0) { s_end(argv); return; } // RS CHA argv[1] if (p!=n) { sprintf(sbuf,"\n%s not a square matrix!",word[1]); sur_print(sbuf); WAIT; return; } k=atoi(word[2]); if (k<1 || k>p-1) { sprintf(sbuf,"Incorrect number (%d) of factors!",k); if (etu==2) { sprintf(tut_info,"___@6@FACTA@%s@",sbuf); s_end(argv); // RS CHA argv[1] return; } sur_print("\n"); sur_print(sbuf); WAIT; return; } *mess=EOS; i=spfind("FEPS"); if (i>=0) { double eps; eps=1.0-atof(spb[i]); for (i=0; i<p; ++i) for (j=0; j<p; ++j) { if (i==j) continue; E[i+j*p]*=eps; } } ind=3; i=spfind("METHOD"); if (i>=0) { if (muste_strcmpi(spb[i],"ULS")==0) ind=1; if (muste_strcmpi(spb[i],"GLS")==0) ind=2; } for (i=0; i<p; ++i) { if (E[i*(p+1)]!=0.0) continue; sprintf(sbuf,"Variable %.*s is a constant!",lr,rlab+i*lr); if (etu==2) { sprintf(tut_info,"___@1@FACTA@%s@",sbuf); s_end(argv); // RS CHA argv[1] return; } sur_print("\n"); sur_print(sbuf); WAIT; return; } /* if (ind==1) { for (i=0; i<p; ++i) { if (fabs(E[i*(p+1)]-1.0)<0.0001) continue; Rprintf("\n%s is not a correlation matrix as supposed in ULS!", word[1]); WAIT; return; } } */ i=varaa_tilat(); if (i<0) return; for (ui=0; ui<p*p; ++ui) S[ui]=E[ui]; sumlogsii=0.0; for (i=0; i<p; ++i) sumlogsii+=log(S[i*(p+1)]); i=nwtrap(); if (i<0) { if (etu!=2) { sur_print("\nSolution not found!"); WAIT; return; } s_end(argv); // RS CHA argv[1] return; } h=output_open(eout); if (h<0) return; strcpy(x,"Factor analysis: "); switch (ind) { case 1: strcat(x,"Unweighted Least Squares (ULS) solution"); break; case 2: strcat(x,"Generalized Least Squares (GLS) solution"); break; case 3: strcat(x,"Maximum Likelihood (ML) solution"); break; } print_line(x); if (*mess) { strcpy(x," "); strcat(x,mess); print_line(x); if (etu!=2) { WAIT; } } i=spfind("N"); if (i>=0) { if (ind>1) { double n1,c0,chi20,d0,m0,ck,chi2k,dk,mk,rho,pr; char *q; n1=atof(spb[i]); if (n1<(double)k) { sur_print("\nIncorrect N!"); WAIT; return; } c0=n1-1.0-(2.0*p+5.0)/6.0; chi20=c0*(sumlogsii-log(det)); d0=p*(p-1.0)/2.0; m0=chi20/d0; ck=c0-2.0*k/3.0; chi2k=ck*f0; dk=((p-k)*(p-k)-p-k)/2.0; mk=chi2k/dk; rho=(m0-mk)/(m0-1.0); // pr=cdf_chi2(chi2k,dk,1e-10); pr=0.0; sprintf(x,"factors=%d Chi^2=%g df=%d P=%5.3f reliability=%g", k,chi2k,(int)dk,pr,rho); if (rho>1.0) { q=strstr(x,"rel"); *q=EOS; } /* 3.6.1995 */ print_line(x); } else { double n1,uu,dk,pr; n1=atof(spb[i]); if (n1<(double)k) { sur_print("\nIncorrect N!"); WAIT; return; } for (i=0; i<p; ++i) for (j=0; j<i; ++j) { da=0.0; for (h=0; h<k; ++h) da+=L[i+p*h]*L[j+p*h]; S[i+p*j]=da; S[j+p*i]=da; } /* Huom. S-diagonaali säilytetään */ mat_dcholinv(S,p,&uu); uu=(n1-1.0)*log(uu/det); dk=((p-k)*(p-k)+p-k)/2.0; /* ei sama kuin yllä! */ // pr=cdf_chi2(uu,dk,1e-10); pr=0.0; sprintf(x,"factors=%d Chi^2=%g df=%d P=%5.3f", k,uu,(int)dk,pr); print_line(x); } } output_close(eout); f_orientation(L,p,k); text_labels(clab,k,lc,"F"); matrix_save("FACT.M",L,p,k,rlab,clab,lr,lc,0,"F",0,0); strncpy(clab+k*lc,"h^2 ",8); for (i=0; i<p; ++i) { da=0.0; for (j=0; j<k; ++j) { db=L[i+p*j]; S[i+p*j]=db; da+=db*db; } S[i+p*k]=da; } strcpy(acc,"12.1234567890123456"); acc[accuracy-1]=EOS; matrix_print(S,p,k+1,rlab,clab,lr,lc,p,k+1,NULL,NULL,acc,c3, results_line,eout,"Factor matrix"); s_end(argv); }
/* * Accept as parameter: * - input file * - algorithm * - S: serial * - P: Pthread parallel * - O: OpenMP parallel * - verbose (show data in console) * - # of threads * * Main steps * 1. Read matrix from file * 2. Solve linear system using Jacobi Method * 3. Write output * * Output * Result file with statistics */ int main(int argc, char *argv[]) { if ( argc != 5 ) { puts("Inform input file and number of threads!"); exit(1); } //algorithm char algorithm = argv[2][0]; //# of threads int thread_count = strtol(argv[3], NULL, 10); //console output level int verbose = strtol(argv[4], NULL, 10); if (verbose) puts("---BEGIN---"); //prints info if (verbose) printf("Input file: '%s', thread count: %i, algorithm: %c\n\n", argv[1], thread_count, algorithm); //loads matrix matrix *m = matrix_load(argv[1]); //prints matrix if (verbose) matrix_print(m); //starts timer timer* t = start_timer(); jacobi_result* result; switch (algorithm) { case 'P': // pthread parallel result = jacobi_parallel_pthread_better(m, thread_count, verbose); break; case 'O': // OpenMP parallel result = jacobi_parallel_omp(m, thread_count, verbose); break; /* case 'M': // MPI parallel break; */ case 'S': // serial default: result = jacobi_serial(m, verbose); } //Sleep(500); //stops timer stop_timer(t, verbose); //prints result if (verbose) { int i; printf("\nResults: "); for (i = 0; i < m->size && i < 200; i++) { printf("%f, ", result->x[i]); } printf("\nIterations: %i ", result->k); } //saves results write_results(t, argv[1], thread_count, algorithm, m->size); //frees matrix matrix_destroy(m); //frees timer free(t); //frees result //free(result->x); free(result); if (verbose) puts("\n\n---END---"); return EXIT_SUCCESS; }
bool test(const char* filename, FILE* fout) { FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); return false; } int fmt; int suc = fscanf(fp, "%d", &fmt); if (suc == EOF || suc != 1) { fclose(fp); return false; } if (fclose(fp) != 0) { perror(filename); } switch (fmt) { case FMT_VECTOR: { Vector vect = { .count = 0, .items = NULL }; if (!vector_load(&vect, filename)) { return false; } vector_print(&vect, fout); vector_free(&vect); } break; case FMT_MATRIX: { Matrix mat = { .rows = 0, .cols = 0, .items = NULL }; if (!matrix_load(&mat, filename)) { return false; } matrix_print(&mat, fout); matrix_free(&mat); } break; case FMT_VECTOR_OF_MATRICES: { VectorOfMatrices vm = { .count = 0, .rows = 0, .cols = 0, .items = NULL }; if (!vm_load(&vm, filename)) { return false; } vm_print(&vm, fout); vm_free(&vm); } break; default: return false; } return true; } bool vector_load(Vector* vect, const char* filename) { assert(vect != NULL); FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); goto return_false; } int fmt; unsigned long count; int suc = fscanf(fp, "%d", &fmt); CHECK_IO(suc, 1, return_false); if (fmt != FMT_VECTOR) { goto return_false; } suc = fscanf(fp, "%lu", &count); CHECK_IO(suc, 1, return_false); if (count <= 0) { goto return_false; } vect->count = count; if (!vector_alloc(vect)) { goto return_false; } int* pVect = vect->items; for (size_t i = 0; i < vect->count; ++i) { suc = fscanf(fp, "%d", pVect++); CHECK_IO(suc, 1, return_false); } if (fclose(fp) != 0) { perror(filename); } return true; return_false: if (fp != NULL && fclose(fp) != 0) { perror(filename); } vector_free(vect); return false; } void vector_print(const Vector* vect, FILE* fp) { assert(vect != NULL); assert(fp != NULL); assert(vect->count > 0); assert(vect->items != NULL); fprintf(fp, "%d\n", FMT_VECTOR); fprintf(fp, "%zu\n", vect->count); int* pVect = vect->items; for (size_t i = 0; i < vect->count; ++i) { fprintf(fp, "%d ", *pVect++); } } void noop_print(FILE* fp) { assert(fp != NULL); fprintf(fp, "false\n"); }