用c++。1、输于两个n×n的矩阵A和B,实现乘积运算,并输出运算结果和计算时间; 2、逐渐增大矩阵A和B的规模,分析运算时间的变化。 3、用分治法的实现矩阵乘积运算,比较使用分治法前后的计算量差异。
时间: 2024-05-01 10:19:59 浏览: 68
1. 以下是C++代码实现矩阵乘积运算并输出计算时间:
```c++
#include <iostream>
#include <ctime>
using namespace std;
const int maxn = 1000;
int A[maxn][maxn], B[maxn][maxn], C[maxn][maxn];
int main() {
int n;
cout << "Enter the size of matrix: ";
cin >> n;
// input matrix A and B
cout << "Enter the matrix A: " << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
cout << "Enter the matrix B: " << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> B[i][j];
}
}
// calculate matrix product
clock_t start = clock();
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];
}
}
}
clock_t end = clock();
// output matrix C and calculate time
cout << "The matrix product C = A * B is:" << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
cout << "The time of calculation is " << (double)(end - start) / CLOCKS_PER_SEC << " seconds." << endl;
return 0;
}
```
2. 逐渐增大矩阵A和B的规模,可以使用循环来多次运行上述代码,并记录每次计算的时间,最后输出时间随矩阵规模变化的曲线图。代码如下:
```c++
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
const int maxn = 1000;
int A[maxn][maxn], B[maxn][maxn], C[maxn][maxn];
int main() {
ofstream out("matrix_time.txt"); // 打开输出文件
out << "n time" << endl; // 输出表头
for(int n = 100; n <= 1000; n += 100) { // 矩阵规模从100逐步增加到1000
// input matrix A and B
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
A[i][j] = rand() % 100; // 随机生成0到99的整数
B[i][j] = rand() % 100;
}
}
// calculate matrix product
clock_t start = clock();
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];
}
}
}
clock_t end = clock();
// output time
out << n << " " << (double)(end - start) / CLOCKS_PER_SEC << endl;
}
out.close(); // 关闭输出文件
return 0;
}
```
运行结果会输出一个文件 "matrix_time.txt",其中每行表示矩阵规模和计算时间。可以使用Matlab或Excel等工具绘制出时间随规模变化的曲线图。
3. 分治法实现矩阵乘积运算的基本思想是将矩阵A和B分成四个子矩阵,分别计算它们的乘积,然后将结果合并成一个大矩阵C。可以使用递归实现该算法。代码如下:
```c++
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
const int maxn = 1000;
int A[maxn][maxn], B[maxn][maxn], C[maxn][maxn];
void matrix_multiply(int n, int A[][maxn], int B[][maxn], int C[][maxn]) {
if(n == 1) { // 当矩阵规模为1时,直接计算
C[0][0] = A[0][0] * B[0][0];
return;
}
int m = n / 2; // 将矩阵分成四个子矩阵
int A11[maxn][maxn], A12[maxn][maxn], A21[maxn][maxn], A22[maxn][maxn];
int B11[maxn][maxn], B12[maxn][maxn], B21[maxn][maxn], B22[maxn][maxn];
int C11[maxn][maxn], C12[maxn][maxn], C21[maxn][maxn], C22[maxn][maxn];
for(int i = 0; i < m; i++) {
for(int j = 0; j < m; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + m];
A21[i][j] = A[i + m][j];
A22[i][j] = A[i + m][j + m];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + m];
B21[i][j] = B[i + m][j];
B22[i][j] = B[i + m][j + m];
}
}
// 递归计算四个子矩阵的乘积
matrix_multiply(m, A11, B11, C11);
matrix_multiply(m, A12, B21, C12);
matrix_multiply(m, A11, B12, C21);
matrix_multiply(m, A12, B22, C22);
// 合并四个子矩阵的结果
for(int i = 0; i < m; i++) {
for(int j = 0; j < m; j++) {
C[i][j] = C11[i][j] + C12[i][j];
C[i][j + m] = C21[i][j] + C22[i][j];
C[i + m][j] = C11[i + m][j] + C12[i + m][j];
C[i + m][j + m] = C21[i + m][j] + C22[i + m][j];
}
}
}
int main() {
int n;
cout << "Enter the size of matrix: ";
cin >> n;
// input matrix A and B
cout << "Enter the matrix A: " << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
cout << "Enter the matrix B: " << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> B[i][j];
}
}
// calculate matrix product using divide and conquer
clock_t start = clock();
matrix_multiply(n, A, B, C);
clock_t end = clock();
// output matrix C and calculate time
cout << "The matrix product C = A * B is:" << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
cout << "The time of calculation is " << (double)(end - start) / CLOCKS_PER_SEC << " seconds." << endl;
return 0;
}
```
可以将分治法的计算时间和普通算法的计算时间进行比较,观察它们之间的差异。
阅读全文