Search in sources :

Example 1 with ObjectMatrix1D

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

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 2 with ObjectMatrix1D

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

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 ObjectMatrix1D

use of cern.colt.matrix.ObjectMatrix1D 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 4 with ObjectMatrix1D

use of cern.colt.matrix.ObjectMatrix1D 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 5 with ObjectMatrix1D

use of cern.colt.matrix.ObjectMatrix1D in project Gemma by PavlidisLab.

the class ExpressionDataBooleanMatrix method getColumn.

@Override
public Boolean[] getColumn(Integer index) {
    ObjectMatrix1D rawResult = this.matrix.viewColumn(index);
    Boolean[] res = new Boolean[rawResult.size()];
    int i = 0;
    for (Object o : rawResult.toArray()) {
        res[i] = (Boolean) o;
        i++;
    }
    return res;
}
Also used : ObjectMatrix1D(cern.colt.matrix.ObjectMatrix1D)

Aggregations

ObjectMatrix1D (cern.colt.matrix.ObjectMatrix1D)11 IntComparator (cern.colt.function.IntComparator)8 Swapper (cern.colt.Swapper)2