I got some code off the Internet and now I just need help to multiply elements of two matrices or vectors.
Matrixf multiply(Matrixf const& left, Matrixf const& right) {
// Error check
if (left.ncols() != right.nrows()) {
throw std::runtime_error("Unable to multiply: matrix dimensions not agree.");
}
/* I have all the other part of the code for matrix */
/** Now I am not sure how to implement multiplication of a vector or matrix. **/
Matrixf ret(1, 1);
return ret;
}
Background: I am a new C++ user and I am also doing a major in mathematics, so thought I would try implement a simple calculator.
I’d recommend you use a library such as Eigen (very fast) or the Boost uBLAS matrix (not so fast, relatively speaking) class. Still, if you’re trying to learn the way to do this, there’s no harm in building your own class. The standard way of doing this is with templates for the type. You can also, with a little bit of further investigation use templates for the size (left as an exercise to the reader).
This has a few C++11 aspects to it, namely the move constructor and assignment operator. If you are not using C++11 yet, replace them with traditional copy and assignment operators respectively. Also, this is a kind of naive multiplier. There are some efficiencies you can employ to eliminate many of the matrix element lookups by replacing them with an iterator style construct instead.