use of cern.colt.matrix.DoubleMatrix2D 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.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method zdemo6.
/**
* Demonstrates advanced sorting.
* Sorts by sum of row.
*/
public static void zdemo6() {
Sorting sort = quickSort;
double[][] values = { { 3, 7, 0 }, { 2, 1, 0 }, { 2, 2, 0 }, { 1, 8, 0 }, { 2, 5, 0 }, { 7, 0, 0 }, { 2, 3, 0 }, { 1, 0, 0 }, { 4, 0, 0 }, { 2, 0, 0 } };
DoubleMatrix2D A = DoubleFactory2D.dense.make(values);
DoubleMatrix2D B, C;
/*
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("\n\nunsorted:" + A);
B = quickSort.sort(A, 1);
C = quickSort.sort(B, 0);
System.out.println("quick sorted :" + C);
B = mergeSort.sort(A, 1);
C = mergeSort.sort(B, 0);
System.out.println("merge sorted :" + C);
}
use of cern.colt.matrix.DoubleMatrix2D 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;
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Statistic method demo2.
/**
* Demonstrates usage of this class.
*/
public static void demo2(int rows, int columns, boolean print) {
System.out.println("\n\ninitializing...");
DoubleFactory2D factory = DoubleFactory2D.dense;
DoubleMatrix2D A = factory.ascending(rows, columns);
// double value = 1;
// DoubleMatrix2D A = factory.make(rows,columns);
// A.assign(value);
System.out.println("benchmarking correlation...");
cern.colt.Timer timer = new cern.colt.Timer().start();
DoubleMatrix2D corr = correlation(covariance(A));
timer.stop().display();
if (print) {
System.out.println("printing result...");
System.out.println(corr);
}
System.out.println("done.");
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Statistic method distance.
/**
* Constructs and returns the distance matrix of the given matrix.
* The distance matrix is a square, symmetric matrix consisting of nothing but distance coefficients.
* The rows and the columns represent the variables, the cells represent distance coefficients.
* The diagonal cells (i.e. the distance between a variable and itself) will be zero.
* 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 (vector).
* @param distanceFunction (EUCLID, CANBERRA, ..., or any user defined distance function operating on two vectors).
* @return the distance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
public static DoubleMatrix2D distance(DoubleMatrix2D matrix, VectorVectorFunction distanceFunction) {
int columns = matrix.columns();
DoubleMatrix2D distance = new cern.colt.matrix.impl.DenseDoubleMatrix2D(columns, columns);
// cache views
DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
for (int i = columns; --i >= 0; ) {
cols[i] = matrix.viewColumn(i);
}
// work out all permutations
for (int i = columns; --i >= 0; ) {
for (int j = i; --j >= 0; ) {
double d = distanceFunction.apply(cols[i], cols[j]);
distance.setQuick(i, j, d);
// symmetric
distance.setQuick(j, i, d);
}
}
return distance;
}
Aggregations