Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/05/13 23:57:18 (11 years ago)
Author:
ascheibe
Message:

#1886 fixed other MDS and added more unit tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/DistanceMatrixToPoints.cs

    r10108 r10109  
    3333     *   
    3434     */
    35     public static double[][] ConvertDistanceMatrixToPoints(double[][] dm) {
     35    public static double[][] ConvertDistanceMatrixToPoints(double[][] dm, int k = 2) {
    3636      double[][] points = new double[dm.Length][];
    3737      double[,] m = new double[dm.Length, dm.Length];
     
    4545      }
    4646
    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);
     47      bool res = alglib.smatrixevd(m, dm.Length, 1, true, out q, out v);
    5448      if (!res) throw new Exception("Eigenvalue computation did not converge!");
    5549
    56       int zeroCnt = q.Count(x => x.IsAlmost(0) || x < 0.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.0) && q[i] > 0.0) {
    64           for (int j = 0; j < dm.Length; j++) {
    65             points[j][pi] = Math.Sqrt(q[i]) * v[j, i];
    66           }
    67           pi++;
     50      //TODO: this should also work without allocating memory for ev and evec
     51      double[] ev = new double[k];
     52      double[][] evec = new double[dm.Length][];
     53      AllocArray(evec, k);
     54      Array.Copy(q, q.Length - k, ev, 0, k);
     55      for (int i = 0; i < k; i++) {
     56        for (int j = 0; j < dm.Length; j++) {
     57          evec[j][i] = v[j, i + (q.Length - k)];
    6858        }
    6959      }
    7060
     61      double k1 = SumIfLZero(ev);
     62      if (k1 < k) {
     63        throw new Exception("Zero-eigenvalues detected. This leads to a degenerate point set. Use constants. ");
     64        //TODO: handling of this case; implement adding of constants
     65      }
     66
     67      AllocArray(points, k);
     68      for (int i = 0; i < k; i++) {
     69        for (int j = 0; j < dm.Length; j++) {
     70          points[j][i] = Math.Sqrt(ev[i]) * evec[j][i];
     71        }
     72      }
    7173      return points;
    7274    }
Note: See TracChangeset for help on using the changeset viewer.