static void submatrices(size_t N, size_t M, int matrix[N][M], size_t rows, size_t cols) { size_t i, j; for (i = 0; i+rows-1 < N; i++) { for (j = 0; j+cols-1 < M; j++) { print_submatrix(N, M, matrix, i, j, rows, cols); } } }
void print_matrix(double** matrix, int n){ print_submatrix(matrix, 0, n, 0, n); }
// this method is not required to be fast, just correct int check_bin_packing_matrix(uint_matrix *A, double items[], unsigned int limit){ assert( A != NULL ); unsigned int i,j, x=0; double room; double fill; double min; int errors = 0; unsigned int max_value; unsigned int *row = calloc(A->width, sizeof(unsigned int)); // check if every present packing is valid min = items[0]; for( i = 0; i < A->width ; i++ ){ if( items[i] < min ){ min = items[i]; } } // the algorithm rather packs a bit-too-small packings // this is due to double precision loss min += 0.0000000000000001; max_value = floor(PACKING_SIZE / min); if( max_value > limit ){ max_value = limit; } for( i = 0; i < A->height ; i++ ){ room = PACKING_SIZE; fill = 0; for( j = 0; j < A->width ; j++ ){ if( A->values[ A->width*i + j ] > limit ){ errors |= 4; } fill += A->values[ A->width*i + j ] * items[j]; room -= A->values[ A->width*i + j ] * items[j]; } if( fill > PACKING_SIZE ){ printf("Packing too big: fill = %f\n", fill); print_submatrix(A, i, i+1, 0, A->width); errors |= 1; } if( room >= min ){ printf("Packing too small: fill = %f\n",fill); print_submatrix(A, i, i+1, 0, A->width); errors |= 2; } } // check if at least some packings are present i = 1; while( i < max_value ){ x = 0; while( x < A->width ){ if( row[x] == 0 ){ row[x] = i; // check if this row is possible room = PACKING_SIZE; for( j = 0; j < A->width ; j++ ){ room -= row[j] * items[j]; } if( room >= 0 ){ // this packing is small enough // there has to be a containing packing if( !matrix_contains_higher_row(A, row) ){ printf("No Packing includes:\n"); print_row( row, A->width); errors |= 8; } } break; }else{ row[x] = 0; } x++; } if( x == A->width ){ i++; } } free(row); return errors; }