c++编写程序实现: 1、输于两个n×n的矩阵A和B,实现乘积运算,并输出运算结果和计算时间; 2、逐渐增大矩阵A和B的规模,分析运算时间的变化。 3、用分治法的实现矩阵乘积运算,比较使用分治法前后的计算量差异。
时间: 2024-05-08 08:21:43 浏览: 156
下面是基于C++的程序实现:
```c++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void matrix_mult(int** A, int** B, int** C, int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
C[i][j] = 0;
for (int k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
void matrix_mult_dc(int** A, int** B, int** C, int n) {
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
}
int** A11 = new int*[n / 2];
int** A12 = new int*[n / 2];
int** A21 = new int*[n / 2];
int** A22 = new int*[n / 2];
int** B11 = new int*[n / 2];
int** B12 = new int*[n / 2];
int** B21 = new int*[n / 2];
int** B22 = new int*[n / 2];
int** C11 = new int*[n / 2];
int** C12 = new int*[n / 2];
int** C21 = new int*[n / 2];
int** C22 = new int*[n / 2];
int** P1 = new int*[n / 2];
int** P2 = new int*[n / 2];
int** P3 = new int*[n / 2];
int** P4 = new int*[n / 2];
int** P5 = new int*[n / 2];
int** P6 = new int*[n / 2];
int** P7 = new int*[n / 2];
for (int i = 0; i < n / 2; ++i) {
A11[i] = new int[n / 2];
A12[i] = new int[n / 2];
A21[i] = new int[n / 2];
A22[i] = new int[n / 2];
B11[i] = new int[n / 2];
B12[i] = new int[n / 2];
B21[i] = new int[n / 2];
B22[i] = new int[n / 2];
C11[i] = new int[n / 2];
C12[i] = new int[n / 2];
C21[i] = new int[n / 2];
C22[i] = new int[n / 2];
P1[i] = new int[n / 2];
P2[i] = new int[n / 2];
P3[i] = new int[n / 2];
P4[i] = new int[n / 2];
P5[i] = new int[n / 2];
P6[i] = new int[n / 2];
P7[i] = new int[n / 2];
for (int j = 0; j < n / 2; ++j) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + n / 2];
A21[i][j] = A[i + n / 2][j];
A22[i][j] = A[i + n / 2][j + n / 2];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + n / 2];
B21[i][j] = B[i + n / 2][j];
B22[i][j] = B[i + n / 2][j + n / 2];
}
}
matrix_mult_dc(A11, B11, P1, n / 2);
matrix_mult_dc(A12, B21, P2, n / 2);
matrix_mult_dc(A11, B12, P3, n / 2);
matrix_mult_dc(A12, B22, P4, n / 2);
matrix_mult_dc(A21, B11, P5, n / 2);
matrix_mult_dc(A22, B21, P6, n / 2);
matrix_mult_dc(A21, B12, P7, n / 2);
matrix_mult_dc(A22, B22, C11, n / 2);
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < n / 2; ++j) {
C[i][j] = P1[i][j] + P2[i][j];
C[i][j + n / 2] = P3[i][j] + P4[i][j];
C[i + n / 2][j] = P5[i][j] + P6[i][j];
C[i + n / 2][j + n / 2] = P7[i][j] + C11[i][j];
}
}
for (int i = 0; i < n / 2; ++i) {
delete[] A11[i];
delete[] A12[i];
delete[] A21[i];
delete[] A22[i];
delete[] B11[i];
delete[] B12[i];
delete[] B21[i];
delete[] B22[i];
delete[] C11[i];
delete[] C12[i];
delete[] C21[i];
delete[] C22[i];
delete[] P1[i];
delete[] P2[i];
delete[] P3[i];
delete[] P4[i];
delete[] P5[i];
delete[] P6[i];
delete[] P7[i];
}
delete[] A11;
delete[] A12;
delete[] A21;
delete[] A22;
delete[] B11;
delete[] B12;
delete[] B21;
delete[] B22;
delete[] C11;
delete[] C12;
delete[] C21;
delete[] C22;
delete[] P1;
delete[] P2;
delete[] P3;
delete[] P4;
delete[] P5;
delete[] P6;
delete[] P7;
}
int main() {
srand(time(NULL));
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
int** A = new int*[n];
int** B = new int*[n];
int** C = new int*[n];
for (int i = 0; i < n; ++i) {
A[i] = new int[n];
B[i] = new int[n];
C[i] = new int[n];
for (int j = 0; j < n; ++j) {
A[i][j] = rand() % 10;
B[i][j] = rand() % 10;
}
}
clock_t start = clock();
matrix_mult(A, B, C, n);
clock_t end = clock();
cout << "The product of the matrices is:\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << C[i][j] << " ";
}
cout << endl;
}
cout << "The time taken by the algorithm is: " << (double)(end - start) / CLOCKS_PER_SEC << " seconds.\n";
for (int i = 0; i < n; ++i) {
delete[] A[i];
delete[] B[i];
delete[] C[i];
}
delete[] A;
delete[] B;
delete[] C;
return 0;
}
```
这个程序通过调用两个函数 `matrix_mult` 和 `matrix_mult_dc` 来实现矩阵乘积运算。函数 `matrix_mult` 使用了直接相乘的方法,而函数 `matrix_mult_dc` 使用了分治法的方法。在 `main` 函数中,我们首先随机生成了两个矩阵 A 和 B,然后调用 `matrix_mult` 函数来计算它们的乘积,并输出结果和运行时间。你可以通过增加矩阵的规模来测试这个程序,并比较使用直接相乘的方法和使用分治法的方法的运行时间的差异。
阅读全文