/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011-2012 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_MATMUL_H
#define TRIQS_ARRAYS_EXPRESSION_MATMUL_H
#include
#include
#include "../blas_lapack/gemm.hpp"
namespace triqs { namespace arrays {
///
template class matmul_lazy;
///
template matmul_lazy matmul (A const & a, B const & b) { return matmul_lazy(a,b); }
// ----------------- implementation -----------------------------------------
template class matmul_lazy : TRIQS_CONCEPT_TAG_NAME(ImmutableMatrix) {
typedef typename boost::remove_const::type V1;
typedef typename boost::remove_const::type V2;
//static_assert((boost::is_same::value),"Different values : not implemented");
public:
typedef BOOST_TYPEOF_TPL( V1() * V2()) value_type; // what is the result of multiplying a V1 by a V2 ?
typedef typename A::domain_type domain_type;
typedef typename const_view_type_if_exists_else_type::type A_type;
typedef typename const_view_type_if_exists_else_type::type B_type;
const A_type a; const B_type b;
private:
typedef matrix matrix_type;
struct internal_data { // implementing the pattern LazyPreCompute
matrix_type R;
internal_data(matmul_lazy const & P): R( first_dim(P.a), second_dim(P.b)) { blas::gemm(1.0,P.a, P.b, 0.0, R); }
};
friend struct internal_data;
mutable std::shared_ptr _id;
void activate() const { if (!_id) _id= std::make_shared(*this);}
public:
matmul_lazy( A const & a_, B const & b_):a(a_),b(b_){
if (second_dim(a) != first_dim(b)) TRIQS_RUNTIME_ERROR<< "Matrix product : dimension mismatch in A*B "<< a<<" "<< b;
}
domain_type domain() const { return mini_vector(first_dim(a), second_dim(b));}
template value_type operator() (K0 const & k0, K1 const & k1) const { activate(); return _id->R(k0,k1); }
// TO BE REMOVED because of the aliasing question
// Optimized implementation of =, +=, -=
template
friend void triqs_arrays_assign_delegation (LHS & lhs, matmul_lazy const & rhs) {
static_assert((is_matrix_or_view::value), "LHS is not a matrix");
resize_or_check_if_view(lhs,make_shape(first_dim(rhs),second_dim(rhs)));
blas::gemm(1.0,rhs.a, rhs.b, 0.0, lhs);
}
template
friend void triqs_arrays_compound_assign_delegation (LHS & lhs, matmul_lazy const & rhs, mpl::char_<'A'>) { rhs.assign_comp_impl(lhs,1.0);}
template
friend void triqs_arrays_compound_assign_delegation (LHS & lhs, matmul_lazy const & rhs, mpl::char_<'S'>) { rhs.assign_comp_impl(lhs,-1.0);}
private:
template void assign_comp_impl (LHS & lhs, double S) const {
static_assert((is_matrix_or_view::value), "LHS is not a matrix");
if (first_dim(lhs) != first_dim(*this))
TRIQS_RUNTIME_ERROR<< "Matmul : +=/-= operator : first dimension mismatch in A*B "<< first_dim(lhs)<<" vs "<< first_dim(*this);
if (second_dim(lhs) != second_dim(*this))
TRIQS_RUNTIME_ERROR<< "Matmul : +=/-= operator : first dimension mismatch in A*B "<< second_dim(lhs)<<" vs "<< second_dim(*this);
blas::gemm(S,a, b, 1.0, lhs);
}
friend std::ostream & operator<<(std::ostream & out, matmul_lazy const & x){return out<