Search in sources :

Example 11 with IntComparator

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

the class Partitioning method partition.

/**
 *Same as {@link cern.colt.Partitioning#partition(int[],int,int,int[],int,int,int[])}
 *except that it <i>synchronously</i> partitions the rows of the given matrix by the values of the given matrix column;
 *This is essentially the same as partitioning a list of composite objects by some instance variable;
 *In other words, two entire rows of the matrix are swapped, whenever two column values indicate so.
 *<p>
 *Let's say, a "row" is an "object" (tuple, d-dimensional point).
 *A "column" is the list of "object" values of a given variable (field, dimension).
 *A "matrix" is a list of "objects" (tuples, points).
 *<p>
 *Now, rows (objects, tuples) are partially sorted according to their values in one given variable (dimension).
 *Two entire rows of the matrix are swapped, whenever two column values indicate so.
 *<p>
 *Note that arguments are not checked for validity.
 *<p>
 *<b>Example:</b>
 *<table border="1" cellspacing="0">
 *  <tr nowrap>
 *	<td valign="top"><tt>8 x 3 matrix:<br>
 *	  23, 22, 21<br>
 *	  20, 19, 18<br>
 *	  17, 16, 15<br>
 *	  14, 13, 12<br>
 *	  11, 10, 9<br>
 *	  8,  7,  6<br>
 *	  5,  4,  3<br>
 *	  2,  1,  0 </tt></td>
 *	<td align="left" valign="top">
 *	  <p><tt>column = 0;<br>
 *	    rowIndexes = {0,1,2,..,matrix.rows()-1};
 *		rowFrom = 0;<br>
 *		rowTo = matrix.rows()-1;<br>
 *		splitters = {5,10,12}<br>
 *		c = 0; <br>
 *		d = splitters.length-1;<br>
 *		partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,c,d,splitIndexes);<br>
 *		==><br>
 *		splitIndexes == {0, 2, 3}<br>
 *		rowIndexes == {7, 6, 5, 4, 0, 1, 2, 3}</tt></p>
 *	  </td>
 *	<td valign="top">
 *	  The matrix IS NOT REORDERED.<br>
 *	  Here is how it would look<br>
 *	  like, if it would be reordered<br>
 *	  accoring to <tt>rowIndexes</tt>.<br>
 *	  <tt>8 x 3 matrix:<br>
 *	  2,  1,  0<br>
 *	  5,  4,  3<br>
 *	  8,  7,  6<br>
 *	  11, 10, 9<br>
 *	  23, 22, 21<br>
 *	  20, 19, 18<br>
 *	  17, 16, 15<br>
 *	  14, 13, 12 </tt></td>
 *  </tr>
 *</table>
 *@param matrix the matrix to be partitioned.
 *@param rowIndexes the index of the i-th row; is modified by this method to reflect partitioned indexes.
 *@param rowFrom the index of the first row (inclusive).
 *@param rowTo the index of the last row (inclusive).
 *@param column the index of the column to partition on.
 *@param splitters the values at which the rows shall be split into intervals.
 *	Must be sorted ascending and must not contain multiple identical values.
 *	These preconditions are not checked; be sure that they are met.
 *
 *@param splitFrom the index of the first splitter element to be considered.
 *@param splitTo the index of the last splitter element to be considered.
 *	The method considers the splitter elements <tt>splitters[splitFrom] .. splitters[splitTo]</tt>.
 *
 *@param splitIndexes a list into which this method fills the indexes of rows delimiting intervals.
 *Upon return <tt>splitIndexes[splitFrom..splitTo]</tt> will be set accordingly.
 *Therefore, must satisfy <tt>splitIndexes.length >= splitters.length</tt>.
 */
public static void partition(DoubleMatrix2D matrix, int[] rowIndexes, int rowFrom, int rowTo, int column, final double[] splitters, int splitFrom, int splitTo, int[] splitIndexes) {
    if (rowFrom < 0 || rowTo >= matrix.rows() || rowTo >= rowIndexes.length)
        throw new IllegalArgumentException();
    if (column < 0 || column >= matrix.columns())
        throw new IllegalArgumentException();
    if (splitFrom < 0 || splitTo >= splitters.length)
        throw new IllegalArgumentException();
    if (splitIndexes.length < splitters.length)
        throw new IllegalArgumentException();
    // this one knows how to swap two row indexes (a,b)
    final int[] g = rowIndexes;
    Swapper swapper = new Swapper() {

        public void swap(int b, int c) {
            int tmp = g[b];
            g[b] = g[c];
            g[c] = tmp;
        }
    };
    // compare splitter[a] with columnView[rowIndexes[b]]
    final DoubleMatrix1D columnView = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            double av = splitters[a];
            double bv = columnView.getQuick(g[b]);
            return av < bv ? -1 : (av == bv ? 0 : 1);
        }
    };
    // compare columnView[rowIndexes[a]] with columnView[rowIndexes[b]]
    IntComparator comp2 = new IntComparator() {

        public int compare(int a, int b) {
            double av = columnView.getQuick(g[a]);
            double bv = columnView.getQuick(g[b]);
            return av < bv ? -1 : (av == bv ? 0 : 1);
        }
    };
    // compare splitter[a] with splitter[b]
    IntComparator comp3 = new IntComparator() {

        public int compare(int a, int b) {
            double av = splitters[a];
            double bv = splitters[b];
            return av < bv ? -1 : (av == bv ? 0 : 1);
        }
    };
    // generic partitioning does the main work of reordering row indexes
    cern.colt.Partitioning.genericPartition(rowFrom, rowTo, splitFrom, splitTo, splitIndexes, comp, comp2, comp3, swapper);
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) IntComparator(cern.colt.function.IntComparator) Swapper(cern.colt.Swapper)

Example 12 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 DoubleMatrix3D sort(DoubleMatrix3D 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 DoubleMatrix1D sliceView = matrix.viewRow(row).viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            double av = sliceView.getQuick(a);
            double bv = sliceView.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(sliceIndexes, 0, sliceIndexes.length, comp);
    // take all rows and columns in the original order
    return matrix.viewSelection(sliceIndexes, null, null);
}
Also used : DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) IntComparator(cern.colt.function.IntComparator)

Example 13 with IntComparator

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

the class Partitioning method partition.

/**
 *Same as {@link cern.colt.Partitioning#partition(int[],int,int,int[],int,int,int[])}
 *except that it <i>synchronously</i> partitions the rows of the given matrix by the values of the given matrix column;
 *This is essentially the same as partitioning a list of composite objects by some instance variable;
 *In other words, two entire rows of the matrix are swapped, whenever two column values indicate so.
 *<p>
 *Let's say, a "row" is an "object" (tuple, d-dimensional point).
 *A "column" is the list of "object" values of a given variable (field, dimension).
 *A "matrix" is a list of "objects" (tuples, points).
 *<p>
 *Now, rows (objects, tuples) are partially sorted according to their values in one given variable (dimension).
 *Two entire rows of the matrix are swapped, whenever two column values indicate so.
 *<p>
 *Note that arguments are not checked for validity.
 *<p>
 *<b>Example:</b>
 *<table border="1" cellspacing="0">
 *  <tr nowrap>
 *	<td valign="top"><tt>8 x 3 matrix:<br>
 *	  23, 22, 21<br>
 *	  20, 19, 18<br>
 *	  17, 16, 15<br>
 *	  14, 13, 12<br>
 *	  11, 10, 9<br>
 *	  8,  7,  6<br>
 *	  5,  4,  3<br>
 *	  2,  1,  0 </tt></td>
 *	<td align="left" valign="top">
 *	  <p><tt>column = 0;<br>
 *	    rowIndexes = {0,1,2,..,matrix.rows()-1};
 *		rowFrom = 0;<br>
 *		rowTo = matrix.rows()-1;<br>
 *		splitters = {5,10,12}<br>
 *		c = 0; <br>
 *		d = splitters.length-1;<br>
 *		partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,c,d,splitIndexes);<br>
 *		==><br>
 *		splitIndexes == {0, 2, 3}<br>
 *		rowIndexes == {7, 6, 5, 4, 0, 1, 2, 3}</tt></p>
 *	  </td>
 *	<td valign="top">
 *	  The matrix IS NOT REORDERED.<br>
 *	  Here is how it would look<br>
 *	  like, if it would be reordered<br>
 *	  accoring to <tt>rowIndexes</tt>.<br>
 *	  <tt>8 x 3 matrix:<br>
 *	  2,  1,  0<br>
 *	  5,  4,  3<br>
 *	  8,  7,  6<br>
 *	  11, 10, 9<br>
 *	  23, 22, 21<br>
 *	  20, 19, 18<br>
 *	  17, 16, 15<br>
 *	  14, 13, 12 </tt></td>
 *  </tr>
 *</table>
 *@param matrix the matrix to be partitioned.
 *@param rowIndexes the index of the i-th row; is modified by this method to reflect partitioned indexes.
 *@param rowFrom the index of the first row (inclusive).
 *@param rowTo the index of the last row (inclusive).
 *@param column the index of the column to partition on.
 *@param splitters the values at which the rows shall be split into intervals.
 *	Must be sorted ascending and must not contain multiple identical values.
 *	These preconditions are not checked; be sure that they are met.
 *
 *@param splitFrom the index of the first splitter element to be considered.
 *@param splitTo the index of the last splitter element to be considered.
 *	The method considers the splitter elements <tt>splitters[splitFrom] .. splitters[splitTo]</tt>.
 *
 *@param splitIndexes a list into which this method fills the indexes of rows delimiting intervals.
 *Upon return <tt>splitIndexes[splitFrom..splitTo]</tt> will be set accordingly.
 *Therefore, must satisfy <tt>splitIndexes.length >= splitters.length</tt>.
 */
public static void partition(ObjectMatrix2D matrix, int[] rowIndexes, int rowFrom, int rowTo, int column, final Object[] splitters, int splitFrom, int splitTo, int[] splitIndexes) {
    if (rowFrom < 0 || rowTo >= matrix.rows() || rowTo >= rowIndexes.length)
        throw new IllegalArgumentException();
    if (column < 0 || column >= matrix.columns())
        throw new IllegalArgumentException();
    if (splitFrom < 0 || splitTo >= splitters.length)
        throw new IllegalArgumentException();
    if (splitIndexes.length < splitters.length)
        throw new IllegalArgumentException();
    // this one knows how to swap two row indexes (a,b)
    final int[] g = rowIndexes;
    Swapper swapper = new Swapper() {

        public void swap(int b, int c) {
            int tmp = g[b];
            g[b] = g[c];
            g[c] = tmp;
        }
    };
    // compare splitter[a] with columnView[rowIndexes[b]]
    final ObjectMatrix1D columnView = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            Comparable av = (Comparable) (splitters[a]);
            Comparable bv = (Comparable) (columnView.getQuick(g[b]));
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r == 0 ? 0 : 1);
        }
    };
    // compare columnView[rowIndexes[a]] with columnView[rowIndexes[b]]
    IntComparator comp2 = new IntComparator() {

        public int compare(int a, int b) {
            Comparable av = (Comparable) (columnView.getQuick(g[a]));
            Comparable bv = (Comparable) (columnView.getQuick(g[b]));
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r == 0 ? 0 : 1);
        }
    };
    // compare splitter[a] with splitter[b]
    IntComparator comp3 = new IntComparator() {

        public int compare(int a, int b) {
            Comparable av = (Comparable) (splitters[a]);
            Comparable bv = (Comparable) (splitters[b]);
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r == 0 ? 0 : 1);
        }
    };
    // generic partitioning does the main work of reordering row indexes
    cern.colt.Partitioning.genericPartition(rowFrom, rowTo, splitFrom, splitTo, splitIndexes, comp, comp2, comp3, swapper);
}
Also used : IntComparator(cern.colt.function.IntComparator) Swapper(cern.colt.Swapper) ObjectMatrix1D(cern.colt.matrix.ObjectMatrix1D)

Example 14 with IntComparator

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

the class Sorting method sort.

/**
 *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 views. To sort columns by rows, use dice views. 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 < 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>
 */
public ObjectMatrix2D sort(final ObjectMatrix2D matrix, final ObjectMatrix1DComparator c) {
    // row indexes to reorder instead of matrix itself
    int[] rowIndexes = new int[matrix.rows()];
    for (int i = rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
    // precompute views for speed
    final ObjectMatrix1D[] views = new ObjectMatrix1D[matrix.rows()];
    for (int i = views.length; --i >= 0; ) views[i] = matrix.viewRow(i);
    IntComparator comp = new IntComparator() {

        public int compare(int a, int b) {
            // return c.compare(matrix.viewRow(a), matrix.viewRow(b));
            return c.compare(views[a], views[b]);
        }
    };
    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 15 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 <i>natural ordering</i>.
 *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 descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<table border="1" cellspacing="0">
 *  <tr nowrap>
 *	<td valign="top"><tt> 7, 1, 3, 1<br>
 *	  </tt></td>
 *	<td valign="top">
 *	  <p><tt> ==&gt; 1, 1, 3, 7<br>
 *		The vector IS NOT SORTED.<br>
 *		The new VIEW IS SORTED.</tt></p>
 *	</td>
 *  </tr>
 *</table>
 *
 *@param vector the vector to be sorted.
 *@return a new sorted vector (matrix) view.
 *		<b>Note that the original matrix is left unaffected.</b>
 */
public ObjectMatrix1D sort(final ObjectMatrix1D vector) {
    // 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) {
            Comparable av = (Comparable) (vector.getQuick(a));
            Comparable bv = (Comparable) (vector.getQuick(b));
            int r = av.compareTo(bv);
            return r < 0 ? -1 : (r > 0 ? 1 : 0);
        }
    };
    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