예제 #1
0
        public DoubleMatrix3D Sort(DoubleMatrix3D matrix, Cern.Colt.Matrix.DoubleAlgorithms.DoubleMatrix2DComparator c)
        {
            int[] sliceIndexes = new int[matrix.Slices]; // indexes to reorder instead of matrix itself
            for (int i = sliceIndexes.Length; --i >= 0;)
            {
                sliceIndexes[i] = i;
            }

            DoubleMatrix2D[] views = new DoubleMatrix2D[matrix.Slices]; // precompute views for speed
            for (int i = views.Length; --i >= 0;)
            {
                views[i] = matrix.ViewSlice(i);
            }

            IntComparator comp = new IntComparator((a, b) => { return(c(views[a], views[b])); });


            RunSort(sliceIndexes, 0, sliceIndexes.Length, comp);

            // view the matrix according to the reordered slice indexes
            // take all rows and columns in the original order
            return(matrix.ViewSelection(sliceIndexes, null, null));
        }
예제 #2
0
        public DoubleMatrix3D Sort(DoubleMatrix3D matrix, int row, int column)
        {
            if (row < 0 || row >= matrix.Rows)
            {
                throw new IndexOutOfRangeException("row=" + row + ", matrix=" + Formatter.Shape(matrix));
            }
            if (column < 0 || column >= matrix.Columns)
            {
                throw new IndexOutOfRangeException("column=" + column + ", matrix=" + Formatter.Shape(matrix));
            }

            int[] sliceIndexes = new int[matrix.Slices]; // indexes to reorder instead of matrix itself
            for (int i = sliceIndexes.Length; --i >= 0;)
            {
                sliceIndexes[i] = i;
            }

            DoubleMatrix1D sliceView = matrix.ViewRow(row).ViewColumn(column);
            IntComparator  comp      = new IntComparator((a, b) =>
            {
                double av = sliceView[a];
                double bv = sliceView[b];
                if (Double.IsNaN(av) || Double.IsNaN(bv))
                {
                    return(CompareNaN(av, bv));                                      // swap NaNs to the end
                }
                return(av < bv ? -1 : (av == bv ? 0 : 1));
            }
                                                         );


            RunSort(sliceIndexes, 0, sliceIndexes.Length, comp);

            // view the matrix according to the reordered slice indexes
            // take all rows and columns in the original order
            return(matrix.ViewSelection(sliceIndexes, null, null));
        }