LINEAR++ library: AFF to LAPACK

◆ main()

int main ( int  iargc,
char *  argv[] 
)

Definition at line 87 of file lsqexample.cc.

References linear::op::dotNxM(), linear::lapack::dposv(), printmatrix(), TF_LSQEXAMPLE_CC_VERSION, and linear::op::transposeNxM().

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 }
linear::TDmatrix Tmatrix
Definition: lsqexample.cc:56
#define TF_LSQEXAMPLE_CC_VERSION
Definition: lsqexample.cc:33
void printmatrix(const Tmatrix::Tcoc &m)
Definition: lsqexample.cc:59
TDmatrix transposeNxM(const TDmatrix::Tcoc &A)
calculate transpose of NxM matrix.
Definition: transpose.cc:44
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
Here is the call graph for this function: