10 #ifndef STOKHOS_SDM_UTILS_HPP
11 #define STOKHOS_SDM_UTILS_HPP
20 #define DGEQP3_F77 F77_BLAS_MANGLE(dgeqp3,DGEQP3)
23 #if defined(HAVE_STOKHOS_MKL)
24 #include "mkl_version.h"
25 #if __INTEL_MKL__ >= 2021
26 #define MKL_NO_EXCEPT noexcept
34 void DGEQP3_F77(
const int*,
const int*,
double*,
const int*,
int*,
38 #include "Stokhos_ConfigDefs.h"
40 #ifdef HAVE_STOKHOS_MATLABLIB
52 template <
typename ordinal_type,
typename scalar_type>
66 template <
typename ordinal_type,
typename scalar_type>
75 x[i] = alpha*x[i] + beta*y[i];
81 template <
typename ordinal_type,
typename scalar_type>
91 os <<
";" << std::endl <<
" ";
93 os <<
"];" << std::endl;
96 #ifdef HAVE_STOKHOS_MATLABLIB
98 template <
typename ordinal_type>
100 save_matlab(
const std::string& file_name,
const std::string& matrix_name,
102 bool overwrite =
true)
110 MATFile *file = matOpen(file_name.c_str(), mode);
114 mxArray *mat = mxCreateDoubleMatrix(0, 0, mxREAL);
121 ordinal_type ret = matPutVariable(file, matrix_name.c_str(), mat);
125 ret = matClose(file);
137 template <
typename ordinal_type,
typename scalar_type>
150 template <
typename ordinal_type,
typename scalar_type>
158 using Teuchos::getCol;
163 SDM& Anc =
const_cast<SDM&
>(A);
182 Qj.
scale(1.0/R(j,j));
192 template <
typename ordinal_type,
typename scalar_type>
200 using Teuchos::getCol;
205 SDM& Anc =
const_cast<SDM&
>(A);
228 Qi.
scale(1.0/R(i,i));
237 template <
typename ordinal_type,
typename scalar_type>
245 using Teuchos::getCol;
250 SDM& Anc =
const_cast<SDM&
>(A);
279 Qi.
scale(1.0/R(i,i));
290 template <
typename ordinal_type,
typename scalar_type>
309 w[i] != 1.0, std::logic_error,
310 "CPQR_Householder_threshold() requires unit weight vector!");
322 lapack.
GEQRF(m, n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
324 info < 0, std::logic_error,
"geqrf returned info = " << info);
327 lapack.
GEQRF(m, n, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
329 info < 0, std::logic_error,
"geqrf returned info = " << info);
343 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
345 info < 0, std::logic_error,
"orgqr returned info = " << info);
348 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
350 info < 0, std::logic_error,
"orgqr returned info = " << info);
371 template <
typename ordinal_type,
typename scalar_type>
402 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
409 TEUCHOS_TEST_FOR_EXCEPTION(
410 info < 0, std::logic_error,
"dgeqp3 returned info = " << info);
422 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
423 TEUCHOS_TEST_FOR_EXCEPTION(
424 info < 0, std::logic_error,
"orgqr returned info = " << info);
427 lapack.
ORGQR(m, k, k, AA.
values(), lda, &tau[0], &work[0], lwork, &info);
428 TEUCHOS_TEST_FOR_EXCEPTION(
429 info < 0, std::logic_error,
"orgqr returned info = " << info);
460 template <
typename ordinal_type,
typename scalar_type>
473 w[i] != 1.0, std::logic_error,
474 "CPQR_Householder_threshold() requires unit weight vector!");
484 for (rank=1; rank<m; rank++) {
489 if (r_min / r_max < rank_threshold)
512 template <
typename ordinal_type,
typename scalar_type>
522 using Teuchos::getCol;
543 SDV Qtmp(m), Rtmp(m);
552 if (piv_orig[
j] != 0) {
555 nrm(
j) = nrm(nfixed);
565 piv[
j] = piv[nfixed];
576 while (j < k && sigma >= rank_threshold) {
584 if (nrm(l) > nrm(pvt))
607 Qj.
scale(1.0/R(j,j));
625 if (sigma < rank_threshold)
645 template <
typename ordinal_type,
typename scalar_type>
660 QR_MGS(rank, A2, w, Q, R2);
666 template <
typename ordinal_type,
typename scalar_type>
690 template <
typename ordinal_type,
typename scalar_type>
701 Qt(i,
j) = w[i]*Q(i,
j);
713 template <
typename ordinal_type,
typename scalar_type>
732 template <
typename ordinal_type,
typename scalar_type>
758 template <
typename ordinal_type,
typename scalar_type>
771 AP(i,
j) = A(i,piv[
j]);
785 template <
typename ordinal_type,
typename scalar_type>
817 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
819 info < 0, std::logic_error,
"dgesvd returned info = " << info);
825 Vt.
values(), ldv, &work[0], lwork, NULL, &info);
827 info < 0, std::logic_error,
"dgesvd returned info = " << info);
831 template <
typename ordinal_type,
typename scalar_type>
846 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