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.
using System;
using Imsl.Math;

public class PrintMatrixFormatEx2 : PrintMatrixFormat
{
    private int ncols;
    
    public PrintMatrixFormatEx2(int ncols)
    {
        this.ncols = ncols;
    }
    
    public override String Format(FormatType type, System.Object entry, int row,
      int col, ParsePosition pos)
    {
        String text = base.Format(type, entry, row, col, pos);
        if (type == FormatType.Entry)
        {
            if (col < ncols - 1)
                text += ",";
        }
        return text;
    }
    
    public static void Main(System.String[] args)
    {
        double[][] a = {
            new double[]{0.0, 1.0, 2.0},
            new double[]{4.0, 5.0, 6.0},
            new double[]{8.0, 9.0, 8.0},
            new double[]{6.0, 3.0, 4.0}
        };
        
        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 C# source.