//LinearEquations构造函数 public LinearEquations(CzSquareMatrix A, CzVector b) { this.m = A; this.c = CzMatrix.transposition((CzMatrix)b); }
//矩阵元素更新方法 protected static void matrixrefresh(CzMatrix m) { double[] temp1 = new double[m.ncol]; double[] temp2 = new double[m.nrow]; for (int i = 0; i < m.nrow; i++) { for (int j = 0; j < m.ncol; j++) { temp1[j] = m.matrix[i,j]; } m.row[i] = new CzVector(temp1); } for (int i = 0; i < m.ncol; i++) { for (int j = 0; j < m.nrow; j++) temp2[j] = m.matrix[j, i]; m.col[i] = new CzVector(temp2); } }
//行向量更新方法 protected static void rowrefresh(CzMatrix m) { double[] temp=new double[m.nrow]; for (int i = 0; i < m.nrow; i++) { for (int j = 0; j < m.ncol; j++) m.matrix[i, j] = m.row[i].values[j]; } for (int i = 0; i < m.ncol; i++) { for (int j = 0; j < m.nrow; j++) temp[j] = m.matrix[j, i]; m.col[i] = new CzVector(temp); } }
//初等行数乘变换 public CzMatrix Mutiplyrow(CzMatrix m, int a, double c) { for (int i = 0; i < m.row[a].length; i++) { m.row[a][i] *= c; } rowrefresh(m); return m; }
//删除行 public CzMatrix Removerow( int a) { CzMatrix Mresult; List<CzVector> list = this.row.ToList(); list.RemoveAt(a); CzVector[] row = list.ToArray(); //for (int i = a ; i < this.nrow-1;i++ ) //this.row[i] = new CzVector(this.row[i + 1].values); this.nrow -= 1; rowrefresh(this); Mresult = new CzMatrix(row); return Mresult; }
//重载TryParse方法 public static bool TryParse(string s, out CzMatrix Mresult) { CzVector[] Vresult; string[] ss = s.Split(';'); Vresult = new CzVector[ss.Length]; for (int i = 0; i < ss.Length; i++) { if (!CzVector.TryParse(ss[i], out Vresult[i])) { Mresult = null; return false; } else Vresult[i] = CzVector.Parse(ss[i]); } Mresult = new CzMatrix(Vresult); return true; }
//初等列数乘变换 public CzMatrix Mutiplycol(CzMatrix m, int a, double c) { for (int i = 0; i < m.col[a].length; i++) { m.col[a][i] *= c; } colrefresh(m); return m; }
//静态删除行 public static void Removerow(CzMatrix m, int a) { List<CzVector> list = m.row.ToList(); list.RemoveAt(a); m.row = list.ToArray(); m.nrow -= 1; rowrefresh(m); }
//矩阵转置 public static CzMatrix transposition(CzMatrix m) { CzMatrix Mresult=new CzMatrix(m.ncol,m.nrow); for (int i = 0; i < m.ncol; i++) { for (int j = 0; j < m.nrow; j++) Mresult.matrix[i, j] = m.matrix[j, i]; } matrixrefresh(Mresult); return Mresult; }
//静态初等行消法变换,第a行乘c加到第b行上 public static void Multiplyaddrow(CzMatrix m, int a, int b, double c) { for (int i = 0; i < m.row[b].length; i++) { m.row[b][i] += c * m.row[a][i]; } rowrefresh(m); }
//静态删除列 public static void Removecol(CzMatrix m, int a) { List<CzVector> list = m.col.ToList(); list.RemoveAt(a); m.col = list.ToArray(); m.ncol -= 1; colrefresh(m); }
//静态初等列消法变换,第a列乘c加到第b列上 public static void Multiplyaddcol(CzMatrix m, int a, int b, double c) { for (int i = 0; i < m.col[b].length; i++) { m.col[b][i] += c * m.col[a][i]; } colrefresh(m); }
//初等行变换 public static CzMatrix Exchangerow(CzMatrix m, int a, int b) { CzVector temp = m.row[a]; m.row[a] = m.row[b]; m.row[b] = temp; rowrefresh(m); return m; }
//初等列变换 public static CzMatrix Exchangecol(CzMatrix m, int a, int b) { CzVector temp = m.col[a]; m.col[a] = m.col[b]; m.col[b] = temp; colrefresh(m); return m; }
//减法重载 public static CzMatrix operator -(CzMatrix m1, CzMatrix m2) { if (m1.ncol != m2.ncol || m1.nrow != m2.nrow) throw new ArgumentException("矩阵减法时维数不同"); CzMatrix Mresult = new CzMatrix(m1.nrow, m1.ncol); for (int i = 0; i < m1.nrow; i++) { Mresult.row[i] = m1.row[i]-m2.row[i]; } rowrefresh(Mresult); return Mresult; }
//列变换计算行列式的值 public static double Determinant(CzSquareMatrix mfactor) { CzMatrix m = new CzMatrix(mfactor.row); int dim = m.nrow; int count=0;//count用来记录矩阵行列变换引起的行列式值的变化 for (int i = dim-1; i >= 0; i--) { if (m.matrix[i, i] == 0) { int k = i; while (m.matrix[k, i] == 0) { k--; if (k < 0) break; } if (k >= 0) { CzMatrix.Exchangerow(m, k, i); count++; } } for(int j=dim-1;j>i;j--) { if (m.matrix[j, j] != 0) { Multiplyaddcol(m, j, i, -m.matrix[j, i] / m.matrix[j, j]); colrefresh(m); } } } colrefresh(m); double product=1; for (int i=0;i<dim;i++) product*=m.matrix[i,i]; return product*Math.Pow(-1,count); }