Beispiel #1
0
        /// <summary>
        /// Returns a string representation of the given matrix.
        /// </summary>
        /// <param name="matrix">the matrix to convert.</param>
        /// <returns></returns>
        public String ToString(ObjectMatrix1D matrix)
        {
            ObjectMatrix2D easy = matrix.Like2D(1, matrix.Size);

            easy.ViewRow(0).Assign(matrix);
            return(ToString(easy));
        }
Beispiel #2
0
 /// <summary>
 /// Returns a string representations of all cells; no alignment considered.
 /// </summary>
 /// <param name="matrix"></param>
 /// <returns></returns>
 protected String[][] Format(ObjectMatrix2D matrix)
 {
     String[][] strings = new String[matrix.Rows][];
     strings = strings.Initialize(matrix.Rows, matrix.Columns);
     for (int row = matrix.Rows; --row >= 0;)
     {
         strings[row] = FormatRow(matrix.ViewRow(row));
     }
     return(strings);
 }
Beispiel #3
0
        /// <summary>
        /// Sorts the matrix rows according to the order induced by the specified comparator.
        /// The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
        /// The algorithm compares two rows (1-d matrices) at a time, determinining whether one is smaller, equal or larger than the other.
        /// To sort ranges use sub-ranging viewsd To sort columns by rows, use dice viewsd To sort descending, use flip views ...
        /// <p>
        /// <b>Example:</b>
        /// <pre>
        /// // sort by sum of values in a row
        /// ObjectMatrix1DComparator comp = new ObjectMatrix1DComparator() {
        /// &nbsp;&nbsp;&nbsp;public int compare(ObjectMatrix1D a, ObjectMatrix1D b) {
        /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object as = a.zSum(); Object bs = b.zSum();
        /// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return as %lt; bs ? -1 : as == bs ? 0 : 1;
        /// &nbsp;&nbsp;&nbsp;}
        /// };
        /// sorted = quickSort(matrix,comp);
        /// </pre>
        ///
        /// @param matrix the matrix to be sorted.
        /// @param c the comparator to determine the order.
        /// @return a new matrix view having rows sorted as specified.
        ///         <b>Note that the original matrix is left unaffected.</b>
        /// </summary>
        public ObjectMatrix2D sort(ObjectMatrix2D matrix, ObjectMatrix1DComparator c)
        {
            int[] rowIndexes = new int[matrix.Rows]; // row indexes to reorder instead of matrix itself
            for (int i = rowIndexes.Length; --i >= 0;)
            {
                rowIndexes[i] = i;
            }

            ObjectMatrix1D[] views = new ObjectMatrix1D[matrix.Rows]; // precompute views for speed
            for (int i = views.Length; --i >= 0;)
            {
                views[i] = matrix.ViewRow(i);
            }

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

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

            // view the matrix according to the reordered row indexes
            // take all columns in the original order
            return(matrix.ViewSelection(rowIndexes, null));
        }
Beispiel #4
0
        /// <summary>
        /// Returns a string representation of the given matrix with axis as well as rows and columns labeled.
        /// Pass <i>null</i> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.
        /// </summary>
        /// <param name="matrix">The matrix to format.</param>
        /// <param name="rowNames">The headers of all rows (to be put to the left of the matrix).</param>
        /// <param name="columnNames">The headers of all columns (to be put to above the matrix).</param>
        /// <param name="rowAxisName">The label of the y-axis.</param>
        /// <param name="columnAxisName">The label of the x-axis.</param>
        /// <param name="title">The overall title of the matrix to be formatted.</param>
        /// <returns>the matrix converted to a string.</returns>
        public String ToTitleString(ObjectMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title)
        {
            if (matrix.Size == 0)
            {
                return("Empty matrix");
            }
            String oldFormat = this.formatString;

            this.formatString = LEFT;

            int rows    = matrix.Rows;
            int columns = matrix.Columns;

            // determine how many rows and columns are needed
            int r = 0;
            int c = 0;

            r += (columnNames == null ? 0 : 1);
            c += (rowNames == null ? 0 : 1);
            c += (rowAxisName == null ? 0 : 1);
            c += (rowNames != null || rowAxisName != null ? 1 : 0);

            int height = r + System.Math.Max(rows, rowAxisName == null ? 0 : rowAxisName.Length);
            int width  = c + columns;

            // make larger matrix holding original matrix and naming strings
            ObjectMatrix2D titleMatrix = matrix.Like(height, width);

            // insert original matrix into larger matrix
            titleMatrix.ViewPart(r, c, rows, columns).Assign(matrix);

            // insert column axis name in leading row
            if (r > 0)
            {
                titleMatrix.ViewRow(0).ViewPart(c, columns).Assign(columnNames);
            }

            // insert row axis name in leading column
            if (rowAxisName != null)
            {
                String[] rowAxisStrings = new String[rowAxisName.Length];
                for (int i = rowAxisName.Length; --i >= 0;)
                {
                    rowAxisStrings[i] = rowAxisName.Substring(i, i + 1);
                }
                titleMatrix.ViewColumn(0).ViewPart(r, rowAxisName.Length).Assign(rowAxisStrings);
            }
            // insert row names in next leading columns
            if (rowNames != null)
            {
                titleMatrix.ViewColumn(c - 2).ViewPart(r, rows).Assign(rowNames);
            }

            // insert vertical "---------" separator line in next leading column
            if (c > 0)
            {
                titleMatrix.ViewColumn(c - 2 + 1).ViewPart(0, rows + r).Assign("|");
            }

            // convert the large matrix to a string
            Boolean oldPrintShape = this.printShape;

            this.printShape = false;
            String str = ToString(titleMatrix);

            this.printShape = oldPrintShape;

            // insert horizontal "--------------" separator line
            StringBuilder total = new StringBuilder(str);

            if (columnNames != null)
            {
                int i = str.IndexOf(rowSeparator);
                total.Insert(i + 1, Repeat('-', i) + rowSeparator);
            }
            else if (columnAxisName != null)
            {
                int i = str.IndexOf(rowSeparator);
                total.Insert(0, Repeat('-', i) + rowSeparator);
            }

            // insert line for column axis name
            if (columnAxisName != null)
            {
                int j = 0;
                if (c > 0)
                {
                    j = str.IndexOf('|');
                }
                String s = Blanks(j);
                if (c > 0)
                {
                    s = s + "| ";
                }
                s = s + columnAxisName + "\n";
                total.Insert(0, s);
            }

            // insert title
            if (title != null)
            {
                total.Insert(0, title + "\n");
            }

            this.formatString = oldFormat;

            return(total.ToString());
        }