'How to multiply multiple matrices with C++
I'm doing a homework assignment which involves multiplying more than 2 matrices.
I thought about multiplying the first 2 matrices then take its result, continue multiplying it with the 3rd matrix, and repeat the same process until I multiply all the matrices together.
A * B * C = (A * B) * C = A * (B * C)
And here is my code so far. All of my matrices are stored in a 3D array of matrix[1][row][column].
// Calculate the result of first 2 matrices
for (int a = 0; a < n7; a++) {
for (int b = 0; b < n7; b++) {
for (int l = 0; l < n7; l++) {
sum += matrix[1][l][b] * matrix[0][a][l];
}
resultMatrix[a][b] = sum;
sum = 0;
}
}
// Check if k > 2, if yes then continue taking matrix[2] multiply with the result
if (k > 2) {
// Calculate to a tempResult matrix
for (int f = 2; f < k; f++) {
for (int a = 0; a < n7; a++) {
for (int b = 0; b < n7; b++) {
for (int l = 0; l < n7; l++) {
sum += matrix[f][l][b] * resultMatrix[a][l];
}
resultMatrix[a][b] = sum;
sum = 0;
}
}
// Pass the result to the original resultMatrix
for (int a = 0; a < n7; a++) {
for (int b = 0; b < n7; b++) {
resultMatrix[a][b] = tempResult[a][b];
}
}
}
}
I could not get the same result with an online matrix calculator and some manual input.
Please point out my mistakes, thank you!
Solution 1:[1]
Here is one solution.. Although I didn't applied dimension check of the matrices. So if you multiply a (3X4) & (2X4) matrix, it will not warn you. It can be done very easily with very simple steps. It worked for me with
A = 1 2 3 4
5 6 7 8 [4X4]
0 1 2 3
7 8 9 0
B = 2 3 4
5 6 7 [4X3]
8 9 0
3 4 5
c = 1
2 [3X1]
3
D = 1 1 -2 -1 [1X4]
Result = 278 278 556 -278
718 718 1436 -718 [4X4]
168 168 336 -168
678 678 1356 -678
The program:
#include <iostream>
#include <iomanip>
void displaymat(double* p, int r, int c)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
std::cout << std::setw(15) << *(p + i * c + j) << "\t";
}
std::cout << std::endl;
}
std::cout << "\n\n" << std::endl;
}
void matrix_multiply(double** a, double* b, int* rf, int* cf, int num)
{
double sum;
double** interim_result = new double* [num - 1]{nullptr};
for (int i = 0; i < num - 1; i++)
{
interim_result[i] = new double[rf[0] * cf[i + 1]]{ 0 };
}
for (int i = 0; i < num - 1; i++)
{
for (int j = 0; j < rf[0]; j++)
{
for (int k = 0; k < cf[i + 1]; k++)
{
sum = 0;
for (int l = 0; l < cf[i]; l++)
{
if (i == 0)
{
sum = sum + a[i][cf[i] * j + l] * a[i + 1][cf[i + 1] * l + k];
}
else
{
sum = sum + interim_result[i - 1][cf[i] * j + l] * a[i + 1][cf[i + 1] * l + k];
}
}
interim_result[i][j * cf[i + 1] + k] = sum;
}
}
//displaymat(interim_result[i], rf[0], cf[i + 1]);
}
for (int i = 0; i < rf[0] * cf[num - 1]; i++)
{
b[i] = interim_result[num - 2][i];
}
for (int i = 0; i < num - 1; i++)
{
delete[] interim_result[i];
}
delete[] interim_result;
}
int main()
{
int num; // total number of matrices
char ch = 'a';
std::cout << "How many matrices/matrix?:";
std::cin >> num;
std::cout << std::endl;
double** mat = new double* [num]; // double pointer for stacks of matrices
int* r = new int[num]{0}; // to store the rows the matrices
int* c = new int[num]{0}; // to store the columns of matrices
for (int n = 0; n < num; n++)
{
std::cout << "matrix:" << n + 1 << "\n" << std::endl;
std::cout << "rows:";
std::cin >> r[n]; // input
std::cout << "columns:";
std::cin >> c[n]; // input
std::cout << std::endl;
mat[n] = new double[(r[n] * c[n])]; // for getting elements
for (int i = 0; i < c[n] * r[n]; i++)
{
std::cout << ch << "[" << i / c[n] + 1 << "][" << i % c[n] + 1 << "]:";//ch << "[" << i / c[n] << "]" << "[" << i % c[n] << "]:";
std::cin >> *(*(mat + n) + i);
}
displaymat(mat[n], r[n], c[n]);
ch++;
}
double* result = new double[r[0] * c[num - 1]];
matrix_multiply(mat, result, r, c, num);
std::cout << "Result=" << std::endl;
displaymat(result, r[0], c[num - 1]);
for (int i = 0; i < num; i++)
{
delete[] * (mat + i);
}
delete[] mat;
delete[] result;
delete[] r;
delete[] c;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ALvi1993 |
