/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. The number of rows and columns are read in on the first line. Then * each row is read in the subsequent lines. * * @param fileName The file being loaded. * @return DenseMatrix64F * @throws IOException */ public static DenseMatrix64F loadCSV( String fileName ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); DenseMatrix64F ret = csv.read(); fileStream.close(); return ret; }
/** * Reads in a DenseMatrix64F from the IO stream. * @return DenseMatrix64F * @throws IOException If anything goes wrong. */ public DenseMatrix64F read() throws IOException { List<String> words = extractWords(); if( words.size() != 2 ) throw new IOException("Unexpected number of words on first line."); int numRows = Integer.parseInt(words.get(0)); int numCols = Integer.parseInt(words.get(1)); if( numRows < 0 || numCols < 0) throw new IOException("Invalid number of rows and/or columns: "+numRows+" "+numCols); return read(numRows,numCols); }
/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. The number of rows and columns are read in on the first line. Then * each row is read in the subsequent lines. * * Works with dense and sparse matrices. * * @param fileName The file being loaded. * @return DMatrix * @throws IOException */ public static <T extends DMatrix>T loadCSV(String fileName , boolean doublePrecision ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); T ret; if( doublePrecision ) ret = csv.read64(); else ret = csv.read32(); fileStream.close(); return ret; }
/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. For a description of the format see {@link MatrixIO#loadCSV(String,boolean)}. * * @param fileName The file being loaded. * @param numRows number of rows in the matrix. * @param numCols number of columns in the matrix. * @return DMatrixRMaj * @throws IOException */ public static DMatrixRMaj loadCSV(String fileName , int numRows , int numCols ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); DMatrixRMaj ret = csv.readDDRM(numRows, numCols); fileStream.close(); return ret; }
List<String> words = extractWords(); return (M) readDDRM(numRows, numCols); else return (M) readZDRM(numRows, numCols); } else if ( words.size() == 4 ) { int numRows = Integer.parseInt(words.get(0)); return (M) readDSTR(numRows, numCols, length); else throw new IllegalArgumentException("Sparse complex not yet supported");
List<String> words = extractWords(); return (M) readFDRM(numRows, numCols); else return (M) readCDRM(numRows, numCols); } else if ( words.size() == 4 ) { int numRows = Integer.parseInt(words.get(0)); return (M) readFSTR(numRows, numCols, length); else throw new IllegalArgumentException("Sparse complex not yet supported");
/** * Reads in a DenseMatrix64F from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return DenseMatrix64F * @throws IOException */ public DenseMatrix64F read( int numRows , int numCols ) throws IOException { DenseMatrix64F A = new DenseMatrix64F(numRows,numCols); for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != numCols ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+numCols); for( int j = 0; j < numCols; j++ ) { A.set(i,j,Double.parseDouble(words.get(j))); } } return A; } }
private FMatrixSparseTriplet readFSTR(int numRows, int numCols, int length) throws IOException { List<String> words; FMatrixSparseTriplet m = new FMatrixSparseTriplet(numRows,numCols,length); for (int i = 0; i < length; i++) { words = extractWords(); if( words.size() != 3 ) throw new IllegalArgumentException("Unexpected number of words on line "+getLineNumber()); int row = Integer.parseInt(words.get(0)); int col = Integer.parseInt(words.get(1)); float value = Float.parseFloat(words.get(2)); m.addItem(row,col,value); } return m; }
/** * Reads in a {@link FMatrixRMaj} from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return FMatrixRMaj * @throws IOException */ public FMatrixRMaj readFDRM(int numRows, int numCols) throws IOException { FMatrixRMaj A = new FMatrixRMaj(numRows,numCols); for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != numCols ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+numCols); for( int j = 0; j < numCols; j++ ) { A.set(i,j,Float.parseFloat(words.get(j))); } } return A; }
private DMatrixSparseTriplet readDSTR(int numRows, int numCols, int length) throws IOException { List<String> words; DMatrixSparseTriplet m = new DMatrixSparseTriplet(numRows,numCols,length); for (int i = 0; i < length; i++) { words = extractWords(); if( words.size() != 3 ) throw new IllegalArgumentException("Unexpected number of words on line "+getLineNumber()); int row = Integer.parseInt(words.get(0)); int col = Integer.parseInt(words.get(1)); double value = Double.parseDouble(words.get(2)); m.addItem(row,col,value); } return m; } }
/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. For a description of the format see {@link MatrixIO#loadCSV(String)}. * * @param fileName The file being loaded. * @param numRows number of rows in the matrix. * @param numCols number of columns in the matrix. * @return DenseMatrix64F * @throws IOException */ public static DenseMatrix64F loadCSV( String fileName , int numRows , int numCols ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); DenseMatrix64F ret = csv.read(numRows,numCols); fileStream.close(); return ret; }
/** * Reads in a {@link DMatrixRMaj} from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return DMatrixRMaj * @throws IOException */ public DMatrixRMaj readDDRM(int numRows, int numCols) throws IOException { DMatrixRMaj A = new DMatrixRMaj(numRows,numCols); for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != numCols ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+numCols); for( int j = 0; j < numCols; j++ ) { A.set(i,j,Double.parseDouble(words.get(j))); } } return A; }
/** * Reads in a DenseMatrix64F from the IO stream. * @return DenseMatrix64F * @throws IOException If anything goes wrong. */ public DenseMatrix64F read() throws IOException { List<String> words = extractWords(); if( words.size() != 2 ) throw new IOException("Unexpected number of words on first line."); int numRows = Integer.parseInt(words.get(0)); int numCols = Integer.parseInt(words.get(1)); if( numRows < 0 || numCols < 0) throw new IOException("Invalid number of rows and/or columns: "+numRows+" "+numCols); return read(numRows,numCols); }
/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. The number of rows and columns are read in on the first line. Then * each row is read in the subsequent lines. * * @param fileName The file being loaded. * @return DenseMatrix64F * @throws IOException */ public static DenseMatrix64F loadCSV( String fileName ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); DenseMatrix64F ret = csv.read(); fileStream.close(); return ret; }
/** * Reads in a DenseMatrix64F from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return DenseMatrix64F * @throws IOException */ public DenseMatrix64F read( int numRows , int numCols ) throws IOException { DenseMatrix64F A = new DenseMatrix64F(numRows,numCols); for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != numCols ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+numCols); for( int j = 0; j < numCols; j++ ) { A.set(i,j,Double.parseDouble(words.get(j))); } } return A; } }
/** * Reads a matrix in which has been encoded using a Column Space Value (CSV) * file format. For a description of the format see {@link MatrixIO#loadCSV(String)}. * * @param fileName The file being loaded. * @param numRows number of rows in the matrix. * @param numCols number of columns in the matrix. * @return DenseMatrix64F * @throws IOException */ public static DenseMatrix64F loadCSV( String fileName , int numRows , int numCols ) throws IOException { FileInputStream fileStream = new FileInputStream(fileName); ReadMatrixCsv csv = new ReadMatrixCsv(fileStream); DenseMatrix64F ret = csv.read(numRows,numCols); fileStream.close(); return ret; }
/** * Reads in a {@link CMatrixRMaj} from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return CMatrixRMaj * @throws IOException */ public CMatrixRMaj readCDRM(int numRows, int numCols) throws IOException { CMatrixRMaj A = new CMatrixRMaj(numRows,numCols); int wordsCol = numCols*2; for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != wordsCol ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+wordsCol); for( int j = 0; j < wordsCol; j += 2 ) { float real = Float.parseFloat(words.get(j)); float imaginary = Float.parseFloat(words.get(j+1)); A.set(i, j, real, imaginary); } } return A; }
/** * Reads in a {@link ZMatrixRMaj} from the IO stream where the user specifies the matrix dimensions. * * @param numRows Number of rows in the matrix * @param numCols Number of columns in the matrix * @return ZMatrixRMaj * @throws IOException */ public ZMatrixRMaj readZDRM(int numRows, int numCols) throws IOException { ZMatrixRMaj A = new ZMatrixRMaj(numRows,numCols); int wordsCol = numCols*2; for( int i = 0; i < numRows; i++ ) { List<String> words = extractWords(); if( words == null ) throw new IOException("Too few rows found. expected "+numRows+" actual "+i); if( words.size() != wordsCol ) throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+wordsCol); for( int j = 0; j < wordsCol; j += 2 ) { double real = Double.parseDouble(words.get(j)); double imaginary = Double.parseDouble(words.get(j+1)); A.set(i, j, real, imaginary); } } return A; }