typedef VectDse<double> vect_t;
typedef VectDse<double,linalg::rep1d<double> > vect_t;
synaps/linalg/VectDse.h
for more details on the implementation of this view.A vector can be constructed and manipulated as follows:
double u[]={1,-2,1}; vect_t V(3,u);
vect_t W= V*2, U; U -= W+3*V;
#include <iostream> #include <synaps/linalg/lapack.h> #include <synaps/linalg/MatrDse.h> #include <synaps/linalg/Eigen.h> #include <synaps/linalg/Svd.h> typedef VectDse<double> vect_t; typedef MatrDse<double,lapack::rep2d<double> > matr_t; int main(int argc, char** argv) { using std::cout; using std::endl; double u[]={1,-2,1}; vect_t V(3,u); vect_t W= V*2, U; U -= W+3*V; cout<<U<<endl; cout<<V<<endl; cout<<W<<endl; }
typedef MatrDse<double> matr_t;
typedef MatrDse<double,lapack::rep2d<double> > matr_t;
#include <synaps/linalg/lapack.h>
More details on the implementation of the matrix interface can be found in the header file synaps/linalg/MatrDse.h
.
Here are some illustrations of its use:
matr_t A(3,3,"1 2 3 4 5 6 7 8 9 10",ByRow());
The usual operations on matrices and vectors are available from this type:
using std::cout; using std::endl; vect_t W = A*V; cout << W <<endl;
matr_t B = A*A; cout << B << endl;
matr_t C(-A); C += B + A*2; cout <<C<< endl;
#include <synaps/linalg/Svd.h>
#include <synaps/linalg/Eigen.h>
are included. Some implementation (such as Svd) are also available for generic matrices.
Here the type of the eigenvalues is complex<double>. They are printed with real and imaginary part between parentheses.
#include <synaps/init.h> #ifdef SYNAPS_WITH_LAPACK #include <iostream> #include <synaps/linalg/lapack.h> #include <synaps/linalg/MatrDse.h> #include <synaps/linalg/Eigen.h> #include <synaps/linalg/Svd.h> typedef VectDse<double> vect_t; typedef MatrDse<double,lapack::rep2d<double> > matr_t; int main(int argc, char** argv) { using std::cout; using std::endl; matr_t A(3,3,"1 2 3 4 5 6 7 8 9",ByRow()); cout<<A<<endl; cout<< Svd(A)<<endl; cout<< Eigenval(A)<<endl; } #else int main(int argc, char** argv) {} #endif //SYNAPS_WITH_LAPACK
Exercise: Estimate the mean time for computing the svd of a nxn matrix by iterating the computation on N random matrices with coefficients between -1 and 1 (using the routine 1-(2.0*rand())/RAND MAX of stdlib.h). Compare it with the representation using lapack routine. Compare with different compilation options. To count the time, we will use the instructions {{Clock c; c.start(); ...; c.stop(); c.time();}} provided by the file synaps/base/Clock.h. |
Col(A,1) = Col(A,0);
into:
Similarly
Row(A,1) -= Row(A,0)*4;
These functions Col and Row produce vector views on the matrix. They can be interprated as references on the internal data. Remember that the memory space of the views Row(A,i), Col(A,j) are not duplicated but shared with the matrix A. Such a vector view can be copied into a standard vector:
vect_t V1 = Col(A,1)*2, V2= Row(A,0)+ Row(A,1); vect_t W = V1+V2;
#include <iostream> #include <synaps/linalg/MatrDse.h> #include <synaps/linalg/lapack.h> #include <synaps/linalg/Eigen.h> #include <synaps/linalg/Svd.h> typedef VectDse<double> vect_t; typedef MatrDse<double,lapack::rep2d<double> > matr_t; int main(int argc, char** argv) { using std::cout; using std::endl; matr_t A(3,3,"1 2 3 4 5 6 7 8 9",ByRow()); std::cout<<A<<std::endl; col(A,1) = col(A,0); std::cout<<A<<std::endl; row(A,1) -= row(A,0)*4; std::cout<<A<<std::endl; // vect_t V1 = col(A,1)*2; // std::cout<<V1<<std::endl; vect_t V2= row(A,0)+ row(A,1); std::cout<<V2<<std::endl; vect_t W = V2+V2; std::cout<<W<<std::endl; }