Update of A0_inv fixed. matMul(...) breaks link between A0_inv and Slater_inv. Fixed by creating a new pointer that points to where Slater_inv points to before it gets reassigned by matMul(...) and then copy the updated elements back to the passed array pointed to by the new pointer.

This commit is contained in:
François Coppens 2021-02-03 14:49:13 +01:00
parent aa6895a4aa
commit d03c9c665b
3 changed files with 36 additions and 35 deletions

View File

@ -72,7 +72,6 @@ T** matMul(T** A, T** B, unsigned int size) {
} }
} }
} }
showMatrix(C, size, "C");
return C; return C;
} }

View File

@ -7,20 +7,15 @@
void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index) { void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index) {
unsigned int k, l, lbar, i, j, tmp, M = *Dim; unsigned int k, l, lbar, i, j, tmp, M = *Dim;
unsigned int *p = new unsigned int[M+1]; unsigned int *p = new unsigned int[M+1];
double *breakdown = new double[M+1]; unsigned int **Id = new unsigned int*[M];
double alpha, beta; double alpha, beta;
double **U, *breakdown = new double[M+1];
for (i = 0; i < M+1; i++) { double **Al = new double*[M];
p[i] = i; p[0] = 0;
}
int **Id = new int*[M];
for (i = 0; i < M; i++) Id[i] = new int[M];
for (i = 0; i < M; i++) { for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) { p[i+1] = i + 1;
if (i != j) Id[i][j] = 0; Id[i] = new unsigned int[M];
else Id[i][j] = 1; Al[i] = new double[M];
}
} }
// Declare auxiliary solution matrix ylk // Declare auxiliary solution matrix ylk
@ -31,6 +26,15 @@ void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, uns
ylk[l][k] = new double[M+1]; ylk[l][k] = new double[M+1];
} }
} }
// Initialize identity matrix
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
if (i != j) Id[i][j] = 0;
else Id[i][j] = 1;
}
}
// Initialize ylk with zeros // Initialize ylk with zeros
for (l = 0; l < M; l++) { for (l = 0; l < M; l++) {
for (k = 0; k < M+1; k++) { for (k = 0; k < M+1; k++) {
@ -73,9 +77,10 @@ void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, uns
} }
// Construct A-inverse from A0-inverse and the ylk // Construct A-inverse from A0-inverse and the ylk
double **U; // Keep the memory location of the passed array 'Slater_inv' before 'Slater_inv'
double **Al = new double*[M]; // gets reassigned by 'matMul(...)' in the next line, by creating a new
for (i = 0; i < M; i++) Al[i] = new double[M]; // pointer 'copy' that points to whereever 'Slater_inv' points to now.
double **copy = Slater_inv;
for (l = 0; l < M; l++) { for (l = 0; l < M; l++) {
k = l+1; k = l+1;
@ -86,23 +91,21 @@ void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, uns
Al[i][j] = Id[i][j] - U[i][j] / beta; Al[i][j] = Id[i][j] - U[i][j] / beta;
} }
} }
showMatrix(Slater_inv, M, "Slater_inv");
Slater_inv = matMul(Al, Slater_inv, M); Slater_inv = matMul(Al, Slater_inv, M);
showMatrix(Slater_inv, M, "Slater_inv");
} }
delete [] p, breakdown; // Assign the new values of 'Slater_inv' to the old values in 'copy[][]'
for (i = 0; i < M; i++) { for (i = 0; i < M; i++) {
delete [] Id[i]; for (j = 0; j < M; j++) {
delete [] U[i]; copy[i][j] = Slater_inv[i][j];
delete [] Al[i]; }
} }
for (l = 0; l < M; l++) { for (l = 0; l < M; l++) {
for (k = 0; k < M+1; k++) { for (k = 0; k < M+1; k++) {
delete [] ylk[l][k]; delete [] ylk[l][k];
} }
delete [] ylk[l]; delete [] ylk[l], Id[l], U[l], Al[l], Slater_inv[l];
} }
delete [] p, breakdown;
} }

View File

@ -66,7 +66,6 @@ int main() {
unsigned int *dim = new unsigned int(M); unsigned int *dim = new unsigned int(M);
unsigned int *n_updates = new unsigned int(M); unsigned int *n_updates = new unsigned int(M);
Sherman_Morrison(A0, A0_inv, dim, n_updates, Ar, Ar_index); Sherman_Morrison(A0, A0_inv, dim, n_updates, Ar, Ar_index);
showMatrix(A0_inv, M, "A0_inv"); showMatrix(A0_inv, M, "A0_inv");
// Deallocate all vectors and matrices // Deallocate all vectors and matrices