![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif) 6 namespace ConsoleApplication4 7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) { 8 class Program 9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 10 static void Main(string[] args) 11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 12 } 13 } 14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 15 //定义矩阵类 16 public class Matrix 17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 25 this.rows = rows; 26 this.columns = columns; 27 this.data = new double[rows][]; 28 for (int i = 0; i < rows; i++) 29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 30 this.data[i] = new double[columns]; 31 } 32 } 33 //参数为行数和列数及一个给定值,构造一个矩阵 34 public Matrix(int rows, int columns, double value) 35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 36 this.rows = rows; 37 this.columns = columns; 38 this.data = new double[rows][]; 39 for (int i = 0; i < rows; i++) 40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 41 data[i] = new double[columns]; 42 } 43 for (int i = 0; i < rows; i++) 44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 45 data[i][i] = value; 46 } 47 } 48 //参数为一个二维数组,表示所有矩阵元素值,构造一个矩阵 49 public Matrix(double[][] value) 50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 51 this.rows = value.Length; 52 this.columns = value[0].Length; 53 for (int i = 0; i < rows; i++) 54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 55 if (value[i].Length!=columns ) 56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 57 throw new ArgumentException("参数超出范围"); 58 } 59 } 60 this.data = value; 61 } 62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 63 public static Matrix Negate(Matrix value) 64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 65 if (value ==null) 66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 67 throw new ArgumentNullException("参数无效"); 68 } 69 int rows = value.rows; 70 int columns = value.columns; 71 double[][] data = value.Array; //不知道Array哪里定义的 72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 73 Matrix X = new Matrix(rows,columns); 74 double[][] x = X.Array; 75 for (int i = 0; i < rows; i++) 76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 77 for (int j = 0; j < columns; j++) 78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 79 x[i][j] = -data[i][j]; 80 } 81 } 82 return X; 83 } 84 //重载“-”负号运算符 85 public static Matrix operator -(Matrix value) 86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 87 if (value ==null) 88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 89 throw new ArgumentNullException("参数无效"); 90 } 91 //重载时调用了Negate方法,实现在Negate方法中 92 return Negate(value); 93 94 } 95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 96 //“==”和“!=”运算符必须成对重载,而且同时必须重载Equals方法, 97 public static bool operator ==(Matrix left,Matrix right) 98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 99 return Equals(let,right ); 100 } 101 public static bool operator !=(Matrix left, Matrix right) 102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 103 return !Equals(left, right); 104 } 105 106 public static Matrix Add(Matrix left,Matrix right) 107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 108 if (left==null) 109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 110 throw new ArgumentNullException("参数1无效"); 111 } 112 if (right == null) 113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 127 for (int j = 0; j < columns; j++) 128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 137 if (left==null) 138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 139 throw new ArgumentNullException("参数1无效"); 140 } 141 if (right == null) 142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 143 throw new ArgumentNullException("参数2无效"); 144 } 145 return Add(left,right ); 146 } 147![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 148 public static Matrix Subtract(Matrix left, Matrix right) 149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 150 if (left==null) 151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 152 throw new ArgumentNullException("参数1无效"); 153 } 154 if (right == null) 155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 169 for (int j = 0; j < columns; j++) 170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 179 if (left == null) 180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 181 throw new ArgumentNullException("参数1无效"); 182 } 183 if (right == null) 184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 185 throw new ArgumentNullException("参数2无效"); 186 } 187 return Subtract(left, right); 188 } 189![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 190 //两个矩阵相乘 191 public static Matrix Multiply(Matrix left, Matrix right) 192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 193 if (left==null) 194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 195 throw new ArgumentNullException("参数1无效"); 196 } 197 if (right == null) 198![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 199 throw new ArgumentNullException("参数2无效"); 200 } 201 int rows = left.rows; 202 double[][] data = left.Array; 203 if (right.rows!==left.columns)) 204![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 205 throw new ArgumentException("矩阵维数不相符"); 206 } 207![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 208 int columns = left.columns; 209 Matrix X=new Matrix(rows ,columns); 210 double[][] x=X.Array; 211![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 212 int size=left.columns; 213 double [] column=new double[size]; 214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 215 for (int j= 0; j < rows; j++) 216![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 217 for (int k = 0; k < size; k++) 218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 219 columns[k]=right [k,j]; 220 } 221 for (int i = 0; i < rows; i++) 222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 223 double [] row=data[i]; 224 double s=0; 225 for (int k = 0; k < size; k++) 226![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 237 if (left == null) 238![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 239 throw new ArgumentNullException("参数1无效"); 240 } 241 if (right == null) 242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 243 throw new ArgumentNullException("参数2无效"); 244 } 245 return Multiply(left, right); 246 } 247![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 248 //一个矩阵和乘数相乘 249 public static Matrix Multiply(Matrix left, double right) 250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 251 if (left==null) 252![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 264 for (int j = 0; j < columns; j++) 265![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 274 if (left == null) 275![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 276 throw new ArgumentNullException("参数1无效"); 277 } 278 return Multiply(left, right); 279 } 280 } 281 } 282![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
|