private void Helper2(IElementalAccessMatrix A, IElementalAccessVector b, IElementalAccessVector x, ILinearSolver solver, IPreconditioner M, ILinearIteration iter) { Random r = new Random(); double K = r.NextDouble(), f = r.NextDouble(), gl = r.NextDouble(), gr = r.NextDouble(); //assemble(A, b, K, f); //boundary(A, b, x, gl, gr); A.SetValue(0, 0, 1); A.SetValue(0, 1, 1); A.SetValue(0, 2, 1.3); A.SetValue(1, 0, -0.5); A.SetValue(1, 1, -0.25); A.SetValue(1, 2, -1.25); A.SetValue(2, 0, 2.5); A.SetValue(2, 1, 3.25); A.SetValue(2, 2, -1.25); b.SetValue(0, 1.25); b.SetValue(1, 3.75); b.SetValue(1, -2.35); double[] ans = solve(A, b, x, solver, M, iter); //double[] ref_Renamed = reference(b.Length - 1, K, f, gl, gr); //checkEqual(ans, ref_Renamed); Console.WriteLine("!: " + solver + " " + M + " " + iter + " " + x.GetValue(0) + " " + x.GetValue(1) + " " + x.GetValue(2)); Blas.Default.Zero(A); Blas.Default.Zero(b); Blas.Default.Zero(x); }
/// <summary> Column-wise assembly using set</summary> /// <param name="nu">Maximum bandwidth on each column /// </param> /// <returns> Dense representation (not a direct copy) /// </returns> public static double[,] setAssembleColumnMatrix(IElementalAccessMatrix A, int nu) { int n = A.RowCount, m = A.ColumnCount; double[,] data = new double[n, m]; // for (int i = 0; i < n; i++) // { // data[i] = new double[m]; // } System.Random r = new System.Random(); // Construct the matrix for (int i = 0; i < m; ++i) { for (int j = 0; j < nu; ++j) { int ind = TesterUtilities.getInt(n, r); double entry = r.NextDouble(); data[ind, i] = entry; A.SetValue(ind, i, entry); } } return(data); }
private void boundary(IElementalAccessMatrix A, IElementalAccessVector b, IElementalAccessVector x, double gl, double gr) { int n = b.Length - 1; int[] boundary = new int[] { 0, n }; double[] boundaryV = new double[] { gl, gr }; x.SetValues(boundary, boundaryV); b.SetValues(boundary, boundaryV); A.SetValue(0, 0, 1); A.SetValue(0, 1, 0); A.SetValue(n, n, 1); A.SetValue(n, n - 1, 0); }
private void grad(IElementalAccessMatrix dy, IElementalAccessVector x) { IElementalAccessVector y0 = new DenseVector(x.Length); IElementalAccessVector y = new DenseVector(x.Length); model(y0, x); for (int i = 0; i < x.Length; i++) { x.AddValue(i, 0.000001); model(y, x); for (int j = 0; j < x.Length; j++) { dy.SetValue(j, i, (y.GetValue(j) - y0.GetValue(j)) / 0.000001); } x.AddValue(i, -0.000001); } }
/// <summary> Row-wise assembly using setValue</summary> /// <param name="nu">Maximum bandwidth on each row /// </param> /// <returns> Dense representation (not a direct copy) /// </returns> public static double[,] SetAssembleRowMatrix(IElementalAccessMatrix A, int nu) { int n = A.RowCount, m = A.ColumnCount; double[,] data = new double[n, m]; // for (int i = 0; i < n; i++) // { // data[i] = new double[m]; // } Random r = new Random(); for (int i = 0; i < n; ++i) { for (int j = 0; j < nu; ++j) { int ind = TesterUtilities.getInt(m, r); double entry = r.NextDouble(); data[i, ind] = entry; A.SetValue(i, ind, entry); } } return(data); }