Example: Matrix Formatting
A matrix is printed in CSV (comma separated value) format. This is done by overriding the format
method of PrintMatrixFormat
to add commas after all but the last number in each row.
import com.imsl.math.*;
import java.text.*;
public class PrintMatrixFormatEx2 extends PrintMatrixFormat {
private int ncols;
public PrintMatrixFormatEx2(int ncols) {
this.ncols = ncols;
}
public String format(int type, Object entry, int row, int col, ParsePosition pos) {
String text = super.format(type, entry, row, col, pos);
if (type == ENTRY) {
if (col < ncols-1) text += ",";
}
return text;
}
public static void main(String args[]) {
double a[][] = {
{0., 1., 2.},
{4., 5., 6.},
{8., 9., 8.},
{6., 3., 4.}
};
PrintMatrixFormat mf = new PrintMatrixFormatEx2(3);
mf.setNoRowLabels();
mf.setNoColumnLabels();
// Print the matrix
new PrintMatrix().print(mf, a);
}
}
Output
0, 1, 2
4, 5, 6
8, 9, 8
6, 3, 4
Link to Java source.