View Javadoc

    1   package baseCode.dataStructure.matrix;
    2
    3   import java.text.NumberFormat;
    4
    5   import cern.colt.list.DoubleArrayList;
    6   import cern.colt.matrix.DoubleMatrix1D;
    7   import cern.colt.matrix.impl.SparseDoubleMatrix2D;
    8
    9   /***
    10   * <p>
    11   * Title: SparseDoubleMatrix2DNamed
    12   * </p>
    13   * <p>
    14   * Description: A sparse matrix that knows about row and column names.
    15   * </p>
    16   * <p>
    17   * Copyright (c) 2004
    18   * </p>
    19   * <p>
    20   * Institution:: Columbia University
    21   * </p>
    22   *
    23   * @author Paul Pavlidis
    24   * @version $Id: SparseDoubleMatrix2DNamed.java,v 1.5 2004/08/16 02:06:01 pavlidis Exp $
    25   */
    26  public class SparseDoubleMatrix2DNamed extends AbstractNamedDoubleMatrix implements
    27        NamedMatrix {
    28
    29     private SparseDoubleMatrix2D matrix;
    30
    31     /***
    32      * @param T double[][]
    33      */
    34     public SparseDoubleMatrix2DNamed( double T[][] ) {
    35        super();
    36        matrix = new SparseDoubleMatrix2D( T );
    37     }
    38
    39     /***
    40      * @param rows int
    41      * @param cols int
    42      */
    43     public SparseDoubleMatrix2DNamed( int rows, int cols ) {
    44        super();
    45        matrix = new SparseDoubleMatrix2D( rows, cols );
    46     }
    47
    48     public void set( int row, int col, Object value ) {
    49        set( row, col, ( ( Double ) value ).doubleValue() );
    50     }
    51
    52     /***
    53      * Return a reference to a specific row.
    54      *
    55      * @param row int
    56      * @return double[]
    57      */
    58     public double[] getRow( int row ) {
    59        return viewRow( row ).toArray();
    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     public Object[] getRowObj( int row ) {
    77        Double[] result = new Double[columns()];
    78        for ( int i = 0; i < columns(); i++ ) {
    79           result[i] = new Double( get( row, i ) );
    80        }
    81        return result;
    82     }
    83
    84     public Object[] getColObj( int col ) {
    85        Double[] result = new Double[rows()];
    86        for ( int i = 0; i < rows(); i++ ) {
    87           result[i] = new Double( get( i, col ) );
    88        }
    89        return result;
    90     }
    91
    92     /***
    93      * @return java.lang.String
    94      */
    95     public String toString() {
    96        NumberFormat nf = NumberFormat.getInstance();
    97        String result = "";
    98        if ( this.hasColNames() || this.hasRowNames() ) {
    99           result = "label";
    100       }
    101
    102       if ( this.hasColNames() ) {
    103          for ( int i = 0; i < columns(); i++ ) {
    104             result = result + "\t" + getColName( i );
    105          }
    106          result += "\n";
    107       }
    108
    109       for ( int i = 0; i < rows(); i++ ) {
    110          if ( this.hasRowNames() ) {
    111             result += getRowName( i );
    112          }
    113          for ( int j = 0; j < columns(); j++ ) {
    114
    115             double value = get( i, j );
    116
    117             if ( value == 0.0 ) {
    118                result = result + "\t";
    119             } else {
    120
    121                result = result + "\t" + nf.format( value );
    122             }
    123          }
    124          result += "\n";
    125       }
    126       return result;
    127    }
    128
    129    /***
    130     * @param s String
    131     * @return double[]
    132     */
    133    public double[] getRowByName( String s ) {
    134       return getRow( getRowIndexByName( s ) );
    135    }
    136
    137    public boolean isMissing( int i, int j ) {
    138       return Double.isNaN( get( i, j ) );
    139    }
    140
    141    /***
    142     * @return
    143     */
    144    public int columns() {
    145       return matrix.columns();
    146    }
    147
    148    /***
    149     * @param row
    150     * @param column
    151     * @return
    152     */
    153    public double get( int row, int column ) {
    154       return matrix.get( row, column );
    155    }
    156
    157    /***
    158     * @param row
    159     * @param column
    160     * @return
    161     */
    162    public double getQuick( int row, int column ) {
    163       return matrix.getQuick( row, column );
    164    }
    165
    166    /***
    167     * @return
    168     */
    169    public int rows() {
    170       return matrix.rows();
    171    }
    172
    173    /***
    174     * @param row
    175     * @param column
    176     * @param value
    177     */
    178    public void set( int row, int column, double value ) {
    179       matrix.set( row, column, value );
    180    }
    181
    182    /***
    183     * @param row
    184     * @param column
    185     * @param value
    186     */
    187    public void setQuick( int row, int column, double value ) {
    188       matrix.setQuick( row, column, value );
    189    }
    190
    191    /***
    192     * @param column
    193     * @return
    194     */
    195    public DoubleMatrix1D viewColumn( int column ) {
    196       return matrix.viewColumn( column );
    197    }
    198
    199    /***
    200     * @param row
    201     * @return
    202     */
    203    public DoubleMatrix1D viewRow( int row ) {
    204       return matrix.viewRow( row );
    205    }
    206
    207    /***
    208     * @return
    209     */
    210    public int cardinality() {
    211       return matrix.cardinality();
    212    }
    213    /***
    214     * @param minNonZeros
    215     */
    216    public void ensureCapacity( int minNonZeros ) {
    217       matrix.ensureCapacity( minNonZeros );
    218    }
    219    /***
    220     * @return
    221     */
    222    public int size() {
    223       return matrix.size();
    224    }
    225    /***
    226     *
    227     */
    228    public void trimToSize() {
    229       matrix.trimToSize();
    230    }
    231
    232    /* (non-Javadoc)
    233     * @see baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix#getRowArrayList(int)
    234     */
    235    public DoubleArrayList getRowArrayList( int i ) {
    236      return new DoubleArrayList(getRow(i));
    237    }
    238
    239
    240 }