C#矩阵基本运算代码
沾化人事考试信息网-命题作文题目
C#矩阵的运算代码
#region 矩阵运算
矩阵对应行列式的值
private double
MatrixValue(double[,] MatrixList)
{
int
Level = gth(1);
double[,] dMatrix = new
double[Level, Level];
for (int i = 0; i <
Level; i++)
{
for (int j = 0; j < Level;
j++)
{
dMatrix[i, j] = MatrixList[i, j];
}
}
int sign = 1;
for (int i = 0,
j = 0; i < Level && j < Level; i++, j++)
{
判断改行dMatrix[i,
j]是否为0,若是,则寻找i后的行(m,m>i,切dMatrix[m, j]!=0)进行交换
if (dMatrix[i, j] == 0)
{
if (i ==
Level - 1)
{
return 0;
}
int m = i
+ 1;
获取一个dMatrix[m, j]不为为0的行
for (;
dMatrix[m, j] == 0; m++)
{
if (m == Level
- 1)
{
return 0;
}
}
判断是否达到矩阵的最大行,若是,则返回0
把i行和m行调换
double
temp;
for (int n = j; n < Level; n++)
{
temp = dMatrix[i, n];
dMatrix[i, n]
= dMatrix[m, n];
dMatrix[m, n] = temp;
}
sign *= (-1);
}
把当前行以后的行所对应的列变成0
double tmp;
for (int s = Level - 1; s > i;
s--)
{
tmp = dMatrix[s, j];
j行后面的所有行
for (int t = j; t < Level; t++)
{
dMatrix[s, t] -= dMatrix[i, t] * (tmp
dMatrix[i, j]);
}
}
}
double
result = 1;
for (int i = 0; i < Level; i++)
{
if (dMatrix[i, i] != 0)
{
result
*= dMatrix[i, i];
}
else
{
return
0;
}
}
return sign * result;
}
矩阵减法
private double[]
SubMatrix(double[] A1, double[] A2)
{
判断矩阵的长短是否一致
int a1 = gth(0);
int a2 =
gth(0);
if (a1 != a2)
{
return
null;
}
矩阵相减
double[] B = new
double[a1];
for (int i = 0; i < a1; i++)
{
B[i] = A1[i] - A2[i];
}
return B;
}
矩阵乘法
private double[,]
MultiplyMatrix(double[,] firstMatrix, double[,]
secondMatrix)
{
double[,] resultMatrix =
new double[gth(0), gth(1)];
判断相乘矩阵是否合法,即第一个矩阵的列要等于第二个矩阵的行
if (gth(1)
!= gth(0))
{
return null;
}
求结果矩阵
for (int rowIndex = 0; rowIndex < gth(0);
rowIndex++)
{
for (int colIndex = 0;
colIndex < gth(1); colIndex++)
{
初始化结果矩阵的元素
resultMatrix[rowIndex,
colIndex] = 0;
for (int i = 0; i < gth(1);
i++)
{
求结果矩阵的元素值
resultMatrix[rowIndex, colIndex] +=
firstMatrix[rowIndex, i] * secondMatrix[i,
colIndex];
}
}
}
return
resultMatrix;
}
求逆矩阵
private double[,]
Athwart(double[,] dMatrix)
{
获取矩阵的行数
int Level = gth(1);
double[,]
dReverseMatrix = new double[Level, 2 * Level];
初始化矩阵Level×(2*Level)
for (int i = 0; i <
Level; i++)
{
for (int j = 0; j < 2 *
Level; j++)
{
if (j < Level)
{
dReverseMatrix[i, j] = dMatrix[i, j];
}
else
{
if (j - Level == i)
{
dReverseMatrix[i, j] = 1;
}
else
{
dReverseMatrix[i, j] = 0;
}
}
}
}
for (int i = 0, j = 0; i < Level && j <
Level; i++, j++)
{
if (dReverseMatrix[i,
j] == 0)
{
if (i == Level - 1)
{
return null;
}
int m = i + 1;
for
(; dMatrix[m, j] == 0; m++)
{
if (m ==
Level - 1)
{
return null;
}
}
if (m == Level)
{
return null;
}
else
{
把i行和m行相加
for (int n =
j; n < 2 * Level; n++)
{
dReverseMatrix[i,
n] += dReverseMatrix[m, n];
}
}
}
double temp = dReverseMatrix[i, j];
if
(temp != 1)
{
把i行数据,变成以1开始的一行数据
for
(int n = j; n < 2 * Level; n++)
{
if
(dReverseMatrix[i, n] != 0)
{
dReverseMatrix[i, n] = temp;
}
}
}
把i行后的所有行的j列变成0
for (int s = Level - 1; s >
i; s--)
{
temp = dReverseMatrix[s, j];
for (int t = j; t < 2 * Level; t++)
{
dReverseMatrix[s, t] -= (dReverseMatrix[i, t]
* temp);
}
}
}
把矩阵Level×(2*Level)前Level×Level转变为单位矩阵
for
(int i = Level - 2; i >= 0; i--)
{
for
(int j = i + 1; j < Level; j++)
{
if
(dReverseMatrix[i, j] != 0)
{
double tmp = dReverseMatrix[i, j];
for
(int n = j; n < 2 * Level; n++)
{
dReverseMatrix[i, n] -= (tmp *
dReverseMatrix[j, n]);
}
}
}
}
返回逆矩阵
double[,] dReturn = new
double[Level, Level];
for (int i = 0; i <
Level; i++)
{
for (int j = 0; j < Level;
j++)
{
dReturn[i, j] = dReverseMatrix[i, j
+ Level];
}
}
return dReturn;
}
#endregion
第二份:
1using
System;
2using
3using stics;
4
5
6namespace Adjust
7
8
{
9 Matrix 的摘要说明。
10
实现矩阵的基本运算
11
12 public class
Matrix
13
14
15 构造方阵
16 public
Matrix(int row)
17 {
{
18 m_data =
new double[row,row];
19
20 }
21 public
Matrix(int row,int col)
22 {
23 m_data =
new double[row,col];
24 }
25 复制构造函数
26
public Matrix(Matrix m)
27 {
28 int row =
;
29 int col =
30 m_data = new
double[row,col];
31
32 for(int
i=0;i
35
36 }
37
38 *
39 分配方阵的大小
40
对于已含有内存的矩阵,将清空数据
41 public void SetSize(int
row)
42 {
43 m_data = new double[row,row];
44 }
45
46
47 分配矩阵的大小
48
对于已含有内存的矩阵,将清空数据
49 public void SetSize(int
row,int col)
50 {
51 m_data = new
double[row,col];
52 }
53 *
54
55 unit matrix:设为单位阵
56 public void
SetUnit()
57 {
58 for(int
i=0;i
61 }
62
63 设置元素值
64
public void SetValue(double d)
65 {
66
for(int i=0;i
69
}
70
71 Value extraction:返中行数
72
public int Row
73 {
74 get
75 {
76
77 return m_gth(0);
78 }
79 }
80
81 返回列数
82 public int Col
83 {
84 get
85 {
86 return
m_gth(1);
87 }
88 }
89
90 重载索引
91 存取数据成员
92 public double this[int
row,int col]
93 {
94 get
95 {
96
return m_data[row,col];
97 }
98 set
99 {
100 m_data[row,col] = value;
101
}
102 }
103
104 primary change
105
初等变换 对调两行:ri<-->rj
106 public Matrix
Exchange(int i,int j)
107 {
108 double
temp;
109
110 for(int k=0;k
112 temp = m_data[i,k];
113 m_data[i,k] = m_data[j,k];
114 m_data[j,k] = temp;
115 }
116 return this;
117 }
118
119
120 初等变换 第index 行乘以mul
121 Matrix Multiple(int index,double mul)
122 {
123 for(int j=0;j
125 m_data[index,j] *= mul;
126 }
127 return this;
128 }
129
130
131 初等变换 第src行乘以mul加到第index行
132 Matrix MultipleAdd(int index,int src,double mul)
133 {
134 for(int j=0;j
136 m_data[index,j] += m_data[src,j]*mul;
137 }
138
139 return this;
140 }
141
142 transpose 转置
143 public Matrix
Transpose()
144 {
145 Matrix ret = new
Matrix(Col,Row);
146
147 for(int
i=0;i
150 ret[j,i] = m_data[i,j];
151
}
152 return ret;
153 }
154
155
binary addition 矩阵加
156 public static Matrix
operator+ (Matrix lhs,Matrix rhs)
157 {
158 if( != ) 异常
159 {
160 ion e = new
Exception(相加的两个矩阵的行数不等
161 throw e;
162 }
163 if( != ) 异常
164 {
165 ion e
= new Exception(相加的两个矩阵的列数不等
166 throw e;
167 }
168
169 int row =
170 int
col =
171 Matrix ret=new Matrix(row,col);
172
173 for(int i=0;i
176 double d
= lhs[i,j] + rhs[i,j];
177 ret[i,j] = d;
178 }
179 return ret;
180
181 }
182
183 binary subtraction 矩阵减
184
public static Matrix operator- (Matrix lhs,Matrix
rhs)
185 {
186 if( != ) 异常
187
{
188 ion e = new
Exception(相减的两个矩阵的行数不等
189 throw e;
190 }
191 if( != ) 异常
192 {
193 ion e = new
Exception(相减的两个矩阵的列数不等
194 throw e;
195 }
196
197 int row =
198 int col =
199 Matrix ret=new Matrix(row,col);
200
201 for(int i=0;i
204 double d =
lhs[i,j] - rhs[i,j];
205 ret[i,j] = d;
206
}
207 return ret;
208 }
209
210
211 binary multiple 矩阵乘
212 public
static Matrix operator* (Matrix lhs,Matrix rhs)
213 {
214 if( != ) 异常
215 {
216
ion e = new Exception(相乘的两个矩阵的行列数不匹配
217 throw
e;
218 }
219
220 Matrix ret = new
Matrix (,);
221 double temp;
222 for(int
i=0;i<;i++)
223 {
224 for(int j=0;j<;j++)
225 {
226 temp = 0;
227 for(int
k=0;k<;k++)
228 {
229 temp += lhs[i,k] *
rhs[k,j];
230 }
231 ret[i,j] =
temp;
232 }
233 }
234
235 return
ret;
236 }
237
238
239 binary
division 矩阵除
240 public static Matrix operator
(Matrix lhs,Matrix rhs)
241 {
242 return
lhs * e();
243 }
244
245 unary
addition单目加
246 public static Matrix operator+
(Matrix m)
247 {
248 Matrix ret = new
Matrix(m);
249 return ret;
250 }
251
252 unary subtraction 单目减
253
public static Matrix operator- (Matrix m)
254
{
255 Matrix ret = new Matrix(m);
256
for(int i=0;i<;i++)
257 for(int j= 0;j<;j++)
258 {
259 ret[i,j] = -ret[i,j];
260 }
261
262 return ret;
263 }
264
265 number multiple 数乘
266 public static
Matrix operator* (double d,Matrix m)
267 {
268 Matrix ret = new Matrix(m);
269
for(int i=0;i<;i++)
270 for(int j=0;j<;j++)
271 ret[i,j] *= d;
272
273 return ret;
274 }
275
276 number division
数除
277 public static Matrix operator (double
d,Matrix m)
278 {
279 return d*e();
280 }
281
282 功能:返回列主元素的行号
283
参数:row为开始查找的行号
284
说明:在行号[row,Col)范围内查找第row列中绝对值最大的元素,返回所在行号
285
int Pivot(int row)
286 {
287 int
index=row;
288
289 for(int
i=row+1;i
291
if(m_data[i,row] > m_data[index,row])
292
index=i;
293 }
294
295 return index;
296 }
297
298 inversion
逆阵:使用矩阵的初等变换,列主元素消去法
299 public Matrix
Inverse()
300 {
301 if(Row != Col) 异常,非方阵
302 {
303 ion e = new
Exception(求逆的矩阵不是方阵
304 throw e;
305 }
306StreamWriter sw = new StreamWriter(
307
Matrix tmp = new Matrix(this);
308 Matrix ret
=new Matrix(Row); 单位阵
309 t();
310
311
int maxIndex;
312 double dMul;
313
314
for(int i=0;i
316 maxIndex
= (i);
317
318
if(tmp.m_data[maxIndex,i]==0)
319 {
320
ion e = new Exception(求逆的矩阵的行列式的值等于0,
321 throw
e;
322 }
323
324 if(maxIndex != i)
下三角阵中此列的最大值不在当前行,交换
325 {
326
ge(i,maxIndex);
327 ge(i,maxIndex);
328
329 }
330
331 le(i,1tmp[i,i]);
332
333 le(i,1tmp[i,i]);
334
335 for(int
j=i+1;j
337 dMul =
-tmp[j,i]tmp[i,i];
338 leAdd(j,i,dMul);
339 leAdd(j,i,dMul);
340
341 }
ine(
ine(
344 }end for
345
346
ine(this*ret);
348
349 for(int
i=Row-1;i>0;i--)
350 {
351 for(int
j=i-1;j>=0;j--)
352 {
353 dMul =
-tmp[j,i]tmp[i,i];
354 leAdd(j,i,dMul);
355 leAdd(j,i,dMul);
356 }
357 }end
for
358
359
ine(
ine(
ine(this*ret);
();
364
365 return ret;
366
367 }end
Inverse
368
369
370
#region
*
371 inversion 逆阵:使用矩阵的初等变换,列主元素消去法
372
public Matrix Inverse()
373 {
374 if(Row
!= Col) 异常,非方阵
375 {
376 ion e = new
Exception(求逆的矩阵不是方阵
377 throw e;
378 }
379
380 StreamWriter sw = new
StreamWriter(
381
382
383 Matrix tmp
= new Matrix(this);
384 Matrix ret =new
Matrix(Row); 单位阵
385 t();
386
387 int
maxIndex;
388 double dMul;
389
390
for(int i=0;i
392
393
maxIndex = (i);
394
395
if(tmp.m_data[maxIndex,i]==0)
396 {
397
ion e = new Exception(求逆的矩阵的行列式的值等于0,
398 throw
e;
399 }
400
401 if(maxIndex != i)
下三角阵中此列的最大值不在当前行,交换
402 {
403
ge(i,maxIndex);
404 ge(i,maxIndex);
405
406 }
407
408 le(i,1tmp[i,i]);
409
410
411 ine(t
412
413
le(i,1tmp[i,i]);
414 ine(t
415
ine(tmp=rn
416 for(int j=i+1;j
418 dMul = -tmp[j,i];
419
leAdd(j,i,dMul);
420 leAdd(j,i,dMul);
421
422 }
423 ine(
424
425 }end for
426
427
428
429 for(int
i=Row-1;i>0;i--)
430 {
431 for(int
j=i-1;j>=0;j--)
432 {
433 dMul =
-tmp[j,i];
434 leAdd(j,i,dMul);
435
leAdd(j,i,dMul);
436 }
437 }end for
438
439
440
441
442 ine(=
rn+ ng());
443
444 ();
445
446
447 return ret;
448
449 }end Inverse
450
451*
452
453 #endregion
454
455 determine if the matrix is
square:方阵
456 public bool IsSquare()
457 {
458 return Row==Col;
459 }
460
461
determine if the matrix is symmetric对称阵
462
public bool IsSymmetric()
463
464
465
if(Row != Col)
466 return false;
467
468 for(int i=0;i
471 return false;
{
472
473 return true;
474 }
475
476 一阶矩阵->实数
477 public double
ToDouble()
478 {
479 (Row==1 && Col==1);
480
481 return m_data[0,0];
482 }
483
484 conert to string
485 public
override string ToString()
486
487
488
string s=
489 for(int i=0;i
{
491 for(int j=0;j
493
494 s +=
495 }
496
return s;
497
498 }
499
500
501 私有数据成员
502 private double[,] m_data;
503
504 }end class Matrix
505}