/** * {@inheritDoc} */ public Matrix getSingularValues() { if (singularValues == null) throw new IllegalStateException( "The matrix has not been factorized yet"); // NOTE: make this read-only? return new DiagonalMatrix(singularValues); }
/** * {@inheritDoc} * * Note that any values are not on the diagonal are ignored. */ public void setColumn(int column, double[] values) { checkIndices(values.length - 1, column); values[column] = values[column]; }
/** * {@inheritDoc} */ public double get(int row, int col) { checkIndices(row, col); if (row == col) return values[row]; return 0; }
/** * Generates a diagonal {@link Matrix} from the special-case file format * that SVDLIBC uses to output the Σ matrix. */ private static Matrix readSVDLIBCsingularVector(File sigmaMatrixFile) throws IOException { BufferedReader br = new BufferedReader(new FileReader(sigmaMatrixFile)); int dimension = -1; int valsSeen = 0; Matrix m = null; for (String line = null; (line = br.readLine()) != null; ) { String[] vals = line.split("\\s+"); for (int i = 0; i < vals.length; ++i) { // the first value seen should be the number of singular values if (dimension == -1) { dimension = Integer.parseInt(vals[i]); m = new DiagonalMatrix(dimension); } else { m.set(valsSeen, valsSeen, Double.parseDouble(vals[i])); ++valsSeen; } } } return m; }
/** * {@inheritDoc} */ public double[] getColumn(int column) { checkIndices(0, column); double[] columnValues = new double[values.length]; columnValues[column] = values[column]; return columnValues; }
/** * Generates a diagonal {@link Matrix} from the special-case file format * that SVDLIBC uses to output the Σ matrix. */ private static Matrix readSVDLIBCsingularVector(File sigmaMatrixFile) throws IOException { BufferedReader br = new BufferedReader(new FileReader(sigmaMatrixFile)); int dimension = -1; int valsSeen = 0; Matrix m = null; for (String line = null; (line = br.readLine()) != null; ) { String[] vals = line.split("\\s+"); for (int i = 0; i < vals.length; ++i) { // the first value seen should be the number of singular values if (dimension == -1) { dimension = Integer.parseInt(vals[i]); m = new DiagonalMatrix(dimension); } else { m.set(valsSeen, valsSeen, Double.parseDouble(vals[i])); ++valsSeen; } } } return m; }
/** * {@inheritDoc} */ public double[] getRow(int row) { checkIndices(row, 0); double[] returnRow = new double[values.length]; returnRow[row] = values[row]; return returnRow; }
Matrix s = new DiagonalMatrix(dimensions); for (int diag = 0; diag < dimensions; ++diag) { s.set(diag, diag, matrixArray[diag][diag]);
/** * {@inheritDoc} * * Note that any values are not on the diagonal are ignored. */ public void setRow(int row, double[] values) { checkIndices(row, values.length - 1); values[row] = values[row]; }
Matrix s = new DiagonalMatrix(dimensions); for (int diag = 0; diag < dimensions; ++diag) { s.set(diag, diag, matrixArray[diag][diag]);
/** * {@inheritDoc} * * Note that any values are not on the diagonal are ignored. */ public void setRow(int row, double[] values) { checkIndices(row, values.length - 1); values[row] = values[row]; }
/** * Creates a new {@code Matrix} based on the provided type, with the * provided dimensions * * @param matrixType the type of matrix to create * @param rows the number of rows in the matrix * @param cols the number of columns in the matrix */ public static Matrix create(int rows, int cols, Type matrixType) { switch (matrixType) { case SPARSE_IN_MEMORY: return new YaleSparseMatrix(rows, cols); case DENSE_IN_MEMORY: return new ArrayMatrix(rows, cols); case DIAGONAL_IN_MEMORY: return new DiagonalMatrix(rows); case SPARSE_ON_DISK: //return new SparseOnDiskMatrix(rows, cols); // REMDINER: implement me return new OnDiskMatrix(rows, cols); case DENSE_ON_DISK: return new OnDiskMatrix(rows, cols); } throw new IllegalArgumentException( "Unknown matrix type: " + matrixType); }
/** * {@inheritDoc} */ public double[] getRow(int row) { checkIndices(row, 0); double[] returnRow = new double[values.length]; returnRow[row] = values[row]; return returnRow; }
Matrix s = new DiagonalMatrix(dimensions);
/** * {@inheritDoc} */ public double get(int row, int col) { checkIndices(row, col); if (row == col) return values[row]; return 0; }
/** * Creates a new {@code Matrix} based on the provided type, with the * provided dimensions * * @param matrixType the type of matrix to create * @param rows the number of rows in the matrix * @param cols the number of columns in the matrix */ public static Matrix create(int rows, int cols, Type matrixType) { switch (matrixType) { case SPARSE_IN_MEMORY: return new YaleSparseMatrix(rows, cols); case DENSE_IN_MEMORY: return new ArrayMatrix(rows, cols); case DIAGONAL_IN_MEMORY: return new DiagonalMatrix(rows); case SPARSE_ON_DISK: //return new SparseOnDiskMatrix(rows, cols); // REMDINER: implement me return new OnDiskMatrix(rows, cols); case DENSE_ON_DISK: return new OnDiskMatrix(rows, cols); } throw new IllegalArgumentException( "Unknown matrix type: " + matrixType); }
/** * {@inheritDoc} */ public double[] getColumn(int column) { checkIndices(0, column); double[] columnValues = new double[values.length]; columnValues[column] = values[column]; return columnValues; }
Matrix s = new DiagonalMatrix(dimensions);
/** * {@inheritDoc} * * Note that any values are not on the diagonal are ignored. */ public void setColumn(int column, double[] values) { checkIndices(values.length - 1, column); values[column] = values[column]; }