The class "multiply" does not make sense, really; classes should usually be things, not actions. So you could have a result class. But there the result of mulitplying a matrix by a vector is another vector.
You could add a method or operator to your matrix class.
1 2
|
//matrix::operator*
vector operator* (const vector& v);
|
In this case, the vector class would need to be a friend of the matrix class (though it would be better to work through accessors...)
Or you could use a global operator* or function
vector operator* (const matrix& m, const vector& v);
This would need to be made a friend of matrix and vector, unless you used e() to access the elements (std::vector defines the method at() for this purpose)
Rather than passing the rows and cols to the multiplication operator or function, it should check the sizes of the inputs to make sure the calculation is valid. You already have getsize() to vector, and you could add getrows() and getcols() to matrix. Or just check the data members directly, if the function is a friend.
To signal an error you could raise an exception, or set a flag to indicate whether a matrix or vector is valid (in this case, the output code would need to be tweaked.)
The maths in condor's "multiply" function is fine, but the function need to check the dimensions and then either populate a new vector with the results, or returns a "bad vector".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
vector multiply(vector v)
{
// check rows of vector v are (its size is) the same as the cols
// of this matrix...
if(cols != v.size)
{
// TODO if not correct, return "bad" vector (or similar)
}
vector v2(rows);
for(int i = 0; i < rows; ++i)
{
v2.p[i] = 0;
for(int j = 0; j < cols; ++j)
v2.p[i] += r[i][j] * v.p[j]; // assume vector is a friend
}
return v2;
}
|
Andy