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
if (n%2==0) sum=n/2*(n+1); else sum=(n+1)/2*n;
C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion
{ int a,b; { a=a^2; } { b=b^2; } { c=a^2+b^2+2*a*b; print f("%d%d%d",&c); get ch(); } ]
#includeint main(){int i;for(i=2;i
100101 1 times 2^0 = 1 PLUS 0 times 2^1 = 0 PLUS 1 times 2^2 = 4 PLUS 0 times 2^3 = 0 PLUS 0 times 2^4 = 0 PLUS 1 times 2^5 = 32 EQUALS 37
X^2-4
The product is a^2 + b^2.
The product of (3x-1)(x+5) = 3x^2 +14x -5 by multiply out the brackets
The product is: x^2 +8x +16
If the product of two matrices is the identity matrix then one matrix is the inverse or reciprocal of the other matrix. EXAMPLE A =(4 1) A-1 = (0.3 -0.1) then AA-1 = (1 0) .....(2 3)......... (-0.2 0.4)................... (1 1) The dots simply maintain the spacing and serve no other purpose.
Sometimes . . A+
Closed . . . .A+
always
Its product is plus 6
The product is(the product of the first term of each)plus(the product of the last term of each) plus(the product of the first term of the first and the last term of the second) plus(the product of the first term of the second and the last term of the first).
2*2 3*3 4*4
always