answersLogoWhite

0

To multiply two matrices, the matrices must support the dot product format. That is, given the multiplication of matrices A * B = C, the dimensions of each array must be A[x][y], B[y][z] and C[x][z]. Note that the number of columns in A must be the same as the number of rows in B.

To calculate the results of elements in C, you are effectively summing the products of corresponding elements in a row of A and a column of B, hence there must be as many columns in A as there are rows in B.

This method of multiplication may seem odd at first, however let's begin with a simple example. Imagine you have a matrix containing the prices for a range of products:

Apple: 30p

Banana: 25p

Orange: 29p

This matrix would normally be represented as a one-dimensional array with three elements.

double A[3] {0.3, 0.25, 0.29};

But as a matrix, it is a two-dimensional array with one row:

double A[1][3] {{0.3, 0.25, 0.29}};

Now imagine you have another array that contains the daily sales quantities for each product:

Day: M T W T F S S

Apple: 5 6 4 3 8 6 7

Banana: 6 9 5 7 3 5 4

Orange: 7 9 4 7 3 5 6

This would be represented as a two dimensional array with three rows and seven columns:

size_t B[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

What you want is the total sales value of each day. Thus the total sales for Monday would be (0.3*5) + (0.25*6) + (0.29*7). Repeating this for the remaining days would reveal a one-dimensional array of 7 elements. But as a matrix, it is a two-dimensional array with just one row. Thus we have the following array declarations:

double A[1][3] {{0.3, 0.25, 0.29}};

size_t B[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

double C[1][7] {};

Where; x=1, y=3 and z=7.

To work out the totals, you need three nested loops:

for (size_t x=0; x!=1; ++x)

{

for (size_t z=0; z!=7; ++z)

{

for (size_t y=0; y!=3; ++y)

{

c[x][z] += a[x][y] * b[y][z];

}

}

}

Note the order of the loops: x, z and y (not x, y and z).

Now let's see a working example of this:

#include<iostream>

#include<random>

#include<time.h>

template<typename T>

void print_matrix (const T& a, const size_t rows, const size_t cols)

{

for (size_t r=0; r!=rows; ++r)

{

for (size_t c=0; c!=cols; ++c)

{

std::cout << a[r][c] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

}

int main()

{

double a[1][3] {{0.3, 0.25, 0.29}};

size_t b[3][7] {{5, 6, 4, 3, 8, 6, 7},{6, 9, 5, 7, 3, 5, 4},{7, 9, 4, 7, 3, 5, 6}};

double c[1][7] {};

for (size_t x=0; x!=1; ++x)

{

for (size_t z=0; z!=7; ++z)

{

for (size_t y=0; y!=3; ++y)

{

c[x][z] += a[x][y] * b[y][z];

}

}

}

std::cout << "\nProduct prices:\nApples\tOranges\tBananas\n"; print_matrix (a,1,3);

std::cout << "\nWeekly sales:\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n"; print_matrix (b,3,7);

std::cout << "\nTotal sales:\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n"; print_matrix (c,1,7);

}

Output:

Product prices:

Apples Oranges Bananas

0.3 0.25 0.29

Weekly sales:

Mon Tue Wed Thu Fri Sat Sun

5 6 4 3 8 6 7

6 9 5 7 3 5 4

7 9 4 7 3 5 6

Total sales:

Mon Tue Wed Thu Fri Sat Sun

5.03 6.66 3.61 4.68 4.02 4.5 4.84

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering