Example #1
0
        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);
        }
Example #2
0
        /// <summary> Column-wise assembly using setValues</summary>
        /// <param name="nu">Maximum bandwidth on each column
        /// </param>
        /// <returns> Dense representation (not a direct copy)
        /// </returns>
        public static double[,] setsAssembleColumnMatrix(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();

            for (int i = 0; i < m; ++i)
            {
                int[] row = TesterUtilities.getIntArray(nu, r), col = TesterUtilities.getIntArray(m, r);
                double[,] entry = TesterUtilities.getDoubleArray(row.Length, col.Length, r);
                for (int ii = 0; ii < row.Length; ++ii)
                {
                    for (int jj = 0; jj < col.Length; ++jj)
                    {
                        data[row[ii], col[jj]] = entry[ii, jj];
                    }
                }
                A.SetValues(row, col, entry);
            }

            return(data);
        }
Example #3
0
        private static void repeatedAssembly(IElementalAccessMatrix A, int nu)
        {
            for (int i = 0; i < repeat; ++i)
            {
                check(A, TesterUtilities.SetAssembleRowMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.setsAssembleRowMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.AddAssembleRowMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.addsAssembleRowMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.setAssembleColumnMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.setsAssembleColumnMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.addAssembleColumnMatrix(A, nu));
                Blas.Default.Zero(A);

                check(A, TesterUtilities.addsAssembleColumnMatrix(A, nu));
                Blas.Default.Zero(A);
            }
        }
Example #4
0
        private void Helper1(IElementalAccessMatrix A, IElementalAccessVector b, IElementalAccessVector x)
        {
            // Test of usual solvers and preconditoners
            ILinearSolver[] solver = new ILinearSolver[]
            {
                new BiCGSolver(),
                new BiCGstabSolver(),
                //new CGSolver(),
                new CGSSolver(),
                new GMRESSolver(),
                new QMRSolver()
            };

            IPreconditioner[] M = new IPreconditioner[]
            {
                new IdentityPreconditioner(),
                //new ILUPreconditioner(new SparseRowMatrix(A.RowCount, A.ColumnCount))
            };

            DefaultLinearIteration iter = new DefaultLinearIteration();

            iter.SetParameters(1e-10, 1e-50, 1e+5, 1000000);

            for (int i = 0; i < solver.Length; ++i)
            {
                for (int j = 0; j < M.Length; ++j)
                {
                    Helper2(A, b, x, solver[i], M[j], iter);
                }
            }
        }
Example #5
0
        /// <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);
        }
Example #6
0
 private static void HelperTranspose(IElementalAccessMatrix A, IElementalAccessMatrix B, double[,] Am)
 {
     Blas.Default.Transpose(A, B);
     double[,] tmpArray = new double[A.ColumnCount, A.RowCount];
     //			for (int i2 = 0; i2 < A.ColumnCount; i2++)
     //			{
     //				tmpArray[i2] = new double[A.RowCount];
     //			}
     double[,] Bm = Transpose(Am, tmpArray);
     AreEqual(Blas.Default.GetArrayCopy(B), Bm);
 }
Example #7
0
        private static void TransposeCheck(IElementalAccessMatrix A, double[,] Am)
        {
            int n = A.RowCount, m = A.ColumnCount;

            HelperTranspose(A, new SparseRowMatrix(m, n), Am);
            HelperTranspose(A, new SparseColumnMatrix(m, n), Am);
            //HelperTranspose(A, new DenseRowMatrix(m, n), Am);
            //HelperTranspose(A, new DenseColumnMatrix(m, n), Am);
            //HelperTranspose(A, new DenseRowColumnMatrix(m, n), Am);
            //HelperTranspose(A, new DenseColumnRowMatrix(m, n), Am);
            HelperTranspose(A, new CoordinateMatrix(m, n), Am);
        }
Example #8
0
 /// <summary> Dense representation of the matrix using getValues</summary>
 public static double[,] getMatrix(IElementalAccessMatrix A)
 {
     int[] row = new int[A.RowCount], col = new int[A.ColumnCount];
     for (int i = 0; i < row.Length; ++i)
     {
         row[i] = i;
     }
     for (int i = 0; i < col.Length; ++i)
     {
         col[i] = i;
     }
     return(A.GetValues(row, col));
 }
Example #9
0
        private static void transCheck(IElementalAccessMatrix A, IElementalAccessVector x,
                                       IElementalAccessVector y, IElementalAccessVector z, double[,] Ad)
        {
            double[] xd = TesterUtilities.setAssembleVector(x),
            yd = TesterUtilities.setAssembleVector(y),
            zd = TesterUtilities.setAssembleVector(z);
            System.Random random = new System.Random();
            double        alpha = random.NextDouble(), beta = random.NextDouble();

            Blas.Default.TransMultAdd(alpha, A, x, beta, y, z);
            transMultAdd(alpha, Ad, xd, beta, yd, zd);

            checkEqual(z, zd);
        }
Example #10
0
        private void check(IElementalAccessMatrix A, IElementalAccessVector x,
                           IElementalAccessVector y, IElementalAccessVector z, double[,] Ad)
        {
            double[] xd = TesterUtilities.setAssembleVector(x),
            yd = TesterUtilities.setAssembleVector(y),
            zd = TesterUtilities.setAssembleVector(z);
            Random r = new Random();
            double alpha = r.NextDouble(), beta = r.NextDouble();


            Blas.Default.MultAdd(alpha, A, x, beta, y, z);
            multAdd(alpha, Ad, xd, beta, yd, zd);

            checkEqual(z, zd);
        }
Example #11
0
 /// <summary> Dense representation of the matrix using getValue</summary>
 public static double[,] getElMatrix(IElementalAccessMatrix A)
 {
     double[,] ret = new double[A.RowCount, A.ColumnCount];
     //			for (int i = 0; i < A.RowCount; i++)
     //			{
     //				ret[i] = new double[A.ColumnCount];
     //			}
     for (int i = 0; i < A.RowCount; ++i)
     {
         for (int j = 0; j < A.ColumnCount; ++j)
         {
             ret[i, j] = A.GetValue(i, j);
         }
     }
     return(ret);
 }
Example #12
0
        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);
        }
Example #13
0
        private void assemble(IElementalAccessMatrix A, IElementalAccessVector b, double K, double f)
        {
            int    n = b.Length - 1;
            double h = 1.0 / ((double)n);

            double[,] Ae = new double[, ] {
                { K / h, (-K) / h }, { (-K) / h, K / h }
            };
            double[] be = new double[] { f *h / 2.0, f *h / 2.0 };

            for (int i = 0; i < n; ++i)
            {
                int[] ind = new int[] { i, i + 1 };
                A.AddValues(ind, ind, Ae);
                b.AddValues(ind, be);
            }
        }
Example #14
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);
            }
        }
Example #15
0
        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);
            double[] ans         = solve(A, b, x, solver, M, iter);
            double[] ref_Renamed = reference(b.Length - 1, K, f, gl, gr);

            checkEqual(ans, ref_Renamed);

            Blas.Default.Zero(A);
            Blas.Default.Zero(b);
            Blas.Default.Zero(x);
        }
Example #16
0
        /// <summary> Row-wise assembly using add</summary>
        /// <param name="nu">Maximum bandwidth on each row
        /// </param>
        /// <returns> Dense representation (not a direct copy)
        /// </returns>
        public static double[,] AddAssembleRowMatrix(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();

            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.AddValue(i, ind, entry);
                }
            }

            return(data);
        }
Example #17
0
 private static void rowCheck(IElementalAccessMatrix A, IElementalAccessVector x,
                              IElementalAccessVector y, IElementalAccessVector z, int nu)
 {
     double[,] Ad = TesterUtilities.SetAssembleRowMatrix(A, nu);
     check(A, x, y, z, Ad);
 }
Example #18
0
 private static void check(IElementalAccessMatrix A, double[,] real)
 {
     AreEqual(TesterUtilities.getElMatrix(A), real);
     AreEqual(TesterUtilities.getMatrix(A), real);
     AreEqual(TesterUtilities.getMatrixCopy(A), real);
 }
Example #19
0
 private static void transColumnCheck(IElementalAccessMatrix A, IElementalAccessVector x,
                                      IElementalAccessVector y, IElementalAccessVector z, int nu)
 {
     double[,] Ad = TesterUtilities.setAssembleColumnMatrix(A, nu);
     transCheck(A, x, y, z, Ad);
 }