|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SparseRaggedDoubleMatrix2DNamed.java | 77.3% | 73.7% | 56.2% | 71.6% |
|
||||||||||||||
| 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 | 8 |
public SparseRaggedDoubleMatrix2DNamed() {
|
| 28 | 8 |
matrix = new Vector();
|
| 29 |
} |
|
| 30 |
|
|
| 31 |
/*
|
|
| 32 |
* (non-Javadoc)
|
|
| 33 |
*
|
|
| 34 |
* @see baseCode.dataStructure.matrix.NamedMatrix#rows()
|
|
| 35 |
*/
|
|
| 36 | 3532 |
public int rows() { |
| 37 | 3532 |
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 | 3094120 |
public int columns() { |
| 46 |
|
|
| 47 | 3094120 |
if ( !isDirty ) {
|
| 48 | 3094116 |
return columns;
|
| 49 |
} |
|
| 50 |
|
|
| 51 | 4 |
int max = 0;
|
| 52 | 4 |
for ( Iterator iter = matrix.iterator(); iter.hasNext(); ) {
|
| 53 | 1767 |
DoubleMatrix1D element = ( DoubleMatrix1D ) iter.next(); |
| 54 |
|
|
| 55 | 1767 |
int value = element.size();
|
| 56 | 1767 |
if ( value > max ) {
|
| 57 | 16 |
max = value; |
| 58 |
} |
|
| 59 |
|
|
| 60 |
} |
|
| 61 |
|
|
| 62 | 4 |
columns = max; |
| 63 | 4 |
isDirty = false;
|
| 64 | 4 |
return columns;
|
| 65 |
} |
|
| 66 |
|
|
| 67 |
/*
|
|
| 68 |
* (non-Javadoc)
|
|
| 69 |
*
|
|
| 70 |
* @see baseCode.dataStructure.matrix.NamedMatrix#set(int, int, java.lang.Object)
|
|
| 71 |
*/
|
|
| 72 | 0 |
public void set( int i, int j, Object val ) { |
| 73 | 0 |
set( i, j, ( ( Double ) val ).doubleValue() ); |
| 74 |
} |
|
| 75 |
|
|
| 76 |
/**
|
|
| 77 |
* @param i row
|
|
| 78 |
* @param j column
|
|
| 79 |
* @param d value
|
|
| 80 |
*/
|
|
| 81 | 0 |
public void set( int i, int j, double d ) { |
| 82 |
|
|
| 83 | 0 |
( ( 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 | 0 |
public Object[] getRowObj( int i ) { |
| 93 | 0 |
Double[] result = new Double[columns()];
|
| 94 |
|
|
| 95 | 0 |
double[] row = getRow( i );
|
| 96 |
|
|
| 97 | 0 |
for ( int j = 0; j < columns(); j++ ) { |
| 98 | 0 |
result[i] = new Double( row[j] );
|
| 99 |
} |
|
| 100 | 0 |
return result;
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
/*
|
|
| 104 |
* (non-Javadoc)
|
|
| 105 |
*
|
|
| 106 |
* @see baseCode.dataStructure.matrix.NamedMatrix#getColObj(int)
|
|
| 107 |
*/
|
|
| 108 | 0 |
public Object[] getColObj( int i ) { |
| 109 | 0 |
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 | 0 |
public boolean isMissing( int i, int j ) { |
| 118 | 0 |
return get( i, j ) == 0.0;
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
/**
|
|
| 122 |
* @return java.lang.String
|
|
| 123 |
*/
|
|
| 124 | 3 |
public String toString() {
|
| 125 | 3 |
NumberFormat nf = NumberFormat.getInstance(); |
| 126 |
|
|
| 127 | 3 |
StringBuffer buf = new StringBuffer();
|
| 128 |
|
|
| 129 | 3 |
String result = "";
|
| 130 | 3 |
if ( this.hasColNames() || this.hasRowNames() ) { |
| 131 | 3 |
buf.append( "label");
|
| 132 |
} |
|
| 133 |
|
|
| 134 | 3 |
if ( this.hasColNames() ) { |
| 135 | 3 |
for ( int i = 0; i < columns(); i++ ) { |
| 136 | 1764 |
buf.append( "\t" + getColName( i ));
|
| 137 |
} |
|
| 138 | 3 |
buf.append( "\n");
|
| 139 |
} |
|
| 140 |
|
|
| 141 | 3 |
for ( int i = 0; i < rows(); i++ ) { |
| 142 | 1764 |
if ( this.hasRowNames() ) { |
| 143 | 1764 |
buf.append( getRowName( i )); |
| 144 |
} |
|
| 145 | 1764 |
for ( int j = 0; j < columns(); j++ ) { |
| 146 |
|
|
| 147 | 3090582 |
double value = get( i, j );
|
| 148 |
|
|
| 149 | 3090582 |
if ( value == 0.0 ) {
|
| 150 | 3067097 |
buf.append( "\t");
|
| 151 |
} else {
|
|
| 152 | 23485 |
buf.append( "\t" + nf.format( value ));
|
| 153 |
} |
|
| 154 |
} |
|
| 155 |
|
|
| 156 | 1764 |
buf.append( "\n");
|
| 157 |
} |
|
| 158 | 3 |
return buf.toString();
|
| 159 |
} |
|
| 160 |
|
|
| 161 |
/**
|
|
| 162 |
* @param row
|
|
| 163 |
* @param column
|
|
| 164 |
* @return
|
|
| 165 |
*/
|
|
| 166 | 3090582 |
public double get( int i, int j ) { |
| 167 | 3090582 |
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 | 1 |
public DoubleArrayList getRowArrayList( int row ) { |
| 178 | 1 |
DoubleArrayList returnVal = new DoubleArrayList();
|
| 179 | 1 |
( ( DoubleMatrix1D ) matrix.get( row ) ).getNonZeros( new IntArrayList(),
|
| 180 |
returnVal ); |
|
| 181 | 1 |
return returnVal;
|
| 182 |
} |
|
| 183 |
|
|
| 184 |
|
|
| 185 |
/*
|
|
| 186 |
* (non-Javadoc)
|
|
| 187 |
* @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#viewRow(int)
|
|
| 188 |
*/
|
|
| 189 | 1 |
public DoubleMatrix1D viewRow( int i ) { |
| 190 | 1 |
return ( DoubleMatrix1D ) matrix.get( i );
|
| 191 |
} |
|
| 192 |
|
|
| 193 |
/*
|
|
| 194 |
* (non-Javadoc)
|
|
| 195 |
*
|
|
| 196 |
* @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getRow(int)
|
|
| 197 |
*/
|
|
| 198 | 1 |
public double[] getRow( int i ) { |
| 199 |
// return getRowMatrix1D( i ).toArray();
|
|
| 200 | 1 |
return ( ( DoubleMatrix1D ) matrix.get( i ) ).toArray();
|
| 201 |
} |
|
| 202 |
|
|
| 203 |
/**
|
|
| 204 |
* @param name
|
|
| 205 |
* @param indexes
|
|
| 206 |
* @param values
|
|
| 207 |
*/
|
|
| 208 | 0 |
public void addRow( String name, IntArrayList indexes, DoubleArrayList values ) { |
| 209 | 0 |
DoubleMatrix1D rowToAdd = new RCDoubleMatrix1D( indexes, values );
|
| 210 |
|
|
| 211 | 0 |
matrix.add( rowToAdd ); |
| 212 | 0 |
this.addColumnName( name, matrix.size() - 1 );
|
| 213 | 0 |
this.addRowName( name, matrix.size() - 1 );
|
| 214 | 0 |
isDirty = true;
|
| 215 |
} |
|
| 216 |
|
|
| 217 |
/**
|
|
| 218 |
* @param matrix1D
|
|
| 219 |
*/
|
|
| 220 | 1779 |
public void addRow( String name, DoubleMatrix1D matrix1D ) { |
| 221 | 1779 |
matrix.add( matrix1D ); |
| 222 | 1779 |
this.addColumnName( name, matrix.size() - 1 );
|
| 223 | 1779 |
this.addRowName( name, matrix.size() - 1 );
|
| 224 | 1779 |
isDirty = true;
|
| 225 |
} |
|
| 226 |
|
|
| 227 |
/*
|
|
| 228 |
* (non-Javadoc)
|
|
| 229 |
*
|
|
| 230 |
* @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getQuick(int, int)
|
|
| 231 |
*/
|
|
| 232 | 0 |
public double getQuick( int i, int j ) { |
| 233 | 0 |
return get( i, j );
|
| 234 |
} |
|
| 235 |
|
|
| 236 |
|
|
| 237 |
} |
|
||||||||||