Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8424


Ignore:
Timestamp:
08/07/12 12:13:04 (12 years ago)
Author:
abeham
Message:

#1913: corrected tests and fixed bugs

Location:
branches/NCA
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/NCA/HeuristicLab.Algorithms.NCA.Tests/MatrixTest.cs

    r8422 r8424  
    6060    private bool IsEqual(Matrix_Accessor a, Matrix_Accessor b) {
    6161      var i1 = a.GetEnumerator();
    62       var i2 = a.GetEnumerator();
     62      var i2 = b.GetEnumerator();
    6363      bool f, g;
    6464      do {
     
    223223      result = new double[,] { { 3, 5 }, { 4, 6 } };
    224224      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose(), new Matrix_Accessor(result)));
     225      var enumMat = new double[] { 3, 4, 5, 6 };
     226      Assert.IsTrue(IsEqual(new Matrix_Accessor(enumMat, 2, 2).Transpose(), new Matrix_Accessor(result)));
     227      mat1 = new double[,] { { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 }, { 11, 12 } };
     228      result = new double[,] { { 3, 5, 7, 9, 11 }, { 4, 6, 8, 10, 12 } };
     229      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose(), new Matrix_Accessor(result)));
    225230    }
    226231  }
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs

    r8422 r8424  
    3232
    3333    private IEnumerable<double> values;
    34     private bool isTransposed;
    35     private int rows, columns;
    36     public int Rows {
    37       get {
    38         if (!isTransposed) return rows;
    39         else return columns;
    40       }
    41       private set {
    42         if (!isTransposed) rows = value;
    43         else columns = value;
    44       }
    45     }
    46     public int Columns {
    47       get {
    48         if (!isTransposed) return columns;
    49         else return rows;
    50       }
    51       private set {
    52         if (!isTransposed) columns = value;
    53         else rows = value;
    54       }
    55     }
     34    public int Rows { get; private set; }
     35    public int Columns { get; private set; }
    5636
    5737    protected Matrix(Matrix original, Cloner cloner) {
    5838      this.values = original.values.ToArray();
    59       this.isTransposed = original.isTransposed;
    60       this.rows = original.rows;
    61       this.columns = original.columns;
     39      this.Rows = original.Rows;
     40      this.Columns = original.Columns;
    6241      cloner.RegisterClonedObject(original, this);
    6342    }
     
    9170
    9271    public Matrix Transpose() {
    93       var result = new Matrix(values, Rows, Columns);
    94       result.isTransposed = true;
     72      var result = new Matrix(Transpose(values, Columns, Rows), Columns, Rows);
    9573      return result;
     74    }
     75
     76    private IEnumerable<double> Transpose(IEnumerable<double> values, int rows, int columns) {
     77      // vectors don't need to be transposed
     78      if (rows == 1 || columns == 1) {
     79        foreach (var v in values) yield return v;
     80        yield break;
     81      }
     82
     83      int skip = 0;
     84      var iter = values.GetEnumerator();
     85      if (!iter.MoveNext()) yield break;
     86      while (skip < rows) {
     87        for (int i = 0; i < skip; i++) iter.MoveNext();
     88        yield return iter.Current;
     89        for (int j = 0; j < columns - 1; j++) {
     90          for (int i = 0; i < rows; i++) iter.MoveNext();
     91          yield return iter.Current;
     92        }
     93        skip++;
     94        if (skip < rows) {
     95          iter = values.GetEnumerator();
     96          iter.MoveNext();
     97        }
     98      }
    9699    }
    97100
     
    197200
    198201    private IEnumerable<double> GetOnlineValues(double[,] matrix) {
    199       if (!isTransposed) {
    200         for (int i = 0; i < matrix.GetLength(0); i++)
    201           for (int j = 0; j < matrix.GetLength(1); j++) {
    202             yield return matrix[i, j];
    203           }
    204       } else {
    205         for (int j = 0; j < matrix.GetLength(1); j++)
    206           for (int i = 0; i < matrix.GetLength(0); i++) {
    207             yield return matrix[i, j];
    208           }
    209       }
     202      for (int i = 0; i < matrix.GetLength(0); i++)
     203        for (int j = 0; j < matrix.GetLength(1); j++) {
     204          yield return matrix[i, j];
     205        }
    210206    }
    211207  }
Note: See TracChangeset for help on using the changeset viewer.