// vres =v1+cv2; public static Interface.IVector Add(Interface.IVector v1, Interface.IVector v2, double c) { Vector result = new Vector(v1.Size); for (int i = 0; i < v2.Size; i++) { v1[i] += v2[i] * c; } return result; }
public void ReadFromFile(out IMatrix A, out Vector b, out Vector x_init) { // Не хватает проверки path using (System.IO.StreamReader file = new System.IO.StreamReader(path)) { var st = file.ReadToEnd().Replace('.',',').Split(new char[] { '\n', ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); int n, m; if (!int.TryParse(st[0], out n)) throw new Exception("Bad file format"); n = int.Parse(st[0]); int[] ia = new int[n + 1]; double[] d = new double[n]; double[] rightPart = new double[n]; double[] x0 = new double[n]; long offset = 1; for (int i = 0; i < n + 1; i++, offset++) ia[i] = int.Parse(st[offset]); m = ia[n] - 1; int[] ja = new int[m]; double[] al = new double[m]; double[] au = new double[m]; for (int i = 0; i < m; i++, offset++) ja[i] = int.Parse(st[offset]); if (ia[0] != 0) { for (int i = 0; i < n + 1; i++) ia[i]--; for (int i = 0; i < m; i++) ja[i]--; } for (int i = 0; i < m; i++,offset++) al[i] = double.Parse(st[offset]); for (int i = 0; i < m; i++, offset++) au[i] = double.Parse(st[offset]); for (int i = 0; i < n; i++,offset++) d[i] = double.Parse(st[offset]); for (int i = 0; i < n; i++,offset++) rightPart[i] = double.Parse(st[offset]); for (int i = 0; i < n; i++, offset++) x0[i] = double.Parse(st[offset]); A = new RowColumnSparseMatrix(n, ia, ja, al, au, d); b = new Vector(rightPart); x_init = new Vector(x0); } }
public CoordinateMatrix(int _size, int[] _iaddr, int[] _jaddr, double[] _elem) { size = _size; iaddr = _iaddr; jaddr = _jaddr; elem = _elem; di = new Vector(size); for (int j = 0; j < size; j++ ) for (int i = 0; i < elem.Length; i++) { if (iaddr[i] == jaddr[i]) di[j] = elem[i]; } }
public override IVector Solve(IMatrix A, IVector b, IVector x0) { IVector result = new Vector(b.Size); IVector r = new Vector(b.Size); IVector p = new Vector(b.Size); IVector z = new Vector(b.Size); double alpha, betta; double p_scal; r.Equalize(b); result.Equalize(x0); r.Add(MatrixAssistant.multMatrixVector(A, result), -1); z.Equalize(r); p.Equalize(MatrixAssistant.multMatrixVector(A, z)); p_scal = VectorAssistant.multVector(p, p); residual = VectorAssistant.multVector(r, r); residual = Math.Sqrt(VectorAssistant.multVector(r, r)) / b.Norm; for (iteration = 1; iteration <= maxIteration && residual > minResidual; iteration++) { alpha = VectorAssistant.multVector(p, r) / p_scal; result.Add(z, alpha); r.Add(p, -alpha); betta = -VectorAssistant.multVector(p, MatrixAssistant.multMatrixVector(A, r)) / p_scal; z = VectorAssistant.multScalar(betta, z); z.Add(r, 1); p = VectorAssistant.multScalar(betta, p); p.Add(MatrixAssistant.multMatrixVector(A, r), 1); /* residual -= alpha * alpha * p_scal; if (residual < 0) //она всё равно отрицательная { residual += alpha * alpha * p_scal; return result; } if (alpha * alpha * p_scal < residual * 10E-5) return result; */ p_scal = VectorAssistant.multVector(p, p); residual = Math.Sqrt(VectorAssistant.multVector(r, r)) / b.Norm; Debugger.DebugSolver(iteration, residual, result); } return result; }
public override IVector Solve(IMatrix A, IVector b, IVector x0) { IVector x_prev= new Vector(b.Size); IVector result = new Vector(b.Size); IVector difference = new Vector(b.Size);//f-Ax double residual1, residual2; double residual_prev;//для проверки изменения невязки residual =1; residual1 = b.Norm; x_prev.Equalize(x0); for (int i = 0; i < b.Size; i++) if (Math.Abs(A.Diagonal[i]) < EPS_NULL) throw new Exception("Divide by NULL in GaussZeidel_solver: diagonal"); residual_prev = 2 * residual; for (iteration = 0; iteration < maxIteration && residual > minResidual && residual_prev > residual; iteration++) { difference.Equalize(b); //result.Nullify(); for (int i = 0; i < b.Size; i++) { for (int j = 0; j < i; j++) difference[i] -= A[i,j]*result[j]; for (int j = i; j < b.Size; j++) { difference[i] -= A[i,j]*x_prev[j]; } difference[i] *= (relaxation/A.Diagonal[i]); result[i] = x_prev[i] + difference[i]; } difference.Equalize(b); difference.Add(MatrixAssistant.multMatrixVector(A, result), -1); residual2 = difference.Norm; residual_prev = residual; residual = residual2 / residual1; //Debugger.DebugSolver(iteration, residual, result); x_prev.Equalize(result); } return result; }
public override IVector Solve(IMatrix A, IVector b, IVector x0) { IVector x_prev= new Vector(b.Size); IVector result = new Vector(b.Size); IVector difference = new Vector(b.Size);//f-Ax double residual1, residual2; double residual_prev;//для проверки изменения невязки residual =1; residual1 = b.Norm; x_prev.Equalize(x0); for (int i = 0; i < b.Size; i++) if (Math.Abs(A.Diagonal[i]) < EPS_NULL) throw new Exception("Divide by NULL in GaussZeidel_solver: diagonal"); residual_prev = 2 * residual; for (iteration = 0; iteration < maxIteration && residual > minResidual && residual_prev > residual; iteration++) { difference.Equalize(b); difference.Add(MatrixAssistant.multMatrixUpperVector(A,x_prev),-1); for (int i = 0; i < b.Size; i++) { double sum = 0; IVector Lx = MatrixAssistant.multMatrixLowerVector(A, result, i); for (int ii = 0; ii < Lx.Size; ii++) sum += Lx[ii]; difference[i] -= sum; result[i] = relaxation*difference[i]/A.Diagonal[i]; } difference.Equalize(b); difference.Add(MatrixAssistant.multMatrixVector(A, result), -1); residual2 = difference.Norm; residual_prev = residual; residual = residual2 / residual1; Debugger.DebugSolver(iteration, residual, result); x_prev.Equalize(result); } return result; }
public override IVector Solve(IMatrix A, IVector b, IVector x0) { IVector x = new Vector(b.Size); IVector r = new Vector(b.Size); IVector z = new Vector(b.Size); // Check errors double Azz, rr; double norm; norm = b.Norm; if (norm < EPS_NULL) { x.Nullify(); return x; } x.Equalize(x0); r.Equalize(b); r.Add(MatrixAssistant.multMatrixVector(A,x),-1); z.Equalize(r); alpha = 1000; for (iteration = 0; iteration < maxIteration && residual > minResidual; iteration++ ) { rr = VectorAssistant.multVector(r, r); var Az = MatrixAssistant.multMatrixVector(A, z); Azz = VectorAssistant.multVector(Az, z); if (Math.Abs(Azz) < EPS_NULL) return x; alpha = rr / Azz; x.Add(z,alpha); r.Add(Az, -alpha); betta = VectorAssistant.multVector(r, r) / rr; z=VectorAssistant.multScalar(z,betta); z.Add(r, 1); residual = r.Norm / norm; } return x; }
public override IVector Solve(IMatrix A, IVector b, IVector x0) { IVector result = new Vector(b.Size); IVector difference = new Vector(b.Size);//f-Ax double residual1, residual2; double residual_prev;//для проверки изменения невязки residual = 1; residual1 = b.Norm; result.Equalize(x0); difference.Equalize(b); difference.Add(MatrixAssistant.multMatrixVector(A, result), -1); for (int i = 0; i < b.Size; i++) if (Math.Abs(A.Diagonal[i]) < EPS_NULL) throw new Exception("Divide by NULL in Jacobi_solver: diagonal"); residual_prev = 2 * residual; for (iteration = 0; iteration < maxIteration && residual > minResidual && residual_prev > residual; iteration++) { for (int i = 0; i < b.Size; i++) { difference[i] = difference[i] / A.Diagonal[i]; } result.Add(difference, relaxation); difference.Equalize(b); difference.Add(MatrixAssistant.multMatrixVector(A, result), -1); residual2 = difference.Norm; if (Math.Abs(residual1) < EPS_NULL) throw new Exception("Divide by NULL in Jacobi_solver: residual1"); residual_prev = residual; residual = residual2 / residual1; //Debugger.DebugSolver(iteration, residual, result); } return result; }
public void ReadFromFileCoordinate(out IMatrix A, out Vector b, out Vector x_init) { using (System.IO.StreamReader file = new System.IO.StreamReader(path)) { var st = file.ReadToEnd().Replace('.',',').Split(new char[] { '\n', ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); int n, m; if (!int.TryParse(st[0], out n)) throw new Exception("Bad file format"); m = int.Parse(st[0]); double[] elem = new double[m]; int[] iaddr = new int[m]; int[] jaddr = new int[m]; long offset = 1; for (int i = 0; i < m; i++, offset++) { iaddr[i] = int.Parse(st[offset]); offset++; jaddr[i] = int.Parse(st[offset]); offset++; elem[i] = double.Parse(st[offset]); } if (iaddr[0] != 0) { for (int i = 0; i < m; i++) iaddr[i]--; for (int i = 0; i < m; i++) jaddr[i]--; } n = int.Parse(st[offset]); double[] rightPart = new double[n]; double[] x0 = new double[n]; offset++; for (int i = 0; i < n; i++, offset++) rightPart[i] = double.Parse(st[offset]); for (int i = 0; i < n; i++ /*offset++*/) x0[i] = 0;/*double.Parse(st[offset]);*/ A = new CoordinateMatrix(n, iaddr, jaddr, elem); b = new Vector(rightPart); x_init = new Vector(x0); } }
public void ReadFromFileDense(out IMatrix A, out Vector b, out Vector x_init) { using (System.IO.StreamReader file = new System.IO.StreamReader(path)) { var st = file.ReadToEnd().Replace('.', ',').Split(new char[] { '\n', ' ', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries); int n; if (!int.TryParse(st[0], out n)) throw new Exception("Bad file format"); n = int.Parse(st[0]); double[,] a = new double[n,n]; double[] rightPart = new double[n]; double[] x0 = new double[n]; long offset = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++, offset++) a[i,j] = double.Parse(st[offset]); for (int i = 0; i < n; i++, offset++) rightPart[i] = double.Parse(st[offset]); for (int i = 0; i < n; i++, offset++) x0[i] = double.Parse(st[offset]); A = new DenseMatrix(n, a); b = new Vector(rightPart); x_init = new Vector(x0); } }
public RowColumnSparseMatrix(int sz, int[] _ia, int[] _ja, double[] _al, double[] _au, double[] _di) { size = sz; ia = _ia; ja = _ja; al = new Vector(_al); di = new Vector(_di); au = new Vector(_au); }
public ProfileMatrix(int sz, int[] _ial, int[] _iau, double[] _al, double[] _au, double[] _di) { size = sz; ial = _ial; iau = _iau; al = new Vector(_al); au = new Vector(_au); di = new Vector(_di); }
public DenseMatrix(int sz, double[,] _a) { size = sz; a = new double[size, size]; a = _a; di = new Vector(size); for (int i=0; i< size; i++) { di[i] = a[i, i]; } }