Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nie używaj zmiennych globalnych, ani dyrektyw #define
- // Utrudniają one zrobienie ogólnych funkcji.
- double ** matrix(int n); // alokuje macierz nxn
- void free_matrix(double **A, int n); // zwalnia macierz nxn
- double det(double **A, int n); // Liczy wyznacznik macierzy nxn
- void transpose(double **A, int n); // Transponuje macierz nxn
- // Kopiuje z A do B wszystkie elementy oprócz elementów z wiersza r i kolumny k
- void copyWithout(double **B, const double **A, int r, int k, int n);
- // Odwraca macierz nxn
- void inverse(double **A, int n) {
- double **B = matrix(n-1); // Macierz pomocnicza.
- double **D = matrix(n); // Macierz dopełnień.
- // W każdej iteracji będziemy rozwijać wzór na elemencie A[r][k]
- for (int r = 0; r < n; r++) {
- for (int k = 0; k < n; k++) {
- copyWithout(B, A, r, k, n);
- int sign = ((r+k)%2 == 0) ? 1 : -1;
- D[r][k] = sign * det(B, n-1);
- }
- }
- transpose(D, n);
- for (int r = 0; r < n; r++) {
- for (int k = 0; k < n; k++) {
- A[r][k] = D[r][k];
- }
- }
- free_matrix(D, n);
- free_matrix(B, n-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment