/* compilation: gcc pivot.c -o pivot */ #include #include #include double *ratios; double **B_mat; double EPSILON = 1e-5; FILE *d; char diary[40]; /***************************************************************/ void read_tableau(f_name, nb_rows, nb_cols, phase) char *f_name; int *nb_rows; int *nb_cols; int *phase; { int i, j; char buff[256]; FILE *f; sprintf(diary,"%s.diary", f_name); printf("f_name = %s\n",f_name); f = fopen(f_name, "r"); d = fopen(diary, "w"); fscanf(f,"%s", buff); while (strcmp(buff, "begin") != 0) { fscanf(f,"%s", buff); } fscanf(f, "%d %d", nb_rows, nb_cols); printf("#rows : %6d #columns : %6d\n",*nb_rows, *nb_cols); fprintf(d,"#rows : %6d #columns : %6d\n",*nb_rows, *nb_cols); fgets(buff, sizeof(buff),f); (*nb_rows)++; (*nb_cols)++; B_mat = (double**) malloc (sizeof(double*)*(*nb_rows)); ratios = (double*) malloc (sizeof(double)*(*nb_rows)); for (i=0; i<*nb_rows; i++) B_mat[i] = (double*) malloc (sizeof(double)*(*nb_cols)); for (i=0; i<*nb_rows; i++) { #ifdef TRACE printf("%d : ", i); #endif for (j=1; j<*nb_cols; j++) { fscanf(f, "%lf", &(B_mat [i] [j])); #ifdef TRACE printf("%f ", B_mat [i] [j]); #endif } #ifdef TRACE printf("\n"); #endif } fclose(f); *phase = 0; } /* read_tableau */ /***************************************************************/ void print_tableau(nb_rows, nb_cols, ph) int nb_rows; int nb_cols; int ph; { int i, j; for(i=0; i -EPSILON)) { B_mat[i][j] = 0; } } } switch(ph) { case 0: printf("PHASE 0\n"); fprintf(d,"PHASE 0\n"); break; case 1: printf("PHASE 1\n"); fprintf(d,"PHASE 1\n"); break; case 2: printf("PHASE 2\n"); fprintf(d,"PHASE 2\n"); break; } printf(" "); fprintf(d," "); if(ph == 1) { printf("x 0 "); fprintf(d,"x 0 "); } for (j=1; j EPSILON) && (fabs(B_mat[i][j]) < 0.005)) { printf("%5.1f* ", B_mat[i][j]); fprintf(d,"%5.1f* ", B_mat[i][j]); } else{ printf("%6.2f ", B_mat[i][j]); fprintf(d,"%6.2f ", B_mat[i][j]); } } printf("| %6.2f\n", B_mat[i][nb_cols-1]); fprintf(d,"| %6.2f\n", B_mat[i][nb_cols-1]); } printf(" "); fprintf(d," "); if(ph == 1) { printf("--------"); fprintf(d,"--------"); } for (j=1; j EPSILON) && (fabs(B_mat[nb_rows-2][j]) < 0.005)) { printf("%5.1f* ", B_mat[nb_rows-2][j]); fprintf(d,"%5.1f* ", B_mat[nb_rows-2][j]); } else{ printf("%6.2f ", B_mat[nb_rows-2][j]); fprintf(d,"%6.2f ", B_mat[nb_rows-2][j]); } } printf("| %6.2f", B_mat[nb_rows-2][nb_cols-1]); fprintf(d,"| %6.2f", B_mat[nb_rows-2][nb_cols-1]); printf("\n"); fprintf(d,"\n"); if(ph == 1) { printf(" "); fprintf(d," "); for (j=0; j nb_ineqs-2)) { printf("ERROR: row index should be in the range [1, %d]\n", nb_ineqs-2); fprintf(d,"ERROR: row index should be in the range [1, %d]\n", nb_ineqs-2); error = 1; } if(phase == 1) { if((c_piv < 0) || (c_piv > nb_vars-2)) { printf("ERROR: column index should be in the range [0, %d]\n", nb_vars-2); fprintf(d,"ERROR: column index should be in the range [0, %d]\n", nb_vars-2); error = 1; } } else { if((c_piv < 0) || (c_piv > nb_vars-2) || ((c_piv == 0) && (r_piv != 0))) { printf("ERROR: column index should be in the range [0, %d]\n", nb_vars-2); fprintf(d,"ERROR: column index should be in the range [0, %d]\n", nb_vars-2); error = 1; } } if((c_piv == 0) && (r_piv == 0)) { phase++; error = 1; if(phase == 1) { for(i=0; i