public static void writeMatrix(Matrix matrix, File output, Format format)
        throws IOException {
        if (matrix.rows() == 0 || matrix.columns() == 0)
            throw new IllegalArgumentException(
                "cannot write 0-dimensional matrix");
        switch (format) {
           
        case DENSE_TEXT: {
            PrintWriter pw = new PrintWriter(output);
            for (int i = 0; i < matrix.rows(); ++i) {
                StringBuffer sb = new StringBuffer(matrix.columns() *  5);
                for (int j = 0; j < matrix.columns(); ++j) {
                    sb.append(matrix.get(i,j)).append(" ");
                }
                pw.println(sb.toString());
            }
            pw.close();
            break;
        }
            
        // These two formats are equivalent
        case CLUTO_DENSE:
        case SVDLIBC_DENSE_TEXT: {
            PrintWriter pw = new PrintWriter(output);
            pw.println(matrix.rows() + " " +  matrix.columns());
            for (int i = 0; i < matrix.rows(); ++i) {
                StringBuffer sb = new StringBuffer(32);
                for (int j = 0; j < matrix.columns(); ++j) {
                    sb.append((float)(matrix.get(i,j))).append(" ");
                }
                pw.println(sb.toString());
            }
            pw.close();
            break;
        }

        case SVDLIBC_DENSE_BINARY: {
            DataOutputStream outStream = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(output)));
            outStream.writeInt(matrix.rows());
            outStream.writeInt(matrix.columns());
            for (int i = 0; i < matrix.rows(); ++i) {
                for (int j = 0; j < matrix.columns(); ++j) {
                    outStream.writeFloat((float)matrix.get(i,j));
                }
            }
            outStream.close();
            break;
        }

        case CLUTO_SPARSE: {
            PrintWriter pw = new PrintWriter(output);
            // Count the number of non-zero values in the matrix
            int nonZero = 0;
            int rows = matrix.rows();
            for (int i = 0; i < rows; ++i) {
                DoubleVector v = matrix.getRowVector(i);
                if (v instanceof SparseVector) 
                    nonZero += ((SparseVector)v).getNonZeroIndices().length;
                else {
                    for (int col = 0; col < v.length(); ++col) {
                        if (v.get(col) != 0) 
                            nonZero++;
                    }
                }
            }
            // Write the header: rows cols non-zero
            pw.println(matrix.rows() + " " + matrix.columns() + " " + nonZero);
            for (int row = 0; row < rows; ++row) {
                StringBuilder sb = new StringBuilder(nonZero / rows);
                // NOTE: the columns in CLUTO start at 1, not 0, so increment
                // one to each of the columns
                DoubleVector v = matrix.getRowVector(row);
                if (v instanceof SparseVector) {
                    int[] nzIndices = ((SparseVector)v).getNonZeroIndices();
                    for (int nz : nzIndices) {
                        sb.append(nz + 1).append(" ").
                            append(v.get(nz)).append(" ");
                    }
                }
                else {
                    for (int col = 0; col < v.length(); ++col) {
                        double d = v.get(col);
                        if (d != 0) 
                            sb.append(col+1).append(" ").append(d).append(" ");
                    }
                }
                pw.println(sb.toString());
            }
            pw.close();
            break;
        }

        case SVDLIBC_SPARSE_TEXT: {
            PrintWriter pw = new PrintWriter(output);
            // count the number of non-zero values for each column as well as
            // the total
            int nonZero = 0;
            int[] nonZeroPerCol = new int[matrix.columns()];
            for (int i = 0; i < matrix.rows(); ++i) {
                for (int j = 0; j < matrix.columns(); ++j) {
                    if (matrix.get(i, j) != 0) {
                        nonZero++;
                        nonZeroPerCol[j]++;
                    }
                }
            }

            // loop through the matrix a second time, printing out the number of
            // non-zero values for each column, followed by those values and
            // their associated row
            pw.println(matrix.rows() + " " + matrix.columns() + " " + nonZero);
            for (int col = 0; col < matrix.columns(); ++col) {
                pw.println(nonZeroPerCol[col]);
                if (nonZeroPerCol[col] > 0) {
                    for (int row = 0; row < matrix.rows(); ++row) {
                        double val = matrix.get(row, col);
                        if (val != 0d) {
                            // NOTE: need to convert to float since this is what
                            // SVDLIBC uses
                            pw.println(row + " " + 
                                       Double.valueOf(val).floatValue());
                        }
                    }
                }
            }
            pw.close();
            break;
        }

        case SVDLIBC_SPARSE_BINARY: {
            DataOutputStream outStream = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(output)));          
            
            // count the number of non-zero values for each column as well as
            // the total
            int nonZero = 0;
            int[] nonZeroPerCol = new int[matrix.columns()];
            for (int i = 0; i < matrix.rows(); ++i) {
                for (int j = 0; j < matrix.columns(); ++j) {
                    if (matrix.get(i, j) != 0) {
                        nonZero++;
                        nonZeroPerCol[j]++;
                    }
                }
            }

            // Write the 12 byte header data
            outStream.writeInt(matrix.rows());
            outStream.writeInt(matrix.columns());
            outStream.writeInt(nonZero);
            
            // loop through the matrix a second time, printing out the number of
            // non-zero values for each column, followed by those values and
            // their associated row
            for (int col = 0; col < matrix.columns(); ++col) {
                outStream.writeInt(nonZeroPerCol[col]);
                if (nonZeroPerCol[col] > 0) {
                    for (int row = 0; row < matrix.rows(); ++row) {
                        double val = matrix.get(row, col);
                        if (val != 0) {
                            // NOTE: need to convert to float since this is what
                            // SVDLIBC uses
                            outStream.writeInt(row);
                            outStream.writeFloat((float)val);
                        }
                    }
                }
            }
            outStream.close();
            break;
        }

        case MATLAB_SPARSE: {
            PrintWriter pw = new PrintWriter(output);
            // NOTE: Matlab's sparse matrix offers no way of specifying the
            // original matrix's dimensions.  This is only problematic if the
            // matrix contains trailing rows or columns that are all
            // zeros. Therefore to ensure that the matrix has the correct size,
            // we track the maximum values written and write a 0-value to extend
            // the matrix to its correct size
            int maxRowSeen = 0;
            int maxColSeen = 0;
            for (int i = 0; i < matrix.rows(); ++i) {
                for (int j = 0; j < matrix.columns(); ++j) {
                    if (matrix.get(i,j) == 0)
                        continue;
                    if (j > maxColSeen)
                        maxColSeen = j;
                    if (i > maxRowSeen)
                        maxRowSeen = i;
                    StringBuffer sb = new StringBuffer(32);
                    // Add 1 to index values since Matlab arrays are 1-based,
                    // not 0-based
                    sb.append(i+1).append(" ").append(j+1);
                    sb.append(" ").append(matrix.get(i,j));
                    pw.println(sb.toString());
                }
            }
            // Check whether we need to extend the matrix
            if (maxRowSeen + 1 !=  matrix.rows() 
                    || maxColSeen + 1 != matrix.columns()) {
                pw.println(matrix.rows() + " " + matrix.columns() + " 0");
            }
            pw.close();
            break;                        
        }

        default:
            throw new UnsupportedOperationException(
                "writing to " + format + " is currently unsupported");
        }
    }
public static void writeMatrix ( Matrix matrix , File output , Format format ) throws IOException { if ( matrix . rows ( ) == 0 || matrix . columns ( ) == 0 ) throw new IllegalArgumentException ( "cannot write 0-dimensional matrix" ) ; switch ( format ) { case DENSE_TEXT : { PrintWriter pw = new PrintWriter ( output ) ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { StringBuffer sb = new StringBuffer ( matrix . columns ( ) * 5 ) ; for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { sb . append ( matrix . get ( i , j ) ) . append ( " " ) ; } pw . println ( sb . toString ( ) ) ; } pw . close ( ) ; break ; } case CLUTO_DENSE : case SVDLIBC_DENSE_TEXT : { PrintWriter pw = new PrintWriter ( output ) ; pw . println ( matrix . rows ( ) + " " + matrix . columns ( ) ) ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { StringBuffer sb = new StringBuffer ( 32 ) ; for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { sb . append ( ( float ) ( matrix . get ( i , j ) ) ) . append ( " " ) ; } pw . println ( sb . toString ( ) ) ; } pw . close ( ) ; break ; } case SVDLIBC_DENSE_BINARY : { DataOutputStream outStream = new DataOutputStream ( new BufferedOutputStream ( new FileOutputStream ( output ) ) ) ; outStream . writeInt ( matrix . rows ( ) ) ; outStream . writeInt ( matrix . columns ( ) ) ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { outStream . writeFloat ( ( float ) matrix . get ( i , j ) ) ; } } outStream . close ( ) ; break ; } case CLUTO_SPARSE : { PrintWriter pw = new PrintWriter ( output ) ; int nonZero = 0 ; int rows = matrix . rows ( ) ; for ( int i = 0 ; i < rows ; ++ i ) { DoubleVector v = matrix . getRowVector ( i ) ; if ( v instanceof SparseVector ) nonZero += ( ( SparseVector ) v ) . getNonZeroIndices ( ) . length ; else { for ( int col = 0 ; col < v . length ( ) ; ++ col ) { if ( v . get ( col ) != 0 ) nonZero ++ ; } } } pw . println ( matrix . rows ( ) + " " + matrix . columns ( ) + " " + nonZero ) ; for ( int row = 0 ; row < rows ; ++ row ) { StringBuilder sb = new StringBuilder ( nonZero / rows ) ; DoubleVector v = matrix . getRowVector ( row ) ; if ( v instanceof SparseVector ) { int [ ] nzIndices = ( ( SparseVector ) v ) . getNonZeroIndices ( ) ; for ( int nz : nzIndices ) { sb . append ( nz + 1 ) . append ( " " ) . append ( v . get ( nz ) ) . append ( " " ) ; } } else { for ( int col = 0 ; col < v . length ( ) ; ++ col ) { double d = v . get ( col ) ; if ( d != 0 ) sb . append ( col + 1 ) . append ( " " ) . append ( d ) . append ( " " ) ; } } pw . println ( sb . toString ( ) ) ; } pw . close ( ) ; break ; } case SVDLIBC_SPARSE_TEXT : { PrintWriter pw = new PrintWriter ( output ) ; int nonZero = 0 ; int [ ] nonZeroPerCol = new int [ matrix . columns ( ) ] ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { if ( matrix . get ( i , j ) != 0 ) { nonZero ++ ; nonZeroPerCol [ j ] ++ ; } } } pw . println ( matrix . rows ( ) + " " + matrix . columns ( ) + " " + nonZero ) ; for ( int col = 0 ; col < matrix . columns ( ) ; ++ col ) { pw . println ( nonZeroPerCol [ col ] ) ; if ( nonZeroPerCol [ col ] > 0 ) { for ( int row = 0 ; row < matrix . rows ( ) ; ++ row ) { double val = matrix . get ( row , col ) ; if ( val != 0d ) { pw . println ( row + " " + Double . valueOf ( val ) . floatValue ( ) ) ; } } } } pw . close ( ) ; break ; } case SVDLIBC_SPARSE_BINARY : { DataOutputStream outStream = new DataOutputStream ( new BufferedOutputStream ( new FileOutputStream ( output ) ) ) ; int nonZero = 0 ; int [ ] nonZeroPerCol = new int [ matrix . columns ( ) ] ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { if ( matrix . get ( i , j ) != 0 ) { nonZero ++ ; nonZeroPerCol [ j ] ++ ; } } } outStream . writeInt ( matrix . rows ( ) ) ; outStream . writeInt ( matrix . columns ( ) ) ; outStream . writeInt ( nonZero ) ; for ( int col = 0 ; col < matrix . columns ( ) ; ++ col ) { outStream . writeInt ( nonZeroPerCol [ col ] ) ; if ( nonZeroPerCol [ col ] > 0 ) { for ( int row = 0 ; row < matrix . rows ( ) ; ++ row ) { double val = matrix . get ( row , col ) ; if ( val != 0 ) { outStream . writeInt ( row ) ; outStream . writeFloat ( ( float ) val ) ; } } } } outStream . close ( ) ; break ; } case MATLAB_SPARSE : { PrintWriter pw = new PrintWriter ( output ) ; int maxRowSeen = 0 ; int maxColSeen = 0 ; for ( int i = 0 ; i < matrix . rows ( ) ; ++ i ) { for ( int j = 0 ; j < matrix . columns ( ) ; ++ j ) { if ( matrix . get ( i , j ) == 0 ) continue ; if ( j > maxColSeen ) maxColSeen = j ; if ( i > maxRowSeen ) maxRowSeen = i ; StringBuffer sb = new StringBuffer ( 32 ) ; sb . append ( i + 1 ) . append ( " " ) . append ( j + 1 ) ; sb . append ( " " ) . append ( matrix . get ( i , j ) ) ; pw . println ( sb . toString ( ) ) ; } } if ( maxRowSeen + 1 != matrix . rows ( ) || maxColSeen + 1 != matrix . columns ( ) ) { pw . println ( matrix . rows ( ) + " " + matrix . columns ( ) + " 0" ) ; } pw . close ( ) ; break ; } default : throw new UnsupportedOperationException ( "writing to " + format + " is currently unsupported" ) ; } }
Writes the matrix to the specified output file in the provided format

@param matrix the matrix to be written
@param output the file in which the matrix should be written
@param format the data format in which the matrix's data should be
written

@throws IllegalArgumentException if the input matrix is 0-dimensional
@throws IOException if an error occurs while writing to the output file