View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2006 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.dataStructure.matrix;
20  
21  import java.util.List;
22  
23  import cern.colt.list.DoubleArrayList;
24  import cern.colt.matrix.DoubleMatrix1D;
25  import cern.colt.matrix.impl.SparseDoubleMatrix2D;
26  
27  /**
28   * A sparse matrix that knows about row and column names.
29   * 
30   * @author Paul Pavlidis
31   * 
32   */
33  public class SparseDoubleMatrix<R, C> extends DoubleMatrix<R, C> {
34  
35      /**
36       * 
37       */
38      private static final long serialVersionUID = -1651885517252689369L;
39      private SparseDoubleMatrix2D matrix;
40  
41      /**
42       * @param T double[][]
43       */
44      public SparseDoubleMatrix( double T[][] ) {
45          super();
46          matrix = new SparseDoubleMatrix2D( T );
47      }
48  
49      /**
50       * @param rows int
51       * @param cols int
52       */
53      public SparseDoubleMatrix( int rows, int cols ) {
54          super();
55          matrix = new SparseDoubleMatrix2D( rows, cols );
56      }
57  
58      /**
59       * @param rows int
60       * @param cols int
61       * @param initlalCapacity int
62       * @param minLoadFactor double
63       * @param maxLoadFactor double
64       */
65      public SparseDoubleMatrix( int rows, int cols, int initialCapacity, double minLoadFactor, double maxLoadFactor ) {
66          super();
67          matrix = new SparseDoubleMatrix2D( rows, cols, initialCapacity, minLoadFactor, maxLoadFactor );
68      }
69  
70      /**
71       * @return double[][]
72       */
73      @Override
74      public double[][] asArray() {
75          double[][] result = new double[rows()][];
76          for ( int i = 0; i < rows(); i++ ) {
77              result[i] = getRow( i );
78          }
79          return result;
80      }
81  
82      /**
83       * @return
84       */
85      public int cardinality() {
86          return matrix.cardinality();
87      }
88  
89      /**
90       * @return
91       */
92  
93      @Override
94      public int columns() {
95          return matrix.columns();
96      }
97  
98      @Override
99      public DoubleMatrix<R, C> copy() {
100         DoubleMatrix<R, C> returnval = new SparseDoubleMatrix<R, C>( this.rows(), this.columns() );
101 
102         for ( int i = 0; i < this.rows(); i++ ) {
103             returnval.setRowName( this.getRowName( i ), i );
104             for ( int j = 0; j < this.columns(); j++ ) {
105                 if ( i == 0 ) {
106                     returnval.setColumnName( this.getColName( j ), j );
107                 }
108                 returnval.set( i, j, this.get( i, j ) );
109             }
110         }
111         return returnval;
112 
113     }
114 
115     /**
116      * @param row
117      * @param column
118      * @return
119      */
120     @Override
121     public double get( int row, int column ) {
122         return matrix.get( row, column );
123     }
124 
125     @Override
126     public Double[] getColObj( int col ) {
127         Double[] result = new Double[rows()];
128         for ( int i = 0; i < rows(); i++ ) {
129             result[i] = new Double( get( i, col ) );
130         }
131         return result;
132     }
133 
134     @Override
135     public DoubleMatrix<R, C> getColRange( int startCol, int endCol ) {
136         super.checkColRange( startCol, endCol );
137 
138         DoubleMatrix<R, C> returnval = new SparseDoubleMatrix<R, C>( this.rows(), 1 + endCol - startCol );
139         int k = 0;
140         for ( int i = startCol; i <= endCol; i++ ) {
141             C colName = this.getColName( i );
142             if ( colName != null ) {
143                 returnval.setColumnName( colName, i );
144             }
145             for ( int j = 0, m = this.rows(); j < m; j++ ) {
146                 if ( i == startCol ) {
147                     R rowName = this.getRowName( j );
148                     returnval.setRowName( rowName, j );
149                 }
150                 returnval.set( j, k, this.get( j, i ) );
151             }
152             k++;
153         }
154         return returnval;
155     }
156 
157     /*
158      * (non-Javadoc)
159      * 
160      * @see basecode.dataStructure.matrix.DoubleMatrixNamed#getColumn(int)
161      */
162     @Override
163     public double[] getColumn( int col ) {
164         double[] result = new double[rows()];
165         for ( int i = 0; i < rows(); i++ ) {
166             result[i] = get( i, col );
167         }
168         return result;
169     }
170 
171     @Override
172     public Double getObject( int row, int col ) {
173         return new Double( get( row, col ) );
174     }
175 
176     /**
177      * Return a reference to a specific row.
178      * 
179      * @param row int
180      * @return double[]
181      */
182     @Override
183     public double[] getRow( int row ) {
184         return viewRow( row ).toArray();
185     }
186 
187     /*
188      * (non-Javadoc)
189      * 
190      * @see basecode.dataStructure.matrix.AbstractNamedDoubleMatrix#getRowArrayList(int)
191      */
192     @Override
193     public DoubleArrayList getRowArrayList( int i ) {
194         return new DoubleArrayList( getRow( i ) );
195     }
196 
197     /**
198      * @param s String
199      * @return double[]
200      */
201     @Override
202     public double[] getRowByName( R s ) {
203         return getRow( getRowIndexByName( s ) );
204     }
205 
206     @Override
207     public Double[] getRowObj( int row ) {
208         Double[] result = new Double[columns()];
209         for ( int i = 0; i < columns(); i++ ) {
210             result[i] = new Double( get( row, i ) );
211         }
212         return result;
213     }
214 
215     /*
216      * (non-Javadoc)
217      * 
218      * @see ubic.basecode.dataStructure.matrix.Matrix2D#getRowRange(int, int)
219      */
220     @Override
221     public DoubleMatrix<R, C> getRowRange( int startRow, int endRow ) {
222         super.checkRowRange( startRow, endRow );
223 
224         DoubleMatrix<R, C> returnval = new SparseDoubleMatrix<R, C>( endRow + 1 - startRow, this.columns() );
225         int k = 0;
226         for ( int i = startRow; i <= endRow; i++ ) {
227             R rowName = this.getRowName( i );
228             if ( rowName != null ) {
229                 returnval.setRowName( rowName, i );
230             }
231             for ( int j = 0, m = this.columns(); j < m; j++ ) {
232                 if ( i == 0 ) {
233                     C colName = this.getColName( j );
234                     returnval.setColumnName( colName, j );
235                 }
236                 returnval.set( k, j, this.get( i, j ) );
237             }
238             k++;
239         }
240         return returnval;
241     }
242 
243     @Override
244     public boolean isMissing( int i, int j ) {
245         return Double.isNaN( get( i, j ) );
246     }
247 
248     /**
249      * @return
250      */
251     @Override
252     public int rows() {
253         return matrix.rows();
254     }
255 
256     @Override
257     public void set( int row, int column, Double value ) {
258         matrix.set( row, column, value );
259     }
260 
261     /**
262      * @return
263      */
264     @Override
265     public int size() {
266         return matrix.size();
267     }
268 
269     /*
270      * (non-Javadoc)
271      * 
272      * @see ubic.basecode.dataStructure.matrix.DoubleMatrix#subsetColumns(java.util.List)
273      */
274     @Override
275     public DoubleMatrix<R, C> subsetColumns( List<C> columns ) {
276 
277         DoubleMatrix<R, C> returnval = new DenseDoubleMatrix<R, C>( this.rows(), columns.size() );
278         returnval.setRowNames( this.getRowNames() );
279         for ( int i = 0; i < this.rows(); i++ ) {
280             int currentColumn = 0;
281             for ( C c : columns ) {
282                 int j = this.getColIndexByName( c );
283 
284                 returnval.set( i, currentColumn, this.get( i, j ) );
285 
286                 if ( i == 0 ) {
287                     returnval.setColumnName( c, currentColumn );
288                 }
289 
290                 currentColumn++;
291 
292             }
293 
294         }
295         return returnval;
296     }
297 
298     @Override
299     public DoubleMatrix<R, C> subsetRows( List<R> rowNames ) {
300         DoubleMatrix<R, C> returnval = new SparseDoubleMatrix<R, C>( rowNames.size(), this.columns() );
301 
302         int currentRow = 0;
303         for ( R rowName : rowNames ) {
304 
305             if ( !this.containsRowName( rowName ) ) continue;
306 
307             int i = this.getRowIndexByName( rowName );
308 
309             returnval.setRowName( rowName, currentRow );
310             for ( int j = 0; j < this.columns(); j++ ) {
311                 if ( currentRow == 0 ) {
312                     returnval.setColumnName( this.getColName( j ), j );
313                 }
314                 returnval.set( currentRow, j, this.get( i, j ) );
315             }
316             currentRow++;
317         }
318 
319         if ( !returnval.getRowNames().containsAll( rowNames ) ) {
320             throw new IllegalArgumentException( "Invalid rows to select, some are not in the original matrix" );
321         }
322 
323         return returnval;
324     }
325 
326     @Override
327     public DoubleMatrix<C, R> transpose() {
328         throw new UnsupportedOperationException();
329     }
330 
331     /**
332      * 
333      */
334     public void trimToSize() {
335         matrix.trimToSize();
336     }
337 
338     /**
339      * @param column
340      * @return
341      */
342     @Override
343     public DoubleMatrix1D viewColumn( int column ) {
344         return matrix.viewColumn( column );
345     }
346 
347     /**
348      * @param row
349      * @return
350      */
351     @Override
352     public DoubleMatrix1D viewRow( int row ) {
353         return matrix.viewRow( row );
354     }
355 }