0% found this document useful (0 votes)
0 views

Assignment4

The document contains a C++ program that implements Strassen's algorithm for matrix multiplication of two 4x4 matrices. It defines functions to calculate specific values from the matrices and then divides the matrices into 2x2 submatrices to perform the necessary calculations. Finally, it outputs the resulting 2x2 matrix after performing the matrix multiplication.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Assignment4

The document contains a C++ program that implements Strassen's algorithm for matrix multiplication of two 4x4 matrices. It defines functions to calculate specific values from the matrices and then divides the matrices into 2x2 submatrices to perform the necessary calculations. Finally, it outputs the resulting 2x2 matrix after performing the matrix multiplication.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

using namespace std;

// function to calculate the value of a11 for a 4x4 mtrix

double cal11(double x[4][4]) {

return (x[1][1] * x[1][2]) + (x[1][2] * x[2][1]);

// function to clculate the Value of a21 for a 4x4 matrix

double cal21(double x[4][4]) {

return (x[3][1] * x[4][2]) + (x[3][2] * x[4][1]);

// Fuction to calculate the vlue of a12 for a 4x4 matrix

double cal12(double x[4][4]) {

return (x[1][3] * x[2][4]) + (x[1][4] * x[2][3]);

// Funtion to calculate the Value of a22 for a 4x4 mtrix

double cal22(double x[4][4]) {

return (x[2][3] * x[1][4]) + (x[2][4] * x[1][3]);

int main() {
double a11, a12, a22, a21, b11, b12, b21, b22;

double p, q, r, s, t, u, v, c11, c12, c21, c22;

double a[4][4];

a[0][0] = 2;

a[0][1] = 3;

a[0][2] = 1;

a[0][3] = 5;

a[1][0] = 6;

a[1][1] = 7;

a[1][2] = 4;

a[1][3] = 8;

a[2][0] = 9;

a[2][1] = 10;

a[2][2] = 11;

a[2][3] = 12;

a[3][0] = 13;

a[3][1] = 14;

a[3][2] = 15;

a[3][3] = 16;

// Declare and initialize matrix b

double b[4][4];

b[0][0] = 1;

b[0][1] = 2;

b[0][2] = 3;
b[0][3] = 4;

b[1][0] = 5;

b[1][1] = 6;

b[1][2] = 7;

b[1][3] = 8;

b[2][0] = 9;

b[2][1] = 10;

b[2][2] = 11;

b[2][3] = 12;

b[3][0] = 13;

b[3][1] = 14;

b[3][2] = 15;

b[3][3] = 16;

// Divide the 4x4 matrices into four 2x2 submatrices

a11 = cal11(a);

a12 = cal12(a);

a21 = cal21(a);

a22 = cal22(a);

b11 = cal11(b);

b12 = cal12(b);

b21 = cal21(b);

b22 = cal22(b);

// Perform calculations according to Strassen's algorithm

p = (a11 + a22) * (b11 + b22);

q = (a21 + a22) * b11;

r = a11 * (b12 - b22);


s = a22 * (b21 - b11);

t = (a11 + a12) * b22;

u = (a11 - a21) * (b11 + b12);

v = (a12 - a22) * (b21 + b22);

// Output the final 2x2 result matrix

cout << "\nFinal matrix:";

cout << "\n" << p + s - t + v << " " << r + t;

cout << "\n" << q + s << " " << p + r - q + u;

return 0;

You might also like