2018-01-13 00:31:02 +00:00
|
|
|
/*************************************************************************************
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/tensors/Tensor_arith_mul.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
2018-01-13 00:31:02 +00:00
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2015-04-18 18:54:30 +01:00
|
|
|
#ifndef GRID_MATH_ARITH_MUL_H
|
|
|
|
#define GRID_MATH_ARITH_MUL_H
|
|
|
|
|
2018-01-13 00:31:02 +00:00
|
|
|
NAMESPACE_BEGIN(Grid);
|
2015-04-18 18:54:30 +01:00
|
|
|
|
2018-01-13 00:31:02 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////// MUL ///////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-04-18 18:54:30 +01:00
|
|
|
|
|
|
|
template<class rtype,class vtype,class mtype>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iScalar<rtype> * __restrict__ ret,const iScalar<mtype> * __restrict__ lhs,const iScalar<vtype> * __restrict__ rhs){
|
2018-01-13 00:31:02 +00:00
|
|
|
mult(&ret->_internal,&lhs->_internal,&rhs->_internal);
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class rrtype,class ltype,class rtype,int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
2015-05-16 04:36:22 +01:00
|
|
|
for(int c1=0;c1<N;c1++){
|
2015-04-18 18:54:30 +01:00
|
|
|
for(int c2=0;c2<N;c2++){
|
2015-05-23 09:31:15 +01:00
|
|
|
mult(&ret->_internal[c1][c2],&lhs->_internal[c1][0],&rhs->_internal[0][c2]);
|
2015-05-16 04:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-23 09:31:15 +01:00
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
for(int c3=1;c3<N;c3++){
|
2015-05-16 04:36:22 +01:00
|
|
|
for(int c2=0;c2<N;c2++){
|
|
|
|
mac(&ret->_internal[c1][c2],&lhs->_internal[c1][c3],&rhs->_internal[c3][c2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-23 09:31:15 +01:00
|
|
|
return;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-16 04:36:22 +01:00
|
|
|
|
2015-04-18 18:54:30 +01:00
|
|
|
template<class rrtype,class ltype,class rtype,int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iScalar<rtype> * __restrict__ rhs){
|
2018-01-13 00:31:02 +00:00
|
|
|
for(int c2=0;c2<N;c2++){
|
2015-04-18 18:54:30 +01:00
|
|
|
for(int c1=0;c1<N;c1++){
|
2018-01-13 00:31:02 +00:00
|
|
|
mult(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
|
2015-04-18 18:54:30 +01:00
|
|
|
}}
|
2018-01-13 00:31:02 +00:00
|
|
|
return;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class rrtype,class ltype,class rtype, int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iScalar<ltype> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
2018-01-13 00:31:02 +00:00
|
|
|
for(int c2=0;c2<N;c2++){
|
2015-04-18 18:54:30 +01:00
|
|
|
for(int c1=0;c1<N;c1++){
|
2018-01-13 00:31:02 +00:00
|
|
|
mult(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
|
2015-04-18 18:54:30 +01:00
|
|
|
}}
|
2018-01-13 00:31:02 +00:00
|
|
|
return;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
// Matrix left multiplies vector
|
|
|
|
template<class rtype,class vtype,class mtype,int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,const iMatrix<mtype,N> * __restrict__ lhs,const iVector<vtype,N> * __restrict__ rhs)
|
2015-04-18 18:54:30 +01:00
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret->_internal[c1],&lhs->_internal[c1][0],&rhs->_internal[0]);
|
|
|
|
for(int c2=1;c2<N;c2++){
|
|
|
|
mac(&ret->_internal[c1],&lhs->_internal[c1][c2],&rhs->_internal[c2]);
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2018-01-13 00:31:02 +00:00
|
|
|
}
|
|
|
|
return;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
template<class rtype,class vtype,class mtype,int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,
|
2018-01-13 00:31:02 +00:00
|
|
|
const iScalar<mtype> * __restrict__ lhs,
|
|
|
|
const iVector<vtype,N> * __restrict__ rhs){
|
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret->_internal[c1],&lhs->_internal,&rhs->_internal[c1]);
|
|
|
|
}
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
template<class rtype,class vtype,class mtype,int N>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,
|
2018-01-13 00:31:02 +00:00
|
|
|
const iVector<vtype,N> * __restrict__ rhs,
|
|
|
|
const iScalar<mtype> * __restrict__ lhs){
|
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret->_internal[c1],&rhs->_internal[c1],&lhs->_internal);
|
|
|
|
}
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class rtype,class vtype,class mtype,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
iVector<rtype,N> operator * (const iMatrix<mtype,N>& lhs,const iVector<vtype,N>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iVector<rtype,N> ret;
|
|
|
|
mult(&ret,&lhs,&rhs);
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class rtype,class vtype,class mtype,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
iVector<rtype,N> operator * (const iScalar<mtype>& lhs,const iVector<vtype,N>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iVector<rtype,N> ret;
|
|
|
|
mult(&ret,&lhs,&rhs);
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class rtype,class vtype,class mtype,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
iVector<rtype,N> operator * (const iVector<mtype,N>& lhs,const iScalar<vtype>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iVector<rtype,N> ret;
|
|
|
|
mult(&ret,&lhs,&rhs);
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2016-09-26 09:43:14 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Divide by scalar
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
template<class rtype,class vtype> strong_inline
|
|
|
|
iScalar<rtype> operator / (const iScalar<rtype>& lhs,const iScalar<vtype>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iScalar<rtype> ret;
|
|
|
|
ret._internal = lhs._internal/rhs._internal;
|
|
|
|
return ret;
|
2016-09-26 09:43:14 +01:00
|
|
|
}
|
|
|
|
template<class rtype,class vtype,int N> strong_inline
|
|
|
|
iVector<rtype,N> operator / (const iVector<rtype,N>& lhs,const iScalar<vtype>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iVector<rtype,N> ret;
|
|
|
|
for(int i=0;i<N;i++){
|
|
|
|
ret._internal[i] = lhs._internal[i]/rhs._internal;
|
|
|
|
}
|
|
|
|
return ret;
|
2016-09-26 09:43:14 +01:00
|
|
|
}
|
|
|
|
template<class rtype,class vtype,int N> strong_inline
|
|
|
|
iMatrix<rtype,N> operator / (const iMatrix<rtype,N>& lhs,const iScalar<vtype>& rhs)
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
iMatrix<rtype,N> ret;
|
|
|
|
for(int i=0;i<N;i++){
|
2016-09-26 09:43:14 +01:00
|
|
|
for(int j=0;j<N;j++){
|
|
|
|
ret._internal[i][j] = lhs._internal[i][j]/rhs._internal;
|
|
|
|
}}
|
2018-01-13 00:31:02 +00:00
|
|
|
return ret;
|
2016-09-26 09:43:14 +01:00
|
|
|
}
|
2015-04-18 18:54:30 +01:00
|
|
|
|
2018-01-13 00:31:02 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Glue operators to mult routines. Must resolve return type cleverly from typeof(internal)
|
|
|
|
// since nesting matrix<scalar> x matrix<matrix>-> matrix<matrix>
|
|
|
|
// while matrix<scalar> x matrix<scalar>-> matrix<scalar>
|
|
|
|
// so return type depends on argument types in nasty way.
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// scal x scal = scal
|
|
|
|
// mat x mat = mat
|
|
|
|
// mat x scal = mat
|
|
|
|
// scal x mat = mat
|
|
|
|
// mat x vec = vec
|
|
|
|
// vec x scal = vec
|
|
|
|
// scal x vec = vec
|
|
|
|
//
|
|
|
|
// We can special case scalar_type ??
|
2015-04-18 18:54:30 +01:00
|
|
|
template<class l,class r>
|
2015-05-15 11:40:31 +01:00
|
|
|
strong_inline auto operator * (const iScalar<l>& lhs,const iScalar<r>& rhs) -> iScalar<decltype(lhs._internal * rhs._internal)>
|
2015-04-18 18:54:30 +01:00
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef iScalar<decltype(lhs._internal*rhs._internal)> ret_t;
|
|
|
|
ret_t ret;
|
|
|
|
mult(&ret,&lhs,&rhs);
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iMatrix<l,N>& lhs,const iMatrix<r,N>& rhs) -> iMatrix<decltype(lhs._internal[0][0]*rhs._internal[0][0]),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal[0][0]*rhs._internal[0][0]) ret_t;
|
|
|
|
iMatrix<ret_t,N> ret;
|
|
|
|
mult(&ret,&lhs,&rhs);
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r, int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iMatrix<r,N>& lhs,const iScalar<l>& rhs) -> iMatrix<decltype(lhs._internal[0][0]*rhs._internal),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal[0][0]*rhs._internal) ret_t;
|
2015-04-18 18:54:30 +01:00
|
|
|
|
2018-01-13 00:31:02 +00:00
|
|
|
iMatrix<ret_t,N> ret;
|
|
|
|
for(int c1=0;c1<N;c1++){
|
2015-04-18 18:54:30 +01:00
|
|
|
for(int c2=0;c2<N;c2++){
|
2018-01-13 00:31:02 +00:00
|
|
|
mult(&ret._internal[c1][c2],&lhs._internal[c1][c2],&rhs._internal);
|
2015-04-18 18:54:30 +01:00
|
|
|
}}
|
2018-01-13 00:31:02 +00:00
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iScalar<l>& lhs,const iMatrix<r,N>& rhs) -> iMatrix<decltype(lhs._internal*rhs._internal[0][0]),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal*rhs._internal[0][0]) ret_t;
|
|
|
|
iMatrix<ret_t,N> ret;
|
|
|
|
for(int c1=0;c1<N;c1++){
|
2015-04-18 18:54:30 +01:00
|
|
|
for(int c2=0;c2<N;c2++){
|
2018-01-13 00:31:02 +00:00
|
|
|
mult(&ret._internal[c1][c2],&lhs._internal,&rhs._internal[c1][c2]);
|
2015-04-18 18:54:30 +01:00
|
|
|
}}
|
2018-01-13 00:31:02 +00:00
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iMatrix<l,N>& lhs,const iVector<r,N>& rhs) -> iVector<decltype(lhs._internal[0][0]*rhs._internal[0]),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal[0][0]*rhs._internal[0]) ret_t;
|
|
|
|
iVector<ret_t,N> ret;
|
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret._internal[c1],&lhs._internal[c1][0],&rhs._internal[0]);
|
|
|
|
for(int c2=1;c2<N;c2++){
|
|
|
|
mac(&ret._internal[c1],&lhs._internal[c1][c2],&rhs._internal[c2]);
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2018-01-13 00:31:02 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iScalar<l>& lhs,const iVector<r,N>& rhs) -> iVector<decltype(lhs._internal*rhs._internal[0]),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal*rhs._internal[0]) ret_t;
|
|
|
|
iVector<ret_t,N> ret;
|
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret._internal[c1],&lhs._internal,&rhs._internal[c1]);
|
|
|
|
}
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
2015-05-15 11:40:31 +01:00
|
|
|
template<class l,class r,int N> strong_inline
|
2015-04-18 18:54:30 +01:00
|
|
|
auto operator * (const iVector<l,N>& lhs,const iScalar<r>& rhs) -> iVector<decltype(lhs._internal[0]*rhs._internal),N>
|
|
|
|
{
|
2018-01-13 00:31:02 +00:00
|
|
|
typedef decltype(lhs._internal[0]*rhs._internal) ret_t;
|
|
|
|
iVector<ret_t,N> ret;
|
|
|
|
for(int c1=0;c1<N;c1++){
|
|
|
|
mult(&ret._internal[c1],&lhs._internal[c1],&rhs._internal);
|
|
|
|
}
|
|
|
|
return ret;
|
2015-04-18 18:54:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:31:02 +00:00
|
|
|
NAMESPACE_END(Grid);
|
2015-04-18 18:54:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|