static int QR_decomp_plus (gretl_matrix *Q, gretl_matrix *R, int *rank, int *warn) { integer k = gretl_matrix_rows(R); double rcond = 0; int r, err; if (warn != NULL) { *warn = 0; } /* basic decomposition */ err = gretl_matrix_QR_decomp(Q, R); if (err) { return err; } /* check rank of QR */ r = gretl_check_QR_rank(R, &err, &rcond); if (err) { return err; } if (r < k) { err = E_SINGULAR; } else { /* then invert the triangular R */ char uplo = 'U'; char diag = 'N'; integer info = 0; dtrtri_(&uplo, &diag, &k, R->val, &k, &info); if (info != 0) { fprintf(stderr, "dtrtri: info = %d\n", (int) info); err = 1; } else if (rcond < RCOND_WARN && warn != NULL) { *warn = 1; } } if (rank != NULL) { *rank = r; } return err; }
/* Subroutine */ int dgetri_(integer *n, doublereal *a, integer *lda, integer *ipiv, doublereal *work, integer *lwork, integer *info) { /* System generated locals */ integer a_dim1, a_offset, i__1, i__2, i__3; /* Local variables */ integer i__, j, jb, nb, jj, jp, nn, iws; extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *), dgemv_(char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *); integer nbmin; extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, doublereal *, integer *), dtrsm_(char *, char *, char *, char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *), xerbla_( char *, integer *); extern integer ilaenv_(integer *, char *, char *, integer *, integer *, integer *, integer *); integer ldwork; extern /* Subroutine */ int dtrtri_(char *, char *, integer *, doublereal *, integer *, integer *); integer lwkopt; logical lquery; /* -- LAPACK routine (version 3.1) -- */ /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */ /* November 2006 */ /* .. Scalar Arguments .. */ /* .. */ /* .. Array Arguments .. */ /* .. */ /* Purpose */ /* ======= */ /* DGETRI computes the inverse of a matrix using the LU factorization */ /* computed by DGETRF. */ /* This method inverts U and then computes inv(A) by solving the system */ /* inv(A)*L = inv(U) for inv(A). */ /* Arguments */ /* ========= */ /* N (input) INTEGER */ /* The order of the matrix A. N >= 0. */ /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */ /* On entry, the factors L and U from the factorization */ /* A = P*L*U as computed by DGETRF. */ /* On exit, if INFO = 0, the inverse of the original matrix A. */ /* LDA (input) INTEGER */ /* The leading dimension of the array A. LDA >= max(1,N). */ /* IPIV (input) INTEGER array, dimension (N) */ /* The pivot indices from DGETRF; for 1<=i<=N, row i of the */ /* matrix was interchanged with row IPIV(i). */ /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */ /* On exit, if INFO=0, then WORK(1) returns the optimal LWORK. */ /* LWORK (input) INTEGER */ /* The dimension of the array WORK. LWORK >= max(1,N). */ /* For optimal performance LWORK >= N*NB, where NB is */ /* the optimal blocksize returned by ILAENV. */ /* If LWORK = -1, then a workspace query is assumed; the routine */ /* only calculates the optimal size of the WORK array, returns */ /* this value as the first entry of the WORK array, and no error */ /* message related to LWORK is issued by XERBLA. */ /* INFO (output) INTEGER */ /* = 0: successful exit */ /* < 0: if INFO = -i, the i-th argument had an illegal value */ /* > 0: if INFO = i, U(i,i) is exactly zero; the matrix is */ /* singular and its inverse could not be computed. */ /* ===================================================================== */ /* .. Parameters .. */ /* .. */ /* .. Local Scalars .. */ /* .. */ /* .. External Functions .. */ /* .. */ /* .. External Subroutines .. */ /* .. */ /* .. Intrinsic Functions .. */ /* .. */ /* .. Executable Statements .. */ /* Test the input parameters. */ /* Parameter adjustments */ a_dim1 = *lda; a_offset = 1 + a_dim1; a -= a_offset; --ipiv; --work; /* Function Body */ *info = 0; nb = ilaenv_(&c__1, "DGETRI", " ", n, &c_n1, &c_n1, &c_n1); lwkopt = *n * nb; work[1] = (doublereal) lwkopt; lquery = *lwork == -1; if (*n < 0) { *info = -1; } else if (*lda < max(1,*n)) { *info = -3; } else if (*lwork < max(1,*n) && ! lquery) { *info = -6; } if (*info != 0) { i__1 = -(*info); xerbla_("DGETRI", &i__1); return 0; } else if (lquery) { return 0; } /* Quick return if possible */ if (*n == 0) { return 0; } /* Form inv(U). If INFO > 0 from DTRTRI, then U is singular, */ /* and the inverse is not computed. */ dtrtri_("Upper", "Non-unit", n, &a[a_offset], lda, info); if (*info > 0) { return 0; } nbmin = 2; ldwork = *n; if (nb > 1 && nb < *n) { /* Computing MAX */ i__1 = ldwork * nb; iws = max(i__1,1); if (*lwork < iws) { nb = *lwork / ldwork; /* Computing MAX */ i__1 = 2, i__2 = ilaenv_(&c__2, "DGETRI", " ", n, &c_n1, &c_n1, & c_n1); nbmin = max(i__1,i__2); } } else { iws = *n; } /* Solve the equation inv(A)*L = inv(U) for inv(A). */ if (nb < nbmin || nb >= *n) { /* Use unblocked code. */ for (j = *n; j >= 1; --j) { /* Copy current column of L to WORK and replace with zeros. */ i__1 = *n; for (i__ = j + 1; i__ <= i__1; ++i__) { work[i__] = a[i__ + j * a_dim1]; a[i__ + j * a_dim1] = 0.; /* L10: */ } /* Compute current column of inv(A). */ if (j < *n) { i__1 = *n - j; dgemv_("No transpose", n, &i__1, &c_b20, &a[(j + 1) * a_dim1 + 1], lda, &work[j + 1], &c__1, &c_b22, &a[j * a_dim1 + 1], &c__1); } /* L20: */ } } else { /* Use blocked code. */ nn = (*n - 1) / nb * nb + 1; i__1 = -nb; for (j = nn; i__1 < 0 ? j >= 1 : j <= 1; j += i__1) { /* Computing MIN */ i__2 = nb, i__3 = *n - j + 1; jb = min(i__2,i__3); /* Copy current block column of L to WORK and replace with */ /* zeros. */ i__2 = j + jb - 1; for (jj = j; jj <= i__2; ++jj) { i__3 = *n; for (i__ = jj + 1; i__ <= i__3; ++i__) { work[i__ + (jj - j) * ldwork] = a[i__ + jj * a_dim1]; a[i__ + jj * a_dim1] = 0.; /* L30: */ } /* L40: */ } /* Compute current block column of inv(A). */ if (j + jb <= *n) { i__2 = *n - j - jb + 1; dgemm_("No transpose", "No transpose", n, &jb, &i__2, &c_b20, &a[(j + jb) * a_dim1 + 1], lda, &work[j + jb], & ldwork, &c_b22, &a[j * a_dim1 + 1], lda); } dtrsm_("Right", "Lower", "No transpose", "Unit", n, &jb, &c_b22, & work[j], &ldwork, &a[j * a_dim1 + 1], lda); /* L50: */ } } /* Apply column interchanges. */ for (j = *n - 1; j >= 1; --j) { jp = ipiv[j]; if (jp != j) { dswap_(n, &a[j * a_dim1 + 1], &c__1, &a[jp * a_dim1 + 1], &c__1); } /* L60: */ } work[1] = (doublereal) iws; return 0; /* End of DGETRI */ } /* dgetri_ */
/* Subroutine */ int dgetri_(integer *n, doublereal *a, integer *lda, integer *ipiv, doublereal *work, integer *lwork, integer *info) { /* System generated locals */ integer a_dim1, a_offset, i__1, i__2, i__3; /* Local variables */ integer i__, j, jb, nb, jj, jp, nn, iws; extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *), dgemv_(char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *); integer nbmin; extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, doublereal *, integer *), dtrsm_(char *, char *, char *, char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *), xerbla_( char *, integer *); extern integer ilaenv_(integer *, char *, char *, integer *, integer *, integer *, integer *); integer ldwork; extern /* Subroutine */ int dtrtri_(char *, char *, integer *, doublereal *, integer *, integer *); integer lwkopt; logical lquery; /* -- LAPACK computational routine (version 3.4.0) -- */ /* -- LAPACK is a software package provided by Univ. of Tennessee, -- */ /* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */ /* November 2011 */ /* .. Scalar Arguments .. */ /* .. */ /* .. Array Arguments .. */ /* .. */ /* ===================================================================== */ /* .. Parameters .. */ /* .. */ /* .. Local Scalars .. */ /* .. */ /* .. External Functions .. */ /* .. */ /* .. External Subroutines .. */ /* .. */ /* .. Intrinsic Functions .. */ /* .. */ /* .. Executable Statements .. */ /* Test the input parameters. */ /* Parameter adjustments */ a_dim1 = *lda; a_offset = 1 + a_dim1; a -= a_offset; --ipiv; --work; /* Function Body */ *info = 0; nb = ilaenv_(&c__1, "DGETRI", " ", n, &c_n1, &c_n1, &c_n1); lwkopt = *n * nb; work[1] = (doublereal) lwkopt; lquery = *lwork == -1; if (*n < 0) { *info = -1; } else if (*lda < max(1,*n)) { *info = -3; } else if (*lwork < max(1,*n) && ! lquery) { *info = -6; } if (*info != 0) { i__1 = -(*info); xerbla_("DGETRI", &i__1); return 0; } else if (lquery) { return 0; } /* Quick return if possible */ if (*n == 0) { return 0; } /* Form inv(U). If INFO > 0 from DTRTRI, then U is singular, */ /* and the inverse is not computed. */ dtrtri_("Upper", "Non-unit", n, &a[a_offset], lda, info); if (*info > 0) { return 0; } nbmin = 2; ldwork = *n; if (nb > 1 && nb < *n) { /* Computing MAX */ i__1 = ldwork * nb; iws = max(i__1,1); if (*lwork < iws) { nb = *lwork / ldwork; /* Computing MAX */ i__1 = 2; i__2 = ilaenv_(&c__2, "DGETRI", " ", n, &c_n1, &c_n1, & c_n1); // , expr subst nbmin = max(i__1,i__2); } } else { iws = *n; } /* Solve the equation inv(A)*L = inv(U) for inv(A). */ if (nb < nbmin || nb >= *n) { /* Use unblocked code. */ for (j = *n; j >= 1; --j) { /* Copy current column of L to WORK and replace with zeros. */ i__1 = *n; for (i__ = j + 1; i__ <= i__1; ++i__) { work[i__] = a[i__ + j * a_dim1]; a[i__ + j * a_dim1] = 0.; /* L10: */ } /* Compute current column of inv(A). */ if (j < *n) { i__1 = *n - j; dgemv_("No transpose", n, &i__1, &c_b20, &a[(j + 1) * a_dim1 + 1], lda, &work[j + 1], &c__1, &c_b22, &a[j * a_dim1 + 1], &c__1); } /* L20: */ } } else { /* Use blocked code. */ nn = (*n - 1) / nb * nb + 1; i__1 = -nb; for (j = nn; i__1 < 0 ? j >= 1 : j <= 1; j += i__1) { /* Computing MIN */ i__2 = nb; i__3 = *n - j + 1; // , expr subst jb = min(i__2,i__3); /* Copy current block column of L to WORK and replace with */ /* zeros. */ i__2 = j + jb - 1; for (jj = j; jj <= i__2; ++jj) { i__3 = *n; for (i__ = jj + 1; i__ <= i__3; ++i__) { work[i__ + (jj - j) * ldwork] = a[i__ + jj * a_dim1]; a[i__ + jj * a_dim1] = 0.; /* L30: */ } /* L40: */ } /* Compute current block column of inv(A). */ if (j + jb <= *n) { i__2 = *n - j - jb + 1; dgemm_("No transpose", "No transpose", n, &jb, &i__2, &c_b20, &a[(j + jb) * a_dim1 + 1], lda, &work[j + jb], & ldwork, &c_b22, &a[j * a_dim1 + 1], lda); } dtrsm_("Right", "Lower", "No transpose", "Unit", n, &jb, &c_b22, & work[j], &ldwork, &a[j * a_dim1 + 1], lda); /* L50: */ } } /* Apply column interchanges. */ for (j = *n - 1; j >= 1; --j) { jp = ipiv[j]; if (jp != j) { dswap_(n, &a[j * a_dim1 + 1], &c__1, &a[jp * a_dim1 + 1], &c__1); } /* L60: */ } work[1] = (doublereal) iws; return 0; /* End of DGETRI */ }
/* Subroutine */ int dgetri_(integer *n, doublereal *a, integer *lda, integer *ipiv, doublereal *work, integer *lwork, integer *info) { /* -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University June 30, 1999 Purpose ======= DGETRI computes the inverse of a matrix using the LU factorization computed by DGETRF. This method inverts U and then computes inv(A) by solving the system inv(A)*L = inv(U) for inv(A). Arguments ========= N (input) INTEGER The order of the matrix A. N >= 0. A (input/output) DOUBLE PRECISION array, dimension (LDA,N) On entry, the factors L and U from the factorization A = P*L*U as computed by DGETRF. On exit, if INFO = 0, the inverse of the original matrix A. LDA (input) INTEGER The leading dimension of the array A. LDA >= max(1,N). IPIV (input) INTEGER array, dimension (N) The pivot indices from DGETRF; for 1<=i<=N, row i of the matrix was interchanged with row IPIV(i). WORK (workspace/output) DOUBLE PRECISION array, dimension (LWORK) On exit, if INFO=0, then WORK(1) returns the optimal LWORK. LWORK (input) INTEGER The dimension of the array WORK. LWORK >= max(1,N). For optimal performance LWORK >= N*NB, where NB is the optimal blocksize returned by ILAENV. If LWORK = -1, then a workspace query is assumed; the routine only calculates the optimal size of the WORK array, returns this value as the first entry of the WORK array, and no error message related to LWORK is issued by XERBLA. INFO (output) INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value > 0: if INFO = i, U(i,i) is exactly zero; the matrix is singular and its inverse could not be computed. ===================================================================== Test the input parameters. Parameter adjustments */ /* Table of constant values */ static integer c__1 = 1; static integer c_n1 = -1; static integer c__2 = 2; static doublereal c_b20 = -1.; static doublereal c_b22 = 1.; /* System generated locals */ integer a_dim1, a_offset, i__1, i__2, i__3; /* Local variables */ static integer i__, j; extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *), dgemv_(char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *); static integer nbmin; extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, doublereal *, integer *), dtrsm_(char *, char *, char *, char *, integer *, integer *, doublereal *, doublereal *, integer *, doublereal *, integer *); static integer jb, nb, jj, jp, nn; extern /* Subroutine */ int xerbla_(char *, integer *); extern integer ilaenv_(integer *, char *, char *, integer *, integer *, integer *, integer *, ftnlen, ftnlen); static integer ldwork; extern /* Subroutine */ int dtrtri_(char *, char *, integer *, doublereal *, integer *, integer *); static integer lwkopt; static logical lquery; static integer iws; #define a_ref(a_1,a_2) a[(a_2)*a_dim1 + a_1] a_dim1 = *lda; a_offset = 1 + a_dim1 * 1; a -= a_offset; --ipiv; --work; /* Function Body */ *info = 0; nb = ilaenv_(&c__1, "DGETRI", " ", n, &c_n1, &c_n1, &c_n1, (ftnlen)6, ( ftnlen)1); lwkopt = *n * nb; work[1] = (doublereal) lwkopt; lquery = *lwork == -1; if (*n < 0) { *info = -1; } else if (*lda < max(1,*n)) { *info = -3; } else if (*lwork < max(1,*n) && ! lquery) { *info = -6; } if (*info != 0) { i__1 = -(*info); xerbla_("DGETRI", &i__1); return 0; } else if (lquery) { return 0; } /* Quick return if possible */ if (*n == 0) { return 0; } /* Form inv(U). If INFO > 0 from DTRTRI, then U is singular, and the inverse is not computed. */ dtrtri_("Upper", "Non-unit", n, &a[a_offset], lda, info); if (*info > 0) { return 0; } nbmin = 2; ldwork = *n; if (nb > 1 && nb < *n) { /* Computing MAX */ i__1 = ldwork * nb; iws = max(i__1,1); if (*lwork < iws) { nb = *lwork / ldwork; /* Computing MAX */ i__1 = 2, i__2 = ilaenv_(&c__2, "DGETRI", " ", n, &c_n1, &c_n1, & c_n1, (ftnlen)6, (ftnlen)1); nbmin = max(i__1,i__2); } } else { iws = *n; } /* Solve the equation inv(A)*L = inv(U) for inv(A). */ if (nb < nbmin || nb >= *n) { /* Use unblocked code. */ for (j = *n; j >= 1; --j) { /* Copy current column of L to WORK and replace with zeros. */ i__1 = *n; for (i__ = j + 1; i__ <= i__1; ++i__) { work[i__] = a_ref(i__, j); a_ref(i__, j) = 0.; /* L10: */ } /* Compute current column of inv(A). */ if (j < *n) { i__1 = *n - j; dgemv_("No transpose", n, &i__1, &c_b20, &a_ref(1, j + 1), lda, &work[j + 1], &c__1, &c_b22, &a_ref(1, j), &c__1); } /* L20: */ } } else { /* Use blocked code. */ nn = (*n - 1) / nb * nb + 1; i__1 = -nb; for (j = nn; i__1 < 0 ? j >= 1 : j <= 1; j += i__1) { /* Computing MIN */ i__2 = nb, i__3 = *n - j + 1; jb = min(i__2,i__3); /* Copy current block column of L to WORK and replace with zeros. */ i__2 = j + jb - 1; for (jj = j; jj <= i__2; ++jj) { i__3 = *n; for (i__ = jj + 1; i__ <= i__3; ++i__) { work[i__ + (jj - j) * ldwork] = a_ref(i__, jj); a_ref(i__, jj) = 0.; /* L30: */ } /* L40: */ } /* Compute current block column of inv(A). */ if (j + jb <= *n) { i__2 = *n - j - jb + 1; dgemm_("No transpose", "No transpose", n, &jb, &i__2, &c_b20, &a_ref(1, j + jb), lda, &work[j + jb], &ldwork, & c_b22, &a_ref(1, j), lda); } dtrsm_("Right", "Lower", "No transpose", "Unit", n, &jb, &c_b22, & work[j], &ldwork, &a_ref(1, j), lda); /* L50: */ } } /* Apply column interchanges. */ for (j = *n - 1; j >= 1; --j) { jp = ipiv[j]; if (jp != j) { dswap_(n, &a_ref(1, j), &c__1, &a_ref(1, jp), &c__1); } /* L60: */ } work[1] = (doublereal) iws; return 0; /* End of DGETRI */ } /* dgetri_ */
/* Subroutine */ int dpotri_(char *uplo, integer *n, doublereal *a, integer * lda, integer *info) { /* -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University March 31, 1993 Purpose ======= DPOTRI computes the inverse of a real symmetric positive definite matrix A using the Cholesky factorization A = U**T*U or A = L*L**T computed by DPOTRF. Arguments ========= UPLO (input) CHARACTER*1 = 'U': Upper triangle of A is stored; = 'L': Lower triangle of A is stored. N (input) INTEGER The order of the matrix A. N >= 0. A (input/output) DOUBLE PRECISION array, dimension (LDA,N) On entry, the triangular factor U or L from the Cholesky factorization A = U**T*U or A = L*L**T, as computed by DPOTRF. On exit, the upper or lower triangle of the (symmetric) inverse of A, overwriting the input factor U or L. LDA (input) INTEGER The leading dimension of the array A. LDA >= max(1,N). INFO (output) INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value > 0: if INFO = i, the (i,i) element of the factor U or L is zero, and the inverse could not be computed. ===================================================================== Test the input parameters. Parameter adjustments */ /* System generated locals */ integer a_dim1, a_offset, i__1; /* Local variables */ extern logical lsame_(char *, char *); extern /* Subroutine */ int xerbla_(char *, integer *), dlauum_( char *, integer *, doublereal *, integer *, integer *), dtrtri_(char *, char *, integer *, doublereal *, integer *, integer *); a_dim1 = *lda; a_offset = 1 + a_dim1 * 1; a -= a_offset; /* Function Body */ *info = 0; if (! lsame_(uplo, "U") && ! lsame_(uplo, "L")) { *info = -1; } else if (*n < 0) { *info = -2; } else if (*lda < max(1,*n)) { *info = -4; } if (*info != 0) { i__1 = -(*info); xerbla_("DPOTRI", &i__1); return 0; } /* Quick return if possible */ if (*n == 0) { return 0; } /* Invert the triangular Cholesky factor U or L. */ dtrtri_(uplo, "Non-unit", n, &a[a_offset], lda, info); if (*info > 0) { return 0; } /* Form inv(U)*inv(U)' or inv(L)'*inv(L). */ dlauum_(uplo, n, &a[a_offset], lda, info); return 0; /* End of DPOTRI */ } /* dpotri_ */
bool CLinkMatrix::build(const CMatrix< C_FLOAT64 > & matrix) { bool success = true; CMatrix< C_FLOAT64 > M(matrix); C_INT NumCols = (C_INT) M.numCols(); C_INT NumRows = (C_INT) M.numRows(); C_INT LDA = std::max<C_INT>(1, NumCols); CVector< C_INT > JPVT(NumRows); JPVT = 0; C_INT32 Dim = std::min(NumCols, NumRows); if (Dim == 0) { C_INT32 i; mRowPivots.resize(NumRows); for (i = 0; i < NumRows; i++) mRowPivots[i] = i; resize(NumRows, 0); return success; } CVector< C_FLOAT64 > TAU(Dim); CVector< C_FLOAT64 > WORK(1); C_INT LWORK = -1; C_INT INFO; // QR factorization of the stoichiometry matrix /* * -- LAPACK routine (version 3.0) -- * Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., * Courant Institute, Argonne National Lab, and Rice University * June 30, 1999 * * Purpose * ======= * * DGEQP3 computes a QR factorization with column pivoting of a * matrix A: A*P = Q*R using Level 3 BLAS. * * Arguments * ========= * * M (input) INTEGER * The number of rows of the matrix A. M >= 0. * * N (input) INTEGER * The number of columns of the matrix A. N >= 0. * * A (input/output) DOUBLE PRECISION array, dimension (LDA,N) * On entry, the M-by-N matrix A. * On exit, the upper triangle of the array contains the * min(M,N)-by-N upper trapezoidal matrix R; the elements below * the diagonal, together with the array TAU, represent the * orthogonal matrix Q as a product of min(M,N) elementary * reflectors. * * LDA (input) INTEGER * The leading dimension of the array A. LDA >= max(1,M). * * JPVT (input/output) INTEGER array, dimension (N) * On entry, if JPVT(J).ne.0, the J-th column of A is permuted * to the front of A*P (a leading column); if JPVT(J)=0, * the J-th column of A is a free column. * On exit, if JPVT(J)=K, then the J-th column of A*P was the * the K-th column of A. * * TAU (output) DOUBLE PRECISION array, dimension (min(M,N)) * The scalar factors of the elementary reflectors. * * WORK (workspace/output) DOUBLE PRECISION array, dimension (LWORK) * On exit, if INFO=0, WORK(1) returns the optimal LWORK. * * LWORK (input) INTEGER * The dimension of the array WORK. LWORK >= 3*N+1. * For optimal performance LWORK >= 2*N+(N+1)*NB, where NB * is the optimal blocksize. * * If LWORK = -1, then a workspace query is assumed; the routine * only calculates the optimal size of the WORK array, returns * this value as the first entry of the WORK array, and no error * message related to LWORK is issued by XERBLA. * * INFO (output) INTEGER * = 0: successful exit. * < 0: if INFO = -i, the i-th argument had an illegal value. * * Further Details * =============== * * The matrix Q is represented as a product of elementary reflectors * * Q = H(1) H(2) . . . H(k), where k = min(m,n). * * Each H(i) has the form * * H(i) = I - tau * v * v' * * where tau is a real/complex scalar, and v is a real/complex vector * with v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in * A(i+1:m,i), and tau in TAU(i). * * Based on contributions by * G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain * X. Sun, Computer Science Dept., Duke University, USA * */ dgeqp3_(&NumCols, &NumRows, M.array(), &LDA, JPVT.array(), TAU.array(), WORK.array(), &LWORK, &INFO); if (INFO < 0) fatalError(); LWORK = (C_INT) WORK[0]; WORK.resize(LWORK); dgeqp3_(&NumCols, &NumRows, M.array(), &LDA, JPVT.array(), TAU.array(), WORK.array(), &LWORK, &INFO); if (INFO < 0) fatalError(); C_INT32 i; mRowPivots.resize(NumRows); for (i = 0; i < NumRows; i++) mRowPivots[i] = JPVT[i] - 1; C_INT independent = 0; while (independent < Dim && fabs(M(independent, independent)) > 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon()) independent++; mIndependent = independent; // Resize mL resize(NumRows - independent, independent); if (NumRows == independent || independent == 0) { return success; } /* to take care of differences between fortran's and c's memory access, we need to take the transpose, i.e.,the upper triangular */ char cL = 'U'; char cU = 'N'; /* values in the diagonal of R */ // Calculate Row Echelon form of R. // First invert R_1,1 /* int dtrtri_(char *uplo, * char *diag, * integer *n, * doublereal * A, * integer *lda, * integer *info); * -- LAPACK routine (version 3.0) -- * Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., * Courant Institute, Argonne National Lab, and Rice University * March 31, 1993 * * Purpose * ======= * * DTRTRI computes the inverse of a real upper or lower triangular * matrix A. * * This is the Level 3 BLAS version of the algorithm. * * Arguments * ========= * * uplo (input) CHARACTER*1 * = 'U': A is upper triangular; * = 'L': A is lower triangular. * * diag (input) CHARACTER*1 * = 'N': A is non-unit triangular; * = 'U': A is unit triangular. * * n (input) INTEGER * The order of the matrix A. n >= 0. * * A (input/output) DOUBLE PRECISION array, dimension (lda,n) * On entry, the triangular matrix A. If uplo = 'U', the * leading n-by-n upper triangular part of the array A contains * the upper triangular matrix, and the strictly lower * triangular part of A is not referenced. If uplo = 'L', the * leading n-by-n lower triangular part of the array A contains * the lower triangular matrix, and the strictly upper * triangular part of A is not referenced. If diag = 'U', the * diagonal elements of A are also not referenced and are * assumed to be 1. * On exit, the (triangular) inverse of the original matrix, in * the same storage format. * * lda (input) INTEGER * The leading dimension of the array A. lda >= max(1,n). * * info (output) INTEGER * = 0: successful exit * < 0: if info = -i, the i-th argument had an illegal value * > 0: if info = i, A(i,i) is exactly zero. The triangular * matrix is singular and its inverse can not be computed. */ dtrtri_(&cL, &cU, &independent, M.array(), &LDA, &INFO); if (INFO < 0) fatalError(); C_INT32 j, k; // Compute Link_0 = inverse(R_1,1) * R_1,2 // :TODO: Use dgemm C_FLOAT64 * pTmp1 = array(); C_FLOAT64 * pTmp2; C_FLOAT64 * pTmp3; for (j = 0; j < NumRows - independent; j++) for (i = 0; i < independent; i++, pTmp1++) { pTmp2 = &M(j + independent, i); pTmp3 = &M(i, i); // assert(&mL(j, i) == pTmp3); *pTmp1 = 0.0; for (k = i; k < independent; k++, pTmp2++, pTmp3 += NumCols) { // assert(&M(j + independent, k) == pTmp2); // assert(&M(k, i) == pTmp3); *pTmp1 += *pTmp3 * *pTmp2; } if (fabs(*pTmp1) < 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon()) *pTmp1 = 0.0; } // We need to convert the pivot vector into a swap vector. mPivotInverse.resize(mRowPivots.size()); size_t * pCurrentIndex = mPivotInverse.array(); size_t * pEnd = pCurrentIndex + mRowPivots.size(); for (size_t i = 0; pCurrentIndex != pEnd; ++pCurrentIndex, ++i) { *pCurrentIndex = i; } CVector< size_t > CurrentColumn(mPivotInverse); size_t * pCurrentColumn = CurrentColumn.array(); mSwapVector.resize(mRowPivots.size()); C_INT * pJPVT = mSwapVector.array(); pCurrentIndex = mPivotInverse.array(); const size_t * pPivot = mRowPivots.array(); for (; pCurrentIndex != pEnd; ++pPivot, ++pJPVT, ++pCurrentIndex, ++pCurrentColumn) { // Swap Index size_t * pToIndex = & mPivotInverse[*pPivot]; size_t * pFromIndex = & mPivotInverse[*pCurrentColumn]; // Swap Column size_t * pToColumn = & CurrentColumn[*pToIndex]; size_t * pFromColumn = pCurrentColumn; // Swap *pJPVT = *pToIndex + 1; size_t tmp = *pFromIndex; *pFromIndex = *pToIndex; *pToIndex = tmp; tmp = *pFromColumn; *pFromColumn = *pToColumn; *pToColumn = tmp; } return success; }
/* Subroutine */ int dchktr_(logical *dotype, integer *nn, integer *nval, integer *nnb, integer *nbval, integer *nns, integer *nsval, doublereal *thresh, logical *tsterr, integer *nmax, doublereal *a, doublereal *ainv, doublereal *b, doublereal *x, doublereal *xact, doublereal *work, doublereal *rwork, integer *iwork, integer *nout) { /* Initialized data */ static integer iseedy[4] = { 1988,1989,1990,1991 }; static char uplos[1*2] = "U" "L"; static char transs[1*3] = "N" "T" "C"; /* Format strings */ static char fmt_9999[] = "(\002 UPLO='\002,a1,\002', DIAG='\002,a1,\002'" ", N=\002,i5,\002, NB=\002,i4,\002, type \002,i2,\002, test(\002," "i2,\002)= \002,g12.5)"; static char fmt_9998[] = "(\002 UPLO='\002,a1,\002', TRANS='\002,a1,\002" "', DIAG='\002,a1,\002', N=\002,i5,\002, NB=\002,i4,\002, type" " \002,i2,\002, test(\002,i2,\002)= \002,g12" ".5)"; static char fmt_9997[] = "(\002 NORM='\002,a1,\002', UPLO ='\002,a1,\002" "', N=\002,i5,\002,\002,11x,\002 type \002,i2,\002, test(\002,i2" ",\002)=\002,g12.5)"; static char fmt_9996[] = "(1x,a6,\002( '\002,a1,\002', '\002,a1,\002', " "'\002,a1,\002', '\002,a1,\002',\002,i5,\002, ... ), type \002,i2," "\002, test(\002,i2,\002)=\002,g12.5)"; /* System generated locals */ address a__1[2], a__2[3], a__3[4]; integer i__1, i__2, i__3[2], i__4, i__5[3], i__6[4]; char ch__1[2], ch__2[3], ch__3[4]; /* Builtin functions */ /* Subroutine */ int s_copy(char *, char *, ftnlen, ftnlen), s_cat(char *, char **, integer *, integer *, ftnlen); integer s_wsfe(cilist *), do_fio(integer *, char *, ftnlen), e_wsfe(void); /* Local variables */ integer i__, k, n, nb, in, lda, inb; char diag[1]; integer imat, info; char path[3]; integer irhs, nrhs; char norm[1], uplo[1]; integer nrun; extern /* Subroutine */ int alahd_(integer *, char *); integer idiag; doublereal scale; extern /* Subroutine */ int dget04_(integer *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *); integer nfail, iseed[4]; extern logical lsame_(char *, char *); doublereal rcond, anorm; integer itran; extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, doublereal *, integer *), dtrt01_(char *, char *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, doublereal *), dtrt02_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *), dtrt03_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, doublereal *, doublereal *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *), dtrt05_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, doublereal *), dtrt06_( doublereal *, doublereal *, char *, char *, integer *, doublereal *, integer *, doublereal *, doublereal *); char trans[1]; integer iuplo, nerrs; doublereal dummy; char xtype[1]; extern /* Subroutine */ int alaerh_(char *, char *, integer *, integer *, char *, integer *, integer *, integer *, integer *, integer *, integer *, integer *, integer *, integer *); doublereal rcondc; extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, doublereal *, integer *, doublereal *, integer *), dlarhs_(char *, char *, char *, char *, integer *, integer *, integer *, integer *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, integer *, integer *, integer *); doublereal rcondi; extern /* Subroutine */ int alasum_(char *, integer *, integer *, integer *, integer *); doublereal rcondo; extern doublereal dlantr_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *); doublereal ainvnm; extern /* Subroutine */ int dlatrs_(char *, char *, char *, char *, integer *, doublereal *, integer *, doublereal *, doublereal *, doublereal *, integer *), dlattr_( integer *, char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, doublereal *, integer *), dtrcon_(char *, char *, char *, integer * , doublereal *, integer *, doublereal *, doublereal *, integer *, integer *), xlaenv_(integer *, integer *), derrtr_(char *, integer *), dtrrfs_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, integer *, doublereal *, doublereal *, doublereal *, integer *, integer *), dtrtri_(char *, char *, integer *, doublereal *, integer *, integer *); doublereal result[9]; extern /* Subroutine */ int dtrtrs_(char *, char *, char *, integer *, integer *, doublereal *, integer *, doublereal *, integer *, integer *); /* Fortran I/O blocks */ static cilist io___27 = { 0, 0, 0, fmt_9999, 0 }; static cilist io___36 = { 0, 0, 0, fmt_9998, 0 }; static cilist io___38 = { 0, 0, 0, fmt_9997, 0 }; static cilist io___40 = { 0, 0, 0, fmt_9996, 0 }; static cilist io___41 = { 0, 0, 0, fmt_9996, 0 }; /* -- LAPACK test routine (version 3.1) -- */ /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */ /* November 2006 */ /* .. Scalar Arguments .. */ /* .. */ /* .. Array Arguments .. */ /* .. */ /* Purpose */ /* ======= */ /* DCHKTR tests DTRTRI, -TRS, -RFS, and -CON, and DLATRS */ /* Arguments */ /* ========= */ /* DOTYPE (input) LOGICAL array, dimension (NTYPES) */ /* The matrix types to be used for testing. Matrices of type j */ /* (for 1 <= j <= NTYPES) are used for testing if DOTYPE(j) = */ /* .TRUE.; if DOTYPE(j) = .FALSE., then type j is not used. */ /* NN (input) INTEGER */ /* The number of values of N contained in the vector NVAL. */ /* NVAL (input) INTEGER array, dimension (NN) */ /* The values of the matrix column dimension N. */ /* NNB (input) INTEGER */ /* The number of values of NB contained in the vector NBVAL. */ /* NBVAL (input) INTEGER array, dimension (NNB) */ /* The values of the blocksize NB. */ /* NNS (input) INTEGER */ /* The number of values of NRHS contained in the vector NSVAL. */ /* NSVAL (input) INTEGER array, dimension (NNS) */ /* The values of the number of right hand sides NRHS. */ /* THRESH (input) DOUBLE PRECISION */ /* The threshold value for the test ratios. A result is */ /* included in the output file if RESULT >= THRESH. To have */ /* every test ratio printed, use THRESH = 0. */ /* TSTERR (input) LOGICAL */ /* Flag that indicates whether error exits are to be tested. */ /* NMAX (input) INTEGER */ /* The leading dimension of the work arrays. */ /* NMAX >= the maximum value of N in NVAL. */ /* A (workspace) DOUBLE PRECISION array, dimension (NMAX*NMAX) */ /* AINV (workspace) DOUBLE PRECISION array, dimension (NMAX*NMAX) */ /* B (workspace) DOUBLE PRECISION array, dimension (NMAX*NSMAX) */ /* where NSMAX is the largest entry in NSVAL. */ /* X (workspace) DOUBLE PRECISION array, dimension (NMAX*NSMAX) */ /* XACT (workspace) DOUBLE PRECISION array, dimension (NMAX*NSMAX) */ /* WORK (workspace) DOUBLE PRECISION array, dimension */ /* (NMAX*max(3,NSMAX)) */ /* RWORK (workspace) DOUBLE PRECISION array, dimension */ /* (max(NMAX,2*NSMAX)) */ /* IWORK (workspace) INTEGER array, dimension (NMAX) */ /* NOUT (input) INTEGER */ /* The unit number for output. */ /* ===================================================================== */ /* .. Parameters .. */ /* .. */ /* .. Local Scalars .. */ /* .. */ /* .. Local Arrays .. */ /* .. */ /* .. External Functions .. */ /* .. */ /* .. External Subroutines .. */ /* .. */ /* .. Scalars in Common .. */ /* .. */ /* .. Common blocks .. */ /* .. */ /* .. Intrinsic Functions .. */ /* .. */ /* .. Data statements .. */ /* Parameter adjustments */ --iwork; --rwork; --work; --xact; --x; --b; --ainv; --a; --nsval; --nbval; --nval; --dotype; /* Function Body */ /* .. */ /* .. Executable Statements .. */ /* Initialize constants and the random number seed. */ s_copy(path, "Double precision", (ftnlen)1, (ftnlen)16); s_copy(path + 1, "TR", (ftnlen)2, (ftnlen)2); nrun = 0; nfail = 0; nerrs = 0; for (i__ = 1; i__ <= 4; ++i__) { iseed[i__ - 1] = iseedy[i__ - 1]; /* L10: */ } /* Test the error exits */ if (*tsterr) { derrtr_(path, nout); } infoc_1.infot = 0; xlaenv_(&c__2, &c__2); i__1 = *nn; for (in = 1; in <= i__1; ++in) { /* Do for each value of N in NVAL */ n = nval[in]; lda = max(1,n); *(unsigned char *)xtype = 'N'; for (imat = 1; imat <= 10; ++imat) { /* Do the tests only if DOTYPE( IMAT ) is true. */ if (! dotype[imat]) { goto L80; } for (iuplo = 1; iuplo <= 2; ++iuplo) { /* Do first for UPLO = 'U', then for UPLO = 'L' */ *(unsigned char *)uplo = *(unsigned char *)&uplos[iuplo - 1]; /* Call DLATTR to generate a triangular test matrix. */ s_copy(srnamc_1.srnamt, "DLATTR", (ftnlen)6, (ftnlen)6); dlattr_(&imat, uplo, "No transpose", diag, iseed, &n, &a[1], & lda, &x[1], &work[1], &info); /* Set IDIAG = 1 for non-unit matrices, 2 for unit. */ if (lsame_(diag, "N")) { idiag = 1; } else { idiag = 2; } i__2 = *nnb; for (inb = 1; inb <= i__2; ++inb) { /* Do for each blocksize in NBVAL */ nb = nbval[inb]; xlaenv_(&c__1, &nb); /* + TEST 1 */ /* Form the inverse of A. */ dlacpy_(uplo, &n, &n, &a[1], &lda, &ainv[1], &lda); s_copy(srnamc_1.srnamt, "DTRTRI", (ftnlen)6, (ftnlen)6); dtrtri_(uplo, diag, &n, &ainv[1], &lda, &info); /* Check error code from DTRTRI. */ if (info != 0) { /* Writing concatenation */ i__3[0] = 1, a__1[0] = uplo; i__3[1] = 1, a__1[1] = diag; s_cat(ch__1, a__1, i__3, &c__2, (ftnlen)2); alaerh_(path, "DTRTRI", &info, &c__0, ch__1, &n, &n, & c_n1, &c_n1, &nb, &imat, &nfail, &nerrs, nout); } /* Compute the infinity-norm condition number of A. */ anorm = dlantr_("I", uplo, diag, &n, &n, &a[1], &lda, & rwork[1]); ainvnm = dlantr_("I", uplo, diag, &n, &n, &ainv[1], &lda, &rwork[1]); if (anorm <= 0. || ainvnm <= 0.) { rcondi = 1.; } else { rcondi = 1. / anorm / ainvnm; } /* Compute the residual for the triangular matrix times */ /* its inverse. Also compute the 1-norm condition number */ /* of A. */ dtrt01_(uplo, diag, &n, &a[1], &lda, &ainv[1], &lda, & rcondo, &rwork[1], result); /* Print the test ratio if it is .GE. THRESH. */ if (result[0] >= *thresh) { if (nfail == 0 && nerrs == 0) { alahd_(nout, path); } io___27.ciunit = *nout; s_wsfe(&io___27); do_fio(&c__1, uplo, (ftnlen)1); do_fio(&c__1, diag, (ftnlen)1); do_fio(&c__1, (char *)&n, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&nb, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&imat, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&c__1, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&result[0], (ftnlen)sizeof( doublereal)); e_wsfe(); ++nfail; } ++nrun; /* Skip remaining tests if not the first block size. */ if (inb != 1) { goto L60; } i__4 = *nns; for (irhs = 1; irhs <= i__4; ++irhs) { nrhs = nsval[irhs]; *(unsigned char *)xtype = 'N'; for (itran = 1; itran <= 3; ++itran) { /* Do for op(A) = A, A**T, or A**H. */ *(unsigned char *)trans = *(unsigned char *)& transs[itran - 1]; if (itran == 1) { *(unsigned char *)norm = 'O'; rcondc = rcondo; } else { *(unsigned char *)norm = 'I'; rcondc = rcondi; } /* + TEST 2 */ /* Solve and compute residual for op(A)*x = b. */ s_copy(srnamc_1.srnamt, "DLARHS", (ftnlen)6, ( ftnlen)6); dlarhs_(path, xtype, uplo, trans, &n, &n, &c__0, & idiag, &nrhs, &a[1], &lda, &xact[1], &lda, &b[1], &lda, iseed, &info); *(unsigned char *)xtype = 'C'; dlacpy_("Full", &n, &nrhs, &b[1], &lda, &x[1], & lda); s_copy(srnamc_1.srnamt, "DTRTRS", (ftnlen)6, ( ftnlen)6); dtrtrs_(uplo, trans, diag, &n, &nrhs, &a[1], &lda, &x[1], &lda, &info); /* Check error code from DTRTRS. */ if (info != 0) { /* Writing concatenation */ i__5[0] = 1, a__2[0] = uplo; i__5[1] = 1, a__2[1] = trans; i__5[2] = 1, a__2[2] = diag; s_cat(ch__2, a__2, i__5, &c__3, (ftnlen)3); alaerh_(path, "DTRTRS", &info, &c__0, ch__2, & n, &n, &c_n1, &c_n1, &nrhs, &imat, & nfail, &nerrs, nout); } /* This line is needed on a Sun SPARCstation. */ if (n > 0) { dummy = a[1]; } dtrt02_(uplo, trans, diag, &n, &nrhs, &a[1], &lda, &x[1], &lda, &b[1], &lda, &work[1], & result[1]); /* + TEST 3 */ /* Check solution from generated exact solution. */ dget04_(&n, &nrhs, &x[1], &lda, &xact[1], &lda, & rcondc, &result[2]); /* + TESTS 4, 5, and 6 */ /* Use iterative refinement to improve the solution */ /* and compute error bounds. */ s_copy(srnamc_1.srnamt, "DTRRFS", (ftnlen)6, ( ftnlen)6); dtrrfs_(uplo, trans, diag, &n, &nrhs, &a[1], &lda, &b[1], &lda, &x[1], &lda, &rwork[1], & rwork[nrhs + 1], &work[1], &iwork[1], & info); /* Check error code from DTRRFS. */ if (info != 0) { /* Writing concatenation */ i__5[0] = 1, a__2[0] = uplo; i__5[1] = 1, a__2[1] = trans; i__5[2] = 1, a__2[2] = diag; s_cat(ch__2, a__2, i__5, &c__3, (ftnlen)3); alaerh_(path, "DTRRFS", &info, &c__0, ch__2, & n, &n, &c_n1, &c_n1, &nrhs, &imat, & nfail, &nerrs, nout); } dget04_(&n, &nrhs, &x[1], &lda, &xact[1], &lda, & rcondc, &result[3]); dtrt05_(uplo, trans, diag, &n, &nrhs, &a[1], &lda, &b[1], &lda, &x[1], &lda, &xact[1], &lda, &rwork[1], &rwork[nrhs + 1], &result[4]); /* Print information about the tests that did not */ /* pass the threshold. */ for (k = 2; k <= 6; ++k) { if (result[k - 1] >= *thresh) { if (nfail == 0 && nerrs == 0) { alahd_(nout, path); } io___36.ciunit = *nout; s_wsfe(&io___36); do_fio(&c__1, uplo, (ftnlen)1); do_fio(&c__1, trans, (ftnlen)1); do_fio(&c__1, diag, (ftnlen)1); do_fio(&c__1, (char *)&n, (ftnlen)sizeof( integer)); do_fio(&c__1, (char *)&nrhs, (ftnlen) sizeof(integer)); do_fio(&c__1, (char *)&imat, (ftnlen) sizeof(integer)); do_fio(&c__1, (char *)&k, (ftnlen)sizeof( integer)); do_fio(&c__1, (char *)&result[k - 1], ( ftnlen)sizeof(doublereal)); e_wsfe(); ++nfail; } /* L20: */ } nrun += 5; /* L30: */ } /* L40: */ } /* + TEST 7 */ /* Get an estimate of RCOND = 1/CNDNUM. */ for (itran = 1; itran <= 2; ++itran) { if (itran == 1) { *(unsigned char *)norm = 'O'; rcondc = rcondo; } else { *(unsigned char *)norm = 'I'; rcondc = rcondi; } s_copy(srnamc_1.srnamt, "DTRCON", (ftnlen)6, (ftnlen) 6); dtrcon_(norm, uplo, diag, &n, &a[1], &lda, &rcond, & work[1], &iwork[1], &info); /* Check error code from DTRCON. */ if (info != 0) { /* Writing concatenation */ i__5[0] = 1, a__2[0] = norm; i__5[1] = 1, a__2[1] = uplo; i__5[2] = 1, a__2[2] = diag; s_cat(ch__2, a__2, i__5, &c__3, (ftnlen)3); alaerh_(path, "DTRCON", &info, &c__0, ch__2, &n, & n, &c_n1, &c_n1, &c_n1, &imat, &nfail, & nerrs, nout); } dtrt06_(&rcond, &rcondc, uplo, diag, &n, &a[1], &lda, &rwork[1], &result[6]); /* Print the test ratio if it is .GE. THRESH. */ if (result[6] >= *thresh) { if (nfail == 0 && nerrs == 0) { alahd_(nout, path); } io___38.ciunit = *nout; s_wsfe(&io___38); do_fio(&c__1, norm, (ftnlen)1); do_fio(&c__1, uplo, (ftnlen)1); do_fio(&c__1, (char *)&n, (ftnlen)sizeof(integer)) ; do_fio(&c__1, (char *)&imat, (ftnlen)sizeof( integer)); do_fio(&c__1, (char *)&c__7, (ftnlen)sizeof( integer)); do_fio(&c__1, (char *)&result[6], (ftnlen)sizeof( doublereal)); e_wsfe(); ++nfail; } ++nrun; /* L50: */ } L60: ; } /* L70: */ } L80: ; } /* Use pathological test matrices to test DLATRS. */ for (imat = 11; imat <= 18; ++imat) { /* Do the tests only if DOTYPE( IMAT ) is true. */ if (! dotype[imat]) { goto L110; } for (iuplo = 1; iuplo <= 2; ++iuplo) { /* Do first for UPLO = 'U', then for UPLO = 'L' */ *(unsigned char *)uplo = *(unsigned char *)&uplos[iuplo - 1]; for (itran = 1; itran <= 3; ++itran) { /* Do for op(A) = A, A**T, and A**H. */ *(unsigned char *)trans = *(unsigned char *)&transs[itran - 1]; /* Call DLATTR to generate a triangular test matrix. */ s_copy(srnamc_1.srnamt, "DLATTR", (ftnlen)6, (ftnlen)6); dlattr_(&imat, uplo, trans, diag, iseed, &n, &a[1], &lda, &x[1], &work[1], &info); /* + TEST 8 */ /* Solve the system op(A)*x = b. */ s_copy(srnamc_1.srnamt, "DLATRS", (ftnlen)6, (ftnlen)6); dcopy_(&n, &x[1], &c__1, &b[1], &c__1); dlatrs_(uplo, trans, diag, "N", &n, &a[1], &lda, &b[1], & scale, &rwork[1], &info); /* Check error code from DLATRS. */ if (info != 0) { /* Writing concatenation */ i__6[0] = 1, a__3[0] = uplo; i__6[1] = 1, a__3[1] = trans; i__6[2] = 1, a__3[2] = diag; i__6[3] = 1, a__3[3] = "N"; s_cat(ch__3, a__3, i__6, &c__4, (ftnlen)4); alaerh_(path, "DLATRS", &info, &c__0, ch__3, &n, &n, & c_n1, &c_n1, &c_n1, &imat, &nfail, &nerrs, nout); } dtrt03_(uplo, trans, diag, &n, &c__1, &a[1], &lda, &scale, &rwork[1], &c_b101, &b[1], &lda, &x[1], &lda, & work[1], &result[7]); /* + TEST 9 */ /* Solve op(A)*X = b again with NORMIN = 'Y'. */ dcopy_(&n, &x[1], &c__1, &b[n + 1], &c__1); dlatrs_(uplo, trans, diag, "Y", &n, &a[1], &lda, &b[n + 1] , &scale, &rwork[1], &info); /* Check error code from DLATRS. */ if (info != 0) { /* Writing concatenation */ i__6[0] = 1, a__3[0] = uplo; i__6[1] = 1, a__3[1] = trans; i__6[2] = 1, a__3[2] = diag; i__6[3] = 1, a__3[3] = "Y"; s_cat(ch__3, a__3, i__6, &c__4, (ftnlen)4); alaerh_(path, "DLATRS", &info, &c__0, ch__3, &n, &n, & c_n1, &c_n1, &c_n1, &imat, &nfail, &nerrs, nout); } dtrt03_(uplo, trans, diag, &n, &c__1, &a[1], &lda, &scale, &rwork[1], &c_b101, &b[n + 1], &lda, &x[1], &lda, &work[1], &result[8]); /* Print information about the tests that did not pass */ /* the threshold. */ if (result[7] >= *thresh) { if (nfail == 0 && nerrs == 0) { alahd_(nout, path); } io___40.ciunit = *nout; s_wsfe(&io___40); do_fio(&c__1, "DLATRS", (ftnlen)6); do_fio(&c__1, uplo, (ftnlen)1); do_fio(&c__1, trans, (ftnlen)1); do_fio(&c__1, diag, (ftnlen)1); do_fio(&c__1, "N", (ftnlen)1); do_fio(&c__1, (char *)&n, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&imat, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&c__8, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&result[7], (ftnlen)sizeof( doublereal)); e_wsfe(); ++nfail; } if (result[8] >= *thresh) { if (nfail == 0 && nerrs == 0) { alahd_(nout, path); } io___41.ciunit = *nout; s_wsfe(&io___41); do_fio(&c__1, "DLATRS", (ftnlen)6); do_fio(&c__1, uplo, (ftnlen)1); do_fio(&c__1, trans, (ftnlen)1); do_fio(&c__1, diag, (ftnlen)1); do_fio(&c__1, "Y", (ftnlen)1); do_fio(&c__1, (char *)&n, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&imat, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&c__9, (ftnlen)sizeof(integer)); do_fio(&c__1, (char *)&result[8], (ftnlen)sizeof( doublereal)); e_wsfe(); ++nfail; } nrun += 2; /* L90: */ } /* L100: */ } L110: ; } /* L120: */ } /* Print a summary of the results. */ alasum_(path, nout, &nfail, &nrun, &nerrs); return 0; /* End of DCHKTR */ } /* dchktr_ */
vpMatrix vpMatrix::inverseByQRLapack() const{ int rowNum_ = (int)this->getRows(); int colNum_ = (int)this->getCols(); int lda = (int)rowNum_; //lda is the number of rows because we don't use a submatrix int dimTau = std::min(rowNum_,colNum_); int dimWork = -1; double *tau = new double[dimTau]; double *work = new double[1]; int info; vpMatrix C; vpMatrix A = *this; try{ //1) Extract householder reflections (useful to compute Q) and R dgeqrf_( &rowNum_, //The number of rows of the matrix A. M >= 0. &colNum_, //The number of columns of the matrix A. N >= 0. A.data, /*On entry, the M-by-N matrix A. On exit, the elements on and above the diagonal of the array contain the min(M,N)-by-N upper trapezoidal matrix R (R is upper triangular if m >= n); the elements below the diagonal, with the array TAU, represent the orthogonal matrix Q as a product of min(m,n) elementary reflectors. */ &lda, //The leading dimension of the array A. LDA >= max(1,M). tau, /*Dimension (min(M,N)) The scalar factors of the elementary reflectors */ work, //Internal working array. dimension (MAX(1,LWORK)) &dimWork, //The dimension of the array WORK. LWORK >= max(1,N). &info //status ); if(info != 0){ std::cout << "dgeqrf_:Preparation:" << -info << "th element had an illegal value" << std::endl; throw vpMatrixException::badValue; } dimWork = allocate_work(&work); dgeqrf_( &rowNum_, //The number of rows of the matrix A. M >= 0. &colNum_, //The number of columns of the matrix A. N >= 0. A.data, /*On entry, the M-by-N matrix A. On exit, the elements on and above the diagonal of the array contain the min(M,N)-by-N upper trapezoidal matrix R (R is upper triangular if m >= n); the elements below the diagonal, with the array TAU, represent the orthogonal matrix Q as a product of min(m,n) elementary reflectors. */ &lda, //The leading dimension of the array A. LDA >= max(1,M). tau, /*Dimension (min(M,N)) The scalar factors of the elementary reflectors */ work, //Internal working array. dimension (MAX(1,LWORK)) &dimWork, //The dimension of the array WORK. LWORK >= max(1,N). &info //status ); if(info != 0){ std::cout << "dgeqrf_:" << -info << "th element had an illegal value" << std::endl; throw vpMatrixException::badValue; } //A now contains the R matrix in its upper triangular (in lapack convention) C = A; //2) Invert R dtrtri_((char*)"U",(char*)"N",&dimTau,C.data,&lda,&info); if(info!=0){ if(info < 0) std::cout << "dtrtri_:"<< -info << "th element had an illegal value" << std::endl; else if(info > 0){ std::cout << "dtrtri_:R("<< info << "," <<info << ")"<< " is exactly zero. The triangular matrix is singular and its inverse can not be computed." << std::endl; std::cout << "R=" << std::endl << C << std::endl; } throw vpMatrixException::badValue; } //3) Zero-fill R^-1 //the matrix is upper triangular for lapack but lower triangular for visp //we fill it with zeros above the diagonal (where we don't need the values) for(unsigned int i=0;i<C.getRows();i++) for(unsigned int j=0;j<C.getRows();j++) if(j>i) C[i][j] = 0.; dimWork = -1; int ldc = lda; //4) Transpose Q and left-multiply it by R^-1 //get R^-1*tQ //C contains R^-1 //A contains Q dormqr_((char*)"R", (char*)"T", &rowNum_, &colNum_, &dimTau, A.data, &lda, tau, C.data, &ldc, work, &dimWork, &info); if(info != 0){ std::cout << "dormqr_:Preparation"<< -info << "th element had an illegal value" << std::endl; throw vpMatrixException::badValue; } dimWork = allocate_work(&work); dormqr_((char*)"R", (char*)"T", &rowNum_, &colNum_, &dimTau, A.data, &lda, tau, C.data, &ldc, work, &dimWork, &info); if(info != 0){ std::cout << "dormqr_:"<< -info << "th element had an illegal value" << std::endl; throw vpMatrixException::badValue; } delete[] tau; delete[] work; }catch(vpMatrixException&){ delete[] tau; delete[] work; throw; } return C; }
int dpotri_(char *uplo, int *n, double *a, int * lda, int *info) { /* System generated locals */ int a_dim1, a_offset, i__1; /* Local variables */ extern int lsame_(char *, char *); extern int xerbla_(char *, int *), dlauum_( char *, int *, double *, int *, int *), dtrtri_(char *, char *, int *, double *, int *, int *); /* -- LAPACK routine (version 3.2) -- */ /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */ /* November 2006 */ /* .. Scalar Arguments .. */ /* .. */ /* .. Array Arguments .. */ /* .. */ /* Purpose */ /* ======= */ /* DPOTRI computes the inverse of a float symmetric positive definite */ /* matrix A using the Cholesky factorization A = U**T*U or A = L*L**T */ /* computed by DPOTRF. */ /* Arguments */ /* ========= */ /* UPLO (input) CHARACTER*1 */ /* = 'U': Upper triangle of A is stored; */ /* = 'L': Lower triangle of A is stored. */ /* N (input) INTEGER */ /* The order of the matrix A. N >= 0. */ /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */ /* On entry, the triangular factor U or L from the Cholesky */ /* factorization A = U**T*U or A = L*L**T, as computed by */ /* DPOTRF. */ /* On exit, the upper or lower triangle of the (symmetric) */ /* inverse of A, overwriting the input factor U or L. */ /* LDA (input) INTEGER */ /* The leading dimension of the array A. LDA >= MAX(1,N). */ /* INFO (output) INTEGER */ /* = 0: successful exit */ /* < 0: if INFO = -i, the i-th argument had an illegal value */ /* > 0: if INFO = i, the (i,i) element of the factor U or L is */ /* zero, and the inverse could not be computed. */ /* ===================================================================== */ /* .. External Functions .. */ /* .. */ /* .. External Subroutines .. */ /* .. */ /* .. Intrinsic Functions .. */ /* .. */ /* .. Executable Statements .. */ /* Test the input parameters. */ /* Parameter adjustments */ a_dim1 = *lda; a_offset = 1 + a_dim1; a -= a_offset; /* Function Body */ *info = 0; if (! lsame_(uplo, "U") && ! lsame_(uplo, "L")) { *info = -1; } else if (*n < 0) { *info = -2; } else if (*lda < MAX(1,*n)) { *info = -4; } if (*info != 0) { i__1 = -(*info); xerbla_("DPOTRI", &i__1); return 0; } /* Quick return if possible */ if (*n == 0) { return 0; } /* Invert the triangular Cholesky factor U or L. */ dtrtri_(uplo, "Non-unit", n, &a[a_offset], lda, info); if (*info > 0) { return 0; } /* Form inv(U)*inv(U)' or inv(L)'*inv(L). */ dlauum_(uplo, n, &a[a_offset], lda, info); return 0; /* End of DPOTRI */ } /* dpotri_ */