public static FixMatrix operator *(FixMatrix lhs, FixMatrix rhs) { if (lhs.Col != rhs.Row) { System.Exception e = new Exception("相乘的两个矩阵的行列数不匹配"); throw e; } FixMatrix ret = new FixMatrix(lhs.Row, rhs.Col); double temp; for (int i = 0; i < lhs.Row; i++) { for (int j = 0; j < rhs.Col; j++) { temp = 0; for (int k = 0; k < lhs.Col; k++) { temp += lhs[i, k] * rhs[k, j]; } ret[i, j] = temp; } } return(ret); }
public static FixMatrix operator -(FixMatrix lhs, FixMatrix rhs) { if (lhs.Row != rhs.Row) { System.Exception e = new Exception("相减的两个矩阵的行数不等"); throw e; } if (lhs.Col != rhs.Col) { System.Exception e = new Exception("相减的两个矩阵的列数不等"); throw e; } int row = lhs.Row; int col = lhs.Col; FixMatrix ret = new FixMatrix(row, col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { double d = lhs[i, j] - rhs[i, j]; ret[i, j] = d; } } return(ret); }
//transpose 转置 public FixMatrix Transpose() { FixMatrix ret = new FixMatrix(Col, Row); for (int i = 0; i < Row; i++) { for (int j = 0; j < Col; j++) { ret[j, i] = m_data[i, j]; } } return(ret); }
public static FixMatrix operator *(double d, FixMatrix m) { FixMatrix ret = new FixMatrix(m); for (int i = 0; i < ret.Row; i++) { for (int j = 0; j < ret.Col; j++) { ret[i, j] *= d; } } return(ret); }
public FixMatrix(FixMatrix m) { int row = m.Row; int col = m.Col; m_data = new double[row, col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { m_data[i, j] = m[i, j]; } } }
//inversion 逆阵:使用矩阵的初等变换,列主元素消去法 public FixMatrix Inverse() { if (Row != Col) //异常,非方阵 { System.Exception e = new Exception("求逆的矩阵不是方阵"); throw e; } StreamWriter sw = new StreamWriter("..\\annex\\close_FixMatrix.txt"); FixMatrix tmp = new FixMatrix(this); FixMatrix ret = new FixMatrix(Row); //单位阵 ret.SetUnit(); int maxIndex; double dMul; for (int i = 0; i < Row; i++) { maxIndex = tmp.Pivot(i); if (tmp.m_data[maxIndex, i] == 0) { System.Exception e = new Exception("求逆的矩阵的行列式的值等于0,"); throw e; } if (maxIndex != i) //下三角阵中此列的最大值不在当前行,交换 { tmp.Exchange(i, maxIndex); ret.Exchange(i, maxIndex); } ret.Multiple(i, 1 / tmp[i, i]); tmp.Multiple(i, 1 / tmp[i, i]); for (int j = i + 1; j < Row; j++) { dMul = -tmp[j, i] / tmp[i, i]; tmp.MultipleAdd(j, i, dMul); } sw.WriteLine("tmp=\r\n" + tmp); sw.WriteLine("ret=\r\n" + ret); } //end for sw.WriteLine("**=\r\n" + this * ret); for (int i = Row - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { dMul = -tmp[j, i] / tmp[i, i]; tmp.MultipleAdd(j, i, dMul); ret.MultipleAdd(j, i, dMul); } } sw.WriteLine("tmp=\r\n" + tmp); sw.WriteLine("ret=\r\n" + ret); sw.WriteLine("***=\r\n" + this * ret); sw.Close(); return(ret); }
public static FixMatrix operator +(FixMatrix m) { FixMatrix ret = new FixMatrix(m); return(ret); }