mirror of
https://github.com/triqs/dft_tools
synced 2024-10-31 19:23:45 +01:00
b534936589
- The previous version of the * operator for matrix was too clever. It was giving a lazy object and then rewriting C = A *B into gemm (a,A,B,0,C). The pb was in case of aliasing : when e.g. C = A, or is a part of A. gemm is not correct that case, and as a result generic code like a = a *b may not be correct in matrix case, which is unacceptable. - So we revert to a simple * operator for matrix that does immediate computation. Same thing for matrix* vector - we also suppress a_x_ty class. -> for M = a * b, when M is a matrix, there is no overhead due to move assignment -> however, when M is a view, there is an additionnal copy. -Correctness comes first, hence the fix. However, if one wants more speed and one can guarantee that there is no aliasing possible, then one has to write a direct gemm call. -> det_manip class was adapted, since in that case, we can show there no alias, and we want the speed gain, so the * ops where replaced by direct blas call (using the array blas interface). -> also gemm, gemv, ger were overloaded in the case the return matrix/vector (i.e. last parameter of the function) is not an lvalue, but a temporary view created on the fly.
105 lines
3.4 KiB
C++
105 lines
3.4 KiB
C++
/*******************************************************************************
|
|
*
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
*
|
|
* Copyright (C) 2011 by O. Parcollet
|
|
*
|
|
* TRIQS is free software: you can redistribute it and/or modify it under the
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
******************************************************************************/
|
|
#include "./common.hpp"
|
|
|
|
#include "./src/array.hpp"
|
|
#include "./src/vector.hpp"
|
|
#include "./src/matrix.hpp"
|
|
#include "./src/linalg/det_and_inverse.hpp"
|
|
#include <iostream>
|
|
|
|
using std::cout; using std::endl;
|
|
using namespace triqs::arrays;
|
|
|
|
template<typename Expr >
|
|
matrix_view <typename Expr::value_type>
|
|
eval_as_matrix( Expr const & e) { return matrix<typename Expr::value_type>(e);}
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
try {
|
|
|
|
triqs::arrays::matrix<double> W(3,3,FORTRAN_LAYOUT),Wi(3,3,FORTRAN_LAYOUT),Wkeep(3,3,FORTRAN_LAYOUT),A(FORTRAN_LAYOUT);
|
|
for (int i =0; i<3; ++i)
|
|
for (int j=0; j<3; ++j)
|
|
W(i,j) = (i>j ? i+2.5*j : i*0.8-j);
|
|
|
|
/*triqs::arrays::vector <int> ipiv22(3);ipiv22()=0;
|
|
lapack::getrf(W, ipiv22);
|
|
std::cout<<"getrf W = "<<W<<std::endl<<std::endl;
|
|
*/
|
|
//return 0;
|
|
|
|
Wkeep = W;
|
|
|
|
std::cout<< inverse(W) << " = "<< eval_as_matrix (inverse(W))<<std::endl<<std::endl;
|
|
std::cout<< inverse(W) << " = "<< triqs::arrays::matrix<double > (inverse(W))<<std::endl<<std::endl;
|
|
std::cout<< inverse(W) << " = "<< triqs::arrays::matrix<double > (inverse(W))<<std::endl<<std::endl;
|
|
|
|
std::cout<< " and det = "<< double(determinant(W))<<std::endl<<std::endl;
|
|
Wi = inverse(W);
|
|
std::cout<< " Wi= "<< Wi<<std::endl<<std::endl;
|
|
|
|
triqs::arrays::matrix<double > should_be_one(W*Wi);
|
|
for (int i =0; i<3; ++i)
|
|
for (int j=0; j<3; ++j)
|
|
assert ( (abs(should_be_one(i,j) - (i==j ? 1 : 0))) <1.e-10 );
|
|
|
|
std::cerr<< "W* inverse(W)" << " = "<< triqs::arrays::matrix<double > (W*Wi)<<std::endl<<std::endl;
|
|
W= inverse(W);
|
|
std::cout<< " invert of W= "<< W<<std::endl<<std::endl;
|
|
|
|
A= inverse(W);
|
|
std::cerr<< " A = inv W= "<< A<<std::endl<<std::endl;
|
|
for (int i =0; i<3; ++i)
|
|
for (int j=0; j<3; ++j)
|
|
assert ( (abs(A(i,j) - Wkeep(i,j))) <1.e-10 );
|
|
|
|
double det = determinant(W);
|
|
std::cout<<"determinant "<<determinant(W)<< " = "<< det<< std::endl<<std::endl;
|
|
|
|
//
|
|
matrix_view<double> V(W(range(0,3,2), range(0,3,2)));
|
|
std::cout<<" view = "<< V<<std::endl<<std::endl;
|
|
std::cout<< inverse(V) << " = "<< eval_as_matrix (inverse(V))<<std::endl<<std::endl;
|
|
|
|
// testing against "manual" call of bindings
|
|
Wi = W;
|
|
triqs::arrays::vector <int> ipiv2(3);ipiv2()=0;
|
|
lapack::getrf(Wi, ipiv2);
|
|
std::cerr<<"getrf W = "<<Wi<<std::endl<<std::endl;
|
|
lapack::getri(Wi, ipiv2);
|
|
std::cerr<<"inverse W = "<<Wi<<std::endl<<std::endl; // avoid printing because of 1.e-18 error, not reproducible
|
|
for (int i =0; i<3; ++i)
|
|
for (int j=0; j<3; ++j)
|
|
if (!( (abs(Wi(i,j) - Wkeep(i,j))) <1.e-10 )) TRIQS_RUNTIME_ERROR<< "TEST FAILED";
|
|
|
|
|
|
}
|
|
catch (std::string ERR) { std::cout<<"ERROR : "<< ERR<<std::endl;}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|