博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
矩阵基本操作的实现(C# 源代码)
阅读量:6682 次
发布时间:2019-06-25

本文共 9379 字,大约阅读时间需要 31 分钟。

第一次搬进自己的blog,打算放点东东到里面。在工程软件开发过程中,会碰到很多有关矩阵的运算。这是一年前的源代码

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

转载地址:http://pygxo.baihongyu.com/

你可能感兴趣的文章
Windows Server 2008 R2之五操作主控的管理
查看>>
java.lang.UnsupportedClassVersionError: Bad version number in .class file
查看>>
grub启动引导装载程序总结。
查看>>
XIV(3)--Read/Write Operations
查看>>
route OS(MikroTik)2.9.27初探
查看>>
tomcat配置优化
查看>>
[转]Javascript 中 String.replace( ) 的妙用
查看>>
分布式系统开发的一些相关理论基础——CAP、ACID、BASE
查看>>
探讨防火墙内核监听模式:ISA2006系列之十六
查看>>
android滑动一个路线后 人物图片按此路线移动的实现
查看>>
【电信增值业务学习笔记】9基于智能网的增值业务实现技术和应用
查看>>
Winform文件下载之WebClient
查看>>
【REACT NATIVE 系列教程之六】重写SHOULDCOMPONENTUPDATE指定组件是否进行重绘
查看>>
《完美软件》读书笔记11:信息摄取
查看>>
剖析SQL Server 2005查询通知之基础篇
查看>>
Windows 2003系统下桌面清理向导
查看>>
Exchange2007中使用各种限制选项
查看>>
NeHe OpenGL第四十四课:3D光晕
查看>>
eclipse + JBoss 5 + EJB3开发指南(13):在Servlet中访问应用程序管制EntityManager对象...
查看>>
NHibernate测试的几个小问题
查看>>