Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Colt 1.2.0
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## cern.colt.matrix.doublealgo
Class Formatter

    java.lang.Object
      cern.colt.PersistentObject
          cern.colt.matrix.impl.AbstractFormatter
              cern.colt.matrix.doublealgo.Formatter

All Implemented Interfaces:
  Cloneable, Serializable

public class Formatter
extends AbstractFormatter

Flexible, well human readable matrix print formatting; By default decimal point aligned. Build on top of the C-like sprintf functionality provided by the `Format` class written by Cay Horstmann. Currenly works on 1-d, 2-d and 3-d matrices. Note that in most cases you will not need to get familiar with this class; just call `matrix.toString()` and be happy with the default formatting. This class is for advanced requirements.

Can't exactly remember the syntax of printf format strings? See `Format` or Henrik Nordberg's documentation, or the Dinkumware's C Library Reference.

Examples:

Examples demonstrate usage on 2-d matrices. 1-d and 3-d matrices formatting works very similar.

Original matrix
`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} }; matrix = new DenseDoubleMatrix2D(values);`

`format` | `Formatter.toString(matrix);` | `Formatter.toSourceCode(matrix);`
`%G` (default) | `5 x 4 matrix 3 0 -3.4 0 5.1 0 3.012346 0 16.37 0 2.5 0 -16.3 0 -0.000301 -1 1236.345679 0 7 -1.2` | `{ { 3 , 0, -3.4 , 0 }, { 5.1 , 0, 3.012346, 0 }, { 16.37 , 0, 2.5 , 0 }, { -16.3 , 0, -0.000301, -1 }, {1236.345679, 0, 7 , -1.2} };`
`%1.10G` | `5 x 4 matrix 3 0 -3.4 0 5.1 0 3.0123456789 0 16.37 0 2.5 0 -16.3 0 -0.0003012346 -1 1236.3456789 0 7 -1.2` | `{ { 3 , 0, -3.4 , 0 }, { 5.1 , 0, 3.0123456789, 0 }, { 16.37 , 0, 2.5 , 0 }, { -16.3 , 0, -0.0003012346, -1 }, {1236.3456789, 0, 7 , -1.2} };`
`%f` | `5 x 4 matrix 3.000000 0.000000 -3.400000 0.000000 5.100000 0.000000 3.012346 0.000000 16.370000 0.000000 2.500000 0.000000 -16.300000 0.000000 -0.000301 -1.000000 1236.345679 0.000000 7.000000 -1.200000` | `{ { 3.000000, 0.000000, -3.400000, 0.000000}, { 5.100000, 0.000000, 3.012346, 0.000000}, { 16.370000, 0.000000, 2.500000, 0.000000}, { -16.300000, 0.000000, -0.000301, -1.000000}, {1236.345679, 0.000000, 7.000000, -1.200000} };`
`%1.2f` | `5 x 4 matrix 3.00 0.00 -3.40 0.00 5.10 0.00 3.01 0.00 16.37 0.00 2.50 0.00 -16.30 0.00 -0.00 -1.00 1236.35 0.00 7.00 -1.20` | `{ { 3.00, 0.00, -3.40, 0.00}, { 5.10, 0.00, 3.01, 0.00}, { 16.37, 0.00, 2.50, 0.00}, { -16.30, 0.00, -0.00, -1.00}, {1236.35, 0.00, 7.00, -1.20} };`
`%0.2e` | `5 x 4 matrix 3.00e+000 0.00e+000 -3.40e+000 0.00e+000 5.10e+000 0.00e+000 3.01e+000 0.00e+000 1.64e+001 0.00e+000 2.50e+000 0.00e+000 -1.63e+001 0.00e+000 -3.01e-004 -1.00e+000 1.24e+003 0.00e+000 7.00e+000 -1.20e+000` | `{ { 3.00e+000, 0.00e+000, -3.40e+000, 0.00e+000}, { 5.10e+000, 0.00e+000, 3.01e+000, 0.00e+000}, { 1.64e+001, 0.00e+000, 2.50e+000, 0.00e+000}, {-1.63e+001, 0.00e+000, -3.01e-004, -1.00e+000}, { 1.24e+003, 0.00e+000, 7.00e+000, -1.20e+000} };`
`null` | `5 x 4 matrix 3.0 0.0 -3.4 0.0 5.1 0.0 3.0123456789 0.0 16.37 0.0 2.5 0.0 -16.3 0.0 -3.012345678E-4 -1.0 1236.3456789 0.0 7.0 -1.2` `` | `{ { 3.0 , 0.0, -3.4 , 0.0}, { 5.1 , 0.0, 3.0123456789 , 0.0}, { 16.37 , 0.0, 2.5 , 0.0}, { -16.3 , 0.0, -3.012345678E-4, -1.0}, {1236.3456789, 0.0, 7.0 , -1.2} };`

Here are some more elaborate examples, adding labels for axes, rows, columns, title and some statistical aggregations.

`double[][] values = { {5 ,10, 20, 40 }, { 7, 8 , 6 , 7 }, {12 ,10, 20, 19 }, { 3, 1 , 5 , 6 } };` `String title = "CPU performance over time [nops/sec]"; String columnAxisName = "Year"; String rowAxisName = "CPU"; String[] columnNames = {"1996", "1997", "1998", "1999"}; String[] rowNames = { "PowerBar", "Benzol", "Mercedes", "Sparcling"}; hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions; // alias hep.aida.bin.BinFunction1D[] aggr = {F.mean, F.rms, F.quantile(0.25), F.median, F.quantile(0.75), F.stdDev, F.min, F.max}; String format = "%1.2G"; DoubleMatrix2D matrix = new DenseDoubleMatrix2D(values); new Formatter(format).toTitleString( matrix,rowNames,columnNames,rowAxisName,columnAxisName,title,aggr);`
`CPU performance over time [nops/sec] | Year | 1996 1997 1998 1999 | Mean RMS 25% Q. Median 75% Q. StdDev Min Max --------------------------------------------------------------------------------------- C PowerBar | 5 10 20 40 | 18.75 23.05 8.75 15 25 15.48 5 40 P Benzol | 7 8 6 7 | 7 7.04 6.75 7 7.25 0.82 6 8 U Mercedes | 12 10 20 19 | 15.25 15.85 11.5 15.5 19.25 4.99 10 20 Sparcling | 3 1 5 6 | 3.75 4.21 2.5 4 5.25 2.22 1 6 --------------------------------------------------------------------------------------- Mean | 6.75 7.25 12.75 18 | RMS | 7.53 8.14 14.67 22.62 | 25% Q. | 4.5 6.25 5.75 6.75 | Median | 6 9 13 13 | 75% Q. | 8.25 10 20 24.25 | StdDev | 3.86 4.27 8.38 15.81 | Min | 3 1 5 6 | Max | 12 10 20 19 |`
`same as above, but now without aggregations aggr=null;`
`CPU performance over time [nops/sec] | Year | 1996 1997 1998 1999 --------------------------------- C PowerBar | 5 10 20 40 P Benzol | 7 8 6 7 U Mercedes | 12 10 20 19 Sparcling | 3 1 5 6`
`same as above, but now without rows labeled aggr=null; rowNames=null; rowAxisName=null;`
`CPU performance over time [nops/sec] Year 1996 1997 1998 1999 ------------------- 5 10 20 40 7 8 6 7 12 10 20 19 3 1 5 6`

A column can be broader than specified by the parameter `minColumnWidth` (because a cell may not fit into that width) but a column is never smaller than `minColumnWidth`. Normally one does not need to specify `minColumnWidth` (default is `1`). This parameter is only interesting when wanting to print two distinct matrices such that both matrices have the same column width, for example, to make it easier to see which column of matrix A corresponds to which column of matrix B.

Implementation:

Note that this class is by no means ment to be used for high performance I/O (serialization is much quicker). It is ment to produce well human readable output.

Analyzes the entire matrix before producing output. Each cell is converted to a String as indicated by the given C-like format string. If `null` is passed as format string, `Double.toString(double)` is used instead, yielding full precision.

Next, leading and trailing whitespaces are removed. For each column the maximum number of characters before and after the decimal point is determined. (No problem if decimal points are missing). Each cell is then padded with leading and trailing blanks, as necessary to achieve decimal point aligned, left justified formatting.

Version:
  1.2, 11/30/99
See Also:
  Serialized Form

Field Summary

Fields inherited from class cern.colt.matrix.impl.AbstractFormatter
`CENTER, DECIMAL, DEFAULT_COLUMN_SEPARATOR, DEFAULT_MIN_COLUMN_WIDTH, DEFAULT_ROW_SEPARATOR, DEFAULT_SLICE_SEPARATOR, LEFT, RIGHT`

Fields inherited from class cern.colt.PersistentObject
`serialVersionUID`

Constructor Summary
`Formatter()` Constructs and returns a matrix formatter with format `"%G"`.
`Formatter(String format)` Constructs and returns a matrix formatter.

Method Summary
`static void` | `demo1()` Demonstrates how to use this class.
`static void` | `demo2()` Demonstrates how to use this class.
`static void` | `demo3(int size, double value)` Demonstrates how to use this class.
`static void` | `demo4()` Demonstrates how to use this class.
`static void` | `demo5()` Demonstrates how to use this class.
`static void` | `demo6()` Demonstrates how to use this class.
`static void` | `demo7()` Demonstrates how to use this class.
`String[][]` | `format(DoubleMatrix2D matrix)` Returns a string representations of all cells; no alignment considered.
`String` | `toSourceCode(DoubleMatrix1D matrix)` Returns a string `s` such that `Object[] m = s` is a legal Java statement.
`String` | `toSourceCode(DoubleMatrix2D matrix)` Returns a string `s` such that `Object[] m = s` is a legal Java statement.
`String` | `toSourceCode(DoubleMatrix3D matrix)` Returns a string `s` such that `Object[] m = s` is a legal Java statement.
`String` | `toString(DoubleMatrix1D matrix)` Returns a string representation of the given matrix.
`String` | `toString(DoubleMatrix2D matrix)` Returns a string representation of the given matrix.
`String` | `toString(DoubleMatrix3D matrix)` Returns a string representation of the given matrix.
`String` | `toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title, BinFunction1D[] aggr)` Same as `toTitleString` except that additionally statistical aggregates (mean, median, sum, etc.) of rows and columns are printed.
`String` | `toTitleString(DoubleMatrix3D matrix, String[] sliceNames, String[] rowNames, String[] columnNames, String sliceAxisName, String rowAxisName, String columnAxisName, String title, BinFunction1D[] aggr)` Returns a string representation of the given matrix with axis as well as rows and columns labeled.

Methods inherited from class cern.colt.matrix.impl.AbstractFormatter
`demo3, setAlignment, setColumnSeparator, setFormat, setMinColumnWidth, setPrintShape, setRowSeparator, setSliceSeparator, shape, shape, shape`

Methods inherited from class cern.colt.PersistentObject
`clone`

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### Formatter

    public Formatter()

  Constructs and returns a matrix formatter with format `"%G"`.

### Formatter

    public Formatter(String format)

  Constructs and returns a matrix formatter.

Parameters:
  `format` - the given format used to convert a single cell value.

Method Detail

### demo1

    public static void demo1()

  Demonstrates how to use this class.

### demo2

    public static void demo2()

  Demonstrates how to use this class.

### demo3

    public static void demo3(int size,
                             double value)

  Demonstrates how to use this class.

### demo4

    public static void demo4()

  Demonstrates how to use this class.

### demo5

    public static void demo5()

  Demonstrates how to use this class.

### demo6

    public static void demo6()

  Demonstrates how to use this class.

### demo7

    public static void demo7()

  Demonstrates how to use this class.

### format

    public String[][] format(DoubleMatrix2D matrix)

  Returns a string representations of all cells; no alignment considered.

### toSourceCode

    public String toSourceCode(DoubleMatrix1D matrix)

  Returns a string `s` such that `Object[] m = s` is a legal Java statement.

  Parameters:
    `matrix` - the matrix to format.

### toSourceCode

    public String toSourceCode(DoubleMatrix2D matrix)

  Returns a string `s` such that `Object[] m = s` is a legal Java statement.

  Parameters:
    `matrix` - the matrix to format.

### toSourceCode

    public String toSourceCode(DoubleMatrix3D matrix)

  Returns a string `s` such that `Object[] m = s` is a legal Java statement.

  Parameters:
    `matrix` - the matrix to format.

### toString

    public String toString(DoubleMatrix1D matrix)

  Returns a string representation of the given matrix.

  Parameters:
    `matrix` - the matrix to convert.

### toString

    public String toString(DoubleMatrix2D matrix)

  Returns a string representation of the given matrix.

  Parameters:
    `matrix` - the matrix to convert.

### toString

    public String toString(DoubleMatrix3D matrix)

  Returns a string representation of the given matrix.

  Parameters:
    `matrix` - the matrix to convert.

### toTitleString

    public String toTitleString(DoubleMatrix2D matrix,
                                String[] rowNames,
                                String[] columnNames,
                                String rowAxisName,
                                String columnAxisName,
                                String title,
                                BinFunction1D[] aggr)

  Same as `toTitleString` except that additionally statistical aggregates (mean, median, sum, etc.) of rows and columns are printed. Pass `null` to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.

  Parameters:
    `matrix` - The matrix to format.
    `rowNames` - The headers of all rows (to be put to the left of the matrix).
    `columnNames` - The headers of all columns (to be put to above the matrix).
    `rowAxisName` - The label of the y-axis.
    `columnAxisName` - The label of the x-axis.
    `title` - The overall title of the matrix to be formatted.
    `aggr` - the aggregation functions to be applied to columns and rows.
  Returns:
    the matrix converted to a string.
  See Also:
    `BinFunction1D`, `BinFunctions1D`

### toTitleString

    public String toTitleString(DoubleMatrix3D matrix,
                                String[] sliceNames,
                                String[] rowNames,
                                String[] columnNames,
                                String sliceAxisName,
                                String rowAxisName,
                                String columnAxisName,
                                String title,
                                BinFunction1D[] aggr)

  Returns a string representation of the given matrix with axis as well as rows and columns labeled. Pass `null` to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.

  Parameters:
    `matrix` - The matrix to format.
    `sliceNames` - The headers of all slices (to be put above each slice).
    `rowNames` - The headers of all rows (to be put to the left of the matrix).
    `columnNames` - The headers of all columns (to be put to above the matrix).
    `sliceAxisName` - The label of the z-axis (to be put above each slice).
    `rowAxisName` - The label of the y-axis.
    `columnAxisName` - The label of the x-axis.
    `title` - The overall title of the matrix to be formatted.
    `aggr` - the aggregation functions to be applied to columns, rows.
  Returns:
    the matrix converted to a string.
  See Also:
    `BinFunction1D`, `BinFunctions1D`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Colt 1.2.0
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Jump to the Colt Homepage