Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2013-2019 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "ClassicalScaling.h"
23 : #include "reference/PointWiseMapping.h"
24 :
25 : namespace PLMD {
26 : namespace analysis {
27 :
28 2 : void ClassicalScaling::run( PointWiseMapping* mymap ) {
29 : // Retrieve the distances from the dimensionality reduction object
30 2 : double half=(-0.5); Matrix<double> distances( half*mymap->modifyDmat() );
31 :
32 : // Apply centering transtion
33 : unsigned n=distances.nrows(); double sum;
34 : // First HM
35 402 : for(unsigned i=0; i<n; ++i) {
36 40200 : sum=0; for(unsigned j=0; j<n; ++j) sum+=distances(i,j);
37 40200 : for(unsigned j=0; j<n; ++j) distances(i,j) -= sum/n;
38 : }
39 : // Now (HM)H
40 402 : for(unsigned i=0; i<n; ++i) {
41 40200 : sum=0; for(unsigned j=0; j<n; ++j) sum+=distances(j,i);
42 40200 : for(unsigned j=0; j<n; ++j) distances(j,i) -= sum/n;
43 : }
44 :
45 : // Diagonalize matrix
46 2 : std::vector<double> eigval(n); Matrix<double> eigvec(n,n);
47 2 : diagMat( distances, eigval, eigvec );
48 :
49 : // Pass final projections to map object
50 402 : for(unsigned i=0; i<n; ++i) {
51 1800 : for(unsigned j=0; j<mymap->getNumberOfProperties(); ++j) mymap->setProjectionCoordinate( i, j, sqrt(eigval[n-1-j])*eigvec(n-1-j,i) );
52 : }
53 2 : }
54 :
55 : }
56 4839 : }
|