Changeset 8424
- Timestamp:
- 08/07/12 12:13:04 (12 years ago)
- Location:
- branches/NCA
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/NCA/HeuristicLab.Algorithms.NCA.Tests/MatrixTest.cs
r8422 r8424 60 60 private bool IsEqual(Matrix_Accessor a, Matrix_Accessor b) { 61 61 var i1 = a.GetEnumerator(); 62 var i2 = a.GetEnumerator();62 var i2 = b.GetEnumerator(); 63 63 bool f, g; 64 64 do { … … 223 223 result = new double[,] { { 3, 5 }, { 4, 6 } }; 224 224 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))); 225 230 } 226 231 } -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs
r8422 r8424 32 32 33 33 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; } 56 36 57 37 protected Matrix(Matrix original, Cloner cloner) { 58 38 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; 62 41 cloner.RegisterClonedObject(original, this); 63 42 } … … 91 70 92 71 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); 95 73 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 } 96 99 } 97 100 … … 197 200 198 201 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 } 210 206 } 211 207 }
Note: See TracChangeset
for help on using the changeset viewer.