42 #ifndef STOKHOS_SDM_UTILS_HPP
43 #define STOKHOS_SDM_UTILS_HPP
52 #define DGEQP3_F77 F77_BLAS_MANGLE(dgeqp3,DGEQP3)
54 void DGEQP3_F77(
const int*,
const int*,
double*,
const int*,
int*,
55 double*,
double*,
const int*,
int*);
60 #ifdef HAVE_STOKHOS_MATLABLIB
72 template <
typename ordinal_type,
typename scalar_type>
86 template <
typename ordinal_type,
typename scalar_type>
95 x[i] = alpha*x[i] + beta*y[i];
101 template <
typename ordinal_type,
typename scalar_type>
111 os <<
";" << std::endl <<
" ";
113 os <<
"];" << std::endl;
116 #ifdef HAVE_STOKHOS_MATLABLIB
118 template <
typename ordinal_type>
120 save_matlab(
const std::string& file_name,
const std::string& matrix_name,
122 bool overwrite =
true)
130 MATFile *file = matOpen(file_name.c_str(), mode);
134 mxArray *mat = mxCreateDoubleMatrix(0, 0, mxREAL);
141 ordinal_type ret = matPutVariable(file, matrix_name.c_str(), mat);
145 ret = matClose(file);
157 template <
typename ordinal_type,
typename scalar_type>
170 template <
typename ordinal_type,
typename scalar_type>
178 using Teuchos::getCol;
183 SDM& Anc =
const_cast<SDM&
>(A);
202 Qj.
scale(1.0/R(j,j));
212 template <
typename ordinal_type,
typename scalar_type>
220 using Teuchos::getCol;
225 SDM& Anc =
const_cast<SDM&
>(A);
248 Qi.
scale(1.0/R(i,i));
257 template <
typename ordinal_type,
typename scalar_type>
265 using Teuchos::getCol;
270 SDM& Anc =
const_cast<SDM&
>(A);
299 Qi.
scale(1.0/R(i,i));
310 template <
typename ordinal_type,
typename scalar_type>
329 w[i] != 1.0, std::logic_error,
330 "CPQR_Householder_threshold() requires unit weight vector!");
342 lapack.
GEQRF(m, n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
344 info < 0, std::logic_error,
"geqrf returned info = " << info);
347 lapack.
GEQRF(m, n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
349 info < 0, std::logic_error,
"geqrf returned info = " << info);
363 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
365 info < 0, std::logic_error,
"orgqr returned info = " << info);
368 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
370 info < 0, std::logic_error,
"orgqr returned info = " << info);
391 template <
typename ordinal_type,
typename scalar_type>
422 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
429 TEUCHOS_TEST_FOR_EXCEPTION(
430 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
442 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
443 TEUCHOS_TEST_FOR_EXCEPTION(
444 info < 0, std::logic_error,
"orgqr returned info = " << info);
447 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
448 TEUCHOS_TEST_FOR_EXCEPTION(
449 info < 0, std::logic_error,
"orgqr returned info = " << info);
480 template <
typename ordinal_type,
typename scalar_type>
493 w[i] != 1.0, std::logic_error,
494 "CPQR_Householder_threshold() requires unit weight vector!");
504 for (rank=1; rank<m; rank++) {
509 if (r_min / r_max < rank_threshold)
532 template <
typename ordinal_type,
typename scalar_type>
542 using Teuchos::getCol;
563 SDV Qtmp(m), Rtmp(m);
572 if (piv_orig[
j] != 0) {
575 nrm(
j) = nrm(nfixed);
585 piv[
j] = piv[nfixed];
596 while (j < k && sigma >= rank_threshold) {
604 if (nrm(l) > nrm(pvt))
627 Qj.
scale(1.0/R(j,j));
645 if (sigma < rank_threshold)
665 template <
typename ordinal_type,
typename scalar_type>
680 QR_MGS(rank, A2, w, Q, R2);
686 template <
typename ordinal_type,
typename scalar_type>
710 template <
typename ordinal_type,
typename scalar_type>
721 Qt(i,
j) = w[i]*Q(i,
j);
733 template <
typename ordinal_type,
typename scalar_type>
752 template <
typename ordinal_type,
typename scalar_type>
778 template <
typename ordinal_type,
typename scalar_type>
791 AP(i,
j) = A(i,piv[
j]);
805 template <
typename ordinal_type,
typename scalar_type>
837 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
839 info < 0, std::logic_error,
"dgesvd returned info = " << info);
845 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
847 info < 0, std::logic_error,
"dgesvd returned info = " << info);
851 template <
typename ordinal_type,
typename scalar_type>
866 while (rank < m && s[rank]/s[0] > rank_threshold) rank++;
ordinal_type CPQR_MGS_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using modified Gram-Schmidt.
ScalarType * values() const
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
void ORGQR(const OrdinalType &m, const OrdinalType &n, const OrdinalType &k, ScalarType *A, const OrdinalType &lda, const ScalarType *TAU, ScalarType *WORK, const OrdinalType &lwork, OrdinalType *info) const
scalar_type residualQRError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute QR residual error.
ordinal_type CPQR_Householder_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using Householder reflections.
void print_matlab(std::ostream &os, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A)
void QR_CGS(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using classical Gram-Schmidt.
void QR_MGS(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using modified Gram-Schmidt.
scalar_type cond_R(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute condition number of upper-triangular R.
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
int scale(const ScalarType alpha)
void CPQR_Householder3(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using Householder reflections.
void saxpy(const scalar_type &alpha, Teuchos::SerialDenseVector< ordinal_type, scalar_type > &x, const scalar_type &beta, const Teuchos::SerialDenseVector< ordinal_type, scalar_type > &y)
Overwrite x with alpha*x + beta*y.
Teuchos::SerialDenseMatrix< int, pce_type > SDM
ordinal_type svd_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, Teuchos::Array< scalar_type > &s, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &U, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Vt)
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
Teuchos::SerialDenseVector< int, pce_type > SDV
scalar_type residualCPQRError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, const Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR residual error.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
scalar_type weightedQROrthogonalizationError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, const Teuchos::Array< scalar_type > &w)
Compute weighted QR orthogonalization error.
Specialization for Sacado::UQ::PCE< Storage<...> >
OrdinalType length() const
scalar_type QROrthogonalizationError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q)
Compute QR orthogonalization error.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
void resize(size_type new_size, const value_type &x=value_type())
void svd(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, Teuchos::Array< scalar_type > &s, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &U, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Vt)
Compute SVD of matrix.
scalar_type vec_norm_inf(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A)
Vector-infinity norm of a matrix.
OrdinalType numCols() const
ScalarTraits< ScalarType >::magnitudeType normInf() const
void GEQRF(const OrdinalType &m, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, ScalarType *TAU, ScalarType *WORK, const OrdinalType &lwork, OrdinalType *info) const
void GESVD(const char &JOBU, const char &JOBVT, const OrdinalType &m, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, MagnitudeType *S, ScalarType *U, const OrdinalType &ldu, ScalarType *V, const OrdinalType &ldv, ScalarType *WORK, const OrdinalType &lwork, MagnitudeType *RWORK, OrdinalType *info) const
void QR_MGS2(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using modified Gram-Schmidt with reorthogonalization.
int reshape(OrdinalType numRows, OrdinalType numCols)
scalar_type weighted_inner_product(const Teuchos::SerialDenseVector< ordinal_type, scalar_type > &x, const Teuchos::SerialDenseVector< ordinal_type, scalar_type > &y, const Teuchos::Array< scalar_type > &w)
Compute weighted inner product between vectors x and y.
int shape(OrdinalType numRows, OrdinalType numCols)
#define TEUCHOS_ASSERT(assertion_test)
void QR_Householder(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using Householder reflections.
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
OrdinalType stride() const
ordinal_type CPQR_MGS_reorthog_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using modified Gram-Schmidt and reorthogonalization.
OrdinalType numRows() const