/* Author: Pate Williams (c) 1997 Algorithm 2.2.1 (Square Linear System). Let M be an n by n matrix and B a column vector. This algorithm either outputs a message saying that M is not invertible, or outputs a column vector X such that MX = B." -Henri Cohen- See "A Course in Computational Algebraic Number Theory" by Henri Cohen pages 48-49. We specialize to the case where b[i] and m[i][j] are in field Zp. */ #include #include long **create_square_matrix(long n) { long i, **matrix = calloc(n, sizeof(long *)); if (!matrix) { fprintf(stderr, "fatal error\ninsufficient memory\n"); fprintf(stderr, "from create_matrix\n"); exit(1); } for (i = 0; i < n; i++) { matrix[i] = calloc(n, sizeof(long)); if (!matrix[i]) { fprintf(stderr, "fatal error\ninsufficient memory\n"); fprintf(stderr, "from create_matrix\n"); exit(1); } } return matrix; } void delete_square_matrix(long n, long **matrix) { long i; for (i = 0; i < n; i++) free(matrix[i]); free(matrix); } void Euclid_extended(long a, long b, long *u, long *v, long *d) { long q, t1, t3, v1, v3; *u = 1, *d = a; if (b == 0) { *v = 0; return; } v1 = 0, v3 = b; #ifdef DEBUG printf("----------------------------------\n"); printf(" q t3 *u *d t1 v1 v3\n"); printf("----------------------------------\n"); #endif while (v3 != 0) { q = *d / v3; t3 = *d - q * v3; t1 = *u - q * v1; *u = v1, *d = v3; #ifdef DEBUG printf("%4ld %4ld %4ld ", q, t3, *u); printf("%4ld %4ld %4ld %4ld\n", *d, t1, v1, v3); #endif v1 = t1, v3 = t3; } *v = (*d - a * *u) / b; #ifdef DEBUG printf("----------------------------------\n"); #endif } long inv(long number, long modulus) { long d, u, v; Euclid_extended(number, modulus, &u, &v, &d); if (d == 1) return u; return 0; } void gaussian_elimination(long n, long p, long *b, long *x, long **m) { int found; long *d = calloc(n, sizeof(long)), ck, dj; long i, j, k, l, sum, t; if (!d) { fprintf(stderr, "fatal error\ninsufficient memory\n"); fprintf(stderr, "from gaussian_elimination\n"); exit(1); } for (j = 0; j < n; j++) { found = 0, i = j; while (!found && i < n) { found = m[i][j] != 0; if (!found) i++; } if (!found) { fprintf(stderr, "fatal error\nnon-invertible matrix\n"); fprintf(stderr, "from gaussian_elimination\n"); for (k = 0; k < n; k++) { for (l = 0; l < n; l++) printf("%ld ", m[k][l]); printf("\n"); } exit(1); } if (i > j) { /* swap elements */ for (l = j; l < n; l++) t = m[i][l], m[i][l] = m[j][l], m[j][l] = t; t = b[i], b[i] = b[j], b[j] = t; } dj = d[j] = inv(m[j][j], p); if (dj == 0) { fprintf(stderr, "fatal error\nnon-invertible element\n"); fprintf(stderr, "from gaussian elimination\n"); fprintf(stderr, "element %ld mod %ld\n", m[j][j], p); exit(1); } for (k = j + 1; k < n; k++) { ck = (dj * m[k][j]) % p; for (l = j + 1; l < n; l++) { m[k][l] = (m[k][l] - ck * m[j][l]) % p; if (m[k][l] < 0) m[k][l] += p; } b[k] = (b[k] - ck * b[j]) % p; if (b[k] < 0) b[k] += p; } } for (i = n - 1; i >= 0; i--) { sum = 0; for (j = i + 1; j < n; j++) sum += (m[i][j] * x[j]) % p; if (sum < 0) sum += p; x[i] = (d[i] * (b[i] - sum)) % p; if (x[i] < 0) x[i] += p; } } void solve(long n, long p, long *b, long *x, long **a, long **m) { long i, j; printf("the modulus p = %ld\n", p); printf("the matrix and right-hand side is as follows:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { m[i][j] = a[i][j]; printf("%ld ", m[i][j]); } printf("%ld\n", b[i]); } gaussian_elimination(n, p, b, x, m); printf("the solution vector is as follows:\n"); for (i = 0; i < n; i++) printf("%ld ", x[i]); printf("\n"); } int main(void) { long a[5][5] = {{1, 2, 3, 4, 5}, {6, 1, 2, 3, 4}, {5, 6, 1, 2, 3}, {4, 5, 6, 1, 2}, {3, 4, 5, 6, 1}}; long b[5], i, j, n = 5, p = 7, x[5]; long **c = create_square_matrix(5); long **m = create_square_matrix(5); for (i = 0; i < n; i++) for (j = 0; j < n; j++) c[i][j] = a[i][j]; b[0] = 1, b[1] = 2, b[2] = 3, b[3] = 4, b[4] = 5; solve(n, p, b, x, c, m); c[0][1] = c[1][2] = c[2][3] = c[3][4] = c[4][1] = 0; b[0] = 1, b[1] = 2, b[2] = 3, b[3] = 4, b[4] = 5; solve(n, p, b, x, c, m); delete_square_matrix(n, c); delete_square_matrix(n, m); return 0; }