use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Formatter method demo1.
/**
* Demonstrates how to use this class.
*/
public static void demo1() {
// parameters
double[][] values = { { 3, 0, -3.4, 0 }, { 5.1, 0, +3.0123456789, 0 }, { 16.37, 0.0, 2.5, 0 }, { -16.3, 0, -3.012345678E-4, -1 }, { 1236.3456789, 0, 7, -1.2 } };
String[] formats = { "%G", "%1.10G", "%f", "%1.2f", "%0.2e", null };
// now the processing
int size = formats.length;
DoubleMatrix2D matrix = cern.colt.matrix.DoubleFactory2D.dense.make(values);
String[] strings = new String[size];
String[] sourceCodes = new String[size];
String[] htmlStrings = new String[size];
String[] htmlSourceCodes = new String[size];
for (int i = 0; i < size; i++) {
String format = formats[i];
strings[i] = new Formatter(format).toString(matrix);
sourceCodes[i] = new Formatter(format).toSourceCode(matrix);
// may not compile because of packages not included in the distribution
// htmlStrings[i] = cern.colt.matrixpattern.Converting.toHTML(strings[i]);
// htmlSourceCodes[i] = cern.colt.matrixpattern.Converting.toHTML(sourceCodes[i]);
}
System.out.println("original:\n" + new Formatter().toString(matrix));
// may not compile because of packages not included in the distribution
for (int i = 0; i < size; i++) {
// System.out.println("\nhtmlString("+formats[i]+"):\n"+htmlStrings[i]);
// System.out.println("\nhtmlSourceCode("+formats[i]+"):\n"+htmlSourceCodes[i]);
}
for (int i = 0; i < size; i++) {
System.out.println("\nstring(" + formats[i] + "):\n" + strings[i]);
System.out.println("\nsourceCode(" + formats[i] + "):\n" + sourceCodes[i]);
}
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Formatter method toTitleString.
/**
*Same as <tt>toTitleString</tt> except that additionally statistical aggregates (mean, median, sum, etc.) of rows and columns are printed.
*Pass <tt>null</tt> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.
*
*@param matrix The matrix to format.
*@param rowNames The headers of all rows (to be put to the left of the matrix).
*@param columnNames The headers of all columns (to be put to above the matrix).
*@param rowAxisName The label of the y-axis.
*@param columnAxisName The label of the x-axis.
*@param title The overall title of the matrix to be formatted.
*@param aggr the aggregation functions to be applied to columns and rows.
*@return the matrix converted to a string.
*@see hep.aida.bin.BinFunction1D
*@see hep.aida.bin.BinFunctions1D
*/
public String toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title, hep.aida.bin.BinFunction1D[] aggr) {
if (matrix.size() == 0)
return "Empty matrix";
if (aggr == null || aggr.length == 0)
return toTitleString(matrix, rowNames, columnNames, rowAxisName, columnAxisName, title);
// hold row aggregations
DoubleMatrix2D rowStats = matrix.like(matrix.rows(), aggr.length);
// hold column aggregations
DoubleMatrix2D colStats = matrix.like(aggr.length, matrix.columns());
// aggregate an entire column at a time
cern.colt.matrix.doublealgo.Statistic.aggregate(matrix, aggr, colStats);
// aggregate an entire row at a time
cern.colt.matrix.doublealgo.Statistic.aggregate(matrix.viewDice(), aggr, rowStats.viewDice());
// turn into strings
// tmp holds "matrix" plus "colStats" below (needed so that numbers in a columns can be decimal point aligned)
DoubleMatrix2D tmp = matrix.like(matrix.rows() + aggr.length, matrix.columns());
tmp.viewPart(0, 0, matrix.rows(), matrix.columns()).assign(matrix);
tmp.viewPart(matrix.rows(), 0, aggr.length, matrix.columns()).assign(colStats);
colStats = null;
String[][] s1 = format(tmp);
align(s1);
tmp = null;
String[][] s2 = format(rowStats);
align(s2);
rowStats = null;
// copy strings into a large matrix holding the source matrix and all aggregations
cern.colt.matrix.ObjectMatrix2D allStats = cern.colt.matrix.ObjectFactory2D.dense.make(matrix.rows() + aggr.length, matrix.columns() + aggr.length + 1);
allStats.viewPart(0, 0, matrix.rows() + aggr.length, matrix.columns()).assign(s1);
allStats.viewColumn(matrix.columns()).assign("|");
allStats.viewPart(0, matrix.columns() + 1, matrix.rows(), aggr.length).assign(s2);
s1 = null;
s2 = null;
// append a vertical "|" separator plus names of aggregation functions to line holding columnNames
if (columnNames != null) {
cern.colt.list.ObjectArrayList list = new cern.colt.list.ObjectArrayList(columnNames);
list.add("|");
// add names of aggregation functions
for (int i = 0; i < aggr.length; i++) list.add(aggr[i].name());
columnNames = new String[list.size()];
list.toArray(columnNames);
}
// append names of aggregation functions to line holding rowNames
if (rowNames != null) {
cern.colt.list.ObjectArrayList list = new cern.colt.list.ObjectArrayList(rowNames);
// add names of aggregation functions
for (int i = 0; i < aggr.length; i++) list.add(aggr[i].name());
rowNames = new String[list.size()];
list.toArray(rowNames);
}
// turn large matrix into string
String s = new cern.colt.matrix.objectalgo.Formatter().toTitleString(allStats, rowNames, columnNames, rowAxisName, columnAxisName, title);
// insert a horizontal "----------------------" separation line above the column stats
// determine insertion position and line width
int last = s.length() + 1;
int secondLast = last;
int v = Math.max(0, rowAxisName == null ? 0 : rowAxisName.length() - matrix.rows() - aggr.length);
for (int k = 0; k < aggr.length + 1 + v; k++) {
// scan "aggr.length+1+v" lines backwards
secondLast = last;
last = s.lastIndexOf(rowSeparator, last - 1);
}
StringBuffer buf = new StringBuffer(s);
buf.insert(secondLast, rowSeparator + repeat('-', secondLast - last - 1));
return buf.toString();
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Formatter method toString.
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(DoubleMatrix1D matrix) {
DoubleMatrix2D easy = matrix.like2D(1, matrix.size());
easy.viewRow(0).assign(matrix);
return toString(easy);
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method zdemo5.
/**
* Demonstrates sorting with precomputation of aggregates (median and sum of logarithms).
*/
public static void zdemo5(int rows, int columns, boolean print) {
Sorting sort = quickSort;
// for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
System.out.println("\n\n");
System.out.print("now initializing... ");
cern.colt.Timer timer = new cern.colt.Timer().start();
final cern.jet.math.Functions F = cern.jet.math.Functions.functions;
DoubleMatrix2D A = cern.colt.matrix.DoubleFactory2D.dense.make(rows, columns);
// initialize randomly
A.assign(new cern.jet.random.engine.DRand());
timer.stop().display();
// also benchmark copying in its several implementation flavours
DoubleMatrix2D B = A.like();
timer.reset().start();
System.out.print("now copying... ");
B.assign(A);
timer.stop().display();
timer.reset().start();
System.out.print("now copying subrange... ");
B.viewPart(0, 0, rows, columns).assign(A.viewPart(0, 0, rows, columns));
timer.stop().display();
// System.out.println(A);
timer.reset().start();
System.out.print("now copying selected... ");
B.viewSelection(null, null).assign(A.viewSelection(null, null));
timer.stop().display();
System.out.print("now sorting - quick version with precomputation... ");
timer.reset().start();
// THE QUICK VERSION (takes some 10 secs)
A = sort.sort(A, hep.aida.bin.BinFunctions1D.median);
// A = sort.sort(A,hep.aida.bin.BinFunctions1D.sumLog);
timer.stop().display();
// so we just show the first 5 rows
if (print) {
int r = Math.min(rows, 5);
hep.aida.bin.BinFunction1D[] funs = { hep.aida.bin.BinFunctions1D.median, hep.aida.bin.BinFunctions1D.sumLog, hep.aida.bin.BinFunctions1D.geometricMean };
String[] rowNames = new String[r];
String[] columnNames = new String[columns];
for (int i = columns; --i >= 0; ) columnNames[i] = Integer.toString(i);
for (int i = r; --i >= 0; ) rowNames[i] = Integer.toString(i);
System.out.println("first part of sorted result = \n" + new cern.colt.matrix.doublealgo.Formatter("%G").toTitleString(A.viewPart(0, 0, r, columns), rowNames, columnNames, null, null, null, funs));
}
System.out.print("now sorting - slow version... ");
A = B;
cern.colt.matrix.doublealgo.DoubleMatrix1DComparator fun = new cern.colt.matrix.doublealgo.DoubleMatrix1DComparator() {
public int compare(DoubleMatrix1D x, DoubleMatrix1D y) {
double a = cern.colt.matrix.doublealgo.Statistic.bin(x).median();
double b = cern.colt.matrix.doublealgo.Statistic.bin(y).median();
// double b = y.aggregate(F.plus,F.log);
return a < b ? -1 : (a == b) ? 0 : 1;
}
};
timer.reset().start();
A = sort.sort(A, fun);
timer.stop().display();
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method zdemo2.
/**
* Demonstrates advanced sorting.
* Sorts by sum of slice.
*/
public static void zdemo2() {
Sorting sort = quickSort;
DoubleMatrix3D matrix = DoubleFactory3D.dense.descending(4, 3, 2);
DoubleMatrix2DComparator comp = new DoubleMatrix2DComparator() {
public int compare(DoubleMatrix2D a, DoubleMatrix2D 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));
}
Aggregations