View Javadoc

1   package baseCode.dataStructure.matrix;
2   
3   import java.text.NumberFormat;
4   import java.util.Iterator;
5   import java.util.Vector;
6   
7   import cern.colt.list.DoubleArrayList;
8   import cern.colt.list.IntArrayList;
9   import cern.colt.matrix.DoubleMatrix1D;
10  
11  /***
12   * A sparse matrix class where the rows are ragged and compressed.
13   * <hr>
14   * <p>
15   * Copyright (c) 2004 Columbia University
16   * 
17   * @author pavlidis
18   * @version $Id: SparseRaggedDoubleMatrix2DNamed.java,v 1.13 2004/08/18 23:44:10 pavlidis Exp $
19   */
20  public class SparseRaggedDoubleMatrix2DNamed extends AbstractNamedDoubleMatrix {
21  
22     private Vector matrix; // a vector of DoubleArrayList containing the values of the matrix
23  
24     int columns = 0;
25     private boolean isDirty = true;
26  
27     public SparseRaggedDoubleMatrix2DNamed() {
28        matrix = new Vector();
29     }
30  
31     /*
32      * (non-Javadoc)
33      * 
34      * @see baseCode.dataStructure.matrix.NamedMatrix#rows()
35      */
36     public int rows() {
37        return matrix.size();
38     }
39  
40     /*
41      * (non-Javadoc) Unfortunately this has to iterate over the entire array.
42      * 
43      * @see baseCode.dataStructure.matrix.NamedMatrix#columns()
44      */
45     public int columns() {
46  
47        if ( !isDirty ) {
48           return columns;
49        }
50  
51        int max = 0;
52        for ( Iterator iter = matrix.iterator(); iter.hasNext(); ) {
53           DoubleMatrix1D element = ( DoubleMatrix1D ) iter.next();
54  
55           int value = element.size();
56           if ( value > max ) {
57              max = value;
58           }
59  
60        }
61  
62        columns = max;
63        isDirty = false;
64        return columns;
65     }
66  
67     /*
68      * (non-Javadoc)
69      * 
70      * @see baseCode.dataStructure.matrix.NamedMatrix#set(int, int, java.lang.Object)
71      */
72     public void set( int i, int j, Object val ) {
73        set( i, j, ( ( Double ) val ).doubleValue() );
74     }
75  
76     /***
77      * @param i row
78      * @param j column
79      * @param d value
80      */
81     public void set( int i, int j, double d ) {
82  
83        ( ( DoubleMatrix1D ) matrix.get( i ) ).set( j, d );
84  
85     }
86  
87     /*
88      * (non-Javadoc)
89      * 
90      * @see baseCode.dataStructure.matrix.NamedMatrix#getRowObj(int)
91      */
92     public Object[] getRowObj( int i ) {
93        Double[] result = new Double[columns()];
94  
95        double[] row = getRow( i );
96  
97        for ( int j = 0; j < columns(); j++ ) {
98           result[i] = new Double( row[j] );
99        }
100       return result;
101    }
102 
103    /*
104     * (non-Javadoc)
105     * 
106     * @see baseCode.dataStructure.matrix.NamedMatrix#getColObj(int)
107     */
108    public Object[] getColObj( int i ) {
109       throw new UnsupportedOperationException();
110    }
111 
112    /***
113     * (non-Javadoc) Note that in a sparse matrix, zero values are considered "missing"!
114     * 
115     * @see baseCode.dataStructure.matrix.NamedMatrix#isMissing(int, int)
116     */
117    public boolean isMissing( int i, int j ) {
118       return get( i, j ) == 0.0;
119    }
120 
121    /***
122     * @return java.lang.String
123     */
124    public String toString() {
125       NumberFormat nf = NumberFormat.getInstance();
126       
127       StringBuffer buf = new StringBuffer();
128       
129       String result = "";
130       if ( this.hasColNames() || this.hasRowNames() ) {
131          buf.append( "label");
132       }
133 
134       if ( this.hasColNames() ) {
135          for ( int i = 0; i < columns(); i++ ) {
136             buf.append( "\t" + getColName( i ));
137          }
138          buf.append( "\n");
139       }
140 
141       for ( int i = 0; i < rows(); i++ ) {
142          if ( this.hasRowNames() ) {
143             buf.append( getRowName( i ));
144          }
145          for ( int j = 0; j < columns(); j++ ) {
146 
147             double value = get( i, j );
148 
149             if ( value == 0.0 ) {
150                buf.append( "\t");
151             } else {
152                buf.append( "\t" + nf.format( value ));
153             }
154          }
155 
156          buf.append( "\n");
157       }
158       return buf.toString();
159    }
160 
161    /***
162     * @param row
163     * @param column
164     * @return
165     */
166    public double get( int i, int j ) {
167       return ( ( DoubleMatrix1D ) matrix.get( i ) ).getQuick( j );
168    }
169 
170    /***
171     * This gives just the list of values in the row - make sure this is what you want. It does not include the zero
172     * values.
173     * 
174     * @param row
175     * @return
176     */
177    public DoubleArrayList getRowArrayList( int row ) {
178       DoubleArrayList returnVal = new DoubleArrayList();
179       ( ( DoubleMatrix1D ) matrix.get( row ) ).getNonZeros( new IntArrayList(),
180             returnVal );
181       return returnVal;
182    }
183 
184 
185    /*
186     *  (non-Javadoc)
187     * @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#viewRow(int)
188     */
189    public DoubleMatrix1D viewRow( int i ) {
190       return ( DoubleMatrix1D ) matrix.get( i );
191    }
192 
193    /*
194     * (non-Javadoc)
195     * 
196     * @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getRow(int)
197     */
198    public double[] getRow( int i ) {
199       // return getRowMatrix1D( i ).toArray();
200       return ( ( DoubleMatrix1D ) matrix.get( i ) ).toArray();
201    }
202 
203    /***
204     * @param name
205     * @param indexes
206     * @param values
207     */
208    public void addRow( String name, IntArrayList indexes, DoubleArrayList values ) {
209       DoubleMatrix1D rowToAdd = new RCDoubleMatrix1D( indexes, values );
210 
211       matrix.add( rowToAdd );
212       this.addColumnName( name, matrix.size() - 1 );
213       this.addRowName( name, matrix.size() - 1 );
214       isDirty = true;
215    }
216 
217    /***
218     * @param matrix1D
219     */
220    public void addRow( String name, DoubleMatrix1D matrix1D ) {
221       matrix.add( matrix1D );
222       this.addColumnName( name, matrix.size() - 1 );
223       this.addRowName( name, matrix.size() - 1 );
224       isDirty = true;
225    }
226 
227    /*
228     * (non-Javadoc)
229     * 
230     * @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getQuick(int, int)
231     */
232    public double getQuick( int i, int j ) {
233       return get( i, j );
234    }
235 
236 
237 }