/// <summary> /// Create a view of the parent matrix in which rows become cols and cols become rows. /// This is a 'view' in the sense that changes to the values in either matrix will be reflected in both. /// </summary> /// <remarks>If two layers of TransposeView are applied to a parent matrix, the parent matrix is returned.</remarks> /// <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> /// <returns>A transposed matrix</returns> static public Matrix<TColKey, TRowKey, TValue> TransposeView<TRowKey, TColKey, TValue>( this Matrix<TRowKey, TColKey, TValue> parentMatrix) { //If this is a transpose of a transpose, simplify TransposeView<TRowKey, TColKey, TValue> parentTransposeViewOrNull = parentMatrix as TransposeView<TRowKey, TColKey, TValue>; if (null != parentTransposeViewOrNull) { return parentTransposeViewOrNull.ParentMatrix; } var transposeView = new TransposeView<TColKey, TRowKey, TValue>(); transposeView.ParentMatrix = parentMatrix; return transposeView; }
private static DenseMatrix <TRowKey, TColKey, TValue> MaterializeTransposeViewToDenseMatrix <TRowKey, TColKey, TValue>(TransposeView <TRowKey, TColKey, TValue> transposeView) { DenseMatrix <TColKey, TRowKey, TValue> parent = (DenseMatrix <TColKey, TRowKey, TValue>)transposeView.ParentMatrix; var matrix = new DenseMatrix <TRowKey, TColKey, TValue>(); matrix._rowKeys = transposeView.RowKeys; matrix._colKeys = transposeView.ColKeys; matrix._indexOfRowKey = transposeView.IndexOfRowKey; matrix._indexOfColKey = transposeView.IndexOfColKey; matrix._missingValue = parent.MissingValue; matrix.ValueArray = new TValue[matrix.RowCount, matrix.ColCount]; for (int newRowIndex = 0; newRowIndex < matrix.RowCount; ++newRowIndex) { for (int newColIndex = 0; newColIndex < matrix.ColCount; ++newColIndex) { matrix.ValueArray[newRowIndex, newColIndex] = parent.ValueArray[newColIndex, newRowIndex]; } } return(matrix); }