//幂法求矩阵绝对值最大的特征值 public static double Characteristic(CzSquareMatrix m,double eps, int N) { int k=1; double[] a=new double[m.dim]; for (int i = 0; i < m.dim; i++) { a[i]=1; } CzVector v = new CzVector(a); CzVector v1 = (CzVector)transposition(m*transposition((CzMatrix)v)); double b = CzVector.max(v1); double z = 0; while (Math.Abs(z - b) > eps && k < N) { k = k + 1; z = b; if (CzVector.max(v1)==0) throw new DivideByZeroException("幂法求解过程中出现全0向量,请检查输入数据后重新运算!"); double mv = CzVector.max(v1); v1 = v1 * (1 / mv); v1 = (CzVector)transposition(m * transposition((CzMatrix)v1)); b = CzVector.max(v1); } return b; }
//矩阵构造 public CzMatrix(params CzVector[] a) { nrow = a.Length; int temp = a[0].length; foreach (CzVector b in a) { if (temp != b.length) throw new ArgumentException("矩阵向量长度不一致"); } ncol = temp; col = new CzVector[ncol]; row = new CzVector[nrow]; double[] temp1 = new double[ncol]; double[] temp2 = new double[nrow]; matrix = new double[nrow, ncol]; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { temp1[j] = a[i].values[j]; matrix[i, j] = a[i].values[j]; } row[i]=(new CzVector(temp1)); } for (int i = 0; i < ncol; i++) { for (int j = 0; j < nrow; j++) temp2[j] = matrix[j, i]; col[i] = new CzVector(temp2); } }
//CzSquareMatrix带参构造函数 public CzSquareMatrix(CzVector[] a) : base(a) { if (a.Length != a[0].Length) throw new ArgumentException("该矩阵不是方阵"); CzSquareMatrix UnitMatrix = new CzSquareMatrix(a.Length,a.Length); CzSquareMatrix ZeroMatrix = new CzSquareMatrix(a.Length,a.Length); dim = this.nrow; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { ZeroMatrix.matrix[i, j] = 0; if (i != j) UnitMatrix.matrix[i, j] = 0; else UnitMatrix.matrix[i, i] = 1; } } }
//LinearEquations构造函数 public LinearEquations(CzSquareMatrix A, CzVector b) { this.m = A; this.c = CzMatrix.transposition((CzMatrix)b); }
//重载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; }
//重载Parse方法 public static CzMatrix Parse(string s) { CzVector[] Vresult; string[] ss = s.Split(';'); Vresult = new CzVector[ss.Length]; for (int i = 0; i < ss.Length; i++) { Vresult[i] = CzVector.Parse(ss[i]); } return (new CzMatrix(Vresult)); }
//矩阵乘法重载 public static CzMatrix operator *(CzMatrix m1, CzMatrix m2) { CzVector[] Vresult; double[] varray=new double[m2.ncol]; if (m1.ncol != m2.nrow) throw new ArgumentException("矩阵乘法时维数不同"); Vresult = new CzVector[m1.nrow]; for (int i = 0; i < m1.nrow; i++) { for (int j=0;j<m2.ncol;j++) varray[j] =m1.row[i]*m2.col[j]; Vresult[i] = new CzVector(varray); } return new CzMatrix(Vresult); }
public static bool TryParse(string s, out CzVector v) { double c; string[] ss = s.Split(' '); v = new CzVector(ss.Length); for (int i = 0; i < v.Length; i++) { if (!double.TryParse(ss[i], out c)) return false; else v.values[i] = double.Parse(ss[i]); } return true; }
public static CzVector Parse(string s) { double c; string[] ss = s.Split(' '); CzVector v = new CzVector(ss.Length); for (int i = 0; i < v.Length; i++) { if (!double.TryParse(ss[i], out c)) throw new FormatException(); else v.values[i] = double.Parse(ss[i]); } return v; }
public static double max(CzVector v) { double rmax = Math.Abs(v.values[0]); for (int i = 1; i < v.Length; i++) { if (Math.Abs(v.values[i]) > rmax) rmax = Math.Abs(v.values[i]); } return rmax; }
public static CzVector operator -(CzVector v1, CzVector v2) { if (v1.Length != v2.Length) throw new ArgumentException(); CzVector result = new CzVector(v1.Length); for (int i = 0; i < v1.Length; i++) result[i] = v1[i] - v2[i]; return result; }
public static CzVector operator *(CzVector v2, double l) { CzVector result = new CzVector(v2.Length); for (int i = 0; i < v2.Length; i++) result[i] = l * v2[i]; return result; }