Matrix*Vector and Vector*Matrix


vektor_multipli
10-24-2007, 09:32 AM
Hi everybody,

I wonder if someone knows how to implement an operator* into NRVec and/or NRMat, respectively, that can perform these kind of operations: Matrix*Vector and Vector*Matrix. Thanks a lot,

Holger

Kevin Dolan
10-24-2007, 12:51 PM
In principle you just write a function which takes as its arguments references to the two objects, and returns the NRVec or NRMat holding the result. To implement it as the * operator, you would just name the function "operator*". So the function would look like this:

NRMat operator*(const NRMat& x, const NRMat& y)
{
NRMat temp(x.ncols(), y.nrows());
.
.
.
return temp;
}

For each variant you would have a different overload of the function.

I would not recommend this approach, though. Returning the result by value in this way will result in the temporary matrix being copied, which is very inefficient. You are better off writing an ordinary function which takes the Matrix that will hold the result as one of its arguments.


Kevin

vektor_multipli
10-25-2007, 03:59 AM
Dear Kevin,

thanks a lot for your help. However, what you do with
"NRMat operator*(const NRMat& x, const NRMat& y)" is most likely multiplying a matrix x by a matrix y coming up with a result matrix. What I need though is something like
"NRVec operator*(const NRMat& x, const NRVec& y)", multiplying a matrix x by a vector y coming up with a result vector rather than a matrix. But when I try to implement it that way its giving me error messages (type specifier) even upon the declaration. Any ideas? Thanks again,

Holger

Kevin Dolan
10-25-2007, 04:06 AM
Did you remember the template perameter?

The prototype should actually look like this:

template <class T>
NRVec<T> operator*(const NRMat<T>& x, const NRVec<T>& y);


Assuming that you want a generic version that works for all types. If you just want it for a specific type, you could do something like:

Vec_DP operator*(const Mat_DP& x, const Vec_DP& y);


Kevin

vektor_multipli
10-25-2007, 04:46 AM
Hi Kevin,

unfortunately I can't even make the declaration within class NRVec, it always comes up with "missing type specifier". Also I think

NRVec operator*(const NRMat &rhs);

could be the right declaration as my vector*vector operation* works with

NRVec operator*(const NRVec &rhs);

Anyway it always turns up that "missing type specifier". Thanks again,

Holger

Kevin Dolan
10-26-2007, 03:03 AM
It sounds to me like you are trying to add it as a member function of your NRVec and NRMat classes themselves. What I suggested would be global functions.

As member functions, the operator*() function will only take one argument (the right hand side of the *). So for the matrix*matrix and matrix*vector operations, the functions would be member functions of NRMat.

template <class T>
class NRMat
{
.
.
.
NRMat operator*(const NRMat& y);
NRVec<T> operator*(const NRVec<T>& y);
};

Note that within the class declaration of NRMat, you do not usually need to stipulate the template parameter of an NRMat. If you do not, the compiler will assume it is type T. But you will need to specify the template parameter for NRVec, since that is a different class. I usually specify the template parameter even if I do not have to. The code is more clear that way.

Kevin

vektor_multipli
10-26-2007, 04:34 AM
Thanks Kevin,

this one works too now. Thanks a lot for your help!!