1、输于两个n×n的矩阵A和B,实现乘积运算,并输出运算结果和计算时间;2、逐渐增大矩阵A和B的规模,分析运算时间的变化。3、用分治法的实现矩阵乘积运算,比较使用分治法前后的计算量差异。c++代码
时间: 2024-05-16 14:15:13 浏览: 273
1、以下是两个矩阵相乘的C++代码:
```c++
#include <iostream>
#include <chrono> // 用于计时
using namespace std;
const int N = 1000;
int A[N][N], B[N][N], C[N][N];
// 矩阵相乘函数
void matrix_product(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
}
int main() {
// 生成随机矩阵
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
}
int n = 100; // 初始矩阵大小
while (n <= N) {
auto start = chrono::steady_clock::now(); // 记录开始时间
matrix_product(n); // 矩阵相乘
auto end = chrono::steady_clock::now(); // 记录结束时间
auto diff = end - start; // 计算时间差
cout << "Matrix size: " << n << ", time: " << chrono::duration<double, milli>(diff).count() << " ms." << endl;
n += 100; // 每次增加100行/列
}
return 0;
}
```
2、随着矩阵规模的增大,运算时间会呈现指数级增长。例如,这里将矩阵大小从100增加到1000,时间从几十毫秒增加到几十秒。
3、以下是使用分治法实现矩阵乘积的C++代码:
```c++
#include <iostream>
#include <chrono> // 用于计时
using namespace std;
const int N = 1000;
int A[N][N], B[N][N], C[N][N];
// 矩阵相加函数
void matrix_add(int n, int A[][N], int B[][N], int C[][N]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
// 矩阵相减函数
void matrix_sub(int n, int A[][N], int B[][N], int C[][N]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
// 矩阵相乘函数
void matrix_product(int n, int A[][N], int B[][N], int C[][N]) {
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
}
int half = n / 2;
// 分治计算四个子矩阵的乘积
int A11[N][N], A12[N][N], A21[N][N], A22[N][N];
int B11[N][N], B12[N][N], B21[N][N], B22[N][N];
int C11[N][N], C12[N][N], C21[N][N], C22[N][N];
for (int i = 0; i < half; i++) {
for (int j = 0; j < half; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j+half];
A21[i][j] = A[i+half][j];
A22[i][j] = A[i+half][j+half];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j+half];
B21[i][j] = B[i+half][j];
B22[i][j] = B[i+half][j+half];
}
}
int M1[N][N], M2[N][N], M3[N][N], M4[N][N], M5[N][N], M6[N][N], M7[N][N];
matrix_product(half, A11, matrix_sub(half, B12, B22, C12), M1);
matrix_product(half, matrix_add(half, A11, A12, C11), B22, M2);
matrix_product(half, matrix_add(half, A21, A22, C21), B11, M3);
matrix_product(half, A22, matrix_sub(half, B21, B11, C11), M4);
matrix_product(half, matrix_add(half, A11, A22, C11), matrix_add(half, B11, B22, C11), M5);
matrix_product(half, matrix_sub(half, A12, A22, C12), matrix_add(half, B21, B22, C21), M6);
matrix_product(half, matrix_sub(half, A11, A21, C12), matrix_add(half, B11, B12, C21), M7);
// 计算四个子矩阵的和
int C111[N][N], C112[N][N], C121[N][N], C122[N][N], C211[N][N], C212[N][N], C221[N][N], C222[N][N];
matrix_add(half, matrix_sub(half, matrix_add(half, M5, M4, C11), M2, C11), M6, C111);
matrix_add(half, M1, M2, C112);
matrix_add(half, M3, M4, C121);
matrix_sub(half, matrix_sub(half, matrix_add(half, M5, M1, C11), M3, C11), M7, C122);
matrix_add(half, M1, M4, C211);
matrix_sub(half, matrix_add(half, M2, M5, C11), matrix_add(half, M4, M6, C11), C212);
matrix_add(half, matrix_add(half, M3, M7, C11), M6, C221);
matrix_add(half, matrix_sub(half, matrix_add(half, M5, M1, C11), M2, C11), M3, C222);
// 将四个子矩阵合并成一个矩阵
for (int i = 0; i < half; i++) {
for (int j = 0; j < half; j++) {
C[i][j] = C111[i][j];
C[i][j+half] = C112[i][j];
C[i+half][j] = C121[i][j];
C[i+half][j+half] = C122[i][j];
}
}
}
int main() {
// 生成随机矩阵
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
}
int n = 100; // 初始矩阵大小
while (n <= N) {
auto start = chrono::steady_clock::now(); // 记录开始时间
matrix_product(n, A, B, C); // 矩阵相乘
auto end = chrono::steady_clock::now(); // 记录结束时间
auto diff = end - start; // 计算时间差
cout << "Matrix size: " << n << ", time: " << chrono::duration<double, milli>(diff).count() << " ms." << endl;
n += 100; // 每次增加100行/列
}
return 0;
}
```
使用分治法可以大大减少计算量,从而减少运算时间。例如,这里将矩阵大小从100增加到1000,时间从几十秒减少到几百毫秒。
阅读全文