Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DistanceMatrixToPoints.cs @ 10077

Last change on this file since 10077 was 10077, checked in by ascheibe, 11 years ago

#1886 fixed a bug in the point calculation from distance matrices

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
27  public static class DistanceMatrixToPoints {
28    /*
29     *  Calculates a matrix of n-dimensional points from the distance matrix dm as described in
30     *  http://math.stackexchange.com/questions/156161/finding-the-coordinates-of-points-from-distance-matrix/423898#423898
31     *  and
32     *  http://stackoverflow.com/questions/10963054/finding-the-coordinates-of-points-from-distance-matrix/17177833#17177833
33     *   
34     */
35    public static double[][] ConvertDistanceMatrixToPoints(double[][] dm) {
36      double[][] points = new double[dm.Length][];
37      double[,] m = new double[dm.Length, dm.Length];
38      double[] q = new double[dm.Length]; //eigenvalues
39      double[,] v = new double[dm.Length, dm.Length]; //eigenvectors
40
41      for (int i = 0; i < dm.Length; i++) {
42        for (int j = 0; j < dm.Length; j++) {
43          m[i, j] = 0.5 * (Math.Pow(dm[0][j], 2) + Math.Pow(dm[i][0], 2) - Math.Pow(dm[i][j], 2));
44        }
45      }
46
47      //QR decomposition to get the upper part for smatrixevd
48      double[] tau;
49      double[,] r;
50      alglib.rmatrixqr(ref m, dm.Length, dm.Length, out tau);
51      alglib.rmatrixqrunpackr(m, dm.Length, dm.Length, out r);
52
53      bool res = alglib.smatrixevd(r, dm.Length, 1, true, out q, out v);
54      if (!res) throw new Exception("Eigenvalue computation did not converge!");
55
56      int zeroCnt = q.Count(x => x.IsAlmost(0));
57      for (int i = 0; i < dm.Length; i++) {
58        points[i] = new double[dm.Length - zeroCnt];
59      }
60
61      int pi = 0;
62      for (int i = 0; i < dm.Length; i++) {
63        if (q[i].IsAlmost(0)) {
64          for (int j = 0; j < dm.Length; j++) {
65            points[j][pi] = Math.Sqrt(q[i]) * v[j, i];
66          }
67          pi++;
68        }
69      }
70
71      return points;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.