I have a source code to compute Cholesky Decomposition in C++. But I can get true result. Thank for editing for me.
#include <iostream>
#include <cmath>
using namespace std;
double **Cholesky_Decomposition(double **p, long m, long n);
double **Create2DArray(long rows, long columns);
void Output2DArray(double **p, long rows, long columns);
// Cholesky_Decomposition returns the Cholesky Decomposition Matrix.
double **Cholesky_Decomposition(double **p, long m, long n)
// Licensing: It is closed and private code.
{
long i, j, k; double temp = 0, temp2 = 0;
if (m != n)
{
cout << "Matrix is not square";
exit(1);
}
// Initialize and populate matrix L which will be the lower Cholesky
double **L = Create2DArray(m, n);
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
{
temp = 0; temp2 = 0;
if (i > j)
{
if (j > 0)
{
for (k = 1; k < j + 1; k++)
temp2 += (L[i][k - 1] * L[j][k - 1]);
}
L[i][j] = (p[i][j] - temp2) / L[j][j];
}
else if (i == j)
{
for (k = 0; k < i; k++)
temp += pow(L[i][k], 2);
L[i][j] = sqrt(p[i][j] - temp);
}
else
L[i][j] = 0;
}
return L;
}
// Create2DArray ia a function that creates a reference (double) dynamic 2D-Array
// using Pointer.
double **Create2DArray(long rows, long columns)
// Licensing: It is closed code.
// Modified Date: 22 Feb 2015
// Author: Huynh Ngoc Vinh, Ho Chi Minh City, Viet Nam
{
double **p = new (nothrow) double *[rows];
for (long i = 0; i < rows; i++) p[i] = new (nothrow) double[columns];
return p;
}
// Output1DArray is a function that prints out the value of 1D-Array.
void Output2DArray(double **p, long rows, long columns)
// Licensing: It is closed code.
// Modified Date: 06 Aug 2015.
// Author: Huynh Ngoc Vinh, Ho Chi Minh City, Viet Nam
{
cout << "2D-matrix: ";
for (long i = 0; i < rows; i++)
{
cout << "\n";
for (long j = 0; j < columns; j++)
cout << p[i][j] << "\t";
}
cout << "\n";
}