source: CIVL/examples/compare/CholeskyDecomposition/cholesky3.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: 2.7 KB
Line 
1/************************************************************
2 * cholesky.c main program for testing Cholesky *
3 * decomposition routine cholesky() *
4 * *
5 * Created by William J. De Meo *
6 * on 11/29/97 *
7 * *
8 * http://www.math.hawaii.edu/~williamdemeo/C/stat243/reports/hw3/hw3/node6.html
9 ************************************************************/
10#include <stdlib.h>
11#include <stdio.h>
12#include <math.h>
13#include "prototypes.h"
14#define MAX_NAME 100
15
16void cholesky(long N, double *A, double *diag);
17void read_name(char *);
18
19int main()
20{
21 char *filename;
22 double *A, *diag;
23 long i, j, dim;
24
25 filename = cmalloc(MAX_NAME);
26
27 printf("\nEnter file name containing the spd matrix: ");
28 read_name(filename);
29 printf("\nEnter its dimension: ");
30 scanf("%d",&dim);
31
32 A = dmalloc(dim*dim);
33 diag = dmalloc(dim);
34
35 matlabread(A, dim, dim, filename);
36 /*matrix is stored contiguously column-wise */
37
38 cholesky(dim,A,diag);
39
40 printf("\nThe Cholesky factor is: \nL = \n");
41 for(i=0;i<dim;i++)
42 {
43 for(j=0;j<i;j++)
44 printf("%4.5lf \t", A[dim*j+i]);
45 printf("%4.5lf", diag[i]);
46 printf("\n");
47 }
48
49}
50
51void read_name(char *name)
52{
53 int c, i = 0;
54
55 while ((c = getchar()) != EOF && c != ' ' && c != '\n')
56 name[i++] = c;
57 name[i] = '\0';
58}
59
60/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 cholesky.c
62
63 Created on 11/29/97 by William J. De Meo
64
65 Purpose: Cholesky decomposition of an n-by-n spd matrix
66
67~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
68
69/* Subroutine cholesky:
70
71 Arguments:
72 N dimension of A
73
74 A
75 on entry: the N by N matrix to be decomposed
76 on exit: upper triangle is still A
77 lower sub-triangle is the sub-trangle
78 of the Cholesky factor L
79
80 diag
81 on entry: an arbitrary vector of length N
82 on exit: the diagonal of the Cholesky factor L
83
84*/
85
86void cholesky(long N, double *A, double *diag)
87{
88 long i,j,k;
89 for(j=0;j<N;j++)
90 diag[j] = A[N*j+j];
91 for(j=0;j<N;j++)
92 {
93 for(k=0;k<j;k++)
94 diag[j] -= A[N*k+j]*A[N*k+j];
95 diag[j] = sqrt(diag[j]);
96 for(i=j+1;i<N;i++)
97 {
98 for(k=0;k<j;k++)
99 A[N*j+i] -= A[N*k+i]*A[N*k+j];
100 A[N*j+i]/=diag[j];
101 }
102 }
103}
Note: See TracBrowser for help on using the repository browser.