3
0
mirror of https://github.com/triqs/dft_tools synced 2025-01-12 05:58:18 +01:00

more complete cookbook for det_manip

This commit is contained in:
Laura Messio 2013-12-18 13:30:17 +01:00
parent 477f5f4ea1
commit dbe2224caf
2 changed files with 205 additions and 55 deletions

View File

@ -1,7 +1,5 @@
Basics
------
Here, an exemple of creation of a class det_manip, of use of insert and remove.
Creation of an empty det_manip class
-------------------------------------
.. compileblock::
@ -9,58 +7,204 @@ Here, an exemple of creation of a class det_manip, of use of insert and remove.
struct fun {
typedef double result_type;
typedef double argument_type;
typedef double result_type;
typedef double argument_type;
//gives the coefficients of the matrix (function F of the documentation)
double operator()(double x, double y) const {
return(x-y);
}
//gives the coefficients of the matrix
double operator()(double x, double y) const {
return(x-y);
}
};
int main() {
//creation of the class det_manip
fun f;
int init_size=100;
triqs::det_manip::det_manip<fun> D(f,init_size);
int init_size = 100; // maximum size of the matrix before a resize
//creation of a class det_manip
triqs::det_manip::det_manip<fun> D(f, init_size);
//the initial matrix is empty:
std::cout<<std::endl<<"After construction: D.matrix()="<<D.matrix()<<std::endl<<std::endl;
std::cout<<std::endl<< "After construction: D.matrix()=" << D.matrix()<<std::endl<<std::endl;
double detratio;
//insertion of a line and a column at position (1,1) in the matrix, with x[i]=x, y[j]=y.
double x=2, y=9;
int i=0, j=0;
detratio = D.try_insert(i,j,x,y);
D.complete_operation();
std::cout<<"We add a line and a column for i="<<i<<", j="<<j<<", x="<<x<<", y="<<y<<" (f(x,y)="<<f(x,y)<<")."<<std::endl;
std::cout<<"D.matrix()="<<D.matrix()<<std::endl<<std::endl;
x=3; y=4; i=0; j=0;
detratio = D.try_insert(i,j,x,y);
D.complete_operation();
std::cout<<"We add a line and a column for i="<<i<<", j="<<j<<", x="<<x<<", y="<<y<<" (f(x,y)="<<f(x,y)<<")."<<std::endl;
std::cout<<"D.matrix()="<<D.matrix()<<std::endl;
std::cout<<"The determinant is "<<D.determinant()<<std::endl;
std::cout<<"The inverse matrix is"<<D.inverse_matrix()<< std::endl<<std::endl;
x=-7; y=1; i=1; j=2;
detratio = D.try_insert(i,j,x,y);
D.complete_operation();
std::cout<<"We add a line and a column for i="<<i<<", j="<<j<<", x="<<x<<", y="<<y<<" (f(x,y)="<<f(x,y)<<")."<<std::endl;
std::cout<<"D.matrix()="<<D.matrix()<<std::endl<<std::endl;
std::cout<<"The size of the matrix is now "<<D.size()<<std::endl;
std::cout<<"The value of the parameters for coefficient (i,j)=(0,2) is (x,y)=("<<D.get_x(0)<<","<<D.get_y(2)<<") (f("<<D.get_x(0)<<","<<D.get_y(2)<<")="<<f(D.get_x(0),D.get_y(2))<<")."<<std::endl<<std::endl;
i=0; j=1;
detratio = D.try_remove(i,j);
D.complete_operation();
std::cout<<"We remove a line and a column for i="<<i<<", j="<<j<<"."<<std::endl;
std::cout<<"D.matrix()="<<D.matrix()<<std::endl;
std::cout<<"The determinant is "<<D.determinant()<<std::endl;
std::cout<<"The inverse matrix is"<<D.inverse_matrix()<< std::endl<<std::endl;
}
Creation of a non empty det_manip class
----------------------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
//gives the coefficients of the matrix (function F of the documentation)
double operator()(double x, double y) const {
return(x-y);
}
};
int main() {
fun f;
std::vector<double> initial_x{1,2}, initial_y{3,4};
//creation of a class det_manip with a 2 by 2 matrix
triqs::det_manip::det_manip<fun> D(f, initial_x, initial_y);
//the initial matrix:
std::cout<<std::endl<< "After construction: D.matrix()=" << D.matrix()<<std::endl<<std::endl;
}
Get informations about a det_manip class
-----------------------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
double operator()(double x, double y) const { return(x-y); }
};
int main() {
fun f;
int i=0, j=1;
std::vector<double> initial_x{1,2}, initial_y{3,4};
triqs::det_manip::det_manip<fun> D(f, initial_x, initial_y);
std::cout<<std::endl<<"D.matrix()=" << D.matrix() <<std::endl<<std::endl;
std::cout<<"The size of the matrix is "<< D.size() <<std::endl<<std::endl;
std::cout<<"The determinant is "<< D.determinant() <<std::endl<<std::endl;
std::cout<<"The inverse matrix is"<< D.inverse_matrix() << std::endl<<std::endl;
std::cout<<"The value of the parameters for coefficient (i,j)=("<<i<<","<<j<<") is (x,y)=("
<<D.get_x(i)<<","<<D.get_y(j)<<")"<<std::endl<<std::endl;
}
Add a line and a column
-------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
double operator()(double x, double y) const { return(exp(x)-y*y); }
};
int main() {
triqs::det_manip::det_manip<fun> D(fun(), std::vector<double>{1,2,2.5}, std::vector<double>{3,4,9});
std::cout<<std::endl<<"After construction, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
double x0 = 2.1, y0 = 7;
int i = 2, j = 0; // number of the added line and column
std::cout<<"We want to add a line and a column for i="<<i<<", j="<<j
<<", x="<<x0<<", y="<<y0<<" (f(x,y)="<<f(x0,y0)<<")."<<std::endl;
// (try of) insertion of a line and a column at position (3,1) in the matrix
// with x[i]=x0, y[j]=y0.
double detratio = D.try_insert(i, j, x0, y0); // the ratio between new and old determinants
// while the operation is not complete, the matrix stays unchanged
std::cout<<"After try_insert, D.matrix()="<<D.matrix()<<std::endl;
// here we validate the insertion: the (inverse) matrix and determinant are updated
D.complete_operation();
std::cout<<"After complete_operation, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
}
Remove a line and a column
---------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
double operator()(double x, double y) const { return(exp(x)-y*y); }
};
int main() {
triqs::det_manip::det_manip<fun> D(fun(), std::vector<double>{1,2,2.5}, std::vector<double>{3,4,9});
std::cout<<std::endl<<"After construction, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
int i = 1, j = 0; // number of the removed line and column
std::cout<<"We want to remove a line and a column for i="<<i<<", j="<<j<<"."<<std::endl;
// (try of) removal of a line and a column at position (1,0) in the matrix.
double detratio = D.try_remove(i, j); // the ratio between new and old determinants
// while the operation is not complete, the matrix stays unchanged
std::cout<<"After try_remove, D.matrix()="<<D.matrix()<<std::endl;
// here we validate the removal: the (inverse) matrix and determinant are updated
D.complete_operation();
std::cout<<"After complete_operation, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
}
Add two lines and two columns
------------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
double operator()(double x, double y) const { return(exp(x)-y*y); }
};
int main() {
triqs::det_manip::det_manip<fun> D(fun(), std::vector<double>{1,2,2.5}, std::vector<double>{3,4,9});
std::cout<<std::endl<<"After construction, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
double x0 = 2.1, y0 = 7, x1 = 3.5, y1 = 5;
int i0 = 2, i1 = 1, j0 = 0, j1 = 3; // number of the added lines and columns
std::cout<<"We want to add a line and a column for i0="<<i0<<", j0="<<j0<<", i1="<<i1<<", j1="<<j1
<<", x0="<<x0<<", y0="<<y0<<", x1="<<x1<<", y1="<<y1<<")."<<std::endl;
// (try of) insertion of 2 lines and 2 columns in the matrix
double detratio = D.try_insert2 (i0, i1, j0, j1, x0, x1, y0, y1); // the ratio between new and old determinants
// while the operation is not complete, the matrix stays unchanged
std::cout<<"After try_insert2, D.matrix()="<<D.matrix()<<std::endl;
// here we validate the insertion: the (inverse) matrix and determinant are updated
D.complete_operation();
std::cout<<"After complete_operation, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
}
Remove two lines and two columns
--------------------------------
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
typedef double result_type;
typedef double argument_type;
double operator()(double x, double y) const { return(exp(x)-y*y); }
};
int main() {
triqs::det_manip::det_manip<fun> D(fun(), std::vector<double>{1,2,2.5}, std::vector<double>{3,4,9});
std::cout<<std::endl<<"After construction, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
int i0 = 2, i1 = 1, j0 = 0, j1 = 1; // number of the removed lines and columns
std::cout<<"We want to remove 2 lines and 2 columns for i0="
<<i0<<", j0="<<j0<<", i1="<<i1<<", j1="<<j1<<"."<<std::endl;
// (try of) removal of a line and a column at position (1,0) in the matrix.
double detratio = D.try_remove2(i0, i1, j0, j1); // the ratio between new and old determinants
// while the operation is not complete, the matrix stays unchanged
std::cout<<"After try_remove2, D.matrix()="<<D.matrix()<<std::endl;
// here we validate the removal: the (inverse) matrix and determinant are updated
D.complete_operation();
std::cout<<"After complete_operation, D.matrix()="<<D.matrix()<<std::endl<<std::endl;
}
Learn more in the full reference, see :ref:`det_manip`

View File

@ -79,9 +79,11 @@ Doxygen documentation
The :doxy:`full C++ documentation<triqs::det_manip::det_manip>` is available here.
Example
-------------
---------
.. code-block:: c
.. compileblock::
#include <triqs/det_manip/det_manip.hpp>
struct fun {
@ -89,7 +91,7 @@ Example
typedef double argument_type;
double operator()(double x, double y) const {
const double pi = acos(-1);
const double pi = acos(-1.);
const double beta = 10.0;
const double epsi = 0.1;
double tau = x-y;
@ -103,14 +105,18 @@ Example
int main() {
fun f;
triqs::det_manip::det_manip<fun> D;
triqs::det_manip::det_manip<fun> D(f,100);
/// ....
double x=2, y=9, detratio;
detratio = D.try_insert(1,3, x,y);
/// insertions of 3 lines and 3 columns
double x=2., y=9., detratio;
detratio = D.try_insert(0, 0, x, y );
D.complete_operation();
detratio = D.try_insert(0, 1, 2., 3.);
D.complete_operation();
detratio = D.try_insert(0, 0, 4., 5.);
D.complete_operation();
///...
/// removal of a line (the 3rd) and a column (the 2nd)
detratio = D.try_remove(2,1);
D.complete_operation();
}