LINEAR++ library: AFF to LAPACK
lsqexample.cc
Go to the documentation of this file.
1 
33 #define TF_LSQEXAMPLE_CC_VERSION \
34  "TF_LSQEXAMPLE_CC V1.0"
35 
36 #include <iostream>
37 
38 // provide interface to LAPACK
39 #include <linearxx/lapackxx.h>
40 // provide matrix and vector operators
41 #include <linearxx/operators.h>
42 
43 // provide iterators for matrix and vector containers
44 #include <aff/iterator.h>
45 // provide elementwise operators for matrix and vector containers
46 #include <aff/arrayoperators.h>
47 
48 // provide interface to random number generator
49 #include <tfxx/rng.h>
50 
51 using std::cout;
52 using std::cerr;
53 using std::endl;
54 
55 // define type of matrix container to be used
57 
58 // print matrix to cout
59 void printmatrix(const Tmatrix::Tcoc& m)
60 {
61  const int width=15;
62  const int precision=5;
63  cout.width(width);
64  cout << " ";
65  for (int i=m.first(1); i<=m.last(1); ++i)
66  {
67  cout.width(width);
68  cout << i;
69  }
70  cout << endl;
71  for (int j=m.first(0); j<=m.last(0); ++j)
72  {
73  cout.width(width);
74  cout << j;
75  for (int i=m.first(1); i<=m.last(1); ++i)
76  {
77  cout.width(width);
78  cout.precision(precision);
79  cout << m(j,i);
80  }
81  cout << endl;
82  }
83 } // void printmatrix(const Tmatrix::Tcoc& m)
84 
85 /* ====================================================================== */
86 
87 int main(int iargc, char* argv[])
88 {
89 
90  // size of matrix for test
91  const int n=3;
92  const int m=4;
93 
94  // set up random number generator
95  tfxx::numeric::RNGgaussian rng;
96 
97  // create matrix of partial derivatives
98  Tmatrix G(m,n);
99  // create data vector
100  Tmatrix d(m,1);
101  // create vector for given model parameters
102  Tmatrix mknown(n,1);
103 
104  // fill matrix with random numbers
105  // enclose code in block to make declarations local
106  {
107  aff::Iterator<Tmatrix> I(G);
108  while (I.valid())
109  {
110  *I = rng();
111  ++I;
112  }
113  }
114 
115  // fill vector with random numbers
116  // enclose code in block to make declarations local
117  {
118  aff::Iterator<Tmatrix> I(mknown);
119  while (I.valid())
120  {
121  *I = rng();
122  ++I;
123  }
124  }
125 
126  cout << TF_LSQEXAMPLE_CC_VERSION "\n" << endl;
127  cout << "Solve least squares problem" << endl;
128  cout << "===========================" << endl;
129 
130  cout << "\n"
131  "Residuals:\n"
132  "r = G * m - d\n"
133  "Find solution such that |r|^2 becomes minimal" << endl;
134 
135  // setup data vector
136  d=linear::op::dotNxM(G,mknown);
137 
138  // report system
139  cout << "\nMatrix of partial derivatives (G):\n";
140  printmatrix(G);
141  cout << "\nData vector (d):\n";
142  printmatrix(d);
143 
144  // setup system of linear equations
145  // --------------------------------
146  // system matrix
148  // right hand side
150 
151  cout << "\nSystem of linear equations to be solved:\n"
152  "M * m = rhs\n" << endl;
153 
154  cout << "With matrix M=G^T*G:\n";
155  printmatrix(M);
156  cout << "and vector rhs=G^T*d:\n";
157  printmatrix(rhs);
158 
159  // solve system
160  // ------------
161  // solution vetcor (optimal model parameters)
162  Tmatrix mopt=linear::lapack::dposv(M,rhs,'U',false);
163 
164  cout << "\nsolution vector mopt:\n";
165  printmatrix(mopt);
166  cout << "\nexpected solution (mknown):\n";
167  printmatrix(mknown);
168  cout << "\nresidual (mopt-mknown):\n";
169  printmatrix(mopt-mknown);
170 }
171 
172 /* ----- END OF lsqexample.cc ----- */
linear::TDmatrix Tmatrix
Definition: lsqexample.cc:56
#define TF_LSQEXAMPLE_CC_VERSION
Definition: lsqexample.cc:33
prototypes for C++ LAPACK interface functions (prototypes)
void printmatrix(const Tmatrix::Tcoc &m)
Definition: lsqexample.cc:59
aff::Array< double > TDmatrix
Definition: matrix.h:46
TDmatrix transposeNxM(const TDmatrix::Tcoc &A)
calculate transpose of NxM matrix.
Definition: transpose.cc:44
int main(int iargc, char *argv[])
Definition: lsqexample.cc:87
matrix and vector operators (prototypes)
TDmatrix dotNxM(const TDmatrix::Tcoc &A, const TDmatrix::Tcoc &B)
dot product for NxM matrices.
Definition: dot.cc:45
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