source: CIVL/examples/compare/CholeskyDecomposition/cholesky2.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: 753 bytes
RevLine 
[9765af0]1/*
2 * Cholesky decomposition;
3 * Code from: https://rosettacode.org/wiki/Cholesky_decomposition#C
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <math.h>
[d420e18]9
[9765af0]10double *cholesky(double *A, int n) {
[d420e18]11 double *L = (double*)malloc(n*n*sizeof(double));
12
13 for (int i = 0; i < n*n; i++)
14 L[i] = 0.0;
15
16 for (int i = 0; i < n; i++)
17 for (int j = 0; j < (i+1); j++) {
18 double sum = 0;
19 for (int k = 0; k < j; k++)
20 sum += L[i * n + k] * L[j * n + k];
21 L[i * n + j] = (i == j) ?
22 sqrt(A[i * n + i] - sum) :
23 (1.0 / L[j * n + j] * (A[i * n + j] - sum));
[9765af0]24 }
[d420e18]25
26 return L;
[9765af0]27}
[d420e18]28
29void show_matrix(double *A, int n) {
30 for (int i = 0; i < n; i++) {
31 for (int j = 0; j < n; j++)
32 printf("%f ", A[i * n + j]);
[9765af0]33 printf("\n");
[d420e18]34 }
[9765af0]35}
[d420e18]36
Note: See TracBrowser for help on using the repository browser.