next home previous multiplication problems contents

12.3 Solutions to Multiplication Problems

    1. Symmetric

    2. Multiply the matrix by the scalar 1.6

    3.  matrix

    1. 4C = [ 20  12  24 ]

    2.  column vector

    3. DA - The dimensions are wrong (3 by 1 multiplied by 3 by 3). The inside dimensions do not agree.

    4. BC - The dimensions are wrong (3 by 3 multiplied by 1 by 3). The inside dimensions do not agree.

    5. 3CB = [ 342  213  180 ]

    6. C (A + B) = [ 160  161  136 ]

    7.  matrix

    8.   matrix

    9. CAD = 980

    10.   DBC - The dimensions are wrong (3 by 1 x 3 by 3 x 1 by 3). The inside dimensions do not agree on either multiplication.

    11.  matrix

    12.  matrix

    13. CD = 50

    1.   matrix Notice that this is a column vector.

    2. GP

    3.  matrix

  1. Yes. The matrix matrix the vector vector and the scalar c = c.

    Because the order of multiplication does not matter for real numbers,

    matrices


    Make sure that you tell the students that c(Ax) = A(cx) for all matrices that have the correct dimensions for Ax to be multiplied.

  2.  matrix
    This grouping requires only 15 simple multiplications to find T.

  3. Yes. , so

    matrices


Computer Program

Qbasic:


REM This program adds, subtracts and multiplies matrices

REM It uses no commands that are specific to matrices

CLS

DIM a(10,10)

DIM b(10,10)

DIM c(10,10)

PRINT “This program will add, subtract or multiply matrices whose”

PRINT “dimensions are less than 10. ”

PRINT

PRINT “Enter the dimensions of the first matrix”

INPUT “ separated by a comma. ”,m,n

REM This loop reads the first matrix

PRINT

PRINT “Please press enter after each element of the matrix. ”

PRINT “Enter all the elements of one row before the next row. ”

FOR i=1 to m

   FOR j=1 to n

      INPUT a(i,j)

   NEXT j

NEXT i



100 REM This allows the user to choose what to do next.

PRINT

INPUT “Do you wish to 1)add 2) subtract 3) multiply or 4) end ”,s

IF s=4 THEN GOTO 500

200 PRINT

INPUT “Enter the dimensions of the next matrix. ”,m2,n2

REM The next statements check to make sure the dimensions are correct

REM for the operation

IF (((s=1 or s=2) AND (m<<>m2 OR n<>n2) OR (s=3 AND n<>n2)) THEN

   PRINT “The matrix dimensions are not correct”

   GOTO 200

END IF

PRINT

PRINT “Enter the next matrix. ”

FOR i=1 to m2

   FOR j=1 to n2

      INPUT b(i,j)

   NEXT j

NEXT i

IF s=1 THEN GOSUB 1000 ELSE IF S=2 THEN GOSUB 2000 ELSE IF S=3 

            THEN GOSUB 3000 ELSE IF S=4 THEN GOTO ELSE GOTO 100

REM  The previous line should be part of the one above it

PRINT

PRINT “The resulting matrix is: ”

FOR i=1 to m

   FOR j=1 to n

      PRINT a(i,j),

   NEXT j

   PRINT

NEXT i

PRINT

GOTO 100

500 PRINT

PRINT “Your final solution is: ”

FOR i=1 to m

   FOR j=1 to n

      PRINT a(i,j),

   NEXT j

   PRINT

NEXT i

END



1000 REM This adds matrices

FOR i=1 to m

   FOR j=1 to n

      a(i,j)=a(i,j) + b(i,j)

   NEXT j

NEXT i

RETURN



2000 REM This subtracts matrices

FOR i=1 to m

   FOR j=1 to n

      a(i,j)=a(i,j) - b(i,j)

   NEXT j

NEXT i

RETURN



3000 REM This multiplies matrices

FOR i=1 to m

   FOR j=1 to n

      c(i,j)= 0

      FOR k=1 to n

         c(i,j)=c(i,j) + a(i,k) $*$ b(k,j)

      NEXT k

   NEXT j

NEXT i

n=n2

FOR i=1 to m

   FOR j=1 to n

      a(i,j) = c(i,j)

   NEXT j

NEXT i

RETURN




Pascal:


Program mult(input, output);

{This program adds, subtracts, and multiplies matrices.}



uses crt;  {Necessary for some Pascal compilers}



type

   matrix=array[1..10,1..10] of real;



var

   choice : integer;  {for choosing operation}

   m,n,m2,n2 : integer; {dimensions of the matrices}

   a,b,c : matrix;  {matrix}



procedure readmatrix(var a:matrix; m,n:integer);

   var 

      i,j: integer; {counters}



   begin {read}

   for i:=1 to m do

      begin {do}

      for j:=1 to n do 

         read(a[i,j]);

      readln;

      end {do}

   end; {read}



procedure writematrix(a:matrix; m,n:integer);

   var 

      i,j: integer; {counters}



   begin {write}

   for i:=1 to m do

      begin {each line}

      writeln;

      for j:=1 to n do 

         write(a[i,j]:6:2);

      end; {each line}

   writeln;

   end; {write}



procedure addmatrix(var a:matrix; b: matrix);

   var 

      i,j: integer; {counters}



   begin {addmatrix}

   for i:=1 to m do

      for j:=1 to n do 

         a[i,j]:=a[i,j]+b[i,j];

   end;{addmatrix}



procedure submatrix(var a:matrix; b: matrix);

   var 

      i,j: integer; {counters}



   begin {submatrix}

   for i:=1 to m do

      for j:=1 to n do 

         a[i,j]:=a[i,j]-b[i,j];

   end;{submatrix}



procedure multmatrix(var a:matrix; b:matrix; m:integer;

                  var n:integer; m2,n2:integer);

   {The above row should be attached to the first row}

   var 

      i,j,k: integer; {counters}

      c: matrix; {temporary matrix}



   begin {multmatrix}

   for i:=1 to m do

      for j:=1 to n2 do

         begin {inner product}

         c[i,j]:=0;

         for k:=1 to n do 

            c[i,j]:=c[i,j]+a[i,k]*b[k,j];

         end; {inner product}

   n:=n2;

   for i:=1 to m do

      for j:=1 to n do a[i,j]:=c[i,j];

   end;{multmatrix}



procedure menu{(var choice:char)};

   begin {menu}

   writeln;

   writeln('Do you wish to :');

   writeln('  1. Add');

   writeln('  2. Subtract');

   writeln('  3. Multiply');

   writeln('  4. End the program');

   readln(choice);

   end;{menu}



procedure operation(var a : matrix);

   begin {operation}

   writeln('Enter the dimensions of the second matrix ');

   writeln('separated by a space. Then hit return.');

   read(m2,n2);

   if (((m<>m2) or (n<>n2)) and ((choice=1) or (choice=2))) then

      begin {if}

      writeln('The dimensions are not correct for that operation.');

      choice := 5;

      end {if m}

   else if ((choice = 3) and (m2 <> n)) then

      begin {else if choice}

      writeln('The dimensions are not correct for multiplication.');

      choice := 5;

      end{elseif choice}

   else if ((choice<>1) and (choice<>2) and (choice<>3) 

         and (choice<>4)) then {this line should be attached 

                                to the previous line} 

      begin {else if}

      writeln('That was not a choice');

      choice := 5;

      end;{else if}

   if (choice<>5) then

      begin {if}

      writeln('Enter your matrix.);

      writeln('Enter the elements of each row separated');

      writeln('by a space. Hit return at the end of each row.');

      readmatrix(b,m2,n2);

      end; {if}

   if choice = 1 then

      addmatrix(a,b)

   else if choice = 2 then

      submatrix(a,b)

   else if choice = 3 then

      multmatrix(a,b,m,n,m2,n2);

   end;{operation}



begin{main program}

   clrscr;

   choice := 0;

   writeln('Enter the dimensions of the first matrix ');

   writeln('separated by a space. Then hit return.');

   read(m,n);

   writeln('Enter your matrix.);

   writeln('Enter each element followed by a return.');

   writeln('Enter the first row before you go to the next row.');

   readmatrix(a,m,n);

   menu;

   while choice <> 4 do

      begin {while}

      operation(a);

      writeln('The resulting matrix is :');

      writematrix(a,m,n);

      menu

   end; {while}

   writeln('The final resulting matrix is :');

   writematrix(a,m,n);

   writeln('Press return to leave the program');

   readln;

end. {main program}

next home previous go to the top multiplication problems contents

Send comments on material to Tamara Carter

These pages are maintained by Hilena Vargas (hvargas@rice.edu)
Updated: September 22, 2000

 Copyright ©1995 - 2000 Tamara Lynn Anthony