矩阵基本操作的实现(C# 源代码)
1
using System;
2using System.IO;
3using System.Diagnostics;
4
5
6namespace Adjust
7{
8 /// <summary>
9 /// Matrix 的摘要说明。
10 /// 实现矩阵的基本运算
11 /// </summary>
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 = m.Row;
29 int col = m.Col;
30 m_data = new double[row,col];
31
32 for(int i=0;i<row;i++)
33 for(int j=0;j<col;j++)
34 m_data[i,j] = m[i,j];
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<m_data.GetLength(0);i++)
59 for(int j=0;j<m_data.GetLength(1);j++)
60 m_data[i,j] = ((i==j)?1:0);
61 }
62
63 //设置元素值
64 public void SetValue(double d)
65 {
66 for(int i=0;i<m_data.GetLength(0);i++)
67 for(int j=0;j<m_data.GetLength(1);j++)
68 m_data[i,j] = d;
69 }
70
71 // Value extraction:返中行数
72 public int Row
73 {
74 get
75 {
76
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 }
98 set
99
2using System.IO;
3using System.Diagnostics;
4
5
6namespace Adjust
7{
8 /// <summary>
9 /// Matrix 的摘要说明。
10 /// 实现矩阵的基本运算
11 /// </summary>
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 = m.Row;
29 int col = m.Col;
30 m_data = new double[row,col];
31
32 for(int i=0;i<row;i++)
33 for(int j=0;j<col;j++)
34 m_data[i,j] = m[i,j];
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<m_data.GetLength(0);i++)
59 for(int j=0;j<m_data.GetLength(1);j++)
60 m_data[i,j] = ((i==j)?1:0);
61 }
62
63 //设置元素值
64 public void SetValue(double d)
65 {
66 for(int i=0;i<m_data.GetLength(0);i++)
67 for(int j=0;j<m_data.GetLength(1);j++)
68 m_data[i,j] = d;
69 }
70
71 // Value extraction:返中行数
72 public int Row
73 {
74 get
75 {
76
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 }
98 set
99