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
The two input matrices must be of the same order and the return value must be another matrix of the same order. Each element of the return value is the sum of the corresponding elements of the input matrices.
#include<array>
// define the matrix type (an table of order 4)
const size_t order = 4;
using matrix = std::array<order, std::array<order, int>>;
// overload operator+ to cater for matrices: time complexity O(order*order)
matrix operator+ (const matrix& a, const matrix& b) {
matrix sum {}; // default initialised
for (size_t row=0; row<order; ++row)
for (size_t col=0; col<order; ++col)
sum[row][col] = a[row][col] + b[row][col];
return sum;
}
int main () {
matrix a {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
matrix b {{4, 3, 2, 1}, {8, 7, 6, 5}, {12, 11, 10, 9}, {16, 15, 14, 13}};
matrix c = a + b;
}
Use a valarray. A valarray is a specialised vector optimised for numeric computation.
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
if (n%2==0) sum=n/2*(n+1); else sum=(n+1)/2*n;
{ 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
Matrix addition is simply the process of summing one array to another array of the same size. The result is another array of the same size where each element is the sum of the corresponding elements in the first two arrays, like so: {1, 2, 3} + {4, 5, 6} = {1+4, 2+5, 3+6} = {5, 7, 9} This can be extended to multi-dimensional matrices (an array of arrays): {1, 2, 3} {4, 5, 6} + {7, 8, 9} {10, 11, 12} = {7+1, 8+2, 9+3} {10+4, 11+5, 12+6} = {8, 10, 12} {14, 16, 18} To achieve this in C++, C-style static arrays are the easiest method: int main() { int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int b[2][3] = {{7, 8, 9}, {10, 11, 12}}; int c[2][3] = {{0, 0, 0}, {0, 0, 0}}; for (size_t row=0; row<2; ++row) for (size_t col=0; col<3; ++col) c[row][col] = a[row][col] + b[row][col]; } C-style dynamic arrays as well as std::vector and std::array can also be used to represent the matrices, but it is important that all dimensions are the same. Matrices of different sizes cannot be added together (the result is undefined).
X^2-4
The product of (3x-1)(x+5) = 3x^2 +14x -5 by multiply out the brackets
The product is a^2 + b^2.
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+
always
Closed . . . .A+
Its product is plus 6
always
2*2 3*3 4*4
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).