Matrix – Vector Multiplication

The general formula for a matrix-vector product is

image

 

  • The number of columns in A (n) has to equal the number of components in the vector x (n).
  • The number of rows in A (m) determines the number of components in the result Ax.

 

For example,

image

Here is the C++ implementation of matrix – vector multiplication.  The complete source code of CMatrix and CVector can be downloaded from here.

template
CVector
CMatrix::operator*(const CVector & v) const
{
    assert(v.Length() == _columns);

    T * data = new T[_rows];
    if (data == NULL)
    {
        printf("errorn");
        exit(1);
    }

    for (int i = 0; i < _rows; i++)
    {
        data[i] = 0;
        for (int j = 0; j < _columns; j++)
            data[i] += get(i, j)*v[j];
    }

    return CVector(_rows, data, true);
}

Leave a Reply