42 #ifndef STOKHOS_SDM_UTILS_HPP 43 #define STOKHOS_SDM_UTILS_HPP 52 #define DGEQPF_F77 F77_BLAS_MANGLE(dgeqpf,DGEQPF) 53 #define DGEQP3_F77 F77_BLAS_MANGLE(dgeqp3,DGEQP3) 55 void DGEQPF_F77(
const int*,
const int*,
double*,
const int*,
int*,
double*,
57 void DGEQP3_F77(
const int*,
const int*,
double*,
const int*,
int*,
58 double*,
double*,
const int*,
int*);
63 #ifdef HAVE_STOKHOS_MATLABLIB 75 template <
typename ordinal_type,
typename scalar_type>
89 template <
typename ordinal_type,
typename scalar_type>
98 x[i] = alpha*
x[i] + beta*
y[i];
104 template <
typename ordinal_type,
typename scalar_type>
113 if (i <
A.numRows()-1)
114 os <<
";" << std::endl <<
" ";
116 os <<
"];" << std::endl;
119 #ifdef HAVE_STOKHOS_MATLABLIB 121 template <
typename ordinal_type>
123 save_matlab(
const std::string& file_name,
const std::string& matrix_name,
125 bool overwrite =
true)
133 MATFile *file = matOpen(file_name.c_str(), mode);
137 mxArray *mat = mxCreateDoubleMatrix(0, 0, mxREAL);
139 mxSetPr(mat,
A.values());
140 mxSetM(mat,
A.numRows());
141 mxSetN(mat,
A.numCols());
144 ordinal_type ret = matPutVariable(file, matrix_name.c_str(), mat);
148 ret = matClose(file);
160 template <
typename ordinal_type,
typename scalar_type>
173 template <
typename ordinal_type,
typename scalar_type>
181 using Teuchos::getCol;
186 SDM& Anc =
const_cast<SDM&
>(
A);
215 template <
typename ordinal_type,
typename scalar_type>
223 using Teuchos::getCol;
228 SDM& Anc =
const_cast<SDM&
>(
A);
251 Qi.
scale(1.0/R(i,i));
260 template <
typename ordinal_type,
typename scalar_type>
268 using Teuchos::getCol;
273 SDM& Anc =
const_cast<SDM&
>(
A);
302 Qi.
scale(1.0/R(i,i));
313 template <
typename ordinal_type,
typename scalar_type>
332 w[i] != 1.0, std::logic_error,
333 "CPQR_Householder_threshold() requires unit weight vector!");
345 lapack.
GEQRF(m,
n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
347 info < 0, std::logic_error,
"geqrf returned info = " << info);
350 lapack.
GEQRF(m,
n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
352 info < 0, std::logic_error,
"geqrf returned info = " << info);
366 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
368 info < 0, std::logic_error,
"orgqr returned info = " << info);
371 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
373 info < 0, std::logic_error,
"orgqr returned info = " << info);
393 template <
typename ordinal_type,
typename scalar_type>
422 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
434 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
436 info < 0, std::logic_error,
"orgqr returned info = " << info);
439 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
441 info < 0, std::logic_error,
"orgqr returned info = " << info);
466 template <
typename ordinal_type,
typename scalar_type>
497 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
505 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
517 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
519 info < 0, std::logic_error,
"orgqr returned info = " << info);
522 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
524 info < 0, std::logic_error,
"orgqr returned info = " << info);
555 template <
typename ordinal_type,
typename scalar_type>
568 w[i] != 1.0, std::logic_error,
569 "CPQR_Householder_threshold() requires unit weight vector!");
579 for (rank=1; rank<m; rank++) {
584 if (r_min / r_max < rank_threshold)
607 template <
typename ordinal_type,
typename scalar_type>
617 using Teuchos::getCol;
638 SDV Qtmp(m), Rtmp(m);
647 if (piv_orig[
j] != 0) {
650 nrm(
j) = nrm(nfixed);
660 piv[
j] = piv[nfixed];
671 while (j < k && sigma >= rank_threshold) {
679 if (nrm(l) > nrm(pvt))
720 if (sigma < rank_threshold)
740 template <
typename ordinal_type,
typename scalar_type>
755 QR_MGS(rank, A2, w, Q, R2);
761 template <
typename ordinal_type,
typename scalar_type>
785 template <
typename ordinal_type,
typename scalar_type>
796 Qt(i,
j) = w[i]*Q(i,
j);
808 template <
typename ordinal_type,
typename scalar_type>
827 template <
typename ordinal_type,
typename scalar_type>
853 template <
typename ordinal_type,
typename scalar_type>
866 AP(i,
j) =
A(i,piv[
j]);
880 template <
typename ordinal_type,
typename scalar_type>
912 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
914 info < 0, std::logic_error,
"dgesvd returned info = " << info);
920 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
922 info < 0, std::logic_error,
"dgesvd returned info = " << info);
926 template <
typename ordinal_type,
typename scalar_type>
941 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.
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
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.
ScalarType * values() const
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.
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
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)
void GEQRF(const OrdinalType m, const OrdinalType n, ScalarType *A, const OrdinalType lda, ScalarType *TAU, ScalarType *WORK, const OrdinalType lwork, OrdinalType *info) const
ScalarTraits< ScalarType >::magnitudeType normInf() const
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.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
OrdinalType numRows() const
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.
Top-level namespace for Stokhos classes and functions.
Specialization for Sacado::UQ::PCE< Storage<...> >
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.
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
const IndexType const IndexType const IndexType const IndexType * Aj
void CPQR_Householder(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 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)
OrdinalType stride() const
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)
OrdinalType numCols() const
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)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
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.