2021-02-09 13:40:52 +01:00
|
|
|
// SM-MaponiA3_f.cpp
|
2021-02-03 12:13:09 +01:00
|
|
|
// Algorithm 3 from P. Maponi,
|
|
|
|
// p. 283, doi:10.1016/j.laa.2006.07.007
|
2021-02-04 11:39:00 +01:00
|
|
|
#include "SM_MaponiA3.hpp"
|
2021-02-03 12:13:09 +01:00
|
|
|
#include "Helpers.hpp"
|
2021-01-27 17:19:41 +01:00
|
|
|
|
2021-03-10 11:41:34 +01:00
|
|
|
void selectBestUpdate(unsigned int l, unsigned int N_updates,
|
|
|
|
unsigned int *Updates_index, unsigned int *p,
|
|
|
|
double ***ylk) {
|
2021-03-10 16:56:22 +01:00
|
|
|
unsigned int lbar = l+1, max =0;
|
|
|
|
unsigned int index = 0, component = 0;
|
2021-03-10 11:41:34 +01:00
|
|
|
unsigned int tmp = 0;
|
|
|
|
double breakdown = 0;
|
|
|
|
for (unsigned int j = lbar; j < N_updates + 1; j++) {
|
|
|
|
index = p[j];
|
|
|
|
component = Updates_index[index - 1];
|
|
|
|
breakdown = abs(1 + ylk[l][index][component]);
|
2021-03-10 12:18:08 +01:00
|
|
|
#ifdef DEBUG
|
2021-03-10 11:41:34 +01:00
|
|
|
cout << "Inside selectBestUpdate()" << endl;
|
|
|
|
cout << "breakdown = abs(1 + ylk[" << l << "][" << index << "][" << component << "])" << endl;
|
|
|
|
cout << endl;
|
2021-03-10 12:18:08 +01:00
|
|
|
#endif
|
2021-03-10 11:41:34 +01:00
|
|
|
if (breakdown > max) {
|
|
|
|
max = breakdown;
|
|
|
|
lbar = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp = p[l+1];
|
|
|
|
p[l+1] = p[lbar];
|
|
|
|
p[lbar] = tmp;
|
|
|
|
}
|
|
|
|
|
2021-02-25 12:17:45 +01:00
|
|
|
void MaponiA3(double *Slater_inv, unsigned int Dim,
|
2021-03-05 17:00:48 +01:00
|
|
|
unsigned int N_updates, double *Updates,
|
2021-02-25 12:17:45 +01:00
|
|
|
unsigned int *Updates_index) {
|
2021-02-09 13:40:52 +01:00
|
|
|
|
2021-03-04 11:10:59 +01:00
|
|
|
/*
|
|
|
|
DECLARE AND INITIALISE ARRAYS
|
|
|
|
*/
|
|
|
|
|
2021-03-10 11:41:34 +01:00
|
|
|
unsigned int k, l, i, j, component;
|
2021-02-25 12:17:45 +01:00
|
|
|
unsigned int *p = new unsigned int[N_updates + 1] {0};
|
2021-02-09 14:46:28 +01:00
|
|
|
double alpha, beta;
|
2021-02-18 19:43:20 +01:00
|
|
|
double *Al = new double[Dim * Dim];
|
2021-03-10 16:56:22 +01:00
|
|
|
double *next = new double[Dim*Dim] {0};
|
|
|
|
double *last = Slater_inv, *tmp;
|
|
|
|
|
2021-02-25 12:17:45 +01:00
|
|
|
// Populate update-order vector
|
2021-02-18 19:43:20 +01:00
|
|
|
for (i = 0; i < N_updates; i++) {
|
2021-02-09 14:46:28 +01:00
|
|
|
p[i + 1] = i + 1;
|
|
|
|
}
|
2021-02-02 15:05:47 +01:00
|
|
|
|
2021-03-10 16:56:22 +01:00
|
|
|
// Declare auxiliary solution matrix ylk[N_updates][N_updates+1][Dim]
|
2021-02-18 19:43:20 +01:00
|
|
|
double ***ylk = new double **[N_updates];
|
|
|
|
for (l = 0; l < N_updates; l++) {
|
|
|
|
ylk[l] = new double *[N_updates + 1];
|
|
|
|
for (k = 0; k < N_updates + 1; k++) {
|
|
|
|
ylk[l][k] = new double[Dim + 1] {0};
|
2021-02-02 15:05:47 +01:00
|
|
|
}
|
2021-02-09 14:46:28 +01:00
|
|
|
}
|
2021-03-10 16:56:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-04 11:10:59 +01:00
|
|
|
/*
|
2021-03-10 16:56:22 +01:00
|
|
|
START ALGORITHM
|
2021-03-04 11:10:59 +01:00
|
|
|
*/
|
2021-03-05 17:00:48 +01:00
|
|
|
|
2021-03-10 16:56:22 +01:00
|
|
|
// Calculate the {y_{0,k}}
|
2021-02-12 17:48:49 +01:00
|
|
|
for (k = 1; k < N_updates + 1; k++) {
|
2021-03-10 12:18:08 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "Compute y0k: " << endl;
|
|
|
|
cout << "ylk[0][" << k << "][:]" << endl;
|
|
|
|
cout << endl;
|
|
|
|
#endif
|
2021-02-18 19:43:20 +01:00
|
|
|
for (i = 1; i < Dim + 1; i++) {
|
2021-02-19 19:11:44 +01:00
|
|
|
for (j = 1; j < Dim + 1; j++) {
|
2021-02-23 08:28:09 +01:00
|
|
|
ylk[0][k][i] += Slater_inv[(i-1)*Dim + (j-1)]
|
The algorithm now works for the following 4x4 example with 2 updates:
S = [1,0,1,-1; 0,1,1,0; -1,0,-1,0; 1,1,1,1]
S_inv = [1,-1,1,1; 1,0,2,1; -1,1,-2,-1; -1,0,-1,0]
u1 = [0,-2,0,0]
u2 = [0,-1,0,0]
upd_idx = [2,4]
To go from Maponi's examples where the number of updates is always equal
to the the dimension of the matrix, and the decomposition is always
diagonal, to cases with a non-diagonal decomposition and a number of
updates unequal to its size, the following changed needed to be made:
* in the calculation of the {y0k} an extra inner for-loop needs to be
added to make it a full matrix-vector multiplication due to the fact
that A0 is not a diagonal matrix
* in some places the use of the update-order vector p needs
the be replaced with that of upd_idx to make sure the correct
component of the ylk is selected and the proper rank-1 matrices are
constructed
* when a matrix is passed from Fortran to C++ with 2D adressing, it is
passed in colum-major order. The passed matrix needs to be transposed
before passing to C++. Doing this inside the algorithm will break
compatibility with called from C/C++.
2021-02-21 18:28:08 +01:00
|
|
|
* Updates[(k-1)*Dim + (j-1)];
|
2021-02-19 19:11:44 +01:00
|
|
|
}
|
2021-02-02 15:05:47 +01:00
|
|
|
}
|
2021-02-09 14:46:28 +01:00
|
|
|
}
|
2021-02-02 15:05:47 +01:00
|
|
|
|
2021-03-10 16:56:22 +01:00
|
|
|
// Calculate the {y_{l,k}} from the {y_{0,k}}
|
2021-03-04 18:33:28 +01:00
|
|
|
for (l = 0; l < N_updates; l++) {
|
2021-03-10 16:56:22 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "In outer compute-ylk-loop: l = " << l << endl;
|
|
|
|
cout << endl;
|
|
|
|
#endif
|
2021-03-04 11:10:59 +01:00
|
|
|
|
2021-03-10 16:56:22 +01:00
|
|
|
// For given l select intermediate update with largest break-down val
|
2021-03-10 11:41:34 +01:00
|
|
|
selectBestUpdate(l, N_updates, Updates_index, p, ylk);
|
2021-03-04 11:10:59 +01:00
|
|
|
|
2021-03-10 11:41:34 +01:00
|
|
|
// Select component and comp. bd-condition.
|
2021-03-04 18:33:28 +01:00
|
|
|
component = Updates_index[p[l+1] - 1];
|
2021-03-04 18:36:51 +01:00
|
|
|
beta = 1 + ylk[l][p[l+1]][component];
|
2021-03-10 12:18:08 +01:00
|
|
|
#ifdef DEBUG
|
2021-03-10 16:56:22 +01:00
|
|
|
cout << "p[l+1] = " << p[l+1] << endl;
|
|
|
|
cout << "component = " << component << endl;
|
2021-03-10 11:41:34 +01:00
|
|
|
cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << endl;
|
|
|
|
cout << endl;
|
2021-03-10 12:18:08 +01:00
|
|
|
#endif
|
2021-03-10 11:41:34 +01:00
|
|
|
if (fabs(beta) < 1e-6) {
|
2021-02-18 19:43:20 +01:00
|
|
|
cout << "Break-down occured. Exiting..." << endl;
|
2021-03-05 17:00:48 +01:00
|
|
|
exit(1);
|
2021-02-18 19:43:20 +01:00
|
|
|
}
|
2021-03-10 11:41:34 +01:00
|
|
|
|
2021-03-10 16:56:22 +01:00
|
|
|
// Compute intermediate update to Slater_inv
|
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "Compute intermediate update to Slater_inv" << endl;
|
|
|
|
cout << "component = " << component << endl;
|
|
|
|
cout << "beta = 1 + ylk[" << l << "][" << p[l+1] << "][" << component << "]" << endl;
|
|
|
|
cout << "ylk[l][p[k]][:] = ylk[" << l << "][" << p[l+1] << "][:]" << endl;
|
|
|
|
cout << 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matMul(Al, last, next, Dim);
|
|
|
|
tmp = next;
|
|
|
|
next = last;
|
|
|
|
last = tmp;
|
|
|
|
#ifdef DEBUG
|
|
|
|
showMatrix(last, Dim, "last");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For given l != 0 compute the next {y_{l,k}}
|
2021-03-10 11:41:34 +01:00
|
|
|
for (k = l+2; k < N_updates+1; k++) {
|
2021-03-04 18:36:51 +01:00
|
|
|
alpha = ylk[l][p[k]][component] / beta;
|
2021-03-10 12:18:08 +01:00
|
|
|
#ifdef DEBUG
|
2021-03-10 16:56:22 +01:00
|
|
|
cout << "Inside k-loop: k = " << k << endl;
|
2021-03-10 11:54:43 +01:00
|
|
|
cout << "ylk[" << l+1 << "][" << p[k] << "][:]" << endl;
|
|
|
|
cout << endl;
|
2021-03-10 12:18:08 +01:00
|
|
|
#endif
|
2021-02-18 19:43:20 +01:00
|
|
|
for (i = 1; i < Dim + 1; i++) {
|
2021-03-04 18:36:51 +01:00
|
|
|
ylk[l+1][p[k]][i] = ylk[l][p[k]][i]
|
|
|
|
- alpha * ylk[l][p[l+1]][i];
|
2021-02-09 14:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 16:08:14 +01:00
|
|
|
memcpy(Slater_inv, last, Dim*Dim*sizeof(double));
|
2021-03-10 16:56:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-04 11:10:59 +01:00
|
|
|
/*
|
|
|
|
CLEANUP MEMORY
|
|
|
|
*/
|
|
|
|
|
2021-02-18 19:43:20 +01:00
|
|
|
for (l = 0; l < N_updates; l++) {
|
|
|
|
for (k = 0; k < N_updates + 1; k++) {
|
2021-02-09 14:46:28 +01:00
|
|
|
delete[] ylk[l][k];
|
|
|
|
}
|
|
|
|
delete[] ylk[l];
|
|
|
|
}
|
2021-03-04 11:10:59 +01:00
|
|
|
delete[] Al, next, p;
|
2021-02-09 13:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
2021-02-18 19:43:20 +01:00
|
|
|
void MaponiA3_f(double **linSlater_inv, unsigned int *Dim,
|
2021-02-12 12:04:21 +01:00
|
|
|
unsigned int *N_updates, double **linUpdates,
|
|
|
|
unsigned int **Updates_index) {
|
2021-03-05 17:00:48 +01:00
|
|
|
MaponiA3(*linSlater_inv, *Dim,
|
2021-02-18 19:43:20 +01:00
|
|
|
*N_updates, *linUpdates,
|
|
|
|
*Updates_index);
|
2021-02-12 12:04:21 +01:00
|
|
|
}
|
2021-02-09 13:40:52 +01:00
|
|
|
}
|