Created
January 19, 2023 22:39
-
-
Save Leowbattle/84760e1bfa3afcb4fb19c3543a4e8128 to your computer and use it in GitHub Desktop.
cholesky.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <math.h> | |
| void cholesky(float* m, int n) { | |
| for (int i = 0; i < n; i++) { | |
| for (int j = 0; j < i; j++) { | |
| float sum = 0; | |
| for (int k = 0; k < j; k++) { | |
| sum += m[i * n + k] * m[j * n + k]; | |
| } | |
| m[i * n + j] = (m[i * n + j] - sum) / m[j * n + j]; | |
| } | |
| float sum = 0; | |
| for (int j = 0; j < i; j++) { | |
| float f = m[i * n + j]; | |
| sum += f * f; | |
| } | |
| m[i * n + i] = sqrtf(m[i * n + i] - sum); | |
| } | |
| } | |
| void print_matrix(float* m, int n) { | |
| for (int i = 0; i < n; i++) { | |
| for (int j = 0; j < n; j++) { | |
| printf("%f ", m[i * n + j]); | |
| } | |
| printf("\n"); | |
| } | |
| printf("\n"); | |
| } | |
| int main(int argc, char** argv) { | |
| int n; | |
| scanf("%d", &n); | |
| float* m = malloc(n * n * sizeof(float)); | |
| for (int i = 0; i < n; i++) { | |
| for (int j = 0; j < n; j++) { | |
| float x; | |
| scanf("%f", &x); | |
| m[i * n + j] = x; | |
| } | |
| } | |
| cholesky(m, n); | |
| print_matrix(m, n); | |
| free(m); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment