/** @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; }
/** @return the cardinary of current matrix */ public int size() { int size = 0; for (MatrixEntry me : this) if (me.get() != 0) size++; return size; }
@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; }
/** * Normalize the matrix entries to (0, 1) by (x-min)/(max-min) * * @param min minimum value * @param max maximum value */ public void normalize(double min, double max) { assert max > min; for (MatrixEntry me : this) { double entry = me.get(); if (entry != 0) me.set((entry - min) / (max - min)); } }
@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(); }
/** @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()); }