Ausgabe u.a mit WebTeX, nicht editiert. Für große Matrizen ist dieser Algorithmus eher nicht geeignet. Laufzeit. Aber, im Gegensatz zu Microsoft Github Copilot, besserere Implementierung, prüft auf Singularität. Hardware: Ryzen 3950 Speicher 64 GB Grafikkarte: RX 5700 XT gpt-oss-b20 We need to write Gauss L‑R decomposition in C. Provide code. Use citation irrelevant. Just answer. Below is a **compact, self‑contained implementation** of the **Gauss (LU) decomposition with partial pivoting** – often called *L* – *R* or *L* – *U* decomposition in numerical linear algebra. The routine: ```c int lu_decompose(double **A, int n, int *pivot); ``` * `A` – matrix (modified in‑place to hold the L‑ and U‑parts). The strictly lower part contains the multipliers of L; the diagonal and upper part contain U. * `n` – order of the square matrix. * `pivot` – array of size *n* that stores the row permutation (1‑based indices for readability). The function returns **0** on success, or a non‑zero error code if the matrix is singular (a pivot element is zero). After the call you can use `A` together with `pivot` to solve linear systems or compute the determinant. ```c #include #include /* ------------------------------------------------------------ */ /* LU decomposition with partial pivoting */ /* ------------------------------------------------------------ */ int lu_decompose(double **A, int n, int *pivot) { /* initialise pivot vector to identity permutation */ for (int i = 0; i < n; ++i) pivot[i] = i; for (int k = 0; k < n - 1; ++k) { /* ----- partial pivoting --------------------------------- */ double max = 0.0; int piv_row = k; for (int i = k; i < n; ++i) { double val = fabs(A[i][k]); if (val > max) { max = val; piv_row = i; } } /* singular matrix? */ if (max == 0.0) return -1; /* pivot is zero – matrix is singular */ /* swap rows k and piv_row in A and record permutation */ if (piv_row != k) { double *tmp = A[k]; A[k] = A[piv_row]; A[piv_row] = tmp; int ptmp = pivot[k]; pivot[k] = pivot[piv_row]; pivot[piv_row] = ptmp; } /* ----- elimination -------------------------------------- */ for (int i = k + 1; i < n; ++i) { A[i][k] /= A[k][k]; // multiplier – part of L double aik = A[i][k]; for (int j = k + 1; j < n; ++j) A[i][j] -= aik * A[k][j]; } } /* final pivot check */ if (fabs(A[n-1][n-1]) == 0.0) return -1; return 0; } /* ------------------------------------------------------------ */ /* Helper to print a matrix */ /* ------------------------------------------------------------ */ void print_matrix(double **A, int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) printf("%10.4f ", A[i][j]); puts(""); } } /* ------------------------------------------------------------ */ /* Example usage */ /* ------------------------------------------------------------ */ int main(void) { int n = 3; double data[3][3] = { { 2, 1, 1 }, { 4, -6, 0 }, {-2, 7, 8 } }; /* Allocate matrix of pointers for the routine */ double *A[n]; for (int i = 0; i < n; ++i) A[i] = data[i]; int pivot[n]; if (lu_decompose(A, n, pivot) != 0) { fprintf(stderr, "Matrix is singular!\n"); return EXIT_FAILURE; } printf("LU decomposition (L on lower part, U on upper part):\n"); print_matrix(A, n); printf("\nPivot permutation:\n"); for (int i = 0; i < n; ++i) printf("%d ", pivot[i] + 1); /* +1 to show 1‑based indices */ puts(""); return EXIT_SUCCESS; } ``` ### How the routine works | Step | What happens | |------|--------------| | **Pivot vector** – starts as the identity permutation. | | **Partial pivoting** – for each column `k`, find the row with the largest absolute value in that column (`max`). Swap rows if needed and record the swap in `pivot`. | | **Elimination** – compute multipliers `A[i][k] /= A[k][k]` (stored in the lower part of the matrix). Then update the remaining sub‑matrix. | | **Result** – after the loop, `A` contains both L and U: *L* has unit diagonal (`1`s on the main diagonal are not stored; you can treat the diagonal of `A` as 1 when using it). The upper triangular part (including the diagonal) is *U*. | | **Pivot vector** – after decomposition, applying the permutation to a right‑hand side vector `b` gives the correctly permuted system. | ### Using the result * **Solving Ax = b**: 1. Apply the pivot permutation to `b`. 2. Forward substitution with L (lower part). 3. Backward substitution with U (upper part). * **Determinant**: \(\det(A) = (-1)^{\text{number of row swaps}} \times \prod_{i=0}^{n-1} A[i][i]\) Feel free to adapt the code for double‑precision (`float`), sparse matrices, or other variations (e.g., no pivoting).