aboutsummaryrefslogtreecommitdiff
path: root/forceScheme.cpp
blob: 22ab2935364f4e1f3b98d03ea25be40f5d4d0731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "mp.h"

#include <algorithm>

static const double EPSILON = 1e-3;

typedef arma::uvec V;

arma::mat mp::forceScheme(const arma::mat &D,
        arma::mat &Y,
        size_t maxIter,
        double tol,
        double fraction)
{
    arma::uword n = Y.n_rows;
    V i(n), j(n);
    for (arma::uword k = 0; k < n; k++) {
        i[k] = j[k] = k;
    }

    double prevDeltaSum = 1. / 0.;
    for (size_t iter = 0; iter < maxIter; iter++) {
        double deltaSum = 0;

        arma::shuffle(i);
        for (V::iterator a = i.begin(); a != i.end(); a++) {
            arma::shuffle(j);
            for (V::iterator b = j.begin(); b != j.end(); b++) {
                if (*a == *b) {
                    continue;
                }

                arma::rowvec direction(Y.row(*b) - Y.row(*a));
                double d2 = std::max(arma::norm(direction, 2), EPSILON);
                double delta = (D(*a, *b) - d2) / fraction;
                deltaSum += fabs(delta);
                Y.row(*b) += delta * (direction / d2);
            }
        }

        if (fabs(prevDeltaSum - deltaSum) < tol) {
            break;
        }
        prevDeltaSum = deltaSum;
    }

    return Y;
}