Blitz logo

Blitz Support :

From: Navneet Dalal (Navneet.Dalal_at_[hidden])
Date: 2003-11-27 08:33:04


Hi ben,

product is already defined for two matrix operations i.e.

using namespace blitz;
TinyMatrix<double,3,4> a;
TinyMatrix<double,4,2> b;

// init a and b

TinyMatrix<double,3,2> c;
c = product(a,b);

std::cout << c << std::endl;

However, note that product uses expression templates. And for efficiency
reasons, uses restrict keyword. So do not write this code:
a = product(a,b);

The results will be catastrophic.

I would strongly advise you to use opertor* overloaded version for element
wise multiplication (not mathematical product of two matrices).
As Julian is updating matrix packages and to be in sprit of blitz,
operator* will probably be elementwise operator. This way, you wont have
to change your code again (when you switch to newer version of blitz).

as for operator*(double,TinyMatrix), here are few simple examples (without
expression templates)
NOTE: it is not obvious to me if one should allow scalar to be an
independent template or same as element type of TinyMatrix. I am providing
the code for scalar to be a Template. (You may have some problem with
names resolution because of this. The best would be to define these
operators in TinyMatrix class)

template<class TD, class TM, int NRows, int NCols>
inline TinyMatrix<typename
blitz::promote_trait<TD,TM>::T_promote,NRows,NCols>
operator*(const TD scalar, const TinyMatrix<TM,NRows,NCols>& matrix)
{
        TinyMatrix<typename blitz::promote_trait<TD,TM>::T_promote,NRows,NCols>
result;
        for (int i=0; i<NRows; ++i)
        for (int j=0; j<NCols; ++j)
                result(i,j) = scalar*matrix(i,j);
        return result;
}

template<class TD, class TM, int NRows, int NCols>
inline TinyMatrix<typename
blitz::promote_trait<TD,TM>::T_promote,NRows,NCols>
operator*(const TinyMatrix<TM,NRows,NCols>& matrix, const TD scalar)
{
        return scalar*matrix;
}

template<class TD, class TM, int NRows, int NCols>
inline TinyMatrix<typename
blitz::promote_trait<TD,TM>::T_promote,NRows,NCols>
operator*(const TinyMatrix<TM,NRows,NCols>& matrixA, const
TinyMatrix<TD,NRows,NCols>& matrixB)
{
        TinyMatrix<typename blitz::promote_trait<TD,TM>::T_promote,NRows,NCols>
result;
        for (int i=0; i<NRows; ++i)
        for (int j=0; j<NCols; ++j)
                result(i,j) = matrixA(i,j)*matrixB(i,j);
        return result;
}

Hope this helps
-navneet

On Thu, 27 Nov 2003 18:16:19 +0800, Ben McLean (finlaylabs)
<bmclean_at_[hidden]> wrote:

> Hi everyone, Happy Thanksgiving to all the Americans!
>
> I am having trouble providing operator* for TinyMatrix as below (managed
> the + and - ok), especially as I want it for non-square (but compatible)
> matrices, eg [2_rows,5_cols]*[5_rows,3_cols]. Also I am wondering about
> (double)*[TinyMatrix].
>
> namespace blitz {
> template <typename T_numtype, int N_rows, int N_cols>
> TinyMatrix<T_numtype,N_rows,N_cols>
> operator+(TinyMatrix<T_numtype,N_rows,N_cols> a,
> TinyMatrix<T_numtype,N_rows,N_cols>& b)
> {
> ??????????????
> }}
>
> Im sure there is a way to do this, I would appreciate an example :-)
> Thanks,
> Ben McLean.
> (Australian, but I LOVE thanksgiving with my USA friends :-)

-- 
Navneet DALAL               http://www.inrialpes.fr/lear/people/Dalal
INRIA Rhone-Alpes                       Ph(work): +33 (0)476 61 54 97
ZIRST-655, av. de l'Europe - 38334      FAX     : +33 (0)476 61 54 54
Montbonnot St. Martin, France           Mobile  : +33 (0)677 54 33 33