Subject: PrintArray utility

From: Ronald Michaels <[email-masked]>
Date: Mon, 09 Mar 1998 15:12:27 +0000

Hi - 

Thanks for all of the comments on how to format numbers.  I have decided
to use the suggestion of Walter Bays for the moment because it is
simple, suits my needs, and only uses what is in the JDK. 

PrintArray will print to standard output a Complex (Java Numerical
Library) or double array having one or two dimensions.  The user may
accept the default formatting or specify formatting in the invocation. 
I show below the test driver and the class file for my little PrintArray
utility.  Please feel free to use or modify it in any way.  Your
comments are welcomed.

Java now reminds me of C for PCs in the mid and late 80's.  A lot of new
stuff was invented or written.  Compilers and libraries got a lot
better.  I remember the joy of my first symbolic debugger.  In
particular I am reminded of the discovery, which I believe was made in
1988, on how to dynamically allocate a multidimensional array in C and
use the my_matrix[][] notation, as Java does now.  Prior to this I had
to use pointer arithmetic and it was a pain.  

Java can only get better for numerical work.  But, it will not replace
FORTRAN for heavy lifting any time soon.

Ron

---begin file
testPrintArray.java----------------------------------------

 import VisualNumerics.math.*;
 import java.io.*;
 class testPrintArray {
    public static void main(String args[]) {

       Complex a[][] = {{new Complex(1000000.0,0.0), new
Complex(0.0,1.0)},
                        {new Complex(0.0009,0.0), new Complex(0.0,0.0)},
                        {new Complex(0.0,0.0), new Complex(1.0,1.0)}};
       new PrintArray(a); // default formatting 
       new PrintArray(a," 00000.0000;-00000.0000"); //user specified
formatting

       double b[][] = {{ 123456789.123456789, 0.66666666},
                       { 1.0, 2.0},
                       {0.3, 4.1777}};
       new PrintArray(b); // default formatting 
       new PrintArray(b," 00000.0000;-00000.0000"); //user specified
formatting

       Complex c[] =  {new Complex(1000000.0,0.0), new Complex(0.0,1.0),
                        new Complex(0.0009,0.0), new Complex(0.0,0.0),
                        new Complex(0.0,0.0), new Complex(1.0,1.0)};
       new PrintArray(c); // default formatting 
       new PrintArray(c," 00000.0000;-00000.0000"); //user specified
formatting

       double d[] =  { 123456789.123456789, 0.66666666,
                       1.0, 2.0,
                       0.3, 4.1777 };
       new PrintArray(d); // default formatting 
       new PrintArray(d," 00000.0000;-00000.0000"); //user specified
formatting
    }
 }

---end file testPrintArray.java---------------------------------------
---begin file PrintArray.java-----------------------------------------
// PrintArray.java written by Ronald Michaels 1998
// This code may be used or modified by anyone for any purpose,
// personal or commercial

import VisualNumerics.math.*;
import java.io.*;
import java.text.DecimalFormat;
/////////////////////////////////
// prints array to stdio 
/////////////////////////////////
public class PrintArray {
  Complex cm[][];
  Complex cv[];
  double dm[];
  double dv[];
  ////////////////////////////////////////////////
  // constructor to print complex matrix
  ///////////////////////////////////////////////
  public PrintArray(Complex cm[][], String fmt){
     DecimalFormat df = new DecimalFormat(fmt);
     System.out.print("complex matrix dim "+cm.length+" x
"+cm[0].length+"\n");
     for(int i=0;i < cm.length;i++){
       for(int j=0;j < cm[i].length;j++){
         System.out.print("("+df.format(cm[i][j].re) +
","+df.format(cm[i][j].im)+")");
       }
       System.out.print("\n");
     } 
  }

  public PrintArray(Complex cm[][]){
     this(cm," 000.00;-000.00");
  }
  ////////////////////////////////////////////////
  // constructor to print complex vector
  ///////////////////////////////////////////////
  public PrintArray(Complex cv[],String fmt){
     DecimalFormat df = new DecimalFormat(fmt);
     System.out.print("complex vector dim "+cv.length+"\n");
     for(int i=0;i < cv.length;i++){
       System.out.print("("+df.format(cv[i].re) +
","+df.format(cv[i].im)+") ");
       System.out.print("\n");
     } 
  }

  public PrintArray(Complex cv[]){
     this(cv," 000.00;-000.00");
  }
  ////////////////////////////////////////////////
  // constructor to print double matrix
  ///////////////////////////////////////////////
  public PrintArray(double dm[][],String fmt){
     DecimalFormat df = new DecimalFormat(fmt);
     System.out.print("double matrix dim "+dm.length+"
x"+dm[0].length+"\n");
     for(int i=0;i < dm.length;i++){
       for(int j=0;j < dm[i].length;j++){
         System.out.print(df.format(dm[i][j]) + " ");
       }
       System.out.print("\n");
     } 
  }

  public PrintArray(double dm[][]){
     this(dm," 000.00;-000.00");
  }
  ////////////////////////////////////////////////
  // constructor to print double vector
  ///////////////////////////////////////////////
  public PrintArray(double dv[], String fmt){
     DecimalFormat df = new DecimalFormat(fmt);
     System.out.print("double vector dim "+dv.length+"\n");
     for(int i=0;i < dv.length;i++){
       System.out.print(df.format(dv[i]) + " ");
       System.out.print("\n");
     } 
  }

  public PrintArray(double dv[]){
     this(dv," 000.00;-000.00");
  }
}

---end file
PrintArray.java-----------------------------------------------
-- 
Ronald Michaels                [email-masked]
714 Burnett Station Rd.        423 573-4049
Seymour, TN 37865