-
Notifications
You must be signed in to change notification settings - Fork 0
/
源.cpp
154 lines (139 loc) · 2.99 KB
/
源.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include <math.h>
using namespace std;
//double** A; //row行数 , col列数
//A = new double* [row];
//for (int i = 0; i < row; i++)
//{
// A[i] = new double[col];
//}
void delete_matrix(double** M, int row, int col)
{
for (int i = 0; i < row; i++)
{
delete[]M[i];
}
delete[]M; //delete p是直接删除p所指向的内存空间,而delete[]p则是先计算p
//所对应空间有多少个变量或对象,然后全部释放或调用相应的析构函数
}
void matrix_multiplication_2(double** M1, double* L, double* Result, int row_1, int col_1, int row_2, int col_2)
{ //矩阵相乘函数
for (int i = 0; i < row_1; i++)
{
Result[i] = 0;
for (int j = 0; j < col_2; j++)
{
for (int k = 0; k < col_1; k++)
{
Result[i] = Result[i] + M1[i][k] * L[k];
}
}
}
}
void matrix_multiplication_1(double** M1, double** M2, double** M3, int row_1, int col_1, int row_2, int col_2)
{ //矩阵相乘函数
for (int i = 0; i < row_1; i++)
{
for (int j = 0; j < col_2; j++)
{
M3[i][j] = 0;
for (int k = 0; k < row_2; k++)
{
M3[i][j] = M3[i][j] + M1[i][k] * M2[k][j];
}
}
}
}
void matrix_transpose(double** M, double** MT, int row, int col)
{ //矩阵转置函数
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++) {
// cout << M[i][j]; cout << '*';
MT[j][i] = M[i][j];
}
//printf("\n");
}
}
int Matrix_inversion(double** a, double** c, int N)
{ //矩阵求逆
//*********************************************
double** b;
b = new double* [N];
for (int i = 0; i < N; i++)
{
b[i] = new double[2 * N];
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < 2 * N; j++)
{
b[i][j] = 1;
}
}
//*********************************************
int i, j, m;
double t = 0;
//增广矩阵(A|E)存入二维数组b中
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
b[i][j] = a[i][j];
for (i = 0; i < N; i++)
for (j = N; j < 2 * N; j++)
b[i][j] = 0;
for (i = 0; i < N; i++)
b[i][N + i] = 1;
/*请对下面部分操作实现的功能及具体步骤描述一下*/
for (m = 0; m < N; m++)
{
t = b[m][m];
i = m;
while (b[m][m] == 0)
{
b[m][m] = b[i + 1][m];
i++;
}
if (i > m)
{
b[i][m] = t;
for (j = 0; j < m; j++)
{
t = b[m][j];
b[m][j] = b[i][j];
b[i][j] = t;
}
for (j = m + 1; j < 2 * N; j++)
{
t = b[m][j];
b[m][j] = b[i][j];
b[i][j] = t;
}
}
for (i = m + 1; i < N; i++)
for (j = 2 * N - 1; j >= m; j--)
b[i][j] -= b[i][m] * b[m][j] / b[m][m];
for (j = 2 * N - 1; j >= m; j--)
b[m][j] /= b[m][m];
}
m = N - 1;
while (m > 0)
{
for (i = 0; i < m; i++)
for (j = 2 * N - 1; j >= m; j--)
b[i][j] -= b[i][m] * b[m][j];
m--;
}
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
c[i][j] = b[i][N + j];
/*cout << "The inverse matrix is:";
cout << endl;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
cout << c[i][j];
cout << endl;
}*/
delete_matrix(b, N, 2 * N);
return 0;
}