Search in sources :

Example 1 with IntComparator

use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.

the class Sorting method sort.

/**
 *Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the given column.
 *The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
 *To sort ranges use sub-ranging views. To sort columns by rows, use dice views. To sort descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<table border="1" cellspacing="0">
 *  <tr nowrap>
 *	<td valign="top"><tt>4 x 2 matrix: <br>
 *	  7, 6<br>
 *	  5, 4<br>
 *	  3, 2<br>
 *	  1, 0 <br>
 *	  </tt></td>
 *	<td align="left" valign="top">
 *	  <p><tt>column = 0;<br>
 *		view = quickSort(matrix,column);<br>
 *		System.out.println(view); </tt><tt><br>
 *		==> </tt></p>
 *	  </td>
 *	<td valign="top">
 *	  <p><tt>4 x 2 matrix:<br>
 *		1, 0<br>
 *		3, 2<br>
 *		5, 4<br>
 *		7, 6</tt><br>
 *		The matrix IS NOT SORTED.<br>
 *		The new VIEW IS SORTED.</p>
 *	  </td>
 *  </tr>
 *</table>
 *
 *@param matrix the matrix to be sorted.
 *@param column the index of the column inducing the order.
 *@return a new matrix view having rows sorted by the given column.
 *		<b>Note that the original matrix is left unaffected.</b>
 *@throws IndexOutOfBoundsException if <tt>column < 0 || column >= matrix.columns()</tt>.
 */
public ObjectMatrix2D sort(ObjectMatrix2D matrix, int column) {
    if (column < 0 || column >= matrix.columns())
        throw new IndexOutOfBoundsException("column=" + column + ", matrix=" + Formatter.shape(matrix));
    // row indexes to reorder instead of matrix itself
    int[] rowIndexes = new int[matrix.rows()];
    for (int i = rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
    final ObjectMatrix1D col = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            Comparable av = (Comparable) (col.getQuick(a));
            Comparable bv = (Comparable) (col.getQuick(b));
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r > 0 ? 1 : 0);
        }
    };
    runSort(rowIndexes, 0, rowIndexes.length, comp);
    // take all columns in the original order
    return matrix.viewSelection(rowIndexes, null);
}
Also used : IntComparator(cern.colt.function.IntComparator) ObjectMatrix1D(cern.colt.matrix.ObjectMatrix1D)

Example 2 with IntComparator

use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.

the class Sorting method sort.

/**
 *Sorts the matrix slices into ascending order, according to the <i>natural ordering</i> of the matrix values in the given <tt>[row,column]</tt> position.
 *The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
 *To sort ranges use sub-ranging views. To sort by other dimensions, use dice views. To sort descending, use flip views ...
 *<p>
 *The algorithm compares two 2-d slices at a time, determinining whether one is smaller, equal or larger than the other.
 *Comparison is based on the cell <tt>[row,column]</tt> within a slice.
 *Let <tt>A</tt> and <tt>B</tt> be two 2-d slices. Then we have the following rules
 *<ul>
 *<li><tt>A &lt;  B  iff A.get(row,column) &lt;  B.get(row,column)</tt>
 *<li><tt>A == B iff A.get(row,column) == B.get(row,column)</tt>
 *<li><tt>A &gt;  B  iff A.get(row,column) &gt;  B.get(row,column)</tt>
 *</ul>
 *
 *@param matrix the matrix to be sorted.
 *@param row the index of the row inducing the order.
 *@param column the index of the column inducing the order.
 *@return a new matrix view having slices sorted by the values of the slice view <tt>matrix.viewRow(row).viewColumn(column)</tt>.
 *		<b>Note that the original matrix is left unaffected.</b>
 *@throws IndexOutOfBoundsException if <tt>row < 0 || row >= matrix.rows() || column < 0 || column >= matrix.columns()</tt>.
 */
public ObjectMatrix3D sort(ObjectMatrix3D matrix, int row, int column) {
    if (row < 0 || row >= matrix.rows())
        throw new IndexOutOfBoundsException("row=" + row + ", matrix=" + Formatter.shape(matrix));
    if (column < 0 || column >= matrix.columns())
        throw new IndexOutOfBoundsException("column=" + column + ", matrix=" + Formatter.shape(matrix));
    // indexes to reorder instead of matrix itself
    int[] sliceIndexes = new int[matrix.slices()];
    for (int i = sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
    final ObjectMatrix1D sliceView = matrix.viewRow(row).viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            Comparable av = (Comparable) (sliceView.getQuick(a));
            Comparable bv = (Comparable) (sliceView.getQuick(b));
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r > 0 ? 1 : 0);
        }
    };
    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) ObjectMatrix1D(cern.colt.matrix.ObjectMatrix1D)

Example 3 with IntComparator

use of cern.colt.function.IntComparator 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 4 with IntComparator

use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.

the class Sorting method sort.

/**
 *Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the given column.
 *The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
 *To sort ranges use sub-ranging views. To sort columns by rows, use dice views. To sort descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<table border="1" cellspacing="0">
 *  <tr nowrap>
 *	<td valign="top"><tt>4 x 2 matrix: <br>
 *	  7, 6<br>
 *	  5, 4<br>
 *	  3, 2<br>
 *	  1, 0 <br>
 *	  </tt></td>
 *	<td align="left" valign="top">
 *	  <p><tt>column = 0;<br>
 *		view = quickSort(matrix,column);<br>
 *		System.out.println(view); </tt><tt><br>
 *		==> </tt></p>
 *	  </td>
 *	<td valign="top">
 *	  <p><tt>4 x 2 matrix:<br>
 *		1, 0<br>
 *		3, 2<br>
 *		5, 4<br>
 *		7, 6</tt><br>
 *		The matrix IS NOT SORTED.<br>
 *		The new VIEW IS SORTED.</p>
 *	  </td>
 *  </tr>
 *</table>
 *
 *@param matrix the matrix to be sorted.
 *@param column the index of the column inducing the order.
 *@return a new matrix view having rows sorted by the given column.
 *		<b>Note that the original matrix is left unaffected.</b>
 *@throws IndexOutOfBoundsException if <tt>column < 0 || column >= matrix.columns()</tt>.
 */
public DoubleMatrix2D sort(DoubleMatrix2D matrix, int column) {
    if (column < 0 || column >= matrix.columns())
        throw new IndexOutOfBoundsException("column=" + column + ", matrix=" + Formatter.shape(matrix));
    // row indexes to reorder instead of matrix itself
    int[] rowIndexes = new int[matrix.rows()];
    for (int i = rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
    final DoubleMatrix1D col = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            double av = col.getQuick(a);
            double bv = col.getQuick(b);
            // swap NaNs to the end
            if (av != av || bv != bv)
                return compareNaN(av, bv);
            return av < bv ? -1 : (av == bv ? 0 : 1);
        }
    };
    runSort(rowIndexes, 0, rowIndexes.length, comp);
    // take all columns in the original order
    return matrix.viewSelection(rowIndexes, null);
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) IntComparator(cern.colt.function.IntComparator)

Example 5 with IntComparator

use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.

the class Sorting method sort.

/**
 *Sorts the vector into ascending order, 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 cells at a time, determinining whether one is smaller, equal or larger than the other.
 *To sort ranges use sub-ranging views. To sort descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<pre>
 *// sort by sinus of cells
 *DoubleComparator comp = new DoubleComparator() {
 *&nbsp;&nbsp;&nbsp;public int compare(double a, double b) {
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double as = Math.sin(a); double bs = Math.sin(b);
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return as < bs ? -1 : as == bs ? 0 : 1;
 *&nbsp;&nbsp;&nbsp;}
 *};
 *sorted = quickSort(vector,comp);
 *</pre>
 *
 *@param vector the vector to be sorted.
 *@param c the comparator to determine the order.
 *@return a new matrix view sorted as specified.
 *		<b>Note that the original vector (matrix) is left unaffected.</b>
 */
public DoubleMatrix1D sort(final DoubleMatrix1D vector, final cern.colt.function.DoubleComparator c) {
    // row indexes to reorder instead of matrix itself
    int[] indexes = new int[vector.size()];
    for (int i = indexes.length; --i >= 0; ) indexes[i] = i;
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            return c.compare(vector.getQuick(a), vector.getQuick(b));
        }
    };
    runSort(indexes, 0, indexes.length, comp);
    return vector.viewSelection(indexes);
}
Also used : IntComparator(cern.colt.function.IntComparator)

Aggregations

IntComparator (cern.colt.function.IntComparator)16 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)4 ObjectMatrix1D (cern.colt.matrix.ObjectMatrix1D)4 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)3 Swapper (cern.colt.Swapper)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 ObjectMatrix2D (cern.colt.matrix.ObjectMatrix2D)1