View Javadoc

1   package baseCode.dataStructure.matrix;
2   
3   import cern.colt.list.DoubleArrayList;
4   import cern.colt.matrix.DoubleMatrix1D;
5   import cern.colt.matrix.impl.DenseDoubleMatrix2D;
6   import corejava.Format;
7   
8   /***
9    * A matrix of doubles that knows about row and column names.
10   * </p>
11   * <p>
12   * Copyright (c) 2004
13   * </p>
14   * <p>
15   * Columbia University
16   * </p>
17   * 
18   * @author Paul Pavlidis
19   * @version $Id: DenseDoubleMatrix2DNamed.java,v 1.7 2005/01/05 02:01:02 pavlidis Exp $
20   */
21  public class DenseDoubleMatrix2DNamed extends AbstractNamedDoubleMatrix {
22  
23     private DenseDoubleMatrix2D matrix;
24  
25     /***
26      * @param T double[][]
27      */
28     public DenseDoubleMatrix2DNamed( double T[][] ) {
29        super();
30        matrix = new DenseDoubleMatrix2D( T );
31     }
32  
33     /***
34      * @param rows int
35      * @param cols int
36      */
37     public DenseDoubleMatrix2DNamed( int rows, int cols ) {
38        super();
39        matrix = new DenseDoubleMatrix2D( rows, cols );
40     }
41  
42     /***
43      * Return a reference to a specific row.
44      * 
45      * @param row int
46      * @return double[]
47      */
48     public double[] getRow( int row ) {
49        return viewRow( row ).toArray();
50     }
51     
52     
53     /***
54      * Return a reference to a specific row.
55      * @param s String
56      * @return double[]
57      */
58     public double[] getRowByName( String s ) {
59        return getRow( getRowIndexByName( s ) );
60     }
61  
62     /***
63      * Return a copy of a given column.
64      * 
65      * @param col int
66      * @return double[]
67      */
68     public double[] getCol( int col ) {
69        double[] result = new double[rows()];
70        for ( int i = 0; i < rows(); i++ ) {
71           result[i] = get( i, col );
72        }
73        return result;
74     }
75     
76     
77     /***
78      * Return a copy of a given column.
79      * 
80      * @param col int
81      * @return double[]
82      */
83     public double[] getColByName( String s ) {
84        int col = getColIndexByName(s);
85        double[] result = new double[rows()];
86        for ( int i = 0; i < rows(); i++ ) {
87           result[i] = get( i, col );
88        }
89        return result;
90     }
91  
92     
93  
94     public Object[] getRowObj( int row ) {
95        Double[] result = new Double[columns()];
96        for ( int i = 0; i < columns(); i++ ) {
97           result[i] = new Double( get( row, i ) );
98        }
99        return result;
100    }
101 
102    public Object[] getColObj( int col ) {
103       Double[] result = new Double[rows()];
104       for ( int i = 0; i < rows(); i++ ) {
105          result[i] = new Double( get( i, col ) );
106       }
107       return result;
108    }
109 
110    /***
111     * @return java.lang.String
112     */
113    public String toString() {
114       Format nf = new Format( "%.4g" );
115       StringBuffer result = new StringBuffer( this.rows() * this.columns() );
116       if ( this.hasColNames() || this.hasRowNames() ) {
117          result.append( "label" );
118       }
119 
120       if ( this.hasColNames() ) {
121          for ( int i = 0; i < columns(); i++ ) {
122             result.append( "\t" + getColName( i ) );
123          }
124          result.append( "\n" );
125       }
126 
127       for ( int i = 0; i < rows(); i++ ) {
128          if ( this.hasRowNames() ) {
129             result.append( getRowName( i ) );
130          }
131          for ( int j = 0; j < columns(); j++ ) {
132             if ( Double.isNaN( get( i, j ) ) ) {
133                result.append( "\t" );
134             } else {
135                result.append( "\t" + nf.format( get( i, j ) ) );
136             }
137          }
138          result.append( "\n" );
139       }
140       return result.toString();
141    }
142 
143   
144 
145    public void set( int row, int col, Object value ) {
146       set( row, col, ( ( Double ) value ).doubleValue() );
147    }
148 
149    /***
150     * Make a copy of a matrix.
151     * @return baseCode.dataStructure.DenseDoubleMatrix2DNamed
152     */
153    public DenseDoubleMatrix2DNamed copy() {
154       DenseDoubleMatrix2DNamed returnval = new DenseDoubleMatrix2DNamed( this
155             .rows(), this.columns() );
156       for ( int i = 0, n = this.rows(); i < n; i++ ) {
157          returnval.addRowName( this.getRowName( i ), i );
158          for ( int j = 0, m = this.columns(); j < m; j++ ) {
159             if ( i == 0 ) {
160                returnval.addColumnName( this.getColName( j ), j );
161             }
162             returnval.set( i, j, this.get( i, j ) );
163          }
164       }
165       return returnval;
166    }
167 
168    public boolean isMissing( int i, int j ) {
169       return Double.isNaN( get( i, j ) );
170    }
171 
172    /***
173     * @param row int
174     * @param column int
175     * @return @see DoubleMatrix2D#get(int, int)
176     */
177    public double get( int row, int column ) {
178       return matrix.get( row, column );
179    }
180 
181    /***
182     * @param row int
183     * @param column int
184     * @return double
185     * @see DenseDoubleMatrix2D#getQuick(int, int)
186     */
187    public double getQuick( int row, int column ) {
188       return matrix.getQuick( row, column );
189    }
190 
191    /***
192     * @param row int
193     * @param column int
194     * @param value double
195     */
196    public void set( int row, int column, double value ) {
197       matrix.set( row, column, value );
198    }
199 
200    /***
201     * @param row int
202     * @param column int
203     * @param value double
204     * @see DenseDoubleMatrix2D#setQuick(int, int, double)
205     */
206    public void setQuick( int row, int column, double value ) {
207       matrix.setQuick( row, column, value );
208    }
209 
210    /***
211     * @param column int
212     * @return cern.colt.matrix.DoubleMatrix1D
213     */
214    public DoubleMatrix1D viewColumn( int column ) {
215       return matrix.viewColumn( column );
216    }
217 
218    /***
219     * @param row int
220     * @return DoubleMatrix1D
221     * @see DenseDoubleMatrix2D#viewRow(int)
222     */
223    public DoubleMatrix1D viewRow( int row ) {
224       return matrix.viewRow( row );
225    }
226 
227    /***
228     * @return the number of columns in the matrix
229     * @see cern.colt.matrix.impl.AbstractMatrix2D#columns()
230     */
231    public int columns() {
232       return matrix.columns();
233    }
234 
235    /***
236     * @return the number of rows in the matrix
237     * @see AbstractMatrix2D#rows()
238     */
239    public int rows() {
240       return matrix.rows();
241    }
242 
243    /***
244     * @return int
245     * @see AbstractMatrix2D#size()
246     */
247    public int size() {
248       return matrix.size();
249    }
250 
251    /***
252     * @return double[][]
253     */
254    public double[][] toArray() {
255       return matrix.toArray();
256    }
257 
258    /* (non-Javadoc)
259     * @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getRowArrayList(int)
260     */
261    public DoubleArrayList getRowArrayList( int i ) {
262       return new DoubleArrayList(getRow(i));
263    }
264    
265   
266 
267     
268 }