/// <summary> /// Creates a new view in which the columns of the matrix (but not the column keys) are permuted. /// This is a 'view' in the sense that changes to the values in either matrix will be reflected in both. /// </summary> /// <typeparam name="TRowKey">The type of the row key. Usually "String"</typeparam> /// <typeparam name="TColKey">The type of the col key. Usually "String"</typeparam> /// <typeparam name="TValue">The type of the value, for example, double, int, char, etc.</typeparam> /// <param name="parentMatrix">The matrix to wrap.</param> /// <param name="colIndexSequence">The indexes of the columns in their new order. Every col index must be mentioned exactly once.</param> /// <remarks>If the permutation puts every column back in the same place, the parent matrix is returned.</remarks> /// <returns>A new matrix with permuted columns.</returns> static public Matrix <TRowKey, TColKey, TValue> PermuteColValuesForEachRowView <TRowKey, TColKey, TValue>(this Matrix <TRowKey, TColKey, TValue> parentMatrix, IEnumerable <int> colIndexSequence) { //If the new won't change anything, just return the parent if (colIndexSequence.SequenceEqual(Enumerable.Range(0, parentMatrix.ColCount))) { return(parentMatrix); } //!!!Could check of this is a PermuteValuesView of a PermuteValuesView and simplify (see TransposeView for an example) var matrixView = new PermuteValuesView <TRowKey, TColKey, TValue>(); matrixView.SetUp(parentMatrix, colIndexSequence); return(matrixView); }
/// <summary> /// Creates a new view in which the columns of the matrix (but not the column keys) are permuted. /// This is a 'view' in the sense that changes to the values in either matrix will be reflected in both. /// </summary> /// <typeparam name="TRowKey">The type of the row key. Usually "String"</typeparam> /// <typeparam name="TColKey">The type of the col key. Usually "String"</typeparam> /// <typeparam name="TValue">The type of the value, for example, double, int, char, etc.</typeparam> /// <param name="parentMatrix">The matrix to wrap.</param> /// <param name="colIndexSequence">The indexes of the columns in their new order. Every col index must be mentioned exactly once.</param> /// <remarks>If the permutation puts every column back in the same place, the parent matrix is returned.</remarks> /// <returns>A new matrix with permuted columns.</returns> static public Matrix<TRowKey, TColKey, TValue> PermuteColValuesForEachRowView<TRowKey, TColKey, TValue>(this Matrix<TRowKey, TColKey, TValue> parentMatrix, IEnumerable<int> colIndexSequence) { //If the new won't change anything, just return the parent if (colIndexSequence.SequenceEqual(Enumerable.Range(0, parentMatrix.ColCount))) { return parentMatrix; } //If this is a permutation of a permutation, simplify PermuteValuesView<TRowKey, TColKey, TValue> parentPermuteValuesViewOrNull = parentMatrix as PermuteValuesView<TRowKey, TColKey, TValue>; if (null != parentPermuteValuesViewOrNull) { var colIndexSequenceTwo = colIndexSequence.Select(i => parentPermuteValuesViewOrNull.IndexOfParentCol[i]); var matrixView = new PermuteValuesView<TRowKey, TColKey, TValue>(); matrixView.SetUp(parentPermuteValuesViewOrNull.ParentMatrix, colIndexSequenceTwo); return matrixView; } else { var matrixView = new PermuteValuesView<TRowKey, TColKey, TValue>(); matrixView.SetUp(parentMatrix, colIndexSequence); return matrixView; } }