LINEAR++ library: AFF to LAPACK
lapacktest.cc
Go to the documentation of this file.
1 
36 #define LAPACKTEST_VERSION \
37  "LAPACKTEST V1.1 Test interfaced LAPACK functions"
38 
39 #include <iostream>
40 #include <tfxx/commandline.h>
41 #include <linearxx/lapackxx.h>
42 #include <linearxx/operators.h>
43 #include <aff/iterator.h>
44 #include <aff/arrayoperators.h>
45 #include <tfxx/rng.h>
46 
47 using std::cout;
48 using std::cerr;
49 using std::endl;
50 
51 struct Options {
52  bool verbose;
53  int nsize;
54 }; // struct Options
55 
57 
58 void dump(const Tmatrix::Tcoc& m)
59 {
60  const int width=15;
61  const int precision=5;
62  cout.width(width);
63  cout << " ";
64  for (int i=m.first(1); i<=m.last(1); ++i)
65  {
66  cout.width(width);
67  cout << i;
68  }
69  cout << endl;
70  for (int j=m.first(0); j<=m.last(0); ++j)
71  {
72  cout.width(width);
73  cout << j;
74  for (int i=m.first(1); i<=m.last(1); ++i)
75  {
76  cout.width(width);
77  cout.precision(precision);
78  cout << m(j,i);
79  }
80  cout << endl;
81  }
82 } // void dump(const Tmatrix::Tcoc& m)
83 
84 #define DUMP( M ) cout << #M << ":" << endl; dump(M);
85 
86 /*----------------------------------------------------------------------*/
87 
88 // test a system defined by a matrix and a vector
89 void test(const Tmatrix::Tcoc& M, const Tmatrix::Tcoc V)
90 {
91  cout << endl;
92  cout << "test function" << endl
93  << "-------------" << endl;
94  cout << "input matrix and vector:" << endl;
95  DUMP(M);
96  DUMP(V);
98  cout << "positive definite system matrix: " << endl;
99  DUMP(A);
101  cout << "right hand side vector: " << endl;
102  DUMP(B);
103  Tmatrix X=linear::lapack::dposv(A,B,'U',false);
104  cout << "solution to system of linear equations: " << endl;
105  DUMP(X);
106  cout << "expected solution: " << endl;
107  DUMP(V);
108  DUMP(X-V);
109 }
110 
111 /*======================================================================*/
112 
113 int main(int iargc, char* argv[])
114 {
115 
116  // define usage information
117  char usage_text[]=
118  {
119  LAPACKTEST_VERSION "\n"
120  "usage: lapacktest [-n n]" "\n"
121  " or: lapacktest --help|-h" "\n"
122  };
123 
124  // define full help text
125  char help_text[]=
126  {
127  "\n"
128  "-v be verbose" "\n"
129  "-n N test system of size NxN" "\n"
130  };
131 
132  // define commandline options
133  using namespace tfxx::cmdline;
134  static Declare options[]=
135  {
136  // 0: print help
137  {"help",arg_no,"-"},
138  // 1: verbose mode
139  {"v",arg_no,"-"},
140  // 2: verbose mode
141  {"n",arg_yes,"3"},
142  {NULL}
143  };
144 
145  // no arguments? print usage...
146  if (iargc<2)
147  {
148  cerr << usage_text << endl;
149  exit(0);
150  }
151 
152  // collect options from commandline
153  Commandline cmdline(iargc, argv, options);
154 
155  // help requested? print full help text...
156  if (cmdline.optset(0))
157  {
158  cerr << usage_text << endl;
159  cerr << help_text << endl;
160  exit(0);
161  }
162 
163  /*
164  // dummy operation: print option settings
165  for (int iopt=0; iopt<2; iopt++)
166  {
167  cout << "option: '" << options[iopt].opt_string << "'" << endl;
168  if (cmdline.optset(iopt)) { cout << " option was set"; }
169  else { cout << "option was not set"; }
170  cout << endl;
171  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
172  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
173  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
174  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
175  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
176  cout << " argument (bool): '";
177  if (cmdline.bool_arg(iopt))
178  { cout << "true"; } else { cout << "false"; }
179  cout << "'" << endl;
180  }
181  while (cmdline.extra()) { cout << cmdline.next() << endl; }
182 
183  // dummy operation: print rest of command line
184  while (cmdline.extra()) { cout << cmdline.next() << endl; }
185  */
186 
187  Options opt;
188  opt.verbose=cmdline.optset(1);
189  opt.nsize=cmdline.int_arg(2);
190 
191  /*----------------------------------------------------------------------*/
192 
193  {
194 
195  if (opt.verbose)
196  {
197  cout << "Define system of size " << opt.nsize << "x" << opt.nsize << endl;
198  }
199 
200  Tmatrix M(opt.nsize,opt.nsize);
201  Tmatrix R(opt.nsize,1);
202 
203  // set up random number generator
204  tfxx::numeric::RNGgaussian rng;
205 
206  {
207  // set up system matrix
208  aff::Iterator<Tmatrix> I(M);
209  while (I.valid())
210  {
211  *I = rng();
212  ++I;
213  }
214  }
215 
216  {
217  // set up system vector
218  aff::Iterator<Tmatrix> I(R);
219  while (I.valid())
220  {
221  *I = rng();
222  ++I;
223  }
224  }
225  test(M,R);
226  }
227 
228  /*----------------------------------------------------------------------*/
229 
230  {
231  Tmatrix M(3,3), V(3,1), W(3,2);
232  M(1,1)=1.;
233  M(1,2)=0.4;
234  M(1,3)=0.6;
235  M(2,1)=-0.2;
236  M(2,2)=-1.;
237  M(2,3)=0.7;
238  M(3,1)=-1.;
239  M(3,2)=-0.3;
240  M(3,3)=1.;
241  DUMP(M);
242  V(1)=-80.;
243  V(2)=70.;
244  V(3)=-35.;
245  DUMP(V);
246  test(M,V);
247  W(1,1)=-80.;
248  W(2,1)=70.;
249  W(3,1)=-35.;
250  W(1,2)=0.;
251  W(2,2)=2.;
252  W(3,2)=-3.;
253  DUMP(W);
254  test(M,W);
255  }
256 }
257 
258 /* ----- END OF lapacktest.cc ----- */
void dump(const Tmatrix::Tcoc &m)
Definition: lapacktest.cc:58
linear::TDmatrix Tmatrix
Definition: lsqexample.cc:56
#define DUMP(M)
Definition: lapacktest.cc:84
prototypes for C++ LAPACK interface functions (prototypes)
aff::Array< double > TDmatrix
Definition: matrix.h:46
#define LAPACKTEST_VERSION
Definition: lapacktest.cc:36
TDmatrix transposeNxM(const TDmatrix::Tcoc &A)
calculate transpose of NxM matrix.
Definition: transpose.cc:44
int nsize
Definition: lapacktest.cc:53
matrix and vector operators (prototypes)
bool verbose
Definition: lapacktest.cc:52
TDmatrix dotNxM(const TDmatrix::Tcoc &A, const TDmatrix::Tcoc &B)
dot product for NxM matrices.
Definition: dot.cc:45
linear::TDmatrix Tmatrix
Definition: lapacktest.cc:56
void test(const Tmatrix::Tcoc &M, const Tmatrix::Tcoc V)
Definition: lapacktest.cc:89
void dposv(char UPLO, TDmatrix &A, TDmatrix &B, int &INFO, const bool &debug)
Compute the solution to a real system of linear equations A * X = B.
Definition: dposv_if.cc:87
int main(int iargc, char *argv[])
Definition: lapacktest.cc:113