mirror of
https://github.com/TREX-CoE/Sherman-Morrison.git
synced 2024-12-27 06:43:55 +01:00
Ran: clang-format --style=LLVM on *.cpp and *.hpp files.
This commit is contained in:
parent
fad8eb2e89
commit
e5648f7485
@ -1,9 +1,9 @@
|
|||||||
// SM_Helpers.hpp
|
// SM_Helpers.hpp
|
||||||
// Some usefull helper functions to support the Maponi algorithm.
|
// Some usefull helper functions to support the Maponi algorithm.
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
void Switch(unsigned int *p, unsigned int l, unsigned int lbar);
|
void Switch(unsigned int *p, unsigned int l, unsigned int lbar);
|
||||||
|
|
||||||
@ -11,8 +11,7 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates,
|
|||||||
unsigned int *Updates_index, unsigned int *p,
|
unsigned int *Updates_index, unsigned int *p,
|
||||||
double ***ylk);
|
double ***ylk);
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void showScalar(T scalar, std::string name) {
|
||||||
void showScalar(T scalar, std::string name) {
|
|
||||||
std::cout << name << " = " << scalar << std::endl << std::endl;
|
std::cout << name << " = " << scalar << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,8 +33,7 @@ void showMatrix(T *matrix, unsigned int M, std::string name) {
|
|||||||
for (unsigned int j = 0; j < M; j++) {
|
for (unsigned int j = 0; j < M; j++) {
|
||||||
if (matrix[i * M + j] >= 0) {
|
if (matrix[i * M + j] >= 0) {
|
||||||
std::cout << " " << matrix[i * M + j] << ",";
|
std::cout << " " << matrix[i * M + j] << ",";
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
std::cout << " " << matrix[i * M + j] << ",";
|
std::cout << " " << matrix[i * M + j] << ",";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,8 +43,7 @@ void showMatrix(T *matrix, unsigned int M, std::string name) {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> T *transpose(T *A, unsigned int M) {
|
||||||
T *transpose(T *A, unsigned int M) {
|
|
||||||
T *B = new T[M * M];
|
T *B = new T[M * M];
|
||||||
for (unsigned int i = 0; i < M; i++) {
|
for (unsigned int i = 0; i < M; i++) {
|
||||||
for (unsigned int j = 0; j < M; j++) {
|
for (unsigned int j = 0; j < M; j++) {
|
||||||
@ -56,8 +53,7 @@ T *transpose(T *A, unsigned int M) {
|
|||||||
return B;
|
return B;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void matMul(T *A, T *B, T *C, unsigned int M) {
|
||||||
void matMul(T *A, T *B, T *C, unsigned int M) {
|
|
||||||
memset(C, 0, M * M * sizeof(T));
|
memset(C, 0, M * M * sizeof(T));
|
||||||
for (unsigned int i = 0; i < M; i++) {
|
for (unsigned int i = 0; i < M; i++) {
|
||||||
for (unsigned int j = 0; j < M; j++) {
|
for (unsigned int j = 0; j < M; j++) {
|
||||||
@ -79,19 +75,17 @@ T1 *outProd(T1 *vec1, T2 *vec2, unsigned int M) {
|
|||||||
return C;
|
return C;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> T matDet(T **A, unsigned int M) {
|
||||||
T matDet(T **A, unsigned int M) {
|
|
||||||
int det = 0, p, h, k, i, j;
|
int det = 0, p, h, k, i, j;
|
||||||
T **temp = new T *[M];
|
T **temp = new T *[M];
|
||||||
for (int i = 0; i < M; i++) temp[i] = new T[M];
|
for (int i = 0; i < M; i++)
|
||||||
|
temp[i] = new T[M];
|
||||||
if (M == 1) {
|
if (M == 1) {
|
||||||
return A[0][0];
|
return A[0][0];
|
||||||
}
|
} else if (M == 2) {
|
||||||
else if(M == 2) {
|
|
||||||
det = (A[0][0] * A[1][1] - A[0][1] * A[1][0]);
|
det = (A[0][0] * A[1][1] - A[0][1] * A[1][0]);
|
||||||
return det;
|
return det;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
for (p = 0; p < M; p++) {
|
for (p = 0; p < M; p++) {
|
||||||
h = 0;
|
h = 0;
|
||||||
k = 0;
|
k = 0;
|
||||||
@ -115,12 +109,13 @@ T matDet(T **A, unsigned int M) {
|
|||||||
delete[] temp;
|
delete[] temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool is_identity(T *A, unsigned int M, double tolerance) {
|
||||||
bool is_identity(T *A, unsigned int M, double tolerance) {
|
|
||||||
for (unsigned int i = 0; i < M; i++) {
|
for (unsigned int i = 0; i < M; i++) {
|
||||||
for (unsigned int j = 0; j < M; j++) {
|
for (unsigned int j = 0; j < M; j++) {
|
||||||
if (i==j && fabs(A[i*M+j]-1) > tolerance) return false;
|
if (i == j && fabs(A[i * M + j] - 1) > tolerance)
|
||||||
if (i!=j && fabs(A[i*M+j]) > tolerance) return false;
|
return false;
|
||||||
|
if (i != j && fabs(A[i * M + j]) > tolerance)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
void MaponiA3(double *Slater_inv, unsigned int Dim,
|
void MaponiA3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
unsigned int N_updates, double *Updates,
|
double *Updates, unsigned int *Updates_index);
|
||||||
unsigned int *Updates_index);
|
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
void MaponiA3S(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
unsigned int N_updates, double *Updates,
|
double *Updates, unsigned int *Updates_index);
|
||||||
unsigned int *Updates_index);
|
|
||||||
|
@ -19,7 +19,8 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates,
|
|||||||
breakdown = abs(1 + ylk[l][index][component]);
|
breakdown = abs(1 + ylk[l][index][component]);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "Inside selectLargestDenominator()" << std::endl;
|
std::cout << "Inside selectLargestDenominator()" << std::endl;
|
||||||
std::cout << "breakdown = abs(1 + ylk[" << l << "][" << index << "][" << component << "]) = " << breakdown << std::endl;
|
std::cout << "breakdown = abs(1 + ylk[" << l << "][" << index << "]["
|
||||||
|
<< component << "]) = " << breakdown << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
if (breakdown > max) {
|
if (breakdown > max) {
|
||||||
|
@ -6,9 +6,8 @@
|
|||||||
|
|
||||||
// #define DEBUG
|
// #define DEBUG
|
||||||
|
|
||||||
void MaponiA3(double *Slater_inv, unsigned int Dim,
|
void MaponiA3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
unsigned int N_updates, double *Updates,
|
double *Updates, unsigned int *Updates_index) {
|
||||||
unsigned int *Updates_index) {
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
DECLARE AND INITIALISE ARRAYS
|
DECLARE AND INITIALISE ARRAYS
|
||||||
@ -35,8 +34,6 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
START ALGORITHM
|
START ALGORITHM
|
||||||
*/
|
*/
|
||||||
@ -50,8 +47,8 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
|
|||||||
#endif
|
#endif
|
||||||
for (i = 1; i < Dim + 1; i++) {
|
for (i = 1; i < Dim + 1; i++) {
|
||||||
for (j = 1; j < Dim + 1; j++) {
|
for (j = 1; j < Dim + 1; j++) {
|
||||||
ylk[0][k][i] += Slater_inv[(i-1)*Dim + (j-1)]
|
ylk[0][k][i] += Slater_inv[(i - 1) * Dim + (j - 1)] *
|
||||||
* Updates[(k-1)*Dim + (j-1)];
|
Updates[(k - 1) * Dim + (j - 1)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@ -75,7 +72,8 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "p[l+1] = " << p[l + 1] << std::endl;
|
std::cout << "p[l+1] = " << p[l + 1] << std::endl;
|
||||||
std::cout << "component = " << component << std::endl;
|
std::cout << "component = " << component << std::endl;
|
||||||
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "] = " << beta << std::endl;
|
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
|
||||||
|
<< "] = " << beta << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
if (fabs(beta) < 1e-3) {
|
if (fabs(beta) < 1e-3) {
|
||||||
@ -86,14 +84,16 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
|
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
|
||||||
std::cout << "component = " << component << std::endl;
|
std::cout << "component = " << component << std::endl;
|
||||||
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << std::endl;
|
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
|
||||||
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l+1] << "][:]" << std::endl;
|
<< "]" << std::endl;
|
||||||
|
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l + 1] << "][:]"
|
||||||
|
<< std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
for (i = 0; i < Dim; i++) {
|
for (i = 0; i < Dim; i++) {
|
||||||
for (j = 0; j < Dim; j++) {
|
for (j = 0; j < Dim; j++) {
|
||||||
Al[i*Dim + j] = (i == j) - (j == component-1)
|
Al[i * Dim + j] =
|
||||||
* ylk[l][p[l+1]][i + 1] / beta;
|
(i == j) - (j == component - 1) * ylk[l][p[l + 1]][i + 1] / beta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
matMul(Al, last, next, Dim);
|
matMul(Al, last, next, Dim);
|
||||||
@ -113,15 +113,12 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
for (i = 1; i < Dim + 1; i++) {
|
for (i = 1; i < Dim + 1; i++) {
|
||||||
ylk[l+1][p[k]][i] = ylk[l][p[k]][i]
|
ylk[l + 1][p[k]][i] = ylk[l][p[k]][i] - alpha * ylk[l][p[l + 1]][i];
|
||||||
- alpha * ylk[l][p[l+1]][i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
|
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
CLEANUP MEMORY
|
CLEANUP MEMORY
|
||||||
*/
|
*/
|
||||||
@ -139,8 +136,6 @@ extern "C" {
|
|||||||
void MaponiA3_f(double **linSlater_inv, unsigned int *Dim,
|
void MaponiA3_f(double **linSlater_inv, unsigned int *Dim,
|
||||||
unsigned int *N_updates, double **linUpdates,
|
unsigned int *N_updates, double **linUpdates,
|
||||||
unsigned int **Updates_index) {
|
unsigned int **Updates_index) {
|
||||||
MaponiA3(*linSlater_inv, *Dim,
|
MaponiA3(*linSlater_inv, *Dim, *N_updates, *linUpdates, *Updates_index);
|
||||||
*N_updates, *linUpdates,
|
|
||||||
*Updates_index);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,8 @@
|
|||||||
|
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
|
|
||||||
void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
void MaponiA3S(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
unsigned int N_updates, double *Updates,
|
double *Updates, unsigned int *Updates_index) {
|
||||||
unsigned int *Updates_index) {
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
DECLARE AND INITIALISE ARRAYS
|
DECLARE AND INITIALISE ARRAYS
|
||||||
*/
|
*/
|
||||||
@ -35,8 +33,6 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
START ALGORITHM
|
START ALGORITHM
|
||||||
*/
|
*/
|
||||||
@ -50,8 +46,8 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
|||||||
#endif
|
#endif
|
||||||
for (i = 1; i < Dim + 1; i++) {
|
for (i = 1; i < Dim + 1; i++) {
|
||||||
for (j = 1; j < Dim + 1; j++) {
|
for (j = 1; j < Dim + 1; j++) {
|
||||||
ylk[0][k][i] += Slater_inv[(i-1)*Dim + (j-1)]
|
ylk[0][k][i] += Slater_inv[(i - 1) * Dim + (j - 1)] *
|
||||||
* Updates[(k-1)*Dim + (j-1)];
|
Updates[(k - 1) * Dim + (j - 1)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@ -75,7 +71,8 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "p[l+1] = " << p[l + 1] << std::endl;
|
std::cout << "p[l+1] = " << p[l + 1] << std::endl;
|
||||||
std::cout << "component = " << component << std::endl;
|
std::cout << "component = " << component << std::endl;
|
||||||
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "] = " << beta << std::endl;
|
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
|
||||||
|
<< "] = " << beta << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
if (fabs(beta) < 1e-3) {
|
if (fabs(beta) < 1e-3) {
|
||||||
@ -86,14 +83,16 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
|
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
|
||||||
std::cout << "component = " << component << std::endl;
|
std::cout << "component = " << component << std::endl;
|
||||||
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << std::endl;
|
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
|
||||||
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l+1] << "][:]" << std::endl;
|
<< "]" << std::endl;
|
||||||
|
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l + 1] << "][:]"
|
||||||
|
<< std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
for (i = 0; i < Dim; i++) {
|
for (i = 0; i < Dim; i++) {
|
||||||
for (j = 0; j < Dim; j++) {
|
for (j = 0; j < Dim; j++) {
|
||||||
Al[i*Dim + j] = (i == j) - (j == component-1)
|
Al[i * Dim + j] =
|
||||||
* ylk[l][p[l+1]][i + 1] / beta;
|
(i == j) - (j == component - 1) * ylk[l][p[l + 1]][i + 1] / beta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
matMul(Al, last, next, Dim);
|
matMul(Al, last, next, Dim);
|
||||||
@ -113,15 +112,12 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
#endif
|
#endif
|
||||||
for (i = 1; i < Dim + 1; i++) {
|
for (i = 1; i < Dim + 1; i++) {
|
||||||
ylk[l+1][p[k]][i] = ylk[l][p[k]][i]
|
ylk[l + 1][p[k]][i] = ylk[l][p[k]][i] - alpha * ylk[l][p[l + 1]][i];
|
||||||
- alpha * ylk[l][p[l+1]][i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
|
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
CLEANUP MEMORY
|
CLEANUP MEMORY
|
||||||
*/
|
*/
|
||||||
@ -139,8 +135,6 @@ extern "C" {
|
|||||||
void MaponiA3S_f(double **linSlater_inv, unsigned int *Dim,
|
void MaponiA3S_f(double **linSlater_inv, unsigned int *Dim,
|
||||||
unsigned int *N_updates, double **linUpdates,
|
unsigned int *N_updates, double **linUpdates,
|
||||||
unsigned int **Updates_index) {
|
unsigned int **Updates_index) {
|
||||||
MaponiA3S(*linSlater_inv, *Dim,
|
MaponiA3S(*linSlater_inv, *Dim, *N_updates, *linUpdates, *Updates_index);
|
||||||
*N_updates, *linUpdates,
|
|
||||||
*Updates_index);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,46 +11,38 @@ double threshold() {
|
|||||||
|
|
||||||
// Naïve Sherman Morrison
|
// Naïve Sherman Morrison
|
||||||
void SM1(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
void SM1(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
double *Updates, unsigned int *Updates_index)
|
double *Updates, unsigned int *Updates_index) {
|
||||||
{
|
|
||||||
|
|
||||||
std::cerr << "Called SM1 with " << N_updates << " updates" << std::endl;
|
std::cerr << "Called SM1 with " << N_updates << " updates" << std::endl;
|
||||||
double C[Dim];
|
double C[Dim];
|
||||||
double D[Dim];
|
double D[Dim];
|
||||||
|
|
||||||
unsigned int l = 0;
|
unsigned int l = 0;
|
||||||
// For each update
|
// For each update
|
||||||
while (l < N_updates)
|
while (l < N_updates) {
|
||||||
{
|
|
||||||
// C = A^{-1} x U_l
|
// C = A^{-1} x U_l
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
|
||||||
C[i] = 0;
|
C[i] = 0;
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denominator
|
// Denominator
|
||||||
double den = 1 + C[Updates_index[l] - 1];
|
double den = 1 + C[Updates_index[l] - 1];
|
||||||
if (fabs(den) < threshold())
|
if (fabs(den) < threshold()) {
|
||||||
{
|
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
|
||||||
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
double iden = 1 / den;
|
double iden = 1 / den;
|
||||||
|
|
||||||
// D = v^T x A^{-1}
|
// D = v^T x A^{-1}
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
||||||
}
|
}
|
||||||
|
|
||||||
// A^{-1} = A^{-1} - C x D / den
|
// A^{-1} = A^{-1} - C x D / den
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
|
||||||
{
|
|
||||||
double update = C[i] * D[j] * iden;
|
double update = C[i] * D[j] * iden;
|
||||||
Slater_inv[i * Dim + j] -= update;
|
Slater_inv[i * Dim + j] -= update;
|
||||||
}
|
}
|
||||||
@ -63,9 +55,7 @@ void SM1(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
// Sherman Morrison, with J. Slagel splitting
|
// Sherman Morrison, with J. Slagel splitting
|
||||||
// http://hdl.handle.net/10919/52966
|
// http://hdl.handle.net/10919/52966
|
||||||
void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
double *Updates, unsigned int *Updates_index)
|
double *Updates, unsigned int *Updates_index) {
|
||||||
{
|
|
||||||
|
|
||||||
std::cerr << "Called SM2 with updates " << N_updates << std::endl;
|
std::cerr << "Called SM2 with updates " << N_updates << std::endl;
|
||||||
double C[Dim];
|
double C[Dim];
|
||||||
double D[Dim];
|
double D[Dim];
|
||||||
@ -76,27 +66,23 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
|
|
||||||
unsigned int l = 0;
|
unsigned int l = 0;
|
||||||
// For each update
|
// For each update
|
||||||
while (l < N_updates)
|
while (l < N_updates) {
|
||||||
{
|
|
||||||
// C = A^{-1} x U_l
|
// C = A^{-1} x U_l
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
|
||||||
C[i] = 0;
|
C[i] = 0;
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denominator
|
// Denominator
|
||||||
double den = 1 + C[Updates_index[l] - 1];
|
double den = 1 + C[Updates_index[l] - 1];
|
||||||
if (fabs(den) < threshold())
|
if (fabs(den) < threshold()) {
|
||||||
{
|
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
|
||||||
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
// U_l = U_l / 2 (do the split)
|
// U_l = U_l / 2 (do the split)
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
later_updates[later * Dim + j] = Updates[l * Dim + j] / 2.0;
|
later_updates[later * Dim + j] = Updates[l * Dim + j] / 2.0;
|
||||||
C[j] /= 2.0;
|
C[j] /= 2.0;
|
||||||
}
|
}
|
||||||
@ -108,16 +94,13 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
double iden = 1 / den;
|
double iden = 1 / den;
|
||||||
|
|
||||||
// D = v^T x A^{-1}
|
// D = v^T x A^{-1}
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
||||||
}
|
}
|
||||||
|
|
||||||
// A^{-1} = A^{-1} - C x D / den
|
// A^{-1} = A^{-1} - C x D / den
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
|
||||||
{
|
|
||||||
double update = C[i] * D[j] * iden;
|
double update = C[i] * D[j] * iden;
|
||||||
Slater_inv[i * Dim + j] -= update;
|
Slater_inv[i * Dim + j] -= update;
|
||||||
}
|
}
|
||||||
@ -125,17 +108,14 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
l += 1;
|
l += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (later > 0)
|
if (later > 0) {
|
||||||
{
|
|
||||||
SM2(Slater_inv, Dim, later, later_updates, later_index);
|
SM2(Slater_inv, Dim, later, later_updates, later_index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sherman Morrison, leaving zero denominators for later
|
// Sherman Morrison, leaving zero denominators for later
|
||||||
void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
||||||
double *Updates, unsigned int *Updates_index)
|
double *Updates, unsigned int *Updates_index) {
|
||||||
{
|
|
||||||
|
|
||||||
std::cerr << "Called SM3 with updates " << N_updates << std::endl;
|
std::cerr << "Called SM3 with updates " << N_updates << std::endl;
|
||||||
double C[Dim];
|
double C[Dim];
|
||||||
double D[Dim];
|
double D[Dim];
|
||||||
@ -146,26 +126,22 @@ void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
|
|
||||||
unsigned int l = 0;
|
unsigned int l = 0;
|
||||||
// For each update
|
// For each update
|
||||||
while (l < N_updates)
|
while (l < N_updates) {
|
||||||
{
|
|
||||||
// C = A^{-1} x U_l
|
// C = A^{-1} x U_l
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
|
||||||
C[i] = 0;
|
C[i] = 0;
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
C[i] += Slater_inv[i * Dim + j] * Updates[l * Dim + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Denominator
|
// Denominator
|
||||||
double den = 1 + C[Updates_index[l] - 1];
|
double den = 1 + C[Updates_index[l] - 1];
|
||||||
if (fabs(den) < threshold())
|
if (fabs(den) < threshold()) {
|
||||||
{
|
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
|
||||||
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
later_updates[later * Dim + j] = Updates[l * Dim + j];
|
later_updates[later * Dim + j] = Updates[l * Dim + j];
|
||||||
}
|
}
|
||||||
later_index[later] = Updates_index[l];
|
later_index[later] = Updates_index[l];
|
||||||
@ -176,16 +152,13 @@ void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
double iden = 1 / den;
|
double iden = 1 / den;
|
||||||
|
|
||||||
// D = v^T x A^{-1}
|
// D = v^T x A^{-1}
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
{
|
|
||||||
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
D[j] = Slater_inv[(Updates_index[l] - 1) * Dim + j];
|
||||||
}
|
}
|
||||||
|
|
||||||
// A^{-1} = A^{-1} - C x D / den
|
// A^{-1} = A^{-1} - C x D / den
|
||||||
for (unsigned int i = 0; i < Dim; i++)
|
for (unsigned int i = 0; i < Dim; i++) {
|
||||||
{
|
for (unsigned int j = 0; j < Dim; j++) {
|
||||||
for (unsigned int j = 0; j < Dim; j++)
|
|
||||||
{
|
|
||||||
double update = C[i] * D[j] * iden;
|
double update = C[i] * D[j] * iden;
|
||||||
Slater_inv[i * Dim + j] -= update;
|
Slater_inv[i * Dim + j] -= update;
|
||||||
}
|
}
|
||||||
@ -193,8 +166,7 @@ void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
|
|||||||
l += 1;
|
l += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (later > 0)
|
if (later > 0) {
|
||||||
{
|
|
||||||
SM3(Slater_inv, Dim, later, later_updates, later_index);
|
SM3(Slater_inv, Dim, later, later_updates, later_index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user