Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:55:41+00:00 2026-06-01T11:55:41+00:00

I got some code off the Internet and now I just need help to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:55:42+00:00Added an answer on June 1, 2026 at 11:55 am

    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).

    template <typename T> class matrix
    {
    private:
        T *_m;
        std::size_t _rows;
        std::size_t _cols;
    
    public:
        matrix(std::size_t rows, std::size_t cols)
          : _m(new T[rows*cols]), _rows(rows), _cols(cols) {}
    
        matrix(matrix<T>&& src)
          : _m(src._m), _rows(src._rows), _cols(src._cols)
        {
            src._m = NULL;
            src._rows = 0;
            src._cols = 0;
        }
    
        matrix<T>& operator=(matrix<T>&& src)
        {
            delete[] this->_m;
            this->_m = src._m;
            this->_rows = src._rows;
            this->_cols = src._cols;
            src._m = NULL;
            src._rows = 0;
            src._cols = 0;
    
            return *this;
        }
    
        virtual ~matrix()
        {
            delete[] this->_m;
        }
    
        inline float& operator()(std::size_t r, std::size_t c)
        {
            assert(r < this->_rows && c < this->_cols);
            return this->_m[r*this->_cols + c];
        }
    
        inline std::size_t rows() { return this->_rows; }
        inline std::size_t cols() { return this->_cols; }
    };
    
    template <typename T>
    matrix<T> operator*(const matrix<T>& l, const matrix<T>& r)
    {
        assert(l.cols() == r.rows());
        matrix<T> rv(l.rows(), r.cols());
    
        for (std::size_t r = 0; r < rv.rows(); ++r)
            for (std::size_t c = 0; c < rv.cols(); ++c);
            {
                rv(r, c) = (T) 0;
                for (std::size_t i = 0; i < l.cols(); ++i)
                    rv(r, c) += l(r, i) * r(i, c);
            }
    
        return rv;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was going through some code that I downloaded off the internet ( Got
I have some C# source code that I got off the Internet and I
I got some code from java httpurlconnection cutting off html and I am pretty
I am working with jQuery tabs and I've got some code that fires off
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
I got some code of a fellow stackflower and for some reason, it's crashing
We've got some code that uses LoadLibrary and GetProcAddress to implement a plugin architecture
I've got some code that uses the Component Categories manager to load all of
I have got some code to load an assembly and get all types, which
I've got some code that will generically get all Controls in a form and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.