// Urysohn.cpp : Defines the entry point for the console application. // #include #include #include #include #include //main method for Cholesky decomposition. //input n size of matrix //input/output a Symmetric positive def. matrix //output p vector of resulting diag of a //author: void choldc1(int n, double** a, double* p) { int i,j,k; double sum; for( i = 0; i < n; i++ ) { for( j = i; j < n; j++ ) { sum = a[i][j]; for( k = i - 1; k >= 0; k-- ) { sum -= a[i][k] * a[j][k]; } if( i == j ) { if( sum <= 0 ) { printf(" a is not positive definite!\n"); } p[i] = sqrt(sum); } else { a[j][i] = sum / p[i]; } } } } //Cholesky decomposition. //input n size of matrix //input A Symmetric positive def. matrix //output a lower deomposed matrix //uses choldc1(int,MAT,VEC) void choldc(int n, double** A, double** a) { int i,j; double* p = (double*)malloc( n * sizeof(double) ); for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) a[i][j] = A[i][j]; choldc1(n, a, p); for( i = 0; i < n; i++ ) { a[i][i] = p[i]; for( j = i + 1; j < n; j++ ) { a[i][j] = 0; } } if( p ) free( p ); } /////////////////////////////////////////////////////////////////// //solution for M * x = y //M must be positive definite void CholeskySolution(double** M, double* x, double* y, int size) { //declare matrix double** T = (double**)malloc( size * sizeof(double*) ); for( int i = 0; i < size; ++i ) { T[i] = (double*)malloc( size * sizeof( double ) ); } //find lower triangular choldc(size, M, T); //make is symmetric for( int i = 0; i < size; ++i ) for( int j = i+1; j < size; ++j ) T[i][j] = T[j][i]; double* v = (double*)malloc( size * sizeof( double ) ); //solve lower triangular for( int i = 0; i < size; ++i) { double sum = 0.0; for( int j = 0; j < i; ++j ) { sum += T[i][j] * v[j]; } v[i] = (y[i] - sum) / T[i][i]; } //solve upper triangular for( int i = size - 1; i >= 0; --i) { double sum = 0.0; for( int j = size - 1; j > i; --j ) { sum += T[i][j] * x[j]; } x[i] = (v[i] - sum) / T[i][i]; } if( v ) free( v); if( T ) { for( int i = 0; i < size; ++i ) { if(T[i]) free(T[i]); } } } bool UrysohnIdentification( int** data, double* result, double** U, //this is for self test and reference only double** UI, const int datarows, const int datacols, const int datamin, const int datamax) { if ((datamax - datamin) < 1) return false; const int range = datamax - datamin + 1; const int vectorsize = range * datacols; //declaration of data matrix double** M = (double**)malloc(vectorsize*sizeof(double*)); if (!M) return false; for (int i=0; i