cern.colt.matrix.tfloat.algo.FloatPartitioning Maven / Gradle / Ivy
Show all versions of parallelcolt Show documentation
/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.tfloat.algo;
import cern.colt.Swapper;
import cern.colt.function.tint.IntComparator;
import cern.colt.matrix.tfloat.FloatMatrix1D;
import cern.colt.matrix.tfloat.FloatMatrix2D;
/**
* Given some interval boundaries, partitions matrices such that cell values
* falling into an interval are placed next to each other.
*
* Performance
*
* Partitioning into two intervals is O( N ). Partitioning into k
* intervals is O( N * log(k)). Constants factors are minimized.
*
* @see cern.colt.Partitioning "Partitioning arrays (provides more
* documentation)"
*
* @author [email protected]
* @version 1.0, 09/24/99
*/
public class FloatPartitioning extends Object {
/**
* Makes this class non instantiable, but still let's others inherit from
* it.
*/
protected FloatPartitioning() {
}
/**
* Same as
* {@link cern.colt.Partitioning#partition(int[],int,int,int[],int,int,int[])}
* except that it synchronously 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.
*
* 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).
*
* 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.
*
* Note that arguments are not checked for validity.
*
* Example:
*
*
* 8 x 3 matrix:
23, 22, 21
20, 19, 18
17, 16, 15
14, 13, 12
11, 10, 9
8, 7, 6
5, 4, 3
2, 1, 0
*
*
* column = 0;
rowIndexes = {0,1,2,..,matrix.rows()-1};
rowFrom = 0;
rowTo = matrix.rows()-1;
splitters = {5,10,12}
c = 0;
d = splitters.length-1;
partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,c,d,splitIndexes);
==>
splitIndexes == {0, 2, 3}
rowIndexes == {7, 6, 5, 4, 0, 1, 2, 3}
*
*
* The matrix IS NOT REORDERED.
* Here is how it would look
* like, if it would be reordered
* accoring to rowIndexes.
* 8 x 3 matrix:
2, 1, 0
5, 4, 3
8, 7, 6
11, 10, 9
23, 22, 21
20, 19, 18
17, 16, 15
14, 13, 12
*
*
*
* @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
* splitters[splitFrom] .. splitters[splitTo].
*
* @param splitIndexes
* a list into which this method fills the indexes of rows
* delimiting intervals. Upon return
* splitIndexes[splitFrom..splitTo] will be set
* accordingly. Therefore, must satisfy
* splitIndexes.length >= splitters.length.
*/
public static void partition(FloatMatrix2D matrix, int[] rowIndexes, int rowFrom, int rowTo, int column,
final float[] 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 FloatMatrix1D columnView = matrix.viewColumn(column);
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
float av = splitters[a];
float 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) {
float av = columnView.getQuick(g[a]);
float 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) {
float av = splitters[a];
float 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);
}
/**
* Same as
* {@link cern.colt.Partitioning#partition(int[],int,int,int[],int,int,int[])}
* except that it synchronously 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.
*
* 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).
*
* 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.
*
* Note that arguments are not checked for validity.
*
* Example:
*
*
* 8 x 3 matrix:
23, 22, 21
20, 19, 18
17, 16, 15
14, 13, 12
11, 10, 9
8, 7, 6
5, 4, 3
2, 1, 0
* column = 0;
splitters = {5,10,12}
partition(matrix,column,splitters,splitIndexes);
==>
splitIndexes == {0, 2, 3}
*
* The matrix IS NOT REORDERED.
* The new VIEW IS REORDERED:
* 8 x 3 matrix:
2, 1, 0
5, 4, 3
8, 7, 6
11, 10, 9
23, 22, 21
20, 19, 18
17, 16, 15
14, 13, 12
*
* @param matrix
* the matrix to be partitioned.
* @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 splitIndexes
* a list into which this method fills the indexes of rows
* delimiting intervals. Therefore, must satisfy
* splitIndexes.length >= splitters.length.
*
* @return a new matrix view having rows partitioned by the given column and
* splitters.
*/
public static FloatMatrix2D partition(FloatMatrix2D matrix, int column, final float[] splitters, int[] splitIndexes) {
int rowFrom = 0;
int rowTo = matrix.rows() - 1;
int splitFrom = 0;
int splitTo = splitters.length - 1;
int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder
// instead of matrix itself
for (int i = rowIndexes.length; --i >= 0;)
rowIndexes[i] = i;
partition(matrix, rowIndexes, rowFrom, rowTo, column, splitters, splitFrom, splitTo, splitIndexes);
// take all columns in the original order
int[] columnIndexes = new int[matrix.columns()];
for (int i = columnIndexes.length; --i >= 0;)
columnIndexes[i] = i;
// view the matrix according to the reordered row indexes
return matrix.viewSelection(rowIndexes, columnIndexes);
}
}