use of cern.colt.matrix.DoubleMatrix1D 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 < B iff A.get(row,column) < B.get(row,column)</tt>
*<li><tt>A == B iff A.get(row,column) == B.get(row,column)</tt>
*<li><tt>A > B iff A.get(row,column) > 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);
}
use of cern.colt.matrix.DoubleMatrix1D in project tdq-studio-se by Talend.
the class Sorting method zdemo1.
/**
* Demonstrates advanced sorting.
* Sorts by sum of row.
*/
public static void zdemo1() {
Sorting sort = quickSort;
DoubleMatrix2D matrix = DoubleFactory2D.dense.descending(4, 3);
DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
double as = a.zSum();
double bs = b.zSum();
return as < bs ? -1 : as == bs ? 0 : 1;
}
};
System.out.println("unsorted:" + matrix);
System.out.println("sorted :" + sort.sort(matrix, comp));
}
use of cern.colt.matrix.DoubleMatrix1D in project tdq-studio-se by Talend.
the class Sorting method zdemo3.
/**
* Demonstrates advanced sorting.
* Sorts by sinus of cell values.
*/
public static void zdemo3() {
Sorting sort = quickSort;
double[] values = { 0.5, 1.5, 2.5, 3.5 };
DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
cern.colt.function.DoubleComparator comp = new cern.colt.function.DoubleComparator() {
public int compare(double a, double b) {
double as = Math.sin(a);
double bs = Math.sin(b);
return as < bs ? -1 : as == bs ? 0 : 1;
}
};
System.out.println("unsorted:" + matrix);
DoubleMatrix1D sorted = sort.sort(matrix, comp);
System.out.println("sorted :" + sorted);
// check whether it is really sorted
sorted.assign(cern.jet.math.Functions.sin);
/*
sorted.assign(
new cern.colt.function.DoubleFunction() {
public double apply(double arg) { return Math.sin(arg); }
}
);
*/
System.out.println("sined :" + sorted);
}
use of cern.colt.matrix.DoubleMatrix1D in project tdq-studio-se by Talend.
the class Sorting method zdemo4.
/**
* Demonstrates applying functions.
*/
protected static void zdemo4() {
double[] values1 = { 0, 1, 2, 3 };
double[] values2 = { 0, 2, 4, 6 };
DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
System.out.println("m1:" + matrix1);
System.out.println("m2:" + matrix2);
matrix1.assign(matrix2, cern.jet.math.Functions.pow);
/*
matrix1.assign(matrix2,
new cern.colt.function.DoubleDoubleFunction() {
public double apply(double x, double y) { return Math.pow(x,y); }
}
);
*/
System.out.println("applied:" + matrix1);
}
use of cern.colt.matrix.DoubleMatrix1D in project tdq-studio-se by Talend.
the class Statistic method covariance.
/**
* Constructs and returns the covariance matrix of the given matrix.
* The covariance matrix is a square, symmetric matrix consisting of nothing but covariance coefficients.
* The rows and the columns represent the variables, the cells represent covariance coefficients.
* The diagonal cells (i.e. the covariance between a variable and itself) will equal the variances.
* The covariance of two column vectors x and y is given by <tt>cov(x,y) = (1/n) * Sum((x[i]-mean(x)) * (y[i]-mean(y)))</tt>.
* See the <A HREF="http://www.cquest.utoronto.ca/geog/ggr270y/notes/not05efg.html"> math definition</A>.
* Compares two column vectors at a time. Use dice views to compare two row vectors at a time.
*
* @param matrix any matrix; a column holds the values of a given variable.
* @return the covariance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
public static DoubleMatrix2D covariance(DoubleMatrix2D matrix) {
int rows = matrix.rows();
int columns = matrix.columns();
DoubleMatrix2D covariance = new cern.colt.matrix.impl.DenseDoubleMatrix2D(columns, columns);
double[] sums = new double[columns];
DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
for (int i = columns; --i >= 0; ) {
cols[i] = matrix.viewColumn(i);
sums[i] = cols[i].zSum();
}
for (int i = columns; --i >= 0; ) {
for (int j = i + 1; --j >= 0; ) {
double sumOfProducts = cols[i].zDotProduct(cols[j]);
double cov = (sumOfProducts - sums[i] * sums[j] / rows) / rows;
covariance.setQuick(i, j, cov);
// symmetric
covariance.setQuick(j, i, cov);
}
}
return covariance;
}
Aggregations