/** * {@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); }
/** * 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; }
/** * 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; }
Matrix s = new DiagonalMatrix(dimensions); for (int diag = 0; diag < dimensions; ++diag) { s.set(diag, diag, matrixArray[diag][diag]);
Matrix s = new DiagonalMatrix(dimensions); for (int diag = 0; diag < dimensions; ++diag) { s.set(diag, diag, matrixArray[diag][diag]);
/** * 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); }
Matrix s = new DiagonalMatrix(dimensions);
Matrix s = new DiagonalMatrix(dimensions);
/** * 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); }
private static Matrix multiplyBothDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = new DiagonalMatrix(m1.rows()); for (int i = 0; i < m1.rows(); ++i) resultMatrix.set(i, i, m1.get(i, i) * m2.get(i, i)); return resultMatrix; }
private static Matrix multiplyBothDiag(Matrix m1, Matrix m2) { Matrix resultMatrix = new DiagonalMatrix(m1.rows()); for (int i = 0; i < m1.rows(); ++i) resultMatrix.set(i, i, m1.get(i, i) * m2.get(i, i)); return resultMatrix; }
Matrix S = new DiagonalMatrix(result.S); result.S = null;
for (int i = 0; i < rows; ++i) sigmaInv[i] = 1d / sigma.get(i, i); DiagonalMatrix sigmaInvMatrix = new DiagonalMatrix(sigmaInv);