# Ublas :

From: eeitan_at_[hidden]
Date: 2006-05-23 14:39:08

i am tring to do "cartesian product" of matrices:
matA =[2,2]((a11,a12),
(a21,a22))
matB =[2,2]((b11,b12),
(b21,b22))
so the cartesian product of them is
matC = [4,4]((a11*matB,a12*matB),
(a21*matB,a22*matB))

// making a cartesian spaces product between matrices
template< class M1,class M2,class M3 >
void cartesianSpacesProduct( M1 & target ,const M2 & left ,const M3 & right)
{// not deleting the target at this function !!!!!!!!
assert(target.size1()==left.size1()*right.size1() &&
target.size2()==left.size2()*right.size2() &&
target.size1()==target.size2() &&
left.size1()==left.size2());
for(typename M2::const_iterator1 it1(left.begin1());it1 != left.end1() ;
++it1){
for (typename M2::const_iterator2 it2(it1.begin());it2 != it1.end();
++it2){
ublas::matrix_range<M1 > mr (target,
ublas::range
(it2.index1()*right.size1()

,(it2.index1()+1)*right.size1()),
ublas::range
(it2.index2()*right.size2()

,(it2.index2()+1)*right.size2()));
mr+=(*it2)*right;
}
}
};
this function work very slow for me what you recommend me to do to boost it up ?

Quoting Gunter Winkler <guwi17_at_[hidden]>:

> On Sunday 21 May 2006 13:25, eeitan_at_[hidden] wrote:
> > hello, which is the best way to calculate:
> > mat1+=scalar*mat2;
> > mat1+=scalar*(mat2+ublas::trans(mat2));
> > in case of dense and sparse matrices ???
>
> This is already the best way and very efficient for dense matrices. However,
> the operation X+trans(X) may be very slow for sparse matrices and should be
> splitted:
>
> mat1 += scalar * mat2;
> mat1 += scalar * trans(mat2);
>
> (if mat1 is sparse, too, this does not help anyway because the left hand side
> in general determines the orientation of the computation (row/columnwise).)
>
> mfg
> Gunter
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]

>

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.