Search in sources :

Example 1 with ObjectMatrix2D

use of cern.colt.matrix.ObjectMatrix2D in project tdq-studio-se by Talend.

the class Formatter method toString.

/**
 * Returns a string representation of the given matrix.
 * @param matrix the matrix to convert.
 */
public String toString(ObjectMatrix1D matrix) {
    ObjectMatrix2D easy = matrix.like2D(1, matrix.size());
    easy.viewRow(0).assign(matrix);
    return toString(easy);
}
Also used : ObjectMatrix2D(cern.colt.matrix.ObjectMatrix2D)

Example 2 with ObjectMatrix2D

use of cern.colt.matrix.ObjectMatrix2D in project tdq-studio-se by Talend.

the class Sorting method sort.

/**
 *Sorts the matrix slices 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 slices (2-d matrices) at a time, determinining whether one is smaller, equal or larger than the other.
 *To sort ranges use sub-ranging views. To sort by other dimensions, use dice views. To sort descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<pre>
 *// sort by sum of values in a slice
 *ObjectMatrix2DComparator comp = new ObjectMatrix2DComparator() {
 *&nbsp;&nbsp;&nbsp;public int compare(ObjectMatrix2D a, ObjectMatrix2D b) {
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object as = a.zSum(); Object bs = b.zSum();
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return as < 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 slices sorted as specified.
 *		<b>Note that the original matrix is left unaffected.</b>
 */
public ObjectMatrix3D sort(final ObjectMatrix3D matrix, final ObjectMatrix2DComparator c) {
    // indexes to reorder instead of matrix itself
    int[] sliceIndexes = new int[matrix.slices()];
    for (int i = sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
    // precompute views for speed
    final ObjectMatrix2D[] views = new ObjectMatrix2D[matrix.slices()];
    for (int i = views.length; --i >= 0; ) views[i] = matrix.viewSlice(i);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            // return c.compare(matrix.viewSlice(a), matrix.viewSlice(b));
            return c.compare(views[a], views[b]);
        }
    };
    runSort(sliceIndexes, 0, sliceIndexes.length, comp);
    // take all rows and columns in the original order
    return matrix.viewSelection(sliceIndexes, null, null);
}
Also used : IntComparator(cern.colt.function.IntComparator) ObjectMatrix2D(cern.colt.matrix.ObjectMatrix2D)

Example 3 with ObjectMatrix2D

use of cern.colt.matrix.ObjectMatrix2D in project tdq-studio-se by Talend.

the class DenseObjectMatrix2D method assign.

/**
 * Replaces all cell values of the receiver with the values of another matrix.
 * Both matrices must have the same number of rows and columns.
 * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>.
 *
 * @param     source   the source matrix to copy from (may be identical to the receiver).
 * @return <tt>this</tt> (for convenience only).
 * @throws	IllegalArgumentException if <tt>columns() != source.columns() || rows() != source.rows()</tt>
 */
public ObjectMatrix2D assign(ObjectMatrix2D source) {
    // overriden for performance only
    if (!(source instanceof DenseObjectMatrix2D)) {
        return super.assign(source);
    }
    DenseObjectMatrix2D other = (DenseObjectMatrix2D) source;
    // nothing to do
    if (other == this)
        return this;
    checkShape(other);
    if (this.isNoView && other.isNoView) {
        // quickest
        System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
        return this;
    }
    if (haveSharedCells(other)) {
        ObjectMatrix2D c = other.copy();
        if (!(c instanceof DenseObjectMatrix2D)) {
            // should not happen
            return super.assign(other);
        }
        other = (DenseObjectMatrix2D) c;
    }
    final Object[] elems = this.elements;
    final Object[] otherElems = other.elements;
    if (elements == null || otherElems == null)
        throw new InternalError();
    int cs = this.columnStride;
    int ocs = other.columnStride;
    int rs = this.rowStride;
    int ors = other.rowStride;
    int otherIndex = other.index(0, 0);
    int index = index(0, 0);
    for (int row = rows; --row >= 0; ) {
        for (int i = index, j = otherIndex, column = columns; --column >= 0; ) {
            elems[i] = otherElems[j];
            i += cs;
            j += ocs;
        }
        index += rs;
        otherIndex += ors;
    }
    return this;
}
Also used : ObjectMatrix2D(cern.colt.matrix.ObjectMatrix2D)

Example 4 with ObjectMatrix2D

use of cern.colt.matrix.ObjectMatrix2D in project tdq-studio-se by Talend.

the class Formatter method toTitleString.

/**
 *Returns a string representation of the given matrix with axis as well as rows and columns labeled.
 *Pass <tt>null</tt> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.
 *
 *@param matrix The matrix to format.
 *@param rowNames The headers of all rows (to be put to the left of the matrix).
 *@param columnNames The headers of all columns (to be put to above the matrix).
 *@param rowAxisName The label of the y-axis.
 *@param columnAxisName The label of the x-axis.
 *@param title The overall title of the matrix to be formatted.
 *@return the matrix converted to a string.
 */
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.format;
    this.format = 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 + Math.max(rows, rowAxisName == null ? 0 : rowAxisName.length());
    int width = c + columns;
    // make larger matrix holding original matrix and naming strings
    cern.colt.matrix.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
    StringBuffer total = new StringBuffer(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.format = oldFormat;
    return total.toString();
}
Also used : ObjectMatrix2D(cern.colt.matrix.ObjectMatrix2D)

Example 5 with ObjectMatrix2D

use of cern.colt.matrix.ObjectMatrix2D in project jAudioGIT by dmcennis.

the class Formatter method toString.

/**
 * Returns a string representation of the given matrix.
 * @param matrix the matrix to convert.
 */
public String toString(ObjectMatrix1D matrix) {
    ObjectMatrix2D easy = matrix.like2D(1, matrix.size());
    easy.viewRow(0).assign(matrix);
    return toString(easy);
}
Also used : ObjectMatrix2D(cern.colt.matrix.ObjectMatrix2D)

Aggregations

ObjectMatrix2D (cern.colt.matrix.ObjectMatrix2D)8 IntComparator (cern.colt.function.IntComparator)2