/* Conver sudoku puzzle*/ void sudoku_to_problem(void){ int row,col,val; int i,j; // initialize row_index for(row=0; row<SIZE; ++row) row_index[row]=row; for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ sudoku_modified[row][col]=EMPTY; } } for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ if((val=sudoku[row][col])!=EMPTY){ sudoku_modified[val][row]=col; ++positive[val]; } } } for(i=0; i<SIZE-1; ++i){ for(j=i+1;j<SIZE; ++j){ if(positive[j]>positive[i]){ swap_row(sudoku_modified,i,j); swap_int(&row_index[i],&row_index[j]); } } } }
void lup_decomposition(int n, double a[][n], int pi[]) { int i, j, k; for(i=0;i<n;i++) pi[i] = i; for(k=0;k<n;k++){ int max = k; for(i=k+1;i<n;i++) if(abs_double(a[i][k]) > abs_double(a[k][k])){ max = i; } if(a[max][k] == 0){ fprintf(stderr, "singular matrix\n"); return; } swap_element(pi, max, k); swap_row(n, a, max, k); for(i=k+1;i<n;i++) a[i][k] = a[i][k]/a[k][k]; for(i=k+1;i<n;i++) for(j=k+1;j<n;j++) a[i][j] = a[i][j] - a[i][k]*a[k][j]; } }
// **************************************************************************** // Calculates the inverse of N x N matrix A and stores it in matrix Ainv. // It is assumed that the memory required for this has already been allocated. // The data in matrix A is not destroyed, unless the same address is supplied for Ainv. // (This case should be successfully handled also.) // The function returns -1 if the matrix is not invertible. // **************************************************************************** int matrix_invert(double *A, double *Ainv, long int N) { double *backup = malloc_1d_array_double(N*N); double pivot, factor; long int pivot_row; if (A==NULL || Ainv==NULL || N<1) quit_error((char*)"Bad input data to matrix_invert"); // Backup the A matrix so that manipulations can be performed here // without damaging the original matrix. for (long int row=0; row<N; row++) for (long int col=0; col<N; col++) backup[row*N+col]=A[row*N+col]; // First, fill Ainv with the identity matrix for (long int row=0; row<N; row++) { for (long int col=0; col<N; col++) { if (row==col) Ainv[row*N+col]=1; else Ainv[row*N+col]=0; } } // Calculate the inverse using Gauss-Jordan elimination for (long int col=0; col<N; col++) { //display_augmented(backup, Ainv, N); pivot_row=-1; pivot=0.0; for (long int row=col; row<N; row++) { if (fabs(backup[row*N+col])>fabs(pivot)) { pivot_row=row; pivot=backup[row*N+col]; } } //printf("Pivot: %lf\n\n", pivot); if (pivot_row<0 || pivot_row>=N || fabs(pivot)<DBL_EPSILON) { free_1d_array_double(backup); return -1; } else { swap_row(col, pivot_row, backup, Ainv, N); multiply_row(col, 1.0/pivot, backup, Ainv, N); for (long int elim_row=0; elim_row<N; elim_row++) { if (elim_row!=col) { factor=-backup[elim_row*N+col]; add_multiple_row(factor, col, elim_row, backup, Ainv, N); } } } } free_1d_array_double(backup); return 0; }
//Helper Functions static int swap_nonzero_cell(matrix_t *m, int row, int col) { if (!m) return ERROR; if (m->get_fcell(m, row, col) > TOLERANCE || m->get_fcell(m, row, col) < -TOLERANCE) return SUCCESS; int init_row = row; for (row=row + 1; row < m->num_rows; row++) if (m->get_fcell(m, row, col) > TOLERANCE || m->get_fcell(m, row, col) < -TOLERANCE) return swap_row(m, init_row, row); return FAIL; }
int swap_matrix_row_col(matrix_t *m, int row_or_col, char *buf, int buf_size) { int idx1 = 0, idx2 = 0; int max_idx = 0; char *row_or_col_str; if (row_or_col == SWAP_ROW) { row_or_col_str = "Row"; max_idx = m->num_rows; } else if (row_or_col == SWAP_COL) { row_or_col_str = "Column"; max_idx = m->num_cols; } else { return FAIL; } printf("Enter %s Index 1: ", row_or_col_str); if (readnum(buf, buf_size, &idx1) == ERROR) return ERROR; if (idx1 >= max_idx) { printf("Bad Input\n"); return FAIL; } printf("Enter %s Index 2: ", row_or_col_str); if (readnum(buf, buf_size, &idx2) == ERROR) return ERROR; if (idx2 >= max_idx) { printf("Bad Input\n"); return FAIL; } print_matrix("Original Matrix", m); if (row_or_col == SWAP_ROW) return swap_row(m, idx1, idx2); if (row_or_col == SWAP_COL) return swap_col(m, idx1, idx2); return FAIL; }
void gauss_eliminate(double *a, double *b, double *x, int n) { #define A(y, x) (*mat_elem(a, y, x, n)) int i, j, col, row, max_row,dia; double max; for (dia = 0; dia < n; dia++) { max_row = dia, max = A(dia, dia); //#pragma omp parallel for for (row = dia + 1; row < n; row++){ double tmp; if ((tmp = fabs(A(row, dia))) > max) max_row = row, max = tmp; } swap_row(a, b, dia, max_row, n); for (row = dia + 1; row < n; row++) { double tmp; tmp = A(row, dia) / A(dia, dia); //#pragma omp parallel for for (col = dia+1; col < n; col++) A(row, col) -= tmp * A(dia, col); A(row, dia) = 0; b[row] -= tmp * b[dia]; } } //#pragma omp parallel for for (row = n - 1; row >= 0; row--) { double tmp; tmp = b[row]; //#pragma omp parallel for for (j = n - 1; j > row; j--) tmp -= x[j] * A(row, j); x[row] = tmp / A(row, row); } #undef A }
//swap two rows and columns inline void swap_row_col(int c1, int c2) { swap_col(c1,c2); swap_row(c1,c2); }