Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Sure, here is the equivalent function written in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); used_for_row[i][0] = i; // Initialize with self } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } int* others = (int*)malloc(rows * sizeof(int)); int others_count = 0; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra]) { others[others_count++] = i; } } for (int i = 0; i < others_count; i++) { if (others[i] == col) continue; int row = others[i]; for (int j = 0; j < rows; j++) { used_for_row[row][j] |= used_for_row[col][j]; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j] * matrix[row][col + extra]) % PH; } } else { while (matrix[row][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] + PH) % PH; // Ensure non-negative } } free(others); col++; } for (int i = 0; i < rows; i++) { free(used_for_row[i]); } free(used_for_row); free(pivots); return matrix; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3; int cols = 3; int graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[1][0] = 4; matrix[1][1] = 5; matrix[1][2] = 6; matrix[2][0] = 7; matrix[2][1] = 8; matrix[2][2] = 9; printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); matrix = rref(matrix, rows, cols, graphlen); printf("\nRREF Matrix:\n"); printMatrix(matrix, rows, cols); for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); used_for_row[i][0] = i; // Initialize with self } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } int* others = (int*)malloc(rows * sizeof(int)); int others_count = 0; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra]) { others[others_count++] = i; } } for (int i = 0; i < others_count; i++) { if (others[i] == col) continue; int row = others[i]; for (int j = 0; j < rows; j++) { used_for_row[row][j] |= used_for_row[col][j]; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j] * matrix[row][col + extra]) % PH; } } else { while (matrix[row][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] + PH) % PH; // Ensure non-negative } } free(others); col++; } for (int i = 0; i < rows; i++) { free(used_for_row[i]); } free(used_for_row); free(pivots); return matrix; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3; int cols = 3; int graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[1][0] = 4; matrix[1][1] = 5; matrix[1][2] = 6; matrix[2][0] = 7; matrix[2][1] = 8; matrix[2][2] = 9; printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); matrix = rref(matrix, rows, cols, graphlen); printf("\nRREF Matrix:\n"); printMatrix(matrix, rows, cols); for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); used_for_row[i][0] = i; // Initialize with self } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } int* others = (int*)malloc(rows * sizeof(int)); int others_count = 0; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra]) { others[others_count++] = i; } } for (int i = 0; i < others_count; i++) { if (others[i] == col) continue; int row = others[i]; for (int j = 0; j < rows; j++) { used_for_row[row][j] |= used_for_row[col][j]; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j] * matrix[row][col + extra]) % PH; } } else { while (matrix[row][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[row][j] = (matrix[row][j] + PH) % PH; // Ensure non-negative } } free(others); col++; } for (int i = 0; i < rows; i++) { free(used_for_row[i]); } free(used_for_row); free(pivots); return matrix; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int __delete_this__main() { int rows = 3; int cols = 3; int graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[1][0] = 4; matrix[1][1] = 5; matrix[1][2] = 6; matrix[2][0] = 7; matrix[2][1] = 8; matrix[2][2] = 9; printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); matrix = rref(matrix, rows, cols, graphlen); printf("\nRREF Matrix:\n"); printMatrix(matrix, rows, cols); for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix); return 0; } #include<stdio.h> #include<stdlib.h> int main() { int arr0[] = {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; int arr1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -10}; int arr2[] = {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 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9}; int arr36[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -20}; int arr37[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -40}; int arr38[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int* arr[] = {arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11, arr12, arr13, arr14, arr15, arr16, arr17, arr18, arr19, arr20, arr21, arr22, arr23, arr24, arr25, arr26, arr27, arr28, arr29, arr30, arr31, arr32, arr33, arr34, arr35, arr36, arr37, arr38}; int** got = rref(arr, 39, 49, 37); int answer = got[3][0]+got[3][1]+got[3][2]+got[3][3]+got[3][4]+got[3][5]+got[3][6]; int expected = 146; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
Wrong answer; got 1 instead of 146.
Testing if the previous output contains the string All tests passed
: False
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Certainly! Here is the equivalent C function for the given Python code. Note that some operations, like `numpy` array manipulations, have been converted to standard C operations. Also, we need to manage the dynamic memory allocations for the 2D array and sets explicitly in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(sizeof(int) * rows); int** used_for_row = (int**)malloc(rows * sizeof(int*)); int* used_for_row_sizes = (int*)malloc(rows * sizeof(int)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int) * rows); used_for_row[i][0] = i; used_for_row_sizes[i] = 1; } // Modulo operation for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; // Swap used_for_row int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; int temp_size = used_for_row_sizes[col]; used_for_row_sizes[col] = used_for_row_sizes[other]; used_for_row_sizes[other] = temp_size; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; int contains = 0; for (int k = 0; k < used_for_row_sizes[col]; k++) { if (used_for_row[i][k] == used_for_row[col][k]) { contains = 1; break; } } if (!contains) { used_for_row[i][used_for_row_sizes[i]] = used_for_row[col][0]; used_for_row_sizes[i]++; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * matrix[i][col + extra]) % PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] + PH) % PH; } } col++; } for (int i = 0; i < rows; i++) { qsort(used_for_row[i], used_for_row_sizes[i], sizeof(int), cmpfunc); } free(pivots); free(used_for_row_sizes); return used_for_row; } // Comparison function for qsort int cmpfunc(const void *a, const void *b) { return (*(int*)a - *(int*)b); } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(sizeof(int) * rows); int** used_for_row = (int**)malloc(rows * sizeof(int*)); int* used_for_row_sizes = (int*)malloc(rows * sizeof(int)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int) * rows); used_for_row[i][0] = i; used_for_row_sizes[i] = 1; } // Modulo operation for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; // Swap used_for_row int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; int temp_size = used_for_row_sizes[col]; used_for_row_sizes[col] = used_for_row_sizes[other]; used_for_row_sizes[other] = temp_size; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; int contains = 0; for (int k = 0; k < used_for_row_sizes[col]; k++) { if (used_for_row[i][k] == used_for_row[col][k]) { contains = 1; break; } } if (!contains) { used_for_row[i][used_for_row_sizes[i]] = used_for_row[col][0]; used_for_row_sizes[i]++; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * matrix[i][col + extra]) % PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] + PH) % PH; } } col++; } for (int i = 0; i < rows; i++) { qsort(used_for_row[i], used_for_row_sizes[i], sizeof(int), cmpfunc); } free(pivots); free(used_for_row_sizes); return used_for_row; } // Comparison function for qsort int cmpfunc(const void *a, const void *b) { return (*(int*)a - *(int*)b); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <stdio.h> #include <stdlib.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int* pivots = (int*)malloc(sizeof(int) * rows); int** used_for_row = (int**)malloc(rows * sizeof(int*)); int* used_for_row_sizes = (int*)malloc(rows * sizeof(int)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int) * rows); used_for_row[i][0] = i; used_for_row_sizes[i] = 1; } // Modulo operation for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp_row = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp_row; // Swap used_for_row int* temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; int temp_size = used_for_row_sizes[col]; used_for_row_sizes[col] = used_for_row_sizes[other]; used_for_row_sizes[other] = temp_size; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; int contains = 0; for (int k = 0; k < used_for_row_sizes[col]; k++) { if (used_for_row[i][k] == used_for_row[col][k]) { contains = 1; break; } } if (!contains) { used_for_row[i][used_for_row_sizes[i]] = used_for_row[col][0]; used_for_row_sizes[i]++; } if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * matrix[i][col + extra]) % PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] + PH) % PH; } } col++; } for (int i = 0; i < rows; i++) { qsort(used_for_row[i], used_for_row_sizes[i], sizeof(int), cmpfunc); } free(pivots); free(used_for_row_sizes); return used_for_row; } // Comparison function for qsort int cmpfunc(const void *a, const void *b) { return (*(int*)a - *(int*)b); } #include<stdio.h> #include<stdlib.h> int main() { int arr0[] = {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -13}; int arr34[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -11}; int arr35[] = {0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9}; int arr36[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -20}; int arr37[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -40}; int arr38[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int* arr[] = {arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11, arr12, arr13, arr14, arr15, arr16, arr17, arr18, arr19, arr20, arr21, arr22, arr23, arr24, arr25, arr26, arr27, arr28, arr29, arr30, arr31, arr32, arr33, arr34, arr35, arr36, arr37, arr38}; int** got = rref(arr, 39, 49, 37); int answer = got[3][0]+got[3][1]+got[3][2]+got[3][3]+got[3][4]+got[3][5]+got[3][6]; int expected = 146; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c: In function 'rref':
main.c:122:68: error: 'cmpfunc' undeclared (first use in this function)
122 | qsort(used_for_row[i], used_for_row_sizes[i], sizeof(int), cmpfunc);
| ^~~~~~~
main.c:122:68: note: each undeclared identifier is reported only once for each function it appears in
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Certainly! Here's the equivalent function in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int **matrix, int rows, int cols, int graphlen) { int **used_for_row = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int *)malloc(sizeof(int)); used_for_row[i][0] = i; } int extra = 0; int col = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; --i) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int *temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int *temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; ++j) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; int nonzero = 0; for (int j = 0; j < cols; ++j) { if (matrix[i][j] != 0) { nonzero = 1; break; } } if (!nonzero) continue; // Union of sets int *new_set = (int *)malloc((2 * sizeof(int))); int k = 0; for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[i][j]; } for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[col][j]; } used_for_row[i] = new_set; if (col < graphlen) { for (int j = 0; j < cols; ++j) { matrix[i][j] -= matrix[col][j] * matrix[i][col + extra]; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int **matrix, int rows, int cols) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3, cols = 4, graphlen = 2; // Example usage int **matrix = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { matrix[i] = (int *)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 5; matrix[1][1] = 6; matrix[1][2] = 7; matrix[1][3] = 8; matrix[2][0] = 9; matrix[2][1] = 10; matrix[2][2] = 11; matrix[2][3] = 12; int **used_for_row = rref(matrix, rows, cols, graphlen); printf("Matrix after RREF:\n"); printMatrix(matrix, rows, cols); printf("Used for row:\n"); for (int i = 0; i < rows; ++i) { printf("%d\n", used_for_row[i][0]); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int **matrix, int rows, int cols, int graphlen) { int **used_for_row = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int *)malloc(sizeof(int)); used_for_row[i][0] = i; } int extra = 0; int col = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; --i) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int *temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int *temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; ++j) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; int nonzero = 0; for (int j = 0; j < cols; ++j) { if (matrix[i][j] != 0) { nonzero = 1; break; } } if (!nonzero) continue; // Union of sets int *new_set = (int *)malloc((2 * sizeof(int))); int k = 0; for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[i][j]; } for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[col][j]; } used_for_row[i] = new_set; if (col < graphlen) { for (int j = 0; j < cols; ++j) { matrix[i][j] -= matrix[col][j] * matrix[i][col + extra]; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int **matrix, int rows, int cols) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3, cols = 4, graphlen = 2; // Example usage int **matrix = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { matrix[i] = (int *)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 5; matrix[1][1] = 6; matrix[1][2] = 7; matrix[1][3] = 8; matrix[2][0] = 9; matrix[2][1] = 10; matrix[2][2] = 11; matrix[2][3] = 12; int **used_for_row = rref(matrix, rows, cols, graphlen); printf("Matrix after RREF:\n"); printMatrix(matrix, rows, cols); printf("Used for row:\n"); for (int i = 0; i < rows; ++i) { printf("%d\n", used_for_row[i][0]); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int **matrix, int rows, int cols, int graphlen) { int **used_for_row = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int *)malloc(sizeof(int)); used_for_row[i][0] = i; } int extra = 0; int col = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; --i) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int *temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int *temp_set = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_set; } int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } for (int j = 0; j < cols; ++j) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; int nonzero = 0; for (int j = 0; j < cols; ++j) { if (matrix[i][j] != 0) { nonzero = 1; break; } } if (!nonzero) continue; // Union of sets int *new_set = (int *)malloc((2 * sizeof(int))); int k = 0; for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[i][j]; } for (int j = 0; j < 1; ++j) { new_set[k++] = used_for_row[col][j]; } used_for_row[i] = new_set; if (col < graphlen) { for (int j = 0; j < cols; ++j) { matrix[i][j] -= matrix[col][j] * matrix[i][col + extra]; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int **matrix, int rows, int cols) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { printf("%d ", matrix[i][j]); } printf("\n"); } } int __delete_this__main() { int rows = 3, cols = 4, graphlen = 2; // Example usage int **matrix = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; ++i) { matrix[i] = (int *)malloc(cols * sizeof(int)); } matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 5; matrix[1][1] = 6; matrix[1][2] = 7; matrix[1][3] = 8; matrix[2][0] = 9; matrix[2][1] = 10; matrix[2][2] = 11; matrix[2][3] = 12; int **used_for_row = rref(matrix, rows, cols, graphlen); printf("Matrix after RREF:\n"); printMatrix(matrix, rows, cols); printf("Used for row:\n"); for (int i = 0; i < rows; ++i) { printf("%d\n", used_for_row[i][0]); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } #include<stdio.h> #include<stdlib.h> int main() { int arr0[] = {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; int arr1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -10}; int arr2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -20}; int arr3[] = {0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, -20}; int arr4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -40}; int arr38[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int* arr[] = {arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11, arr12, arr13, arr14, arr15, arr16, arr17, arr18, arr19, arr20, arr21, arr22, arr23, arr24, arr25, arr26, arr27, arr28, arr29, arr30, arr31, arr32, arr33, arr34, arr35, arr36, arr37, arr38}; int** got = rref(arr, 39, 49, 37); int answer = got[3][0]+got[3][1]+got[3][2]+got[3][3]+got[3][4]+got[3][5]+got[3][6]; int expected = 146; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
Wrong answer; got 73 instead of 146.
Testing if the previous output contains the string All tests passed
: False
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Sure, here is the equivalent function in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <stdio.h> #include <stdlib.h> #include <math.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0, col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int)); used_for_row[i][0] = i; } // Apply modulo PH to the matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* tempUsed = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = tempUsed; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { if (abs(pivot) != 1 && abs(pivot) != PH - 1) { printf("Assertion failed\n"); exit(1); } } else { if (abs(pivot) != 2 && abs(pivot) != PH - 2) { printf("Assertion failed\n"); exit(1); } pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; used_for_row[i] = (int*)realloc(used_for_row[i], sizeof(int) * (sizeof(used_for_row[i]) / sizeof(int) + 1)); int k = 0; while (used_for_row[i][k] != '\0') { k++; } used_for_row[i][k] = used_for_row[col][0]; if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] -= (matrix[col][j] * matrix[i][col + extra]) % PH; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3, cols = 4, graphlen = 2; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Example matrix int data[3][4] = { {2, 4, 6, 8}, {1, 3, 5, 7}, {9, 7, 5, 3} }; // Copy data to matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] = data[i][j]; } } printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); int** used_for_row = rref(matrix, rows, cols, graphlen); printf("RREF Matrix:\n"); printMatrix(matrix, rows, cols); printf("Used for Row:\n"); for (int i = 0; i < rows; i++) { int j = 0; while (used_for_row[i][j] != '\0') { printf("%d ", used_for_row[i][j]); j++; } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; i++) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <stdio.h> #include <stdlib.h> #include <math.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0, col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int)); used_for_row[i][0] = i; } // Apply modulo PH to the matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* tempUsed = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = tempUsed; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { if (abs(pivot) != 1 && abs(pivot) != PH - 1) { printf("Assertion failed\n"); exit(1); } } else { if (abs(pivot) != 2 && abs(pivot) != PH - 2) { printf("Assertion failed\n"); exit(1); } pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; used_for_row[i] = (int*)realloc(used_for_row[i], sizeof(int) * (sizeof(used_for_row[i]) / sizeof(int) + 1)); int k = 0; while (used_for_row[i][k] != '\0') { k++; } used_for_row[i][k] = used_for_row[col][0]; if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] -= (matrix[col][j] * matrix[i][col + extra]) % PH; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int rows = 3, cols = 4, graphlen = 2; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Example matrix int data[3][4] = { {2, 4, 6, 8}, {1, 3, 5, 7}, {9, 7, 5, 3} }; // Copy data to matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] = data[i][j]; } } printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); int** used_for_row = rref(matrix, rows, cols, graphlen); printf("RREF Matrix:\n"); printMatrix(matrix, rows, cols); printf("Used for Row:\n"); for (int i = 0; i < rows; i++) { int j = 0; while (used_for_row[i][j] != '\0') { printf("%d ", used_for_row[i][j]); j++; } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; i++) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | #include <stdio.h> #include <stdlib.h> #include <math.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0, col = 0; int* pivots = (int*)malloc(cols * sizeof(int)); int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { used_for_row[i] = (int*)malloc(sizeof(int)); used_for_row[i][0] = i; } // Apply modulo PH to the matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; i++) { if (matrix[i][col] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = rows - 1; i >= 0; i--) { if (matrix[i][col + extra] != 0) { other = i; break; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* tempUsed = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = tempUsed; } pivots[col] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { if (abs(pivot) != 1 && abs(pivot) != PH - 1) { printf("Assertion failed\n"); exit(1); } } else { if (abs(pivot) != 2 && abs(pivot) != PH - 2) { printf("Assertion failed\n"); exit(1); } pivot /= 2; } for (int j = 0; j < cols; j++) { matrix[col][j] *= pivot; matrix[col][j] %= PH; } for (int i = 0; i < rows; i++) { if (i == col) continue; used_for_row[i] = (int*)realloc(used_for_row[i], sizeof(int) * (sizeof(used_for_row[i]) / sizeof(int) + 1)); int k = 0; while (used_for_row[i][k] != '\0') { k++; } used_for_row[i][k] = used_for_row[col][0]; if (col < graphlen) { for (int j = 0; j < cols; j++) { matrix[i][j] -= (matrix[col][j] * matrix[i][col + extra]) % PH; matrix[i][j] %= PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; j++) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; } } } } col++; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] %= PH; } } return used_for_row; } // Helper function to print the matrix void printMatrix(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int __delete_this__main() { int rows = 3, cols = 4, graphlen = 2; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Example matrix int data[3][4] = { {2, 4, 6, 8}, {1, 3, 5, 7}, {9, 7, 5, 3} }; // Copy data to matrix for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] = data[i][j]; } } printf("Original Matrix:\n"); printMatrix(matrix, rows, cols); int** used_for_row = rref(matrix, rows, cols, graphlen); printf("RREF Matrix:\n"); printMatrix(matrix, rows, cols); printf("Used for Row:\n"); for (int i = 0; i < rows; i++) { int j = 0; while (used_for_row[i][j] != '\0') { printf("%d ", used_for_row[i][j]); j++; } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; i++) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } #include<stdio.h> #include<stdlib.h> int main() { int arr0[] = {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; int arr1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -11}; int arr35[] = {0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9}; int arr36[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -20}; int arr37[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -40}; int arr38[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int* arr[] = {arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11, arr12, arr13, arr14, arr15, arr16, arr17, arr18, arr19, arr20, arr21, arr22, arr23, arr24, arr25, arr26, arr27, arr28, arr29, arr30, arr31, arr32, arr33, arr34, arr35, arr36, arr37, arr38}; int** got = rref(arr, 39, 49, 37); int answer = got[3][0]+got[3][1]+got[3][2]+got[3][3]+got[3][4]+got[3][5]+got[3][6]; int expected = 146; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
realloc(): invalid old size
main.sh: line 2: 16 Aborted (core dumped) ./a.out
Testing if the previous output contains the string All tests passed
: False
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Rewrite this function in C. You can assume that the input is a 2d matrix correctly formatted. Call it rref(int** matrix, int rows, int cols, int graphlen) where the size is matrix[rows][cols]. Return an int** of used_for_row. def rref(matrix, graphlen): PH = 16 extra = 0 col = 0 pivots = [] used_for_row = [set([i]) for i in range(matrix.shape[0])] matrix = matrix%PH while col+extra < matrix.shape[1]-1 and col < matrix.shape[0]: if matrix[col,col+extra] == 0: if np.all(matrix[:,col] == 0): extra += 1 continue other = np.argwhere(matrix[:,col+extra] != 0).flatten()[-1] if other < col: extra += 1 continue matrix[col], matrix[other] = list(matrix[other]), list(matrix[col]) used_for_row[col], used_for_row[other] = used_for_row[other], used_for_row[col] pivots.append(col+extra) pivot = matrix[col,col+extra] if col+extra < graphlen: assert np.abs(pivot) == 1 or np.abs(pivot) == PH-1 else: assert np.abs(pivot) == 2 or np.abs(pivot) == PH-2 pivot //= 2 matrix[col] *= pivot matrix[col] %= PH others = np.argwhere(matrix[:,col+extra]).flatten() for i in others: if i == col: continue used_for_row[i] |= used_for_row[col] if col < graphlen: matrix[i] -= matrix[col]*matrix[i,col+extra] else: while matrix[i,col+extra] != 0: matrix[i] = (matrix[i]-matrix[col])%PH matrix[i] %= PH col += 1 matrix = np.array(matrix)%PH return [sorted(x) for x in used_for_row]
Sure, here's the equivalent C function for the provided Python function. This function will convert the input matrix to its reduced row echelon form (RREF) and return an array of sets representing the `used_for_row`. Note that we cannot directly use sets in C, so we will use a 2D array to simulate the sets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int *pivots = (int*)malloc(rows * sizeof(int)); int pivot_count = 0; int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); for (int j = 0; j < rows; ++j) { used_for_row[i][j] = (i == j) ? 1 : 0; } } // Modulo operation for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { other = i; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* temp_row = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_row; } pivots[pivot_count++] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } // Normalize the pivot row for (int j = 0; j < cols; ++j) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; for (int j = 0; j < rows; ++j) { used_for_row[i][j] |= used_for_row[col][j]; } if (col < graphlen) { int factor = matrix[i][col + extra]; for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * factor) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } free(pivots); return used_for_row; } // Example usage int main() { int rows = 3, cols = 4, graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Initialize the matrix matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 2; matrix[1][1] = 3; matrix[1][2] = 4; matrix[1][3] = 5; matrix[2][0] = 3; matrix[2][1] = 4; matrix[2][2] = 5; matrix[2][3] = 6; int** used_for_row = rref(matrix, rows, cols, graphlen); // Print the used_for_row matrix for (int i = 0; i < rows; ++i) { for (int j = 0; j < rows; ++j) { printf("%d ", used_for_row[i][j]); } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int *pivots = (int*)malloc(rows * sizeof(int)); int pivot_count = 0; int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); for (int j = 0; j < rows; ++j) { used_for_row[i][j] = (i == j) ? 1 : 0; } } // Modulo operation for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { other = i; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* temp_row = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_row; } pivots[pivot_count++] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } // Normalize the pivot row for (int j = 0; j < cols; ++j) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; for (int j = 0; j < rows; ++j) { used_for_row[i][j] |= used_for_row[col][j]; } if (col < graphlen) { int factor = matrix[i][col + extra]; for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * factor) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } free(pivots); return used_for_row; } // Example usage int main() { int rows = 3, cols = 4, graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Initialize the matrix matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 2; matrix[1][1] = 3; matrix[1][2] = 4; matrix[1][3] = 5; matrix[2][0] = 3; matrix[2][1] = 4; matrix[2][2] = 5; matrix[2][3] = 6; int** used_for_row = rref(matrix, rows, cols, graphlen); // Print the used_for_row matrix for (int i = 0; i < rows; ++i) { for (int j = 0; j < rows; ++j) { printf("%d ", used_for_row[i][j]); } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define PH 16 int** rref(int** matrix, int rows, int cols, int graphlen) { int extra = 0; int col = 0; int *pivots = (int*)malloc(rows * sizeof(int)); int pivot_count = 0; int** used_for_row = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { used_for_row[i] = (int*)malloc(rows * sizeof(int)); for (int j = 0; j < rows; ++j) { used_for_row[i][j] = (i == j) ? 1 : 0; } } // Modulo operation for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } while (col + extra < cols - 1 && col < rows) { if (matrix[col][col + extra] == 0) { int all_zero = 1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { all_zero = 0; break; } } if (all_zero) { extra++; continue; } int other = -1; for (int i = 0; i < rows; ++i) { if (matrix[i][col + extra] != 0) { other = i; } } if (other < col) { extra++; continue; } // Swap rows int* temp = matrix[col]; matrix[col] = matrix[other]; matrix[other] = temp; // Swap used_for_row int* temp_row = used_for_row[col]; used_for_row[col] = used_for_row[other]; used_for_row[other] = temp_row; } pivots[pivot_count++] = col + extra; int pivot = matrix[col][col + extra]; if (col + extra < graphlen) { assert(abs(pivot) == 1 || abs(pivot) == PH - 1); } else { assert(abs(pivot) == 2 || abs(pivot) == PH - 2); pivot /= 2; } // Normalize the pivot row for (int j = 0; j < cols; ++j) { matrix[col][j] = (matrix[col][j] * pivot) % PH; } for (int i = 0; i < rows; ++i) { if (i == col) continue; for (int j = 0; j < rows; ++j) { used_for_row[i][j] |= used_for_row[col][j]; } if (col < graphlen) { int factor = matrix[i][col + extra]; for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j] * factor) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } else { while (matrix[i][col + extra] != 0) { for (int j = 0; j < cols; ++j) { matrix[i][j] = (matrix[i][j] - matrix[col][j]) % PH; if (matrix[i][j] < 0) matrix[i][j] += PH; } } } } col++; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { matrix[i][j] %= PH; } } free(pivots); return used_for_row; } // Example usage int __delete_this__main() { int rows = 3, cols = 4, graphlen = 3; int** matrix = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { matrix[i] = (int*)malloc(cols * sizeof(int)); } // Initialize the matrix matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[0][3] = 4; matrix[1][0] = 2; matrix[1][1] = 3; matrix[1][2] = 4; matrix[1][3] = 5; matrix[2][0] = 3; matrix[2][1] = 4; matrix[2][2] = 5; matrix[2][3] = 6; int** used_for_row = rref(matrix, rows, cols, graphlen); // Print the used_for_row matrix for (int i = 0; i < rows; ++i) { for (int j = 0; j < rows; ++j) { printf("%d ", used_for_row[i][j]); } printf("\n"); } // Free allocated memory for (int i = 0; i < rows; ++i) { free(matrix[i]); free(used_for_row[i]); } free(matrix); free(used_for_row); return 0; } #include<stdio.h> #include<stdlib.h> int main() { int arr0[] = {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; int arr1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -11}; int arr35[] = {0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9}; int arr36[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -20}; int arr37[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -40}; int arr38[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int* arr[] = {arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11, arr12, arr13, arr14, arr15, arr16, arr17, arr18, arr19, arr20, arr21, arr22, arr23, arr24, arr25, arr26, arr27, arr28, arr29, arr30, arr31, arr32, arr33, arr34, arr35, arr36, arr37, arr38}; int** got = rref(arr, 39, 49, 37); int answer = got[3][0]+got[3][1]+got[3][2]+got[3][3]+got[3][4]+got[3][5]+got[3][6]; int expected = 146; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
Wrong answer; got 7 instead of 146.
Testing if the previous output contains the string All tests passed
: False