Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace ConsoleApplication4
7{
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 }
13 }
14
15 //定义矩阵类
16 public class Matrix
17 {
18 private double[][] data;
19 private int rows;
20 private int columns;
21 private static Random random = new Random();
22 //构造一个空矩阵
23 public Matrix(int rows, int columns)
24 {
25 this.rows = rows;
26 this.columns = columns;
27 this.data = new double[rows][];
28 for (int i = 0; i < rows; i++)
29 {
30 this.data[i] = new double[columns];
31 }
32 }
33 //参数为行数和列数及一个给定值,构造一个矩阵
34 public Matrix(int rows, int columns, double value)
35 {
36 this.rows = rows;
37 this.columns = columns;
38 this.data = new double[rows][];
39 for (int i = 0; i < rows; i++)
40 {
41 data[i] = new double[columns];
42 }
43 for (int i = 0; i < rows; i++)
44 {
45 data[i][i] = value;
46 }
47 }
48 //参数为一个二维数组,表示所有矩阵元素值,构造一个矩阵
49 public Matrix(double[][] value)
50 {
51 this.rows = value.Length;
52 this.columns = value[0].Length;
53 for (int i = 0; i < rows; i++)
54 {
55 if (value[i].Length!=columns )
56 {
57 throw new ArgumentException("参数超出范围");
58 }
59 }
60 this.data = value;
61 }
62
63 public static Matrix Negate(Matrix value)
64 {
65 if (value ==null)
66 {
67 throw new ArgumentNullException("参数无效");
68 }
69 int rows = value.rows;
70 int columns = value.columns;
71 double[][] data = value.Array; //不知道Array哪里定义的
72
73 Matrix X = new Matrix(rows,columns);
74 double[][] x = X.Array;
75 for (int i = 0; i < rows; i++)
76 {
77 for (int j = 0; j < columns; j++)
78 {
79 x[i][j] = -data[i][j];
80 }
81 }
82 return X;
83 }
84 //重载“-”负号运算符
85 public static Matrix operator -(Matrix value)
86 {
87 if (value ==null)
88 {
89 throw new ArgumentNullException("参数无效");
90 }
91 //重载时调用了Negate方法,实现在Negate方法中
92 return Negate(value);
93
94 }
95
96 //“==”和“!=”运算符必须成对重载,而且同时必须重载Equals方法,
97 public static bool operator ==(Matrix left,Matrix right)
98 {
99 return Equals(let,right );
100 }
101 public static bool operator !=(Matrix left, Matrix right)
102 {
103 return !Equals(left, right);
104 }
105
106 public static Matrix Add(Matrix left,Matrix right)
107 {
108 if (left==null)
109 {
110 throw new ArgumentNullException("参数1无效");
111 }
112 if (right == null)
113 {
114 throw new ArgumentNullException("参数2无效");
115 }
116 int rows = left.rows;
117 int columns = left.columns;
118 double[][] data = left.Array;
119 if ((rows!=right.rows)||(columns!==right.columns))
120 {
121 throw new ArgumentException("矩阵不相符");
122 }
123 Matrix X=new Matrix(rows ,columns);
124 double[][] x=X.Array;
125 for (int i = 0; i < rows; i++)
126 {
127 for (int j = 0; j < columns; j++)
128 {
129 x[i][j]=data[i][j]+right[i,j];
130 }
131 }
132 return X ;
133 }
134 //重载“+”运算符
135 public static Matrix operator +(Matrix left, Matrix right)
136 {
137 if (left==null)
138 {
139 throw new ArgumentNullException("参数1无效");
140 }
141 if (right == null)
142 {
143 throw new ArgumentNullException("参数2无效");
144 }
145 return Add(left,right );
146 }
147
148 public static Matrix Subtract(Matrix left, Matrix right)
149 {
150 if (left==null)
151 {
152 throw new ArgumentNullException("参数1无效");
153 }
154 if (right == null)
155 {
156 throw new ArgumentNullException("参数2无效");
157 }
158 int rows = left.rows;
159 int columns = left.columns;
160 double[][] data = left.Array;
161 if ((rows!=right.rows)||(columns!==right.columns))
162 {
163 throw new ArgumentException("矩阵不相符");
164 }
165 Matrix X=new Matrix(rows ,columns);
166 double[][] x=X.Array;
167 for (int i = 0; i < rows; i++)
168 {
169 for (int j = 0; j < columns; j++)
170 {
171 x[i][j]=data[i][j]-right[i,j];
172 }
173 }
174 return X;
175 }
176 //重载“-”减号运算符
177 public static Matrix operator -(Matrix left, Matrix right)
178 {
179 if (left == null)
180 {
181 throw new ArgumentNullException("参数1无效");
182 }
183 if (right == null)
184 {
185 throw new ArgumentNullException("参数2无效");
186 }
187 return Subtract(left, right);
188 }
189
190 //两个矩阵相乘
191 public static Matrix Multiply(Matrix left, Matrix right)
192 {
193 if (left==null)
194 {
195 throw new ArgumentNullException("参数1无效");
196 }
197 if (right == null)
198 {
199 throw new ArgumentNullException("参数2无效");
200 }
201 int rows = left.rows;
202 double[][] data = left.Array;
203 if (right.rows!==left.columns))
204 {
205 throw new ArgumentException("矩阵维数不相符");
206 }
207
208 int columns = left.columns;
209 Matrix X=new Matrix(rows ,columns);
210 double[][] x=X.Array;
211
212 int size=left.columns;
213 double [] column=new double[size];
214
215 for (int j= 0; j < rows; j++)
216 {
217 for (int k = 0; k < size; k++)
218 {
219 columns[k]=right [k,j];
220 }
221 for (int i = 0; i < rows; i++)
222 {
223 double [] row=data[i];
224 double s=0;
225 for (int k = 0; k < size; k++)
226 {
227 s+=row[k]*column[k];
228 }
229 x[i][j]=s;
230 }
231 }
232 return X ;
233 }
234 //重载“*”运算符,用于两个矩阵相乘
235 public static Matrix operator *(Matrix left, Matrix right)
236 {
237 if (left == null)
238 {
239 throw new ArgumentNullException("参数1无效");
240 }
241 if (right == null)
242 {
243 throw new ArgumentNullException("参数2无效");
244 }
245 return Multiply(left, right);
246 }
247
248 //一个矩阵和乘数相乘
249 public static Matrix Multiply(Matrix left, double right)
250 {
251 if (left==null)
252 {
253 throw new ArgumentNullException("参数1无效");
254 }
255
256 int rows = left.rows;
257 int columns = left.columns;
258 double[][] data = left.Array;
259 Matrix X=new Matrix(rows ,columns);
260 double[][] x=X.Array;
261
262 for (int i= 0; i < rows; i++)
263 {
264 for (int j = 0; j < columns; j++)
265 {
266 x[i][j] = data[i][j] + right;
267 }
268 }
269 return X ;
270 }
271 //重载“*”运算符,用于一个矩阵和乘数相乘
272 public static Matrix operator *(Matrix left,double right)
273 {
274 if (left == null)
275 {
276 throw new ArgumentNullException("参数1无效");
277 }
278 return Multiply(left, right);
279 }
280 }
281}
282
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace ConsoleApplication4
7{
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 }
13 }
14
15 //定义矩阵类
16 public class Matrix
17 {
18 private double[][] data;
19 private int rows;
20 private int columns;
21 private static Random random = new Random();
22 //构造一个空矩阵
23 public Matrix(int rows, int columns)
24 {
25 this.rows = rows;
26 this.columns = columns;
27 this.data = new double[rows][];
28 for (int i = 0; i < rows; i++)
29 {
30 this.data[i] = new double[columns];
31 }
32 }
33 //参数为行数和列数及一个给定值,构造一个矩阵
34 public Matrix(int rows, int columns, double value)
35 {
36 this.rows = rows;
37 this.columns = columns;
38 this.data = new double[rows][];
39 for (int i = 0; i < rows; i++)
40 {
41 data[i] = new double[columns];
42 }
43 for (int i = 0; i < rows; i++)
44 {
45 data[i][i] = value;
46 }
47 }
48 //参数为一个二维数组,表示所有矩阵元素值,构造一个矩阵
49 public Matrix(double[][] value)
50 {
51 this.rows = value.Length;
52 this.columns = value[0].Length;
53 for (int i = 0; i < rows; i++)
54 {
55 if (value[i].Length!=columns )
56 {
57 throw new ArgumentException("参数超出范围");
58 }
59 }
60 this.data = value;
61 }
62
63 public static Matrix Negate(Matrix value)
64 {
65 if (value ==null)
66 {
67 throw new ArgumentNullException("参数无效");
68 }
69 int rows = value.rows;
70 int columns = value.columns;
71 double[][] data = value.Array; //不知道Array哪里定义的
72
73 Matrix X = new Matrix(rows,columns);
74 double[][] x = X.Array;
75 for (int i = 0; i < rows; i++)
76 {
77 for (int j = 0; j < columns; j++)
78 {
79 x[i][j] = -data[i][j];
80 }
81 }
82 return X;
83 }
84 //重载“-”负号运算符
85 public static Matrix operator -(Matrix value)
86 {
87 if (value ==null)
88 {
89 throw new ArgumentNullException("参数无效");
90 }
91 //重载时调用了Negate方法,实现在Negate方法中
92 return Negate(value);
93
94 }
95
96 //“==”和“!=”运算符必须成对重载,而且同时必须重载Equals方法,
97 public static bool operator ==(Matrix left,Matrix right)
98 {
99 return Equals(let,right );
100 }
101 public static bool operator !=(Matrix left, Matrix right)
102 {
103 return !Equals(left, right);
104 }
105
106 public static Matrix Add(Matrix left,Matrix right)
107 {
108 if (left==null)
109 {
110 throw new ArgumentNullException("参数1无效");
111 }
112 if (right == null)
113 {
114 throw new ArgumentNullException("参数2无效");
115 }
116 int rows = left.rows;
117 int columns = left.columns;
118 double[][] data = left.Array;
119 if ((rows!=right.rows)||(columns!==right.columns))
120 {
121 throw new ArgumentException("矩阵不相符");
122 }
123 Matrix X=new Matrix(rows ,columns);
124 double[][] x=X.Array;
125 for (int i = 0; i < rows; i++)
126 {
127 for (int j = 0; j < columns; j++)
128 {
129 x[i][j]=data[i][j]+right[i,j];
130 }
131 }
132 return X ;
133 }
134 //重载“+”运算符
135 public static Matrix operator +(Matrix left, Matrix right)
136 {
137 if (left==null)
138 {
139 throw new ArgumentNullException("参数1无效");
140 }
141 if (right == null)
142 {
143 throw new ArgumentNullException("参数2无效");
144 }
145 return Add(left,right );
146 }
147
148 public static Matrix Subtract(Matrix left, Matrix right)
149 {
150 if (left==null)
151 {
152 throw new ArgumentNullException("参数1无效");
153 }
154 if (right == null)
155 {
156 throw new ArgumentNullException("参数2无效");
157 }
158 int rows = left.rows;
159 int columns = left.columns;
160 double[][] data = left.Array;
161 if ((rows!=right.rows)||(columns!==right.columns))
162 {
163 throw new ArgumentException("矩阵不相符");
164 }
165 Matrix X=new Matrix(rows ,columns);
166 double[][] x=X.Array;
167 for (int i = 0; i < rows; i++)
168 {
169 for (int j = 0; j < columns; j++)
170 {
171 x[i][j]=data[i][j]-right[i,j];
172 }
173 }
174 return X;
175 }
176 //重载“-”减号运算符
177 public static Matrix operator -(Matrix left, Matrix right)
178 {
179 if (left == null)
180 {
181 throw new ArgumentNullException("参数1无效");
182 }
183 if (right == null)
184 {
185 throw new ArgumentNullException("参数2无效");
186 }
187 return Subtract(left, right);
188 }
189
190 //两个矩阵相乘
191 public static Matrix Multiply(Matrix left, Matrix right)
192 {
193 if (left==null)
194 {
195 throw new ArgumentNullException("参数1无效");
196 }
197 if (right == null)
198 {
199 throw new ArgumentNullException("参数2无效");
200 }
201 int rows = left.rows;
202 double[][] data = left.Array;
203 if (right.rows!==left.columns))
204 {
205 throw new ArgumentException("矩阵维数不相符");
206 }
207
208 int columns = left.columns;
209 Matrix X=new Matrix(rows ,columns);
210 double[][] x=X.Array;
211
212 int size=left.columns;
213 double [] column=new double[size];
214
215 for (int j= 0; j < rows; j++)
216 {
217 for (int k = 0; k < size; k++)
218 {
219 columns[k]=right [k,j];
220 }
221 for (int i = 0; i < rows; i++)
222 {
223 double [] row=data[i];
224 double s=0;
225 for (int k = 0; k < size; k++)
226 {
227 s+=row[k]*column[k];
228 }
229 x[i][j]=s;
230 }
231 }
232 return X ;
233 }
234 //重载“*”运算符,用于两个矩阵相乘
235 public static Matrix operator *(Matrix left, Matrix right)
236 {
237 if (left == null)
238 {
239 throw new ArgumentNullException("参数1无效");
240 }
241 if (right == null)
242 {
243 throw new ArgumentNullException("参数2无效");
244 }
245 return Multiply(left, right);
246 }
247
248 //一个矩阵和乘数相乘
249 public static Matrix Multiply(Matrix left, double right)
250 {
251 if (left==null)
252 {
253 throw new ArgumentNullException("参数1无效");
254 }
255
256 int rows = left.rows;
257 int columns = left.columns;
258 double[][] data = left.Array;
259 Matrix X=new Matrix(rows ,columns);
260 double[][] x=X.Array;
261
262 for (int i= 0; i < rows; i++)
263 {
264 for (int j = 0; j < columns; j++)
265 {
266 x[i][j] = data[i][j] + right;
267 }
268 }
269 return X ;
270 }
271 //重载“*”运算符,用于一个矩阵和乘数相乘
272 public static Matrix operator *(Matrix left,double right)
273 {
274 if (left == null)
275 {
276 throw new ArgumentNullException("参数1无效");
277 }
278 return Multiply(left, right);
279 }
280 }
281}
282