@Override public RQ factor(DenseMatrix A) { if (Q.numRows() != A.numRows()) throw new IllegalArgumentException("Q.numRows() != A.numRows()"); else if (Q.numColumns() != A.numColumns()) throw new IllegalArgumentException("Q.numColumns() != A.numColumns()"); else if (R == null) throw new IllegalArgumentException("R == null"); /* * Calculate factorisation, and extract the triangular factor */ intW info = new intW(0); LAPACK.getInstance().dgerqf(m, n, A.getData(), Matrices.ld(m), tau, work, work.length, info); if (info.val < 0) throw new IllegalArgumentException(); R.zero(); for (MatrixEntry e : A) if (e.column() >= (n - m) + e.row()) R.set(e.row(), e.column() - (n - m), e.get()); /* * Generate the orthogonal matrix */ info.val = 0; LAPACK .getInstance() .dorgrq(m, n, k, A.getData(), Matrices.ld(m), tau, workGen, workGen.length, info); if (info.val < 0) throw new IllegalArgumentException(); Q.set(A); return this; }
/** @return the data table of this matrix as (row, column, value) cells */ public Table<Integer, Integer, Double> getDataTable() { Table<Integer, Integer, Double> res = HashBasedTable.create(); for (MatrixEntry me : this) { if (me.get() != 0) res.put(me.row(), me.column(), me.get()); } return res; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(String.format("%d\t%d\t%d\n", new Object[] {numRows, numColumns, size()})); for (MatrixEntry me : this) if (me.get() != 0) sb.append( String.format("%d\t%d\t%f\n", new Object[] {me.row() + 1, me.column() + 1, me.get()})); return sb.toString(); }
@Test public void testIterator() throws Exception { File f = getSparseBinarySVDLIBCFile(); Iterator<MatrixEntry> it = new SvdlibcSparseBinaryFileIterator(f); MatrixEntry me = it.next(); // Col 0 assertEquals(0, me.column()); assertEquals(0, me.row()); me = it.next(); assertEquals(0, me.column()); assertEquals(2, me.row()); me = it.next(); // Col 1 assertEquals(1, me.column()); assertEquals(1, me.row()); me = it.next(); // Col 2 assertEquals(2, me.column()); assertEquals(0, me.row()); me = it.next(); assertEquals(2, me.column()); assertEquals(1, me.row()); me = it.next(); assertEquals(2, me.column()); assertEquals(2, me.row()); assertFalse(it.hasNext()); }
/** @return the transpose of current matrix */ public SparseMatrix transpose() { if (isCCSUsed) { SparseMatrix tr = new SparseMatrix(numColumns, numRows); tr.copyCRS(this.rowData, this.rowPtr, this.colInd); tr.copyCCS(this.colData, this.colPtr, this.rowInd); return tr; } else { Table<Integer, Integer, Double> dataTable = HashBasedTable.create(); for (MatrixEntry me : this) dataTable.put(me.column(), me.row(), me.get()); return new SparseMatrix(numColumns, numRows, dataTable); } }
@Override void copy(Matrix A) { for (MatrixEntry e : A) if (e.row() >= e.column()) set(e.row(), e.column(), e.get()); }