45 #ifndef ROL_STDLINEAROPERATORFACTORY_H
46 #define ROL_STDLINEAROPERATORFACTORY_H
64 class StdLinearOperatorFactory {
66 template <
typename T>
using ROL::Ptr = ROL::Ptr<T>;
68 typedef LinearOperator<Real>
OP;
69 typedef StdLinearOperator<Real> StdOP;
71 typedef std::vector<Real> vector;
76 Teuchos::BLAS<int,Real> blas_;
77 ROL::LAPACK<int,Real> lapack_;
80 void randomize( vector &x, Real lower=0.0, Real upper=1.0 ) {
82 for(
int i=0; i<N; ++i ) {
83 x[i] = lower+(upper-lower)*static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
87 void diagonal( vector &D,
const vector& d ) {
92 for(
int i=0; i<N; ++i ) {
98 void multiply( vector &C,
const vector &A,
const vector &B,
bool transA=
false,
bool transB=
false ) {
100 int N = (std::round(std::sqrt(N2)));
101 bool isSquare = N*N == N2;
102 ROL_TEST_FOR_EXCEPTION( !isSquare, std::invalid_argument,
103 "Error: vector representation A of matrix must have a square "
104 "number of elements.");
105 ROL_TEST_FOR_EXCEPTION( B.size() != N2, std::invalid_argument,
106 "Error: vectors A and B must have the same length.");
107 ROL_TEST_FOR_EXCEPTION( C.size() != N2, std::invalid_argument,
108 "Error: vectors A and C must have the same length.");
110 char tra = transA :
'T' :
'N';
111 char trb = transB :
'T' :
'N';
113 blas_.GEMM(tra,trb,N,N,N,1.0,&A[0],&B[0],N,0.0,N);
118 void orthogonalize( vector &A ) {
120 int N = (std::round(std::sqrt(N2)));
121 bool isSquare = N*N == N2;
122 ROL_TEST_FOR_EXCEPTION( !isSquare, std::invalid_argument,
123 "Error: vector representation of matrix must have a square "
124 "number of elements.");
133 lapack_.GEQRF(N,N,&A[0],LDA,&TAU[0],&LWORK1,-1,&INFO);
134 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK GEQRF LWORK query failed.");
136 lapack_.ORGQR(N,N,N,&A[0],LDA,&TAU[0],&LWORK2,-1,&INFO);
137 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK ORGQR LWORK query failed.");
139 const int LWORK = std::max(std::abs(LWORK1),std::abs(LWORK2));
144 lapack_.GEQRF(N,N,&A[0],LDA,&TAU[0],LWORK,&INFO);
145 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK GEQRF failed with INFO = " << INFO );
148 lapack_.ORGQR(N,N,N,&A[0],LDA,&TAU[0],&WORK[0],LWORK,&INFO);
149 ROL_TEST_FOR_EXCEPTION(INFO,std::logic_error,
"LAPACK ORGQR failed with INFO = " << INFO );
156 enum class EMatrixType unsigned {
166 ~StdLinearOperatorFactor(
void) {}
168 EMatrixType StringToEMatrixType( satd::string s ) {
170 for ( EMatrixType mt = MATRIX_SPD; mt < MATRIX_LAST; ++ ) {
176 std::string EMatrixTypeToString( EMatrixType mt ) {
177 std::string retString;
179 case MATRIX_SPD: retString =
"Symmetric Positive Definite";
break;
180 case MATRIX_SYMMETRIC: retString =
"Symmetric Indefinite";
break;
181 case MATRIX_UNITARY: retString =
"Unitary";
break;
182 case MATRIX_SINGULAR_S: retString =
"Singular Symmetric";
break;
183 case MATRIX_SINGILAR_N: retString =
"Singular Nonsymmetric";
break;
184 case MATRIX_DEFAULT: retString =
"Default";
break;
185 case MATRIX_LAST: retString =
"Last (dummy)";
break;
190 ROL::Ptr<LinearOperator<Real> >
getOperator(
int size,
const std::string &type=
"" )
const {
191 EMatrixType emt = StringToEMatrixType(type);
197 ROL::Ptr<vector> Ap = ROL::makePtr<vector>(n2);
203 randomize(d,1.0,2.0);
209 randomize(Q,-1.0,1.0);
216 multiply(*Ap,Q,*Ap,
true);
221 case MATRIX_SYMMETRIC: {
229 randomize(Q,-1.0,1.0);
236 multiply(*Ap,Q,*Ap,
true);
240 case MATRIX_UNITARY: {
245 case MATRIX_SINGULAR_S: {
255 randomize(Q,-1.0,1.0);
262 multiply(*Ap,Q,*Ap,
true);
265 case MATRIX_SINGULAR_N: {
268 randomize(d,0.0,1.0);
276 randomize(
V,-1.0,1.0);
280 multiply(*Ap,*Ap,Q,
false,
true);
283 randomize(U,-1.0,1.0);
297 return ROL::makePtr<StdOP>(Ap);
307 #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)