Ran: clang-format --style=LLVM on *.cpp and *.hpp files.

This commit is contained in:
Francois Coppens 2021-04-15 14:38:42 +02:00
parent fad8eb2e89
commit e5648f7485
8 changed files with 226 additions and 271 deletions

View File

@ -1,9 +1,9 @@
// SM_Helpers.hpp
// Some usefull helper functions to support the Maponi algorithm.
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
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,
double ***ylk);
template<typename T>
void showScalar(T scalar, std::string name) {
template <typename T> void showScalar(T scalar, std::string name) {
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++) {
if (matrix[i * M + j] >= 0) {
std::cout << " " << matrix[i * M + j] << ",";
}
else {
} else {
std::cout << " " << matrix[i * M + j] << ",";
}
}
@ -45,8 +43,7 @@ void showMatrix(T *matrix, unsigned int M, std::string name) {
std::cout << std::endl;
}
template<typename T>
T *transpose(T *A, unsigned int M) {
template <typename T> T *transpose(T *A, unsigned int M) {
T *B = new T[M * M];
for (unsigned int i = 0; i < M; i++) {
for (unsigned int j = 0; j < M; j++) {
@ -56,8 +53,7 @@ T *transpose(T *A, unsigned int M) {
return B;
}
template<typename T>
void matMul(T *A, T *B, T *C, unsigned int M) {
template <typename T> void matMul(T *A, T *B, T *C, unsigned int M) {
memset(C, 0, M * M * sizeof(T));
for (unsigned int i = 0; i < M; i++) {
for (unsigned int j = 0; j < M; j++) {
@ -79,19 +75,17 @@ T1 *outProd(T1 *vec1, T2 *vec2, unsigned int M) {
return C;
}
template<typename T>
T matDet(T **A, unsigned int M) {
template <typename T> T matDet(T **A, unsigned int M) {
int det = 0, p, h, k, i, j;
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) {
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]);
return det;
}
else {
} else {
for (p = 0; p < M; p++) {
h = 0;
k = 0;
@ -115,12 +109,13 @@ T matDet(T **A, unsigned int M) {
delete[] temp;
}
template<typename T>
bool is_identity(T *A, unsigned int M, double tolerance) {
template <typename T> bool is_identity(T *A, unsigned int M, double tolerance) {
for (unsigned int i = 0; i < M; i++) {
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]) > tolerance) return false;
if (i == j && fabs(A[i * M + j] - 1) > tolerance)
return false;
if (i != j && fabs(A[i * M + j]) > tolerance)
return false;
}
}
return true;

View File

@ -1,3 +1,2 @@
void MaponiA3(double *Slater_inv, unsigned int Dim,
unsigned int N_updates, double *Updates,
unsigned int *Updates_index);
void MaponiA3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
double *Updates, unsigned int *Updates_index);

View File

@ -1,3 +1,2 @@
void MaponiA3S(double *Slater_inv, unsigned int Dim,
unsigned int N_updates, double *Updates,
unsigned int *Updates_index);
void MaponiA3S(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
double *Updates, unsigned int *Updates_index);

View File

@ -19,7 +19,8 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates,
breakdown = abs(1 + ylk[l][index][component]);
#ifdef DEBUG
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;
#endif
if (breakdown > max) {

View File

@ -6,9 +6,8 @@
// #define DEBUG
void MaponiA3(double *Slater_inv, unsigned int Dim,
unsigned int N_updates, double *Updates,
unsigned int *Updates_index) {
void MaponiA3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
double *Updates, unsigned int *Updates_index) {
/*
DECLARE AND INITIALISE ARRAYS
@ -35,8 +34,6 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
}
}
/*
START ALGORITHM
*/
@ -50,8 +47,8 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
#endif
for (i = 1; i < Dim + 1; i++) {
for (j = 1; j < Dim + 1; j++) {
ylk[0][k][i] += Slater_inv[(i-1)*Dim + (j-1)]
* Updates[(k-1)*Dim + (j-1)];
ylk[0][k][i] += Slater_inv[(i - 1) * Dim + (j - 1)] *
Updates[(k - 1) * Dim + (j - 1)];
}
}
#ifdef DEBUG
@ -75,7 +72,8 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
#ifdef DEBUG
std::cout << "p[l+1] = " << p[l + 1] << 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;
#endif
if (fabs(beta) < 1e-3) {
@ -86,14 +84,16 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
#ifdef DEBUG
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
std::cout << "component = " << component << std::endl;
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << std::endl;
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l+1] << "][:]" << std::endl;
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
<< "]" << std::endl;
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l + 1] << "][:]"
<< std::endl;
std::cout << std::endl;
#endif
for (i = 0; i < Dim; i++) {
for (j = 0; j < Dim; j++) {
Al[i*Dim + j] = (i == j) - (j == component-1)
* ylk[l][p[l+1]][i + 1] / beta;
Al[i * Dim + j] =
(i == j) - (j == component - 1) * ylk[l][p[l + 1]][i + 1] / beta;
}
}
matMul(Al, last, next, Dim);
@ -113,15 +113,12 @@ void MaponiA3(double *Slater_inv, unsigned int Dim,
std::cout << std::endl;
#endif
for (i = 1; i < Dim + 1; i++) {
ylk[l+1][p[k]][i] = ylk[l][p[k]][i]
- alpha * ylk[l][p[l+1]][i];
ylk[l + 1][p[k]][i] = ylk[l][p[k]][i] - alpha * ylk[l][p[l + 1]][i];
}
}
}
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
/*
CLEANUP MEMORY
*/
@ -139,8 +136,6 @@ extern "C" {
void MaponiA3_f(double **linSlater_inv, unsigned int *Dim,
unsigned int *N_updates, double **linUpdates,
unsigned int **Updates_index) {
MaponiA3(*linSlater_inv, *Dim,
*N_updates, *linUpdates,
*Updates_index);
MaponiA3(*linSlater_inv, *Dim, *N_updates, *linUpdates, *Updates_index);
}
}

View File

@ -6,10 +6,8 @@
#define DEBUG
void MaponiA3S(double *Slater_inv, unsigned int Dim,
unsigned int N_updates, double *Updates,
unsigned int *Updates_index) {
void MaponiA3S(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
double *Updates, unsigned int *Updates_index) {
/*
DECLARE AND INITIALISE ARRAYS
*/
@ -35,8 +33,6 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
}
}
/*
START ALGORITHM
*/
@ -50,8 +46,8 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
#endif
for (i = 1; i < Dim + 1; i++) {
for (j = 1; j < Dim + 1; j++) {
ylk[0][k][i] += Slater_inv[(i-1)*Dim + (j-1)]
* Updates[(k-1)*Dim + (j-1)];
ylk[0][k][i] += Slater_inv[(i - 1) * Dim + (j - 1)] *
Updates[(k - 1) * Dim + (j - 1)];
}
}
#ifdef DEBUG
@ -75,7 +71,8 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
#ifdef DEBUG
std::cout << "p[l+1] = " << p[l + 1] << 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;
#endif
if (fabs(beta) < 1e-3) {
@ -86,14 +83,16 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
#ifdef DEBUG
std::cout << "Compute intermediate update to Slater_inv" << std::endl;
std::cout << "component = " << component << std::endl;
std::cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << std::endl;
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l+1] << "][:]" << std::endl;
std::cout << "beta = 1 + ylk[" << l << "][" << p[l + 1] << "][" << component
<< "]" << std::endl;
std::cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l + 1] << "][:]"
<< std::endl;
std::cout << std::endl;
#endif
for (i = 0; i < Dim; i++) {
for (j = 0; j < Dim; j++) {
Al[i*Dim + j] = (i == j) - (j == component-1)
* ylk[l][p[l+1]][i + 1] / beta;
Al[i * Dim + j] =
(i == j) - (j == component - 1) * ylk[l][p[l + 1]][i + 1] / beta;
}
}
matMul(Al, last, next, Dim);
@ -113,15 +112,12 @@ void MaponiA3S(double *Slater_inv, unsigned int Dim,
std::cout << std::endl;
#endif
for (i = 1; i < Dim + 1; i++) {
ylk[l+1][p[k]][i] = ylk[l][p[k]][i]
- alpha * ylk[l][p[l+1]][i];
ylk[l + 1][p[k]][i] = ylk[l][p[k]][i] - alpha * ylk[l][p[l + 1]][i];
}
}
}
memcpy(Slater_inv, last, Dim * Dim * sizeof(double));
/*
CLEANUP MEMORY
*/
@ -139,8 +135,6 @@ extern "C" {
void MaponiA3S_f(double **linSlater_inv, unsigned int *Dim,
unsigned int *N_updates, double **linUpdates,
unsigned int **Updates_index) {
MaponiA3S(*linSlater_inv, *Dim,
*N_updates, *linUpdates,
*Updates_index);
MaponiA3S(*linSlater_inv, *Dim, *N_updates, *linUpdates, *Updates_index);
}
}

View File

@ -11,46 +11,38 @@ double threshold() {
// Naïve Sherman Morrison
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;
double C[Dim];
double D[Dim];
unsigned int l = 0;
// For each update
while (l < N_updates)
{
while (l < N_updates) {
// 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;
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];
}
}
// Denominator
double den = 1 + C[Updates_index[l] - 1];
if (fabs(den) < threshold())
{
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
if (fabs(den) < threshold()) {
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
<< std::endl;
}
double iden = 1 / den;
// 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];
}
// A^{-1} = A^{-1} - C x D / den
for (unsigned int i = 0; i < Dim; i++)
{
for (unsigned int j = 0; j < Dim; j++)
{
for (unsigned int i = 0; i < Dim; i++) {
for (unsigned int j = 0; j < Dim; j++) {
double update = C[i] * D[j] * iden;
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
// http://hdl.handle.net/10919/52966
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;
double C[Dim];
double D[Dim];
@ -76,27 +66,23 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
unsigned int l = 0;
// For each update
while (l < N_updates)
{
while (l < N_updates) {
// 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;
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];
}
}
// Denominator
double den = 1 + C[Updates_index[l] - 1];
if (fabs(den) < threshold())
{
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
if (fabs(den) < threshold()) {
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
<< std::endl;
// 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;
C[j] /= 2.0;
}
@ -108,16 +94,13 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
double iden = 1 / den;
// 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];
}
// A^{-1} = A^{-1} - C x D / den
for (unsigned int i = 0; i < Dim; i++)
{
for (unsigned int j = 0; j < Dim; j++)
{
for (unsigned int i = 0; i < Dim; i++) {
for (unsigned int j = 0; j < Dim; j++) {
double update = C[i] * D[j] * iden;
Slater_inv[i * Dim + j] -= update;
}
@ -125,17 +108,14 @@ void SM2(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
l += 1;
}
if (later > 0)
{
if (later > 0) {
SM2(Slater_inv, Dim, later, later_updates, later_index);
}
}
// Sherman Morrison, leaving zero denominators for later
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;
double C[Dim];
double D[Dim];
@ -146,26 +126,22 @@ void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
unsigned int l = 0;
// For each update
while (l < N_updates)
{
while (l < N_updates) {
// 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;
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];
}
}
// Denominator
double den = 1 + C[Updates_index[l] - 1];
if (fabs(den) < threshold())
{
std::cerr << "Breakdown condition triggered at " << Updates_index[l] << std::endl;
if (fabs(den) < threshold()) {
std::cerr << "Breakdown condition triggered at " << Updates_index[l]
<< 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_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;
// 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];
}
// A^{-1} = A^{-1} - C x D / den
for (unsigned int i = 0; i < Dim; i++)
{
for (unsigned int j = 0; j < Dim; j++)
{
for (unsigned int i = 0; i < Dim; i++) {
for (unsigned int j = 0; j < Dim; j++) {
double update = C[i] * D[j] * iden;
Slater_inv[i * Dim + j] -= update;
}
@ -193,8 +166,7 @@ void SM3(double *Slater_inv, unsigned int Dim, unsigned int N_updates,
l += 1;
}
if (later > 0)
{
if (later > 0) {
SM3(Slater_inv, Dim, later, later_updates, later_index);
}
}