/* * CHOLESKI'S ALGORITHM 6.6 * * To factor the positive definite n by n matrix A into LL**T, * where L is lower triangular. * Example 6.6.3 * * INPUT: the dimension n; entries A(I,J), 1<=I, J<=n of A. * * OUTPUT: the entries L(I,J), 1<=J<=I, 1<=I<=n of L. * * the entries of U = L**T are U(I,J) = L(J,I), I<=J<=n, 1<=I<=n */ #include #include #define true 1 #define false 0 #define N 3 // void INPUT(int *, double [][N], int *); void OUTPUT(double [][N]); main() { double A[N][N] = {{4.0, -1.0, 1.0}, {-1.0, 4.25, 2.75}, {1.0, 2.75, 3.5} }; double S; int I,J,K,NN,JJ,KK; /* STEP 1 */ A[0][0] = sqrt(A[0][0]); /* STEP 2 */ for (J=2; J<=N; J++) A[J-1][0] = A[J-1][0] / A[0][0]; /* STEP 3 */ NN = N - 1; for (I=2; I<=NN; I++) { /* STEP 4 */ KK = I - 1; S = 0.0; for (K=1; K<=KK; K++) S = S - A[I-1][K-1] * A[I-1][K-1]; A[I-1][I-1] = sqrt(A[I-1][I-1] + S); /* STEP 5 */ JJ = I + 1; for (J=JJ; J<=N; J++) { S = 0.0; KK = I - 1; for (K=1; K<=KK; K++) S = S - A[J-1][K-1] * A[I-1][K-1]; A[J-1][I-1] = (A[J-1][I-1] + S) / A[I-1][I-1]; } } /* STEP 6 */ S = 0.0; for (K=1; K<=NN; K++) S = S - A[N-1][K-1] * A[N-1][K-1]; A[N-1][N-1] = sqrt( A[N-1][N-1] + S); /* STEP 7 */ OUTPUT(A); return 0; } void OUTPUT(double A[][N]) { int I, J, FLAG; char NAME[30]; FILE *OUP; printf("Choice of output method:\n"); printf("1. Output to screen\n"); printf("2. Output to text file\n"); printf("Please enter 1 or 2.\n"); scanf("%d", &FLAG); if (FLAG == 2) { printf("Input the file name in the form - drive:name.ext\n"); printf("for example: A:OUTPUT.DTA\n"); scanf("%s", NAME); OUP = fopen(NAME, "w"); } else OUP = stdout; fprintf(OUP, "CHOLESKI FACTORIZATION\n\n"); fprintf(OUP, "The matrix L output by rows:\n"); for (I=1; I<=N; I++) { for (J=1; J<=I; J++) fprintf(OUP, " %12.8f", A[I-1][J-1]); fprintf(OUP, "\n"); } fclose(OUP); }