矩阵基本操作的实现(C# 源代码)
1
using System;
2
using System.IO;
3
using System.Diagnostics;
4
5
6
namespace 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
{
100

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