001package cnslab.cnsmath;
002import java.io.PrintWriter;
003import java.text.NumberFormat;
004import java.text.DecimalFormat;
005import java.text.DecimalFormatSymbols;
006import java.util.Locale;
007public class Matrix<T>
008{
009        private T[][] A;
010        private int m, n;
011
012        public Matrix (int m, int n) {
013                this.m = m;
014                this.n = n;
015                A = (T[][]) new Object[m][n];
016        }
017
018        public Matrix (int m, int n, T s) {
019                this.m = m;
020                this.n = n;
021                A = (T[][]) new Object[m][n];
022                for (int i = 0; i < m; i++) {
023                        for (int j = 0; j < n; j++) {
024                                A[i][j] = s;
025                        }
026                }
027        }
028
029        /** Construct a matrix from a 2-D array.
030          @param A    Two-dimensional array of doubles.
031          @exception  IllegalArgumentException All rows must have the same length
032          @see        #constructWithCopy
033          */
034
035        public Matrix (T[][] A) {
036                m = A.length;
037                n = A[0].length;
038                for (int i = 0; i < m; i++) {
039                        if (A[i].length != n) {
040                                throw new IllegalArgumentException("All rows must have the same length.");
041                        }
042                }
043                this.A = A;
044        }
045
046
047        /** Construct a matrix from a one-dimensional packed array
048          @param vals One-dimensional array of doubles, packed by rows (ala C++).
049          @param m    Number of rows.
050          @exception  IllegalArgumentException Array length must be a multiple of m.
051          */
052
053        public Matrix (T vals[], int m) {
054                this.m = m;
055                n = (m != 0 ? vals.length/m : 0);
056                if (m*n != vals.length) {
057                        throw new IllegalArgumentException("Array length must be a multiple of m.");
058                }
059                A = (T[][]) new Object[m][n];
060                for (int i = 0; i < m; i++) {
061                        for (int j = 0; j < n; j++) {
062                                A[i][j] = vals[i*m+j];
063                        }
064                }
065        }
066
067
068        /** Print the matrix to stdout.  Line the elements up in columns.
069         * Use the format object, and right justify within columns of width
070         * characters.                                                                                             
071         * Note that is the matrix is to be read back in, you probably will want                                             
072         * to use a NumberFormat that is set to US Locale.                                                                   
073         @param format A  Formatting object for individual elements.
074         @param width     Field width for each column.
075         @see java.text.DecimalFormat#setDecimalFormatSymbols
076         */
077        public void print (PrintWriter output, NumberFormat format, int width) {
078                output.println();  // start on new line.
079                for (int i = 0; i < m; i++) {  
080                        for (int j = 0; j < n; j++) {
081                                String s = format.format(A[i][j]); // format the number
082                                int padding = Math.max(1,width-s.length()); // At _least_ 1 space
083                                for (int k = 0; k < padding; k++)
084                                        output.print(' ');
085                                output.print(s);
086                        }
087                        output.println();
088                }
089                output.println();   // end with blank line.
090        }
091
092        /** Print the matrix to the output stream.   Line the elements up in
093         * columns with a Fortran-like 'Fw.d' style format.
094         @param output Output stream.
095         @param w      Column width.
096         @param d      Number of digits after the decimal.
097         */
098        public void print (PrintWriter output, int w, int d) {
099                DecimalFormat format = new DecimalFormat();
100                format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
101                format.setMinimumIntegerDigits(1);
102                format.setMaximumFractionDigits(d);
103                format.setMinimumFractionDigits(d);
104                format.setGroupingUsed(false);
105                print(output,format,w+2);
106        }
107
108        /** Print the matrix to stdout.   Line the elements up in columns
109         * with a Fortran-like 'Fw.d' style format.
110         @param w    Column width.
111         @param d    Number of digits after the decimal.
112         */
113        public void print (int w, int d) {
114                print(new PrintWriter(System.out,true),w,d); }
115
116        public void print () {
117                print(new PrintWriter(System.out,true),2,1); }
118}