for (MatrixEntry e : A) {
Afact.set(e.row(), e.column(), e.get());
}
intW info = new intW(0);
LAPACK lapack = LAPACK.getInstance();
/*
* Calculate factorisation
*/
lapack.dgeqp3(m, n, Afact.getData(), Matrices.ld(m), jpvt, tau, work,
work.length, info);
if (info.val < 0)
throw new IllegalArgumentException();
/*
* Get R from Afact
*/
R.zero();
for (MatrixEntry e : Afact) {
if (e.row() <= e.column() && e.column() < R.numColumns()) {
R.set(e.row(), e.column(), e.get());
}
}
/*
* Calculate the rank based on a precision EPS
*/
final double EPS = 1e-12;
for (rank = 0; rank < k; rank++) {
if (Math.abs(R.get(rank, rank)) < EPS)
break;
}
/*
* Explicit the orthogonal matrix
*/
lapack.dorgqr(m, m, k, Afact.getData(), Matrices.ld(m), tau, work,
work.length, info);
for (MatrixEntry e : Afact) {
if (e.column() < Q.numColumns())
Q.set(e.row(), e.column(), e.get());
}