10 #ifndef ROL_STDLINEAROPERATORFACTORY_H
11 #define ROL_STDLINEAROPERATORFACTORY_H
29 class StdLinearOperatorFactory {
31 template <
typename T>
using ROL::Ptr = ROL::Ptr<T>;
33 typedef LinearOperator<Real>
OP;
34 typedef StdLinearOperator<Real> StdOP;
36 typedef std::vector<Real> vector;
41 Teuchos::BLAS<int,Real> blas_;
42 ROL::LAPACK<int,Real> lapack_;
45 void randomize( vector &x, Real lower=0.0, Real upper=1.0 ) {
47 for(
int i=0; i<N; ++i ) {
48 x[i] = lower+(upper-lower)*static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
52 void diagonal( vector &D,
const vector& d ) {
57 for(
int i=0; i<N; ++i ) {
63 void multiply( vector &C,
const vector &A,
const vector &B,
bool transA=
false,
bool transB=
false ) {
65 int N = (std::round(std::sqrt(N2)));
66 bool isSquare = N*N == N2;
67 ROL_TEST_FOR_EXCEPTION( !isSquare, std::invalid_argument,
68 "Error: vector representation A of matrix must have a square "
69 "number of elements.");
70 ROL_TEST_FOR_EXCEPTION( B.size() != N2, std::invalid_argument,
71 "Error: vectors A and B must have the same length.");
72 ROL_TEST_FOR_EXCEPTION( C.size() != N2, std::invalid_argument,
73 "Error: vectors A and C must have the same length.");
75 char tra = transA :
'T' :
'N';
76 char trb = transB :
'T' :
'N';
78 blas_.GEMM(tra,trb,N,N,N,1.0,&A[0],&B[0],N,0.0,N);
83 void orthogonalize( vector &A ) {
85 int N = (std::round(std::sqrt(N2)));
86 bool isSquare = N*N == N2;
87 ROL_TEST_FOR_EXCEPTION( !isSquare, std::invalid_argument,
88 "Error: vector representation of matrix must have a square "
89 "number of elements.");
98 lapack_.GEQRF(N,N,&A[0],LDA,&TAU[0],&LWORK1,-1,&INFO);
99 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK GEQRF LWORK query failed.");
101 lapack_.ORGQR(N,N,N,&A[0],LDA,&TAU[0],&LWORK2,-1,&INFO);
102 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK ORGQR LWORK query failed.");
104 const int LWORK = std::max(std::abs(LWORK1),std::abs(LWORK2));
109 lapack_.GEQRF(N,N,&A[0],LDA,&TAU[0],LWORK,&INFO);
110 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK GEQRF failed with INFO = " << INFO );
113 lapack_.ORGQR(N,N,N,&A[0],LDA,&TAU[0],&WORK[0],LWORK,&INFO);
114 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK ORGQR failed with INFO = " << INFO );
121 enum class EMatrixType unsigned {
131 ~StdLinearOperatorFactor(
void) {}
133 EMatrixType StringToEMatrixType( satd::string s ) {
135 for ( EMatrixType mt = MATRIX_SPD; mt < MATRIX_LAST; ++ ) {
141 std::string EMatrixTypeToString( EMatrixType mt ) {
142 std::string retString;
144 case MATRIX_SPD: retString =
"Symmetric Positive Definite";
break;
145 case MATRIX_SYMMETRIC: retString =
"Symmetric Indefinite";
break;
146 case MATRIX_UNITARY: retString =
"Unitary";
break;
147 case MATRIX_SINGULAR_S: retString =
"Singular Symmetric";
break;
148 case MATRIX_SINGILAR_N: retString =
"Singular Nonsymmetric";
break;
149 case MATRIX_DEFAULT: retString =
"Default";
break;
150 case MATRIX_LAST: retString =
"Last (dummy)";
break;
155 ROL::Ptr<LinearOperator<Real> >
getOperator(
int size,
const std::string &type=
"" )
const {
156 EMatrixType emt = StringToEMatrixType(type);
162 ROL::Ptr<vector> Ap = ROL::makePtr<vector>(n2);
168 randomize(d,1.0,2.0);
174 randomize(Q,-1.0,1.0);
181 multiply(*Ap,Q,*Ap,
true);
186 case MATRIX_SYMMETRIC: {
194 randomize(Q,-1.0,1.0);
201 multiply(*Ap,Q,*Ap,
true);
205 case MATRIX_UNITARY: {
210 case MATRIX_SINGULAR_S: {
220 randomize(Q,-1.0,1.0);
227 multiply(*Ap,Q,*Ap,
true);
230 case MATRIX_SINGULAR_N: {
233 randomize(d,0.0,1.0);
241 randomize(
V,-1.0,1.0);
245 multiply(*Ap,*Ap,Q,
false,
true);
248 randomize(U,-1.0,1.0);
262 return ROL::makePtr<StdOP>(Ap);
272 #endif // ROL_STDLINEAROPERATORFACTORY_H
ROL::Ptr< LinearOperator< Real > > getOperator(int row, int col) const
std::string removeStringFormat(std::string s)
LinearOperator< Real > OP
std::string EStepToString(EStep tr)