source: CIVL/examples/compare/CholeskyDecomposition/driver.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.6 KB
Line 
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
16double *cholesky(double *, int );
17void show_matrix(double *, int );
18void showTranspose(double *A, int n, double (*B)[n]);
19
20int 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
59void 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
Note: See TracBrowser for help on using the repository browser.