SHOW:
|
|
- or go back to the newest paste.
| 1 | // Nie używaj zmiennych globalnych, ani dyrektyw #define | |
| 2 | // Utrudniają one zrobienie ogólnych funkcji. | |
| 3 | ||
| 4 | - | double ** matrix(int n); // alokuje macierz nxn |
| 4 | + | #include <cmath> |
| 5 | - | void free_matrix(double **A, int n); // zwalnia macierz nxn |
| 5 | + | #include <cstdio> |
| 6 | - | double det(double **A, int n); // Liczy wyznacznik macierzy nxn |
| 6 | + | #include <cstdlib> |
| 7 | - | void transpose(double **A, int n); // Transponuje macierz nxn |
| 7 | + | |
| 8 | double ** matrix(int n); // alokuje macierz nxn | |
| 9 | void free_matrix(double **A, int n); // zwalnia macierz nxn | |
| 10 | - | void copyWithout(double **B, const double **A, int r, int k, int n); |
| 10 | + | void transpose(double **A, int n); // Transponuje macierz nxn |
| 11 | void print_matrix(double **A, int n); // Wypisuje macierz nxn na ekran | |
| 12 | ||
| 13 | // Liczy wyznacznik macierzy nxn. Nie powinien zmieniac macierzy A. | |
| 14 | double det(double **A, int n); | |
| 15 | ||
| 16 | // Kopiuje z A do B wszystkie elementy oprócz elementów z wiersza r i kolumny k | |
| 17 | void copy_without(double **B, double **A, int r, int k, int n); | |
| 18 | ||
| 19 | // Odwraca macierz nxn | |
| 20 | - | copyWithout(B, A, r, k, n); |
| 20 | + | |
| 21 | static const double eps = 1e-10; | |
| 22 | double detA = det(A, n); | |
| 23 | if (fabs(detA) < eps) {
| |
| 24 | printf("Macierz jest osobliwa!\n");
| |
| 25 | return; | |
| 26 | } | |
| 27 | ||
| 28 | - | A[r][k] = D[r][k]; |
| 28 | + | |
| 29 | double **D = matrix(n); // Macierz dopełnień. | |
| 30 | ||
| 31 | // W każdej iteracji będziemy rozwijać wzór na elemencie A[r][k] | |
| 32 | for (int r = 0; r < n; r++) {
| |
| 33 | for (int k = 0; k < n; k++) {
| |
| 34 | copy_without(B, A, r, k, n); | |
| 35 | int sign = ((r+k)%2 == 0) ? 1 : -1; | |
| 36 | D[r][k] = sign * det(B, n-1); | |
| 37 | } | |
| 38 | } | |
| 39 | transpose(D, n); | |
| 40 | for (int r = 0; r < n; r++) {
| |
| 41 | for (int k = 0; k < n; k++) {
| |
| 42 | A[r][k] = D[r][k] / detA; | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | free_matrix(D, n); | |
| 47 | free_matrix(B, n-1); | |
| 48 | } | |
| 49 | ||
| 50 | int main(void) {
| |
| 51 | printf("dla A:\n");
| |
| 52 | ||
| 53 | double **A = matrix(2); | |
| 54 | A[0][0] = 2; A[0][1] = 1; | |
| 55 | A[1][0] = 5; A[1][1] = 3; | |
| 56 | print_matrix(A, 2); | |
| 57 | inverse(A, 2); | |
| 58 | print_matrix(A, 2); | |
| 59 | free_matrix(A, 2); | |
| 60 | ||
| 61 | /* | |
| 62 | dla A: | |
| 63 | 2 1 | |
| 64 | 5 3 | |
| 65 | ||
| 66 | 3 -1 | |
| 67 | -5 2 | |
| 68 | */ | |
| 69 | ||
| 70 | printf("dla B:\n");
| |
| 71 | srand(4); | |
| 72 | double **B = matrix(4); | |
| 73 | for (int i = 0; i < 4; i++) {
| |
| 74 | for (int j = 0; j < 4; j++) {
| |
| 75 | B[i][j] = (rand() % 10); | |
| 76 | } | |
| 77 | } | |
| 78 | print_matrix(B, 4); | |
| 79 | inverse(B, 4); | |
| 80 | print_matrix(B, 4); | |
| 81 | free_matrix(B, 4); | |
| 82 | ||
| 83 | /* | |
| 84 | dla B: | |
| 85 | 1 5 9 6 | |
| 86 | 5 4 5 6 | |
| 87 | 0 0 1 7 | |
| 88 | 2 0 2 7 | |
| 89 | ||
| 90 | -0.0894569 0.111821 -0.284345 0.265176 | |
| 91 | -0.0734824 0.341853 0.587859 -0.817891 | |
| 92 | 0 0 -0.43131 0.469649 | |
| 93 | 0 0 0.204473 -0.0670927 | |
| 94 | */ | |
| 95 | ||
| 96 | return 0; | |
| 97 | } |