C矩阵基本运算代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#矩阵的运算代码
#region 矩阵运算
/// <summary>
/// 矩阵对应行列式的值
/// </summary>
/// <param ></param>
/// <returns></returns>
private double MatrixValue(double[,] MatrixList)
{
int Level = MatrixList.GetLength(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;
}
/// <summary>
/// 矩阵减法
/// </summary>
/// <param ></param>
/// <param ></param>
/// <param ></param>
/// <param ></param>
private double[] SubMatrix(double[] A1, double[] A2) {
//判断矩阵的长短是否一致
int a1 = A1.GetLength(0);
int a2 = A2.GetLength(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;
}
/// <summary>
/// 矩阵乘法
/// </summary>
/// <param ></param>
/// <param ></param>
/// <returns></returns>
private double[,] MultiplyMatrix(double[,] firstMatrix, double[,] secondMatrix)
{
double[,] resultMatrix = new double[firstMatrix.GetLength(0), secondMatrix.GetLength(1)]; //判断相乘矩阵是否合法,即第一个矩阵的列要等于第二个矩阵的行
if (firstMatrix.GetLength(1) != secondMatrix.GetLength(0))
{
return null;
}
//求结果矩阵
for (int rowIndex = 0; rowIndex < firstMatrix.GetLength(0); rowIndex++)
{
for (int colIndex = 0; colIndex < secondMatrix.GetLength(1); colIndex++)
{
//初始化结果矩阵的元素
resultMatrix[rowIndex, colIndex] = 0;
for (int i = 0; i < firstMatrix.GetLength(1); i++)
{
//求结果矩阵的元素值
resultMatrix[rowIndex, colIndex] += firstMatrix[rowIndex, i] * secondMatrix[i, colIndex];
}
}
}
return resultMatrix;
}
/// <summary>
/// 求逆矩阵
/// </summary>
/// <param ></param>
/// <returns></returns>
private double[,] Athwart(double[,] dMatrix)
{
//获取矩阵的行数
int Level = dMatrix.GetLength(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 System.IO;
3using System.Diagnostics;
4
5
6namespace Adjust
7{
8///<summary>
9///Matrix 的摘要说明。
10///实现矩阵的基本运算
11///</summary>
12public class Matrix
13{
14
15//构造方阵
16public Matrix(int row)
17{
18m_data = new double[row,row];
19
20}
21public Matrix(int row,int col)
22{
23m_data = new double[row,col];
24}
25//复制构造函数
26public Matrix(Matrix m)
27{
28int row = m.Row;
29int col = m.Col;
30m_data = new double[row,col];
31
32for(int i=0;i<row;i++)
33for(int j=0;j<col;j++)
34m_data[i,j] = m[i,j];
35
36}
37
38/*
39//分配方阵的大小
40//对于已含有内存的矩阵,将清空数据
41public void SetSize(int row)
42{
43m_data = new double[row,row];
44}
45
46
47//分配矩阵的大小
48//对于已含有内存的矩阵,将清空数据
49public void SetSize(int row,int col)
50{
51m_data = new double[row,col];
52}
53*/
55//unit matrix:设为单位阵
56public void SetUnit()
57{
58for(int i=0;i<m_data.GetLength(0);i++)
59for(int j=0;j<m_data.GetLength(1);j++)
60m_data[i,j] = ((i==j)?1:0);
61}
62
63//设置元素值
64public void SetValue(double d)
65{
66for(int i=0;i<m_data.GetLength(0);i++)
67for(int j=0;j<m_data.GetLength(1);j++)
68m_data[i,j] = d;
69}
70
71// Value extraction:返中行数
72public int Row
73{
74get
75{
77 return m_data.GetLength(0);
78 }
79 }
80
81 //返回列数
82 public int Col
83 {
84 get
85 {
86 return m_data.GetLength(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 }
98set
99{
100m_data[row,col] = value;
101}
102}
103
104//primary change
105// 初等变换 对调两行:ri<-->rj
106public Matrix Exchange(int i,int j)
107{
108double temp;
109
110for(int k=0;k<Col;k++)
111{
112temp = m_data[i,k];
113m_data[i,k] = m_data[j,k];
114m_data[j,k] = temp;
115}
116return this;
117}
118
119
120//初等变换 第index 行乘以mul
121Matrix Multiple(int index,double mul)
122{
123for(int j=0;j<Col;j++)
124{
125m_data[index,j] *= mul;
126}
127return this;
128}
129
130
131//初等变换第src行乘以mul加到第index行
132Matrix MultipleAdd(int index,int src,double mul)
133{
134for(int j=0;j<Col;j++)
135{
136m_data[index,j] += m_data[src,j]*mul;
137}
138
139return this;
140}
141
142//transpose 转置
143public Matrix Transpose()
144{
145Matrix ret = new Matrix(Col,Row);
146
147for(int i=0;i<Row;i++)
148for(int j=0;j<Col;j++)
149{
150ret[j,i] = m_data[i,j];
151}
152return ret;
153}
154
155//binary addition 矩阵加
156public static Matrix operator+ (Matrix lhs,Matrix rhs)
157{
158if(lhs.Row != rhs.Row) //异常
159{
160System.Exception e = new Exception("相加的两个矩阵的行数不等");
161throw e;
162}
163if(lhs.Col != rhs.Col) //异常
164{
165System.Exception e = new Exception("相加的两个矩阵的列数不等");
166throw e;
167}
168
169int row = lhs.Row;
170int col = lhs.Col;
171Matrix ret=new Matrix(row,col);
172
173for(int i=0;i<row;i++)
174for(int j=0;j<col;j++)
175{
176double d = lhs[i,j] + rhs[i,j];
177ret[i,j] = d;
178}
179return ret;
180
181}
182
183//binary subtraction 矩阵减
184public static Matrix operator- (Matrix lhs,Matrix rhs)
185{
186if(lhs.Row != rhs.Row) //异常
187{
188System.Exception e = new Exception("相减的两个矩阵的行数不等");
189throw e;
190}
191if(lhs.Col != rhs.Col) //异常
192{
193System.Exception e = new Exception("相减的两个矩阵的列数不等");
194throw e;
195}
196
197int row = lhs.Row;
198int col = lhs.Col;
199Matrix ret=new Matrix(row,col);
200
201for(int i=0;i<row;i++)
202for(int j=0;j<col;j++)
203{
204double d = lhs[i,j] - rhs[i,j];
205ret[i,j] = d;
206}
207return ret;
209
210
211//binary multiple 矩阵乘
212public static Matrix operator* (Matrix lhs,Matrix rhs)
213{
214if(lhs.Col != rhs.Row) //异常
215{
216System.Exception e = new Exception("相乘的两个矩阵的行列数不匹配");
217throw e;
218}
219
220Matrix ret = new Matrix (lhs.Row,rhs.Col);
221double temp;
222for(int i=0;i<lhs.Row;i++)
223{
224for(int j=0;j<rhs.Col;j++)
225{
226temp = 0;
227for(int k=0;k<lhs.Col;k++)
228{
229temp += lhs[i,k] * rhs[k,j];
231ret[i,j] = temp;
232}
233}
234
235return ret;
236}
237
238
239//binary division 矩阵除
240public static Matrix operator/ (Matrix lhs,Matrix rhs)
241{
242return lhs * rhs.Inverse();
243}
244
245//unary addition单目加
246public static Matrix operator+ (Matrix m)
247{
248Matrix ret = new Matrix(m);
249return ret;
250}
251
252//unary subtraction 单目减
253public static Matrix operator- (Matrix m)
254{
255Matrix ret = new Matrix(m);
256for(int i=0;i<ret.Row;i++)
257for(int j= 0;j<ret.Col;j++)
258{
259ret[i,j] = -ret[i,j];
260}
261
262return ret;
263}
264
265//number multiple 数乘
266public static Matrix operator* (double d,Matrix m)
267{
268Matrix ret = new Matrix(m);
269for(int i=0;i<ret.Row;i++)
270for(int j=0;j<ret.Col;j++)
271ret[i,j] *= d;
272
273return ret;
275
276//number division 数除
277public static Matrix operator/ (double d,Matrix m)
278{
279return d*m.Inverse();
280}
281
282//功能:返回列主元素的行号
283//参数:row为开始查找的行号
284//说明:在行号[row,Col)范围内查找第row列中绝对值最大的元素,返回所在行号
285int Pivot(int row)
286{
287int index=row;
288
289for(int i=row+1;i<Row;i++)
290{
291if(m_data[i,row] > m_data[index,row])
292index=i;
293}
294
295return index;
297
298//inversion 逆阵:使用矩阵的初等变换,列主元素消去法
299public Matrix Inverse()
300{
301if(Row != Col) //异常,非方阵
302{
303System.Exception e = new Exception("求逆的矩阵不是方阵");
304throw e;
305}
306StreamWriter sw = new StreamWriter("..\\annex\\close_matrix.txt");
307Matrix tmp = new Matrix(this);
308Matrix ret =new Matrix(Row); //单位阵
309ret.SetUnit();
310
311int maxIndex;
312double dMul;
313
314for(int i=0;i<Row;i++)
315{
316maxIndex = tmp.Pivot(i);
317
318if(tmp.m_data[maxIndex,i]==0)
319{
320System.Exception e = new Exception("求逆的矩阵的行列式的值等于0,");
321throw e;
322}
323
324if(maxIndex != i) //下三角阵中此列的最大值不在当前行,交换
325{
326tmp.Exchange(i,maxIndex);
327ret.Exchange(i,maxIndex);
328
329}
330
331ret.Multiple(i,1/tmp[i,i]);
332
333tmp.Multiple(i,1/tmp[i,i]);
334
335for(int j=i+1;j<Row;j++)
336{
337dMul = -tmp[j,i]/tmp[i,i];
338tmp.MultipleAdd(j,i,dMul);
339ret.MultipleAdd(j,i,dMul);
340
341}
342sw.WriteLine("tmp=\r\n"+tmp); 343sw.WriteLine("ret=\r\n"+ret);
344}//end for
345
346
347sw.WriteLine("**=\r\n"+ this*ret);
348
349for(int i=Row-1;i>0;i--)
350{
351for(int j=i-1;j>=0;j--)
352{
353dMul = -tmp[j,i]/tmp[i,i];
354tmp.MultipleAdd(j,i,dMul);
355ret.MultipleAdd(j,i,dMul);
356}
357}//end for
358
359
360sw.WriteLine("tmp=\r\n"+tmp); 361sw.WriteLine("ret=\r\n"+ret);
362sw.WriteLine("***=\r\n"+ this*ret);
363sw.Close();
364
365return ret;
366
367}//end Inverse
368
369#region
370/*
371//inversion 逆阵:使用矩阵的初等变换,列主元素消去法
372public Matrix Inverse()
373{
374if(Row != Col) //异常,非方阵
375{
376System.Exception e = new Exception("求逆的矩阵不是方阵");
377throw e;
378}
379///////////////
380StreamWriter sw = new StreamWriter("..\\annex\\matrix_mul.txt");
381////////////////////
382///
383Matrix tmp = new Matrix(this);
384Matrix ret =new Matrix(Row); //单位阵
385ret.SetUnit();
386
387int maxIndex;
388double dMul;
389
390for(int i=0;i<Row;i++)
391{
392
393maxIndex = tmp.Pivot(i);
394
395if(tmp.m_data[maxIndex,i]==0)
396{
397System.Exception e = new Exception("求逆的矩阵的行列式的值等于0,");
398throw e;
399}
400
401if(maxIndex != i) //下三角阵中此列的最大值不在当前行,交换
402{
403tmp.Exchange(i,maxIndex);
404ret.Exchange(i,maxIndex);
405
406}
407
408ret.Multiple(i,1/tmp[i,i]);
409
410/////////////////////////
411//sw.WriteLine("nul \t"+tmp[i,i]+"\t"+ret[i,i]);
412////////////////
413tmp.Multiple(i,1/tmp[i,i]);
414//sw.WriteLine("mmm \t"+tmp[i,i]+"\t"+ret[i,i]);
415sw.WriteLine("111111111 tmp=\r\n"+tmp);
416for(int j=i+1;j<Row;j++)
417{
418dMul = -tmp[j,i];
419tmp.MultipleAdd(j,i,dMul);
420ret.MultipleAdd(j,i,dMul);
421
422}
423sw.WriteLine("222222222222=\r\n"+tmp);
424
425}//end for
426
427
429for(int i=Row-1;i>0;i--)
430{
431for(int j=i-1;j>=0;j--)
432{
433dMul = -tmp[j,i];
434tmp.MultipleAdd(j,i,dMul);
435ret.MultipleAdd(j,i,dMul);
436}
437}//end for
438
439//////////////////////////
440
441
442sw.WriteLine("tmp = \r\n" + tmp.ToString());
443
444sw.Close();
445///////////////////////////////////////
446///
447return ret;
448
449}//end Inverse
451*/
452
453#endregion
454
455//determine if the matrix is square:方阵
456public bool IsSquare()
457{
458return Row==Col;
459}
460
461//determine if the matrix is symmetric对称阵
462public bool IsSymmetric()
463{
464
465if(Row != Col)
466return false;
467
468for(int i=0;i<Row;i++)
469for(int j=i+1;j<Col;j++)
470if( m_data[i,j] != m_data[j,i])
471return false;
473return true;
474}
475
476//一阶矩阵->实数
477public double ToDouble()
478{
479Trace.Assert(Row==1 && Col==1);
480
481return m_data[0,0];
482}
483
484//conert to string
485public override string ToString()
486{
487
488string s="";
489for(int i=0;i<Row;i++)
490{
491for(int j=0;j<Col;j++)
492s += string.Format("{0} ",m_data[i,j]);
493
494s += "\r\n";
495}
496return s;
497
498}
499
500
501//私有数据成员
502private double[,] m_data;
503
504}//end class Matrix
505}。