Skip to main content
permalink

Unit 1.3.3 Matrix-vector multiplication via dot products

permalink
Homework 1.3.3.1.

Compute (221221022222)(2101)=221221022222⎜ ⎜ ⎜2101⎟ ⎟ ⎟=

Solution
\begin{equation*} \begin{array}{rcl} \left(\begin{array}{rrrr} 2 \amp 2 \amp -1 \amp 2\\ \hline 2 \amp 1 \amp 0 \amp -2\\ \hline -2 \amp -2 \amp 2 \amp 2 \end{array}\right) \left(\begin{array}{r} 2\\ -1\\ 0\\ -1 \end{array}\right) \amp=\amp \left( \begin{array}{r} \left(\begin{array}{rrrr} 2 \amp 2 \amp -1 \amp 2 \end{array}\right) \left(\begin{array}{r} 2\\ -1\\ 0\\ -1 \end{array}\right) \\ \hline \left(\begin{array}{rrrr} 2 \amp 1 \amp 0 \amp -2 \end{array}\right) \left(\begin{array}{c} 2\\ -1\\ 0\\ -1 \end{array}\right) \\ \hline \left(\begin{array}{rrrr} -2 \amp -2 \amp 2 \amp 2 \end{array}\right) \left(\begin{array}{c} 2\\ -1\\ 0\\ -1 \end{array}\right) \end{array} \right) \\ \amp=\amp \left( \begin{array}{r} 0 \\ 5 \\ -4 \end{array} \right) \end{array} \end{equation*}

(Here we really care more about exposing the dot products of rows with the vector than the final answer.)

permalink

permalinkConsider the matrix-vector multiplication

y:=Ax+y.

permalinkThe way one is usually taught to compute this operation is that each element of y, ψi, is updated with the dot product of the corresponding row of A, ˜aTi, with vector x. With our notation, we can describe this as

(ψ0ψ1ψm1):=(˜aT0˜aT1˜aTm1)x+(ψ0ψ1ψm1)=(˜aT0x˜aT1x˜aTm1x)+(ψ0ψ1ψm1)=(˜aT0x+ψ0˜aT1x+ψ1˜aTm1x+ψm1).
permalink
Remark 1.3.3.

The way we remember how to do the partitioned matrix-vector multiplication

(˜aT0˜aT1˜aTm1)x=(˜aT0x˜aT1x˜aTm1x)

is to replace the m with a specific integer, 3, and each symbol with a number:

(123)(1).

If you view the first as a 3×1 matrix and the second as a 1×1 matrix, the result is

(123)(1)=((1)×(1)(2)×(1)(3)×(1)).

You should then notice that multiplication with the partitioned matrix and vector works the same way:

(˜aT0˜aT1˜aTm1)x=((˜aT0)×(x)(˜aT1)×(x)(˜aTm1)×(x))

with the change that in (1.3.1) the multiplication with numbers commutes while in (1.3.1) the multiplication does not (in general) commute:

(2)×(1)=(1)×(2)

but, in general,

˜aTixx˜aTi.

Indeed, ˜aTix is a dot product (inner product) while we will later be reminded that x˜aTi is an outerproduct of vectors ˜ai and x.

permalinkIf we then expose the individual elements of A and y we get

(ψ0ψ1ψm1)   :=(α0,0χ0+α0,1χ1+α0,n1χn1+ψ0α1,0χ0+α1,1χ1+α1,n1χn1+ψ1αm1,0χ0+αm1,1χ1+αm1,n1χn1+ψm1)

permalink This discussion explains the IJ loop for computing y:=Ax+y:

for i:=0,,m1   for j:=0,,n1      ψi:=αi,jχj+ψiend}   ψi:=˜aTix+ψiend

permalinkwhere we notice that again the i index ranges over the rows of the matrix and j index ranges over the columns of the matrix.

permalink
Homework 1.3.3.2.

In directory Assignments/Week1/C complete the implementation of matrix-vector multiplication in terms of dot operations

#define alpha( i,j ) A[ (j)*ldA + i ]   // map alpha( i,j ) to array A 
#define chi( i )  x[ (i)*incx ]            // map chi( i )  to array x
#define psi( i )  y[ (i)*incy ]            // map psi( i )  to array y

void Dots( int, const double *, int, const double *, int, double * );

void MyGemv( int m, int n, double *A, int ldA,
           double *x, int incx, double *y, int incy )
{
  for ( int i=0; i<m; i++ )
    Dots(   ,      ,     ,     ,    ,      );
}

in file Assignments/Week1/C/Gemv_I_Dots.c. You can test it by executing

make I_Dots

Upon completion, this should print
It appears all is well
Admittedly, this is a very incomplete test of the routine. However, it doesn't play much of a role in the course, so we choose to be sloppy.

permalink
Remark 1.3.4.

The file name Gemv_I_Dots.c can be decoded as follows:

  • The Gemv stands for "GEneral Matrix-Vector multiplication."

  • The I indicates a loop indexed by i (a loop over rows of the matrix).

  • The Dots indicates that the operation performed in the body of the loop is a dot product (hence the Dot) with the result added to a scalar (hence the s).