Exemple #1
0
        /// <summary>
        /// Create a view of a parent matrix with a subset of the rows (perhaps in a different order) and
        /// a subset of the cols (perhaps in a different order). If the subsets happen to include all
        /// rows and cols in the same order, the parent matrix is returned.
        /// 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="rowKeySequence">A sequence of rowKeys that specifies the subset of rows to include and their desired order.</param>
        /// <param name="colKeySequence">A sequence of colKeys that specifies the subset of cols to include and their desired order.</param>
        /// <returns>A matrix with the desired rows and cols in their desired order.</returns>
        static public Matrix <TRowKey, TColKey, TValue> SelectRowsAndColsView <TRowKey, TColKey, TValue>(this Matrix <TRowKey, TColKey, TValue> parentMatrix, IEnumerable <TRowKey> rowKeySequence, IEnumerable <TColKey> colKeySequence)
        {
            //If the new won't change anything, just return the parent
            if (parentMatrix.RowKeys.SequenceEqual(rowKeySequence) && parentMatrix.ColKeys.SequenceEqual(colKeySequence))
            {
                return(parentMatrix);
            }

            //!!!Could check of this is a SelectRowsAndColsView of a SelectRowsAndColsView and simplify (see TransposeView for an example)

            var matrixView = new SelectRowsAndColsView <TRowKey, TColKey, TValue>();

            matrixView.SetUp(parentMatrix, rowKeySequence, colKeySequence);
            return(matrixView);
        }
Exemple #2
0
        private static DenseMatrix <TRowKey, TColKey, TValue> MaterializeSelectRowsColsViewToDenseMatrix <TRowKey, TColKey, TValue>(SelectRowsAndColsView <TRowKey, TColKey, TValue> selectRowsAndColsView)
        {
            DenseMatrix <TRowKey, TColKey, TValue> parent = (DenseMatrix <TRowKey, TColKey, TValue>)selectRowsAndColsView.ParentMatrix;

            var matrix = new DenseMatrix <TRowKey, TColKey, TValue>();

            matrix._rowKeys       = selectRowsAndColsView.RowKeys;
            matrix._colKeys       = selectRowsAndColsView.ColKeys;
            matrix._indexOfRowKey = selectRowsAndColsView.IndexOfRowKey;
            matrix._indexOfColKey = selectRowsAndColsView.IndexOfColKey;
            matrix._missingValue  = parent.MissingValue;
            matrix.ValueArray     = new TValue[matrix.RowCount, matrix.ColCount];

            IList <int> oldRowIndexList = selectRowsAndColsView.IndexOfParentRowKey;
            IList <int> oldColIndexList = selectRowsAndColsView.IndexOfParentColKey;

            for (int newRowIndex = 0; newRowIndex < matrix.RowCount; ++newRowIndex)
            {
                int oldRowIndex = oldRowIndexList[newRowIndex];

                for (int newColIndex = 0; newColIndex < matrix.ColCount; ++newColIndex)
                {
                    matrix.ValueArray[newRowIndex, newColIndex] = parent.ValueArray[oldRowIndex, oldColIndexList[newColIndex]];
                }
            }

            return(matrix);
        }