| 1 | /*
|
|---|
| 2 | * Driver to verify the correctness of Cholesky decomposition
|
|---|
| 3 | * function by CIVL.
|
|---|
| 4 | * Command: civl verify -checkDivisionByZero=false driver.c cholesky2.c
|
|---|
| 5 | * Author: Si Li (sili@udel.edu)
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include <civlc.cvh>
|
|---|
| 9 | #include <stdlib.h>
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 |
|
|---|
| 12 | $input int n = 3;
|
|---|
| 13 | $input double diag1,diag2,diag3,off1,off2,off3;
|
|---|
| 14 | //$input double a1, a2, a3;
|
|---|
| 15 |
|
|---|
| 16 | double *cholesky(double *, int );
|
|---|
| 17 | void show_matrix(double *, int );
|
|---|
| 18 | void showTranspose(double *A, int n, double (*B)[n]);
|
|---|
| 19 |
|
|---|
| 20 | int main () {
|
|---|
| 21 | // double A[] = {a1, a2,
|
|---|
| 22 | // a2, a3};
|
|---|
| 23 | double A[] = {diag1, off1, off2,
|
|---|
| 24 | off1, diag1, off3,
|
|---|
| 25 | off2, off3, diag1};
|
|---|
| 26 | double B[n][n];
|
|---|
| 27 | double BT[n][n];
|
|---|
| 28 | double C[n][n];
|
|---|
| 29 |
|
|---|
| 30 | double *factor = cholesky(A, n);
|
|---|
| 31 |
|
|---|
| 32 | printf("\nThe factor is:\n");
|
|---|
| 33 | show_matrix(factor, n);
|
|---|
| 34 |
|
|---|
| 35 | printf("\nThe transpose of factor is:\n");
|
|---|
| 36 | showTranspose(factor, n, B);
|
|---|
| 37 |
|
|---|
| 38 | for (int i = 0; i < n; i++)
|
|---|
| 39 | for (int j = 0; j < n; j++)
|
|---|
| 40 | BT[i][j] = B[j][i];
|
|---|
| 41 |
|
|---|
| 42 | //calculate factor multiply its transpose
|
|---|
| 43 | for (int i = 0; i < n; i++)
|
|---|
| 44 | for (int j = 0; j < n; j++) {
|
|---|
| 45 | C[i][j] = 0.0;
|
|---|
| 46 | for (int k =0; k < n; k++)
|
|---|
| 47 | C[i][j] += B[i][k]*BT[k][j];
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // check if factor is correct
|
|---|
| 51 | for (int i = 0; i < n; i++)
|
|---|
| 52 | for (int j = 0; j < n; j++)
|
|---|
| 53 | $assert(C[i][j] == A[i * n + j]);
|
|---|
| 54 |
|
|---|
| 55 | free(factor);
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void showTranspose(double *A, int n, double (*B)[n])
|
|---|
| 60 | {
|
|---|
| 61 | for (int i = 0; i < n; i++) {
|
|---|
| 62 | for (int j = 0; j < n; j++) {
|
|---|
| 63 | B[i][j] = A[i * n + j];
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | for (int i = 0; i < n; i++) {
|
|---|
| 68 | for (int j = 0; j < n; j++)
|
|---|
| 69 | printf("%f ", B[j][i]);
|
|---|
| 70 | printf("\n");
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|