/******************************************************************************* * * TRIQS: a Toolbox for Research in Interacting Quantum Systems * * Copyright (C) 2011-2013 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 . * ******************************************************************************/ #ifndef TRIQS_ARRAYS_EXPRESSION_MATRIX_ALGEBRA_H #define TRIQS_ARRAYS_EXPRESSION_MATRIX_ALGEBRA_H #include "./vector_algebra.hpp" #include "../matrix.hpp" #include "../linalg/det_and_inverse.hpp" #include "../blas_lapack/gemv.hpp" #include "../blas_lapack/gemm.hpp" namespace triqs { namespace arrays { // matrix * matrix template struct _matmul_rvalue {}; template struct _matmul_rvalue::value && ImmutableMatrix::value)> { typedef typename std::remove_const::type V1; typedef typename std::remove_const::type V2; typedef matrix::type> type; }; template typename _matmul_rvalue::type operator * (A const & a, B const & b) { if (second_dim(a) != first_dim(b)) TRIQS_RUNTIME_ERROR<< "Matrix product : dimension mismatch in A*B "<< a<<" "<< b; auto R = typename _matmul_rvalue::type( first_dim(a), second_dim(b)); blas::gemm(1.0,a, b, 0.0, R); return R; } // matrix * vector template struct _mat_vec_mul_rvalue {}; template struct _mat_vec_mul_rvalue::value && ImmutableVector::value)> { typedef typename std::remove_const::type V1; typedef typename std::remove_const::type V2; typedef vector::type> type; }; template typename _mat_vec_mul_rvalue::type operator * (M const & m, V const & v) { if (second_dim(m) != v.size()) TRIQS_RUNTIME_ERROR<< "Matrix product : dimension mismatch in Matrix*Vector "<< m<<" "<< v; auto R = typename _mat_vec_mul_rvalue::type(first_dim(m)); blas::gemv(1.0,m,v,0.0,R); return R; } // expression template template struct matrix_expr : TRIQS_CONCEPT_TAG_NAME(ImmutableMatrix) { typedef typename std::remove_reference::type L_t; typedef typename std::remove_reference::type R_t; static_assert( get_rank::value==0 || get_rank::value==0 || get_rank::value == get_rank::value, "rank mismatch in matrix operations"); //typedef typename std::result_of(typename L_t::value_type,typename R_t::value_type )>::type value_type; typedef typename std::remove_cv< typename std::remove_reference::type>::type>::type domain_type; L l; R r; using value_type = decltype(utility::operation()(l(0, 0), r(0, 0))); template matrix_expr(LL && l_, RR && r_) : l(std::forward(l_)), r(std::forward(r_)) {} domain_type domain() const { return combine_domain()(l,r); } template value_type operator()(Args && ... args) const { return utility::operation()(l(std::forward(args)...) , r(std::forward(args)...));} friend std::ostream &operator <<(std::ostream &sout, matrix_expr const &expr){return sout << "("<::name << " "< // a special case : the unary operator ! struct matrix_unary_m_expr : TRIQS_CONCEPT_TAG_NAME(ImmutableMatrix) { typedef typename std::remove_reference::type L_t; typedef typename L_t::value_type value_type; typedef typename L_t::domain_type domain_type; L l; template matrix_unary_m_expr(LL && l_) : l(std::forward(l_)) {} domain_type domain() const { return l.domain(); } template value_type operator()(Args && ... args) const { return -l(std::forward(args)...);} friend std::ostream &operator <<(std::ostream &sout, matrix_unary_m_expr const &expr){return sout << '-'<\ typename std::enable_if::value && TRAIT2 ::value, \ matrix_expr::type, typename node_t::type>>::type\ operator OP (A1 && a1, A2 && a2) { return {std::forward(a1),std::forward(a2)};} DEFINE_OPERATOR(plus, +, ImmutableMatrix,ImmutableMatrix); DEFINE_OPERATOR(minus, -, ImmutableMatrix,ImmutableMatrix); DEFINE_OPERATOR(minus, -, ImmutableMatrix,is_in_ZRC); DEFINE_OPERATOR(minus, -, is_in_ZRC,ImmutableMatrix); DEFINE_OPERATOR(multiplies, *, is_in_ZRC,ImmutableMatrix); DEFINE_OPERATOR(multiplies, *, ImmutableMatrix,is_in_ZRC); DEFINE_OPERATOR(divides, /, ImmutableMatrix,is_in_ZRC); #undef DEFINE_OPERATOR // the addition/substraction of diagonal matrix is special : all scalar are diagonal matrices here... #define DEFINE_OPERATOR(TAG, OP, TRAIT1, TRAIT2) \ template\ typename std::enable_if::value && TRAIT2 ::value, \ matrix_expr::type, typename node_t::type>>::type\ operator OP (A1 && a1, A2 && a2) { return {std::forward(a1),std::forward(a2)};} DEFINE_OPERATOR(plus, +, ImmutableMatrix,is_in_ZRC); DEFINE_OPERATOR(plus, +, is_in_ZRC,ImmutableMatrix); DEFINE_OPERATOR(minus, -, ImmutableMatrix,is_in_ZRC); DEFINE_OPERATOR(minus, -, is_in_ZRC,ImmutableMatrix); #undef DEFINE_OPERATOR // the unary is special template typename std::enable_if< ImmutableMatrix::value, matrix_unary_m_expr::type > >::type operator - (A1 && a1) { return {std::forward(a1)};} template::value>::type > struct _a_div_matrix { template auto operator() (A && a, M && m) DECL_AND_RETURN ( std::forward(a) * inverse(std::forward(m))); }; //typedef decltype ( std::declval::type>() * inverse(std::declval::type>() )) type; template // anything / matrix ---> anything * inverse(matrix) //typename boost::lazy_enable_if< ImmutableMatrix, std::result_of<_a_div_matrix(A,M)__type_of_mult_expr_matrix >::type //typename std::result_of<_a_div_matrix(A,M)>::type //operator/ (A && a, M && m) {return _a_div_matrix()( std::forward(a), std::forward(m));} auto operator/ (A && a, M && m) DECL_AND_RETURN( _a_div_matrix()( std::forward(a), std::forward(m))); // -> typename std::enable_if< ImmutableMatrix::value, decltype(std::forward(a) * inverse(std::forward(m)))>::type //{ return std::forward(a) * inverse(std::forward(m));} }}//namespace triqs::arrays #endif